I am registered as a paypal user with sendbox, so I can already do my payment tests.
I added the paypal payment script to my small ecommerce site a few days ago. On my ecommerce site there is a shopping cart that stores the products you want to buy and automatically calculates the shipping costs, giving as a final result the total amount to be paid for the buyer. So based on that, the script I put on my payment page is this (you can see the $ pptotalpay variable which allows me to complete the payment with the set amount):
<script src="https://www.paypal.com/sdk/js?client-id=XXXXXXXXXXXX¤cy=EUR"></script> <script> paypal.Buttons({ style: { layout: 'vertical', color: 'blue', shape: 'rect', label: 'paypal' }, // Sets up the transaction when a payment button is clicked createOrder: (data, actions) => { return actions.order.create({ purchase_units: [{ amount: { value: '<?php echo $pptotalpay; ?>' } }] }); }, // Finalize the transaction after payer approval onApprove: (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)); const 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: //const element = document.getElementById('paypal-button-container'); //element.innerHTML = `<h3>Thank you for your payment! ${transaction.status} ${transaction.id} </h3>`; // Or go to another URL: actions.redirect('thank_you.html'); actions.redirect(`https://www.example.com/thank_you.php?transactionid=${transaction.id}&transactionstatus=${transaction.status}`); //actions.redirect(`https://www.example.com/cart.php`); }); } }).render('#paypal-button-container'); </script> <div id="paypal-button-container"></div> I have two options to manage the next phase (the payment message and the archiving of the order), to that of the actual payment with paypal: the first is to activate the two lines:
const element = document.getElementById ('paypal-button-container'); element.innerHTML = `<h3> Thank you for your payment! $ {transaction.status} $ {transaction.id} </h3> `; or to redirect the user to a personalized page:
actions.redirect (`https://www.example.com/thank_you.php?transactionid=$ {transaction.id} & transactionstatus = $ {transaction.status}`); Now, I'd like to use the second option, just because I can have a personalized page and freely write whatever I want as a purchase confirmation response. As you can see, however, there are two parameters that are passed via _GET; these are very important for me as they allow me to understand the outcome of the payment, and, when storing the purchased items in the database, associate the transaction ID with them.
My concern, however, is given by the fact that a "playful" user could play with the variables found in the URL, jeopardizing the security of the site; for example, it could start a new session for the purchase of products, skip the part of the payment with paypal and load the result page (where the code that stores the transaction in the database is found). I would notice it later when I would not see the correspondence between archived orders and amounts collected by paypal.
In this case, in addition to using the _GET method, paypal can use the _POST method? If so, how?
Or there is the first option (perhaps safer), which is to activate those two lines:
const element = document.getElementById ('paypal-button-container'); element.innerHTML = `<h3> Thank you for your payment! $ {transaction.status} $ {transaction.id} </h3> `; In this case I would have to incorporate my payment message in that string, but I don't know how I could make the $ {transaction.status} and $ {transaction.id} variables available in PHP so that I can manage the page dynamically and store the 'id of the transaction.
If anyone can give me advice I would be grateful.