简体   繁体   中英

Migrate classic PayPal api to new Client side REST API

I have the following form:

<form id="pp" action="https://www.paypal.com/cgi-bin/webscr" method="post">
    <input type="hidden" name="cmd" value="_xclick-subscriptions">
    <input type="hidden" name="business" value="xxx">
    <input type="hidden" name="lc" value="US">
    <input id="full_desc" type="hidden" name="item_name" value="Test">
    <input type="hidden" name="no_note" value="1">
    <input id="month1_total" type="hidden" name="a1" value="1.00"><!-- total -->
    <input type="hidden" name="p1" value="30"><!-- each days -->
    <input type="hidden" name="t1" value="D">
    <input type="hidden" name="src" value="1">
    <input id="month_fee" type="hidden" name="a3" value="0.50"><!-- each month -->
    <input type="hidden" name="p3" value="1">
    <input type="hidden" name="t3" value="M">
    <input type="hidden" name="currency_code" value="USD">
[...]

And would like to migrate to PayPal's new API who displays like this:

https://developer.paypal.com/demo/checkout/#/pattern/client

In addition to this, can I pass metadata for example "gold-package" which will be passed to PayPal payment and returned on a custom URL?

Please note that the item above is a subscription with setup fees, not a regular payment.

You can follow the link you provided above to implement REST way of doing payment with PayPal, Sign in to your developer portal create an app and use the client id generated in your account and replace the below snippet

client: { sandbox:  'AZDxjDScFpQtjWTOUtWKbyN_bDt4OgqaF4eYXlewfBP48aqX3PiV8e1GWU6liB2CUXlkA59kJXE7M6R'}

you can change the transaction array json object to reflect your form data above, in the paypal payload you can use custom key to send any specific information like gold-package etc.

please refer the code below:

<!DOCTYPE html>

<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://www.paypalobjects.com/api/checkout.js"></script>
</head>

<body>
    <div id="paypal-button-container"></div>

    <script>
        paypal.Button.render({

            env: 'sandbox', // sandbox | production

            // PayPal Client IDs - replace with your own
            // Create a PayPal app: https://developer.paypal.com/developer/applications/create
            client: {
                sandbox:    'AZDxjDScFpQtjWTOUtWKbyN_bDt4OgqaF4eYXlewfBP4-8aqX3PiV8e1GWU6liB2CUXlkA59kJXE7M6R',
                production: '<insert production client id>'
            },

            // 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: '0.01', currency: 'USD' },
                                custom:"true"
                                
                            }
                        ]
                    }
                });
            },

            // 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(resp) {
                    // here you can get the custum flag
                    window.alert('Payment Complete!');
                });
            }

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

    </script>
</body>
    

Note : you can get your custom message in the resp in method

return actions.payment.execute().then(function(resp) {
                        // here you can get the custum flag

Note : this is for client side integration, if you can handle server side implementation then you can handle the whole scenario differently

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM