Integrating the paypal javascript SDK to calculate tax/shipping charges before OrderCreate.

anilthakur1
New Community Member

Hi,

 

I have a setup where i want to calculate Tax and Shipping charges of a customer based on their shipping address. I am using the new javascript SDK implementation. How do i first fetch the shipping address from Paypal, update the amount with tax and shipping charges and then charge the customer?

 

Implementation of Paypal button below:

paypal.Buttons({
style: {
tagline : false,
height : 50,
layout: 'horizontal',
fundingicons: false
},
createOrder: function(data, actions){
return actions.order.create({
purchase_units: [{
amount: {
value: parseFloat(self.get("finalPrice"))
}
}]
});
},
onApprove: function(data, actions){
return actions.order.capture().then(function(details){
self.set('controllers.index.isLoading', true);
self.send("webhookCall",details);
});
}
}).render('#paypal-button-container');

Login to Me Too
1 REPLY 1

Tranzzporter
Contributor
Contributor

I tried the follow, which, it seems to me, should work.  And, it does display the button set, and a transaction does process, but it dosen't alter the sales tax based on the State [I had to test this LIVE because the sandbox account has no [known] way to designate a State [of the Union]]!!?!

 

Anybody have any ideas?

 

var da_description    = "Glorious Gadgit!";
var da_price          = 64.95;
var da_shipping       = 11.00;
var da_sales_tax_rate = 9.5;  // Default tax rate in case the adjustment doesn' happen

var da_tax            = da_sales_tax_rate/100 * da_price;
da_tax                = da_tax.toFixed(2);
var total_cost        = da_price + da_shipping + da_tax;
total_cost            = total_cost.toFixed(2);

paypal.Buttons({
    style: {
       color: 'blue',
       shape: 'pill',
       size: 'responsive'
    },
    onInit: function(data, actions) {
    },
    onShippingChange: function(data, actions) {
        // Reject non-US addresses
        if (data.shipping_address.country_code !== 'US') {
          return actions.reject();
        }
        // Patch the tax amount
        var taxRate = data.shipping_address.state === 'CA' ? '9.5' : '0.0';
        var da_tax_adjusted = parseFloat(taxRate)/100 * da_price;
        da_tax_adjusted = da_tax_adjusted.toFixed(2);
        return actions.order.patch([
            {
                op: 'replace',
                path: '/purchase_units/@reference_id==\'default\'/amount',
                value: {
                    currency_code: 'USD',
                    value: (parseFloat(da_price) + parseFloat(da_shipping)).toFixed(2) + da_tax_adjusted,
                    breakdown: {
                        item_total: {
                            currency_code: 'USD',
                            value: da_price
                        },
                        shipping: {
                            currency_code: 'USD',
                            value: da_shipping
                        },
                        tax_total:{
                            currency_code:"USD",
                            value: da_tax_adjusted
                        }
                    }
                }
            }
        ]);
    },
    createOrder: function(data, actions) {
        return actions.order.create({
            purchase_units: [{
                description:da_description,
                amount:{
                    currency_code:"USD",
                    value:total_cost,
                    breakdown:{
                        item_total:{
                            currency_code:"USD",
                            value:da_price
                        },
                        shipping:{
                            currency_code:"USD",
                            value:da_shipping
                        },
                        tax_total:{
                            currency_code:"USD",
                            value: da_tax
                        }
                    }
                }
            }]
        });
    },
    onShippingChange: function(data, actions) {
        // If customer address is NOT in the USA, reject transaction.
        if (data.shipping_address.country_code !== 'US') {
            return actions.reject();
        } else {
            return actions.resolve();
        }
    },
    onApprove: function(data, actions) {
        return actions.order.capture().then(function(details) {
//            console.log(da_url+"?time="+details.create_time+"&given_name="+details.payer.name.given_name);
            window.location.replace(da_url_yup+"?time="+details.create_time+"&given_name="+details.payer.name.given_name);
        });
    },
    onCancel: function(data) {
        window.location.replace(da_url_nope);
    }
}).render('#paypal-payment-button');

 


 

Login to Me Too

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.