简体   繁体   English

将经典的 PayPal api 迁移到新的客户端 REST API

[英]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:并希望迁移到 PayPal 的新 API,其显示如下:

https://developer.paypal.com/demo/checkout/#/pattern/client 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?除此之外,我是否可以传递元数据,例如将传递给 PayPal 付款并在自定义 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您可以按照上面提供的链接实现使用 PayPal 付款的 REST 方式,登录到您的开发人员门户创建一个应用程序并使用您帐户中生成的客户端 ID 并替换以下代码段

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.您可以更改交易数组 json 对象以反映上面的表单数据,在贝宝有效负载中,您可以使用自定义密钥发送任何特定信息,如黄金包裹等。

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注意:您可以在 resp in 方法中获取自定义消息

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注意:这是用于客户端集成,如果您可以处理服务器端实现,那么您可以以不同的方式处理整个场景

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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