I am trying to integrate Paypal with my website and facing issues with this. The problem is that I cannot seem to find a way to send the PHP variables value, that is the TOTAL AMOUNT to Paypal code
Suppose I have a variable
$finalAmount = 220;
This value is dynamic and will change with each user. I need to pass it below inside the Paypal code
<script>
paypal.Button.render({
// Configure environment
env: 'sandbox', // sandbox | production
// PayPal Client IDs - replace with your own
// Create a PayPal app: https://developer.paypal.com/developer/applications/create
client: {
sandbox: 'CODE_HERE',
// production: 'demo_production_client_id'
},
// Customize button (optional)
locale: 'en_US',
style: {
layout: 'vertical', // horizontal | vertical
size: 'medium', // medium | large | responsive
shape: 'rect', // pill | rect
color: 'gold'
},
// Set up a payment
payment: function (data, actions) {
return actions.payment.create({
transactions: [{
amount: {
total: '1',
currency: 'USD'
},
invoice_number : 123111241, // invoice must be unique for each transaction.
item_list: {
items: [
{
name: 'hat',
description: 'Brown hat.',
quantity: '2',
price: '3',
sku: '1',
currency: 'INR'
},
{
name: 'handbag',
description: 'Black handbag.',
quantity: '1',
price: '7',
sku: 'product34',
currency: 'INR'
}
],
shipping_phone_number : '8888888888',
shipping_address: {
recipient_name: 'Logan loga',
line1: '4th Floor',
line2: 'Unit #34',
city: 'Chennai',
country_code: 'IN',
postal_code: '600113',
phone: '9999999999',
state: 'Tamil Nadu'
}
}
}],
payer: {
payment_method: "paypal",
payer_info : {
email: 'test@gmail.com',
first_name :'Logan',
last_name :'loga',
billing_address : {
line1: '4th Floor',
line2: 'Unit #34',
city: 'Chennai',
country_code: 'IN',
postal_code: '600114',
phone: '7777777777',
state: 'Tamil Nadu'
}
}
},
});
},
// Execute the payment
onAuthorize: function (data, actions) {
return actions.payment.execute()
.then(function (res2) {
// Show a confirmation message to the buyer
console.log(res2);
console.log(res2.transactions[0].related_resources[0].sale.state);
//state codes - completed, partially_refunded, pending, refunded, denied
if(res2.transactions[0].related_resources[0].sale.state == 'completed'){
alert("Payment received for the invoice number" + res2.transactions[0].invoice_number);
}
});
},
onCancel: function (data, actions) {
// Show a cancel page or return to cart
alert("Payment cancelled");
},
onError: function (err) {
// Show an error page here, when an error occurs
alert("Payment error");
}
}, '#paypal-button');
</script>
I want to pass the value of $finalAmount to the PayPal script at
amount: {
total: 'HERE'
}
How can I implement this?