JavaScript & 贝宝结账按钮问题
JavaScript & PayPal Checkout Button Question
我目前正在使用 Javascript 测试 PayPal 结帐集成的沙箱。
在 Javascript 中测试我们的按钮时,我看到了如何使用“沙盒”模式与使用“实时”模式。
我想问的是如何设置产品的标题,以便买家在点击“立即付款”按钮完成交易之前能够准确地看到他们要购买的商品?
这是我目前正在使用的代码,如下所示:
//HTML GOES HERE TO CONTAIN THE PAYPAL BUTTON
<div id="paypal-button-container"></div>
<script src="https://www.paypal.com/sdk/js?&client-id=[MY-CLIENT-ID-GOES-HERE]&merchant-id=[MY-MERCHANT-EMAIL-ADDRESS-GOES-HERE]¤cy=USD"></script>
paypal.Buttons({
style: {
layout: 'vertical',
color: 'gold',
shape: 'pill',
label: 'buynow'
},
// Sets up the transaction when a payment button is clicked
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
amount: {
value: 50, // Can reference variables or functions. Example: `value: document.getElementById('...').value`
currency: 'USD'
},
}]
});
},
// Finalize the transaction after payer approval
onApprove: function(data, actions) {
return actions.order.capture().then(function(orderData) {
// Successful capture! For dev/demo purposes:
console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
var transaction = orderData.purchase_units[0].payments.captures[0];
alert('Transaction '+ transaction.status + ': ' + transaction.id + '\n\nSee console for all available details');
// When ready to go live, remove the alert and show a success message within this page. For example:
// var element = document.getElementById('paypal-button-container');
// element.innerHTML = '';
// element.innerHTML = '<h3>Thank you for your payment!</h3>';
actions.redirect('https://SomeWebsiteURLiWillForwardThemTo.com');
});
}
}).render('#paypal-button-container');
在 paypals 文档中,您可以看到购买单位对象。 https://developer.paypal.com/docs/api/orders/v2/#definition-purchase_unit inside that object you can see that you can pass an items array. This is an array of objects that you can view here https://developer.paypal.com/docs/api/orders/v2/#definition-item。您可以定义每个产品的参数,包括名称。因此,对于您的情况,我认为您可以尝试以下操作:
purchase_units: [{
amount: {
value: 50,
currency: 'USD',
breakdown: {
item_total: {
currency_code: 'USD',
value: 50
},
}
},
items: [
{
name: 'Product Name',
unit_amount: {
currency_code: 'USD',
value: 50
},
quantity: '1'
}
]
}]
这是一个有效的 fiddle 只需弹出您的客户端 ID 进行测试。 Fiddle Demo
注意:查看文档并了解哪些是必填字段。我还认为您需要对金额进行细目分类才能起作用。
我目前正在使用 Javascript 测试 PayPal 结帐集成的沙箱。
在 Javascript 中测试我们的按钮时,我看到了如何使用“沙盒”模式与使用“实时”模式。
我想问的是如何设置产品的标题,以便买家在点击“立即付款”按钮完成交易之前能够准确地看到他们要购买的商品?
这是我目前正在使用的代码,如下所示:
//HTML GOES HERE TO CONTAIN THE PAYPAL BUTTON
<div id="paypal-button-container"></div>
<script src="https://www.paypal.com/sdk/js?&client-id=[MY-CLIENT-ID-GOES-HERE]&merchant-id=[MY-MERCHANT-EMAIL-ADDRESS-GOES-HERE]¤cy=USD"></script>
paypal.Buttons({
style: {
layout: 'vertical',
color: 'gold',
shape: 'pill',
label: 'buynow'
},
// Sets up the transaction when a payment button is clicked
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
amount: {
value: 50, // Can reference variables or functions. Example: `value: document.getElementById('...').value`
currency: 'USD'
},
}]
});
},
// Finalize the transaction after payer approval
onApprove: function(data, actions) {
return actions.order.capture().then(function(orderData) {
// Successful capture! For dev/demo purposes:
console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
var transaction = orderData.purchase_units[0].payments.captures[0];
alert('Transaction '+ transaction.status + ': ' + transaction.id + '\n\nSee console for all available details');
// When ready to go live, remove the alert and show a success message within this page. For example:
// var element = document.getElementById('paypal-button-container');
// element.innerHTML = '';
// element.innerHTML = '<h3>Thank you for your payment!</h3>';
actions.redirect('https://SomeWebsiteURLiWillForwardThemTo.com');
});
}
}).render('#paypal-button-container');
在 paypals 文档中,您可以看到购买单位对象。 https://developer.paypal.com/docs/api/orders/v2/#definition-purchase_unit inside that object you can see that you can pass an items array. This is an array of objects that you can view here https://developer.paypal.com/docs/api/orders/v2/#definition-item。您可以定义每个产品的参数,包括名称。因此,对于您的情况,我认为您可以尝试以下操作:
purchase_units: [{
amount: {
value: 50,
currency: 'USD',
breakdown: {
item_total: {
currency_code: 'USD',
value: 50
},
}
},
items: [
{
name: 'Product Name',
unit_amount: {
currency_code: 'USD',
value: 50
},
quantity: '1'
}
]
}]
这是一个有效的 fiddle 只需弹出您的客户端 ID 进行测试。 Fiddle Demo
注意:查看文档并了解哪些是必填字段。我还认为您需要对金额进行细目分类才能起作用。