【问题标题】:Migrate classic PayPal api to new Client side REST API将经典 PayPal api 迁移到新的客户端 REST API
【发布时间】:2018-07-18 11:48:52
【问题描述】:

我有以下表格:

<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">
[...]

并希望迁移到显示如下的 PayPal 的新 API:

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

除此之外,我是否可以传递元数据,例如“gold-package”,它将传递到 PayPal 付款并在自定义 URL 上返回?

请注意,上述项目是带有安装费的订阅,而不是常规付款。

【问题讨论】:

    标签: javascript php html paypal express-checkout


    【解决方案1】:

    您可以按照上面提供的链接实现使用 PayPal 进行支付的 REST 方式,登录到您的开发者门户创建一个应用程序并使用您帐户中生成的客户端 ID 并替换下面的 sn-p

    client: { sandbox:  'AZDxjDScFpQtjWTOUtWKbyN_bDt4OgqaF4eYXlewfBP48aqX3PiV8e1GWU6liB2CUXlkA59kJXE7M6R'}
    

    您可以更改交易数组 json 对象以反映您上面的表单数据,在 paypal 有效负载中,您可以使用 custom 键发送任何特定信息,例如 gold-package 等。

    请参考以下代码:

    <!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>
        
    

    注意:您可以在 resp in 方法中获取自定义消息

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

    注意:这是用于客户端集成,如果您可以处理服务器端实现,那么您可以以不同方式处理整个场景

    【讨论】:

    • 您提供的代码用于定期付款,我需要用于订阅和设置费用的代码,请您提供吗?
    猜你喜欢
    • 2015-01-16
    • 2014-06-03
    • 2021-09-04
    • 2014-01-18
    • 1970-01-01
    • 2016-11-25
    • 1970-01-01
    • 2013-11-29
    • 2015-02-22
    相关资源
    最近更新 更多