Have to charge sales tax if the order is in Texas, but do not know the address until after customer has entered it. The only event that seemed reasonable was onShippingChange, but after the customer clicks continue, PayPal sends back an error page saying this are not working as expected. I can not be the only person that needs to charge sales tax with these new "Smart" buttons.
<script>
const baseOrderAmount = 20.00;
function initPayPalButton() {
paypal.Buttons({
style: {
shape: 'pill',
color: 'blue',
layout: 'vertical',
label: 'paypal',
},
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [
{
"description": "Add product to buy",
"custom_id": "xxx-yyy-zzz",
"amount": {
"currency_code": "USD",
"value": baseOrderAmount,
"breakdown": {
"item_total": {
"currency_code": "USD",
"value": baseOrderAmount
},
"tax_total": {
"currency_code": "USD",
"value": 0
}
}
}
}]
});
},
onShippingChange: function (data, actions) {
const taxAmount = (data.shipping_address.state === 'TX' || data.shipping_address.state === 'Texas') ? baseOrderAmount * 0.0825 : '0.00';
return actions.order.patch([
{
op: 'replace',
path: "/purchase_units/@@referenceId='default'/amount",
value: {
currency_code: 'USD',
value: (parseFloat(baseOrderAmount) + parseFloat(taxAmount)).toFixed(2),
breakdown: {
item_total: {
currency_code: 'USD',
value: baseOrderAmount
},
tax_total: {
currency_code: 'USD',
value: taxAmount
}
}
}
}
]);
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(orderData) {
const element = document.getElementById('paypal-button-container');
element.innerHTML = '';
element.innerHTML = '<h3>Thank you for your payment!</h3>';
});
},
onError: function(err) {
console.log(err);
}
}).render('#paypal-button-container');
}
initPayPalButton();
</script>
Via Active questions tagged javascript - Stack Overflow https://ift.tt/FUSptel
Comments
Post a Comment