Skip to content

Instantly share code, notes, and snippets.

@raphaeldealmeida
Created June 13, 2024 21:55
Show Gist options
  • Save raphaeldealmeida/a3afd4bb36a3c0a0856bd02635baa87d to your computer and use it in GitHub Desktop.
Save raphaeldealmeida/a3afd4bb36a3c0a0856bd02635baa87d to your computer and use it in GitHub Desktop.
function initPayPalButton() {
var shipping = 0;
var orderDescription = 'Additional Services';
if(orderDescription === '') {
orderDescription = 'Item';
}
paypal.Buttons({
style: {
shape: 'rect',
color: 'blue',
layout: 'vertical',
label: 'buynow',
},
createOrder: function(data, actions) {
var selectedItemDescription = 'Added Services';
var selectedItemPrice = document.getElementById('payment-value').value;
var tax = (0 === 0 || false) ? 0 : (selectedItemPrice * (parseFloat(0)/100));
quantity = 1;
tax *= quantity;
tax = Math.round(tax * 100) / 100;
var priceTotal = quantity * selectedItemPrice + parseFloat(shipping) + tax;
priceTotal = Math.round(priceTotal * 100) / 100;
var itemTotalValue = Math.round((selectedItemPrice * quantity) * 100) / 100;
return actions.order.create({
purchase_units: [{
description: orderDescription,
amount: {
currency_code: 'USD',
value: priceTotal,
breakdown: {
item_total: {
currency_code: 'USD',
value: itemTotalValue,
},
shipping: {
currency_code: 'USD',
value: shipping,
},
tax_total: {
currency_code: 'USD',
value: tax,
}
}
},
items: [{
name: selectedItemDescription,
unit_amount: {
currency_code: 'USD',
value: selectedItemPrice,
},
quantity: quantity
}]
}]
});
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(orderData) {
// Full available details
console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
// Show a success message within this page, e.g.
const element = document.getElementById('paypal-button-container');
element.innerHTML = '';
element.innerHTML = '<h3>Thank you for your payment!</h3>';
// Or go to another URL: actions.redirect('thank_you.html');
});
},
onError: function(err) {
console.log(err);
},
}).render('#paypal-button-container');
}
initPayPalButton();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment