Skip to content

Instantly share code, notes, and snippets.

@damianobarbati
Last active March 31, 2020 19:37
Show Gist options
  • Save damianobarbati/068d6bebf354ba733f6e75d96dc8e923 to your computer and use it in GitHub Desktop.
Save damianobarbati/068d6bebf354ba733f6e75d96dc8e923 to your computer and use it in GitHub Desktop.
paypal.js
window.paypal.Buttons({
// reference: https://developer.paypal.com/docs/checkout/integration-features/customize-button/
style: {
layout: 'horizontal',
shape: 'rect',
color: 'blue',
fundingicons: false,
tagline: false,
},
// reference: https://developer.paypal.com/docs/checkout/integration-features/auth-capture/#integrate-authorize-capture
// this is fired when the user clicks on the paypal button
createOrder: async (data, actions) => {
// TATIANA! create the order and save somewhere the orderId
const orderId = await axios.post('/api/order/add', { courseIds: [], packIds: [] })
// this redirect the user on paypal.com to complete or deny the paymeny
return actions.order.create({
purchase_units: [{
amount: {
// TATIANA! total for order
value: total,
},
}],
});
},
// this is fired once the user has done on paypal.com
onApprove: async (data, actions) => {
try {
const order = await actions.order.capture();
const { id: paypalOrderId, status } = order;
// TATIANA! confirm the order: we can collect the $$$ thanks to the paypalOrderId
if (status === 'COMPLETED')
await axios.post('/api/order/confirm', { orderId, paypalOrderId };
}
catch (error) {
// handle errors
console.error(error);
}
},
}).render('#paypal-button');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment