Charge Stripe Processing Fee to the user

Following is how I've implemented the possibility to charge the stripe fee onto the user.
Unfortunately, Stripe does not offer any API endpoint to get the stripe fees for a specified country so I had to do it manually.
It is not perfect nor elegant but it works.

var stripe.charge = true;  
var euCountries = ['AT','BE','BG','CY','CZ','DK','EE','FI','FR','DE','GR','HU','IE','IT','LV','LT','LU','MT','NL','PL','PT','RO','SK','SI','ES','SE','GB'];

/**
 *  the following hash-map is used to calculate the stripe fee to apply to the user
 *  Add here any new country we intend to serve
 */
var stripe_fees = {  
    GB: {
        percentage: function(card){
            return (euCountries.indexOf(card.country)>=0) ? 0.014 : 0.029;
        },
        fixed:0.20,
        VAT:1
    },
    IE: {
        percentage: function(card){
            return (euCountries.indexOf(card.country)>=0) ? 0.014 : 0.029;
        },
        fixed:0.25,
        VAT:1.23
    }
};

function charge(card, amount, country){

    // Calculate the Stripe fee to charge the user
    // Setting Stripe percentage and fixed price as GB by default
    var stripePercentage    = stripe_fees.GB.percentage(card) * stripe_fees.GB.VAT
    var stripeFixed         = stripe_fees.GB.fixed * stripe_fees.GB.VAT;

    // ifjacking the stripe percentage and fixed value with the value of the country in the config,if any
    if(country && stripe_fees[country]){
        stripePercentage    = stripe_fees[country].percentage(card) * stripe_fees[country].VAT
        stripeFixed         = stripe_fees[country].fixed * stripe_fees[country].VAT;
    }

    /**
     * Formula to calculate the total amount
     *
     * P(amount)     = amount + transaction_fee
     * S(fixed)      = stripe_fixed * country_vat
     * S(percentage) = stripe_percentage * country_vat
     * T(amount)     = P(amount) + S(fixed) / (1-S(percentage))
     *
     * Example:
     * Irish payment of 40.00 with no transaction_fee
     * P(amount)     = 40 + 0
     * S(fixed)      = 0.25  * 1.23
     * S(Percentage) = 0.014 * 1.23
     * T(amount)     = 40 + 0.3075 / (1 - 0.0174) = 41.01
     */

    return ( parseFloat(amount) + stripeFixed) / (1 - stripePercentage);
};  

Davide Andreazzini

Read more posts by this author.