Integrating the paypal javascript SDK to calculate tax/shipping charges before OrderCreate.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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');
- Labels:
-
Mobile SDKs
-
REST SDK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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');

Haven't Found your Answer?
It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.
- How can I revoke SamCart's access my recurring payments? in NVP/SOAP APIs
- How to integrate PayPal during user signup to avoid re-entering credentials for future payments? in REST APIs
- Create Pay Links using PayPal API in REST APIs
- Shopify PayPal integration, customer authorized multiple times in PayPal Payments Standard
- Why does PayPal redirect to different pages (login vs credit card form) depending on browser mode? in Braintree Server-side Integration (PHP, Java, .NET, Ruby, Python, NodeJS SDKs)