Item Variables In Smart Button
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi there,
I am struggling through the setup of the new smart payments and javascript. I am trying to add items to the transaction from my website's shopping cart, but I cannot understand why the variable won't work.
// Render the PayPal button into #paypal-button-container
var itemsData = '{name: "Basic Class Fee", unit_amount: {currency_code: "CAD",value: "50.00"},quantity: "1"}';
console.log(itemsData)
paypal.Buttons({
// Set up the transaction
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
amount: {
currency_code: "CAD",
value: "50.00",
breakdown: {
item_total: {
currency_code: "CAD",
value: "50.00"
}}
},
items: [ (itemsData) ]
}]
});
},
// Finalize the transaction
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
// Show a success message to the buyer
alert('Transaction completed by ' + details.payer.name.given_name + '!');
});
}
}).render('#paypal-button-container');
If I replace the itemsData variable with the string itself, the button works fine, however, as soon as I use the variable, the button breaks and returns a 400 error from PayPal.
Thanks for reading
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Figured it out, my variable should have been using brackets and not quotes when it was declared. Working code below:
// Render the PayPal button into #paypal-button-container
paypal.Buttons({
// Set up the transaction
createOrder: function(data, actions) {
var itemsData = ({name: "Basic Class Fee", unit_amount: {currency_code: "CAD",value: "50.00"},quantity: "1"});
return actions.order.create({
purchase_units: [{
amount: {
currency_code: "CAD",
value: (subtotal),
breakdown: {
item_total: {
currency_code: "CAD",
value: (subtotal)
}}
},
items: [(itemsData)]
}]
});
},
// Finalize the transaction
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
// Show a success message to the buyer
alert('Transaction completed by ' + details.payer.name.given_name + '!');
});
}
}).render('#paypal-button-container');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This is amazing. It has taken me days to find this. But I am still looking to pass through a few more options, like Invoice Number or similar. Something I can link to the original transaction.
And also what values get passed back in the successful events.
Where did you find the information on the items variable?
Cheers from Australia.
Haven't Found your Answer?
It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.
- How do you dynamically pass an item name through an 'add to cart' button? in Sandbox Environment
- PayPal Buttons in PayPal Payments Standard
- payment button error with return variable in PayPal Payments Standard
- PayPal buttons kenya in PayPal Payments Standard
- Payment confirmation comes to a customer in Hebrew in PayPal Payments Standard