Paypal Smart Buttons 按州销售税
如果订单位于德克萨斯州,但在客户输入地址后才知道地址,则必须收取销售税。唯一看似合理的事件是 onShippingChange,但在客户单击“继续”后,PayPal 发回一个错误页面,称该事件未按预期工作。我不可能是唯一需要使用这些新的“智能”按钮收取销售税的人。
<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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在浏览器开发者工具的“网络”选项卡中,您可以看到补丁操作的 422 或 400 响应。
显然,您的路径中有一个额外的 at 符号 (@),并且格式错误...请参阅 SDK 参考:https://developer.paypal.com/sdk/js/reference/#onshippingchange
那里给出的内容有效:
In the browser developer tools Network tab, you can see a 422 or 400 response from the patch operation.
Apparently you have an extra at sign (@) in your path, and it has the wrong format...see the SDK reference: https://developer.paypal.com/sdk/js/reference/#onshippingchange
What's given there works:
对于任何其他尝试实现的人,您必须修改脚本 url 以添加 commit=false,如下所示:
在 paypal 脚本块上方放置一个代码块。
然后修改补丁中的路径
For anyone else trying to implement, you have to modify the script url to add the commit=false as shown below:
Placed a code block above the paypal script blocks.
and then change with path in the patch