JS Paypal Script

Soldato
Joined
28 Apr 2011
Posts
15,221
Location
Barnet, London
I have a cart.php which calculates a total amount owed into $toPay. I want this amount to then be charged in this script on the actual shop page (the keys are edited) -

HTML:
<div id="paypal-button-container"></div>

<script>
                paypal.Button.render({

                    env: 'sandbox', // sandbox | production

                    // Specify the style of the button

                    style: {
                        label: 'checkout',
                        size: 'small', // small | medium | large | responsive
                        shape: 'pill', // pill | rect
                        color: 'gold' // gold | blue | silver | black
                    },

                    // PayPal Client IDs - replace with your own
                    // Create a PayPal app: https://developer.paypal.com/developer/applications/create
                    client: {
                        sandbox: 'AZDxjDScFpQtjbyN_bDt4OgqaF4eYXlewfBP4-8aqX3PiV8e1GWU6E7M6R',
                        production: 'AUbmPNC-HX_OKAufXgkoiiUpieaFH4BRG69J9Bwy_XiqxW_9iWWzh3sMzB'
                    },

                    // Show the buyer a 'Pay Now' button in the checkout flow
                    commit: true,

                    // payment() is called when the button is clicked
                    payment: function(data, actions) {

                        // Make a call to the REST api to create the payment
                        return actions.payment.create({
                            payment: {
                                transactions: [{
                                    amount: {
                                        total: '1.00',
                                        currency: 'GBP'
                                    }
                                }]
                            }
                        });
                    },

                    // onAuthorize() is called when the buyer approves the payment
                    onAuthorize: function(data, actions) {

                        // Make a call to the REST api to execute the payment
                        return actions.payment.execute().then(function() {
                            window.alert('Payment Complete!');
                        });
                    }

                }, '#paypal-button-container');

            </script>

How can I pass the variable in instead of '1.00'. I tried passing $toPay into $_SESSION['$toPay'] and then replacing 1.00 with <?php $_SESSION['$toPay'] ?> but then the Paypay button didn't show, so I assume that's not valid code.

Any ideas? Thanks.

**EDIT** For now I've got it working an older way where it diverts you all the way to Paypal. I would prefer to get this new way working if anyone can help, where it just uses a popup to take payment.

Also, in the onAuthorize, how would I get it to process success.php. Simply open <php include success.php ?> ? Can I do that in JS?
 
Last edited:
Thanks, but no, the button doesn't show still. Could it be the ' getting things confused? Should one of them be " or `? (I've tried some combinations, but no luck)

HTML:
total: '<?php echo $_SESSION['$toPay ']; ?>',
 
As above, you'd need to echo or use the <?= ... ?> syntax.

If you want to load a webpage on success, then it would be easiest to redirect from JavaScript:
window.location.replace('/success.php');

Using replace means the user won't be able to go back, which I assume would be desired.

Okay, so I can insert php code, great. It's not so much a redirect (well, it will) but I need to deduct the shopping basket from the current stock if the payment is successful.

Try outputting the single quotes within your PHP echo:

total: <?php echo "'".$_SESSION['$toPay ']."'"; ?>

Thanks, but doesn't work either :(
 
Edit: just noticed your session var is labelled '$toPay' - shouldn't it just be 'toPay' at that point? Updated my code above.

It was this, thanks. I hadn't used $ initially, so not sure when that crept in, but it works without $, thanks.

For success.php to work you'll need to request the page, either by redirecting to it or using ajax. You can't just include it in your output.

Okay, thanks. I've not really done my Javascript yet, but it looks like I would simply use -

Code:
window.location.href = "success.php";

** EDIT ** Great, that works using Paypal Sandbox. Now to write the code that changes stock holding and emails the order details to me!

Thanks for the help.
 
Back
Top Bottom