PAYPAL 支付客户端 REST 脚本

Posted

技术标签:

【中文标题】PAYPAL 支付客户端 REST 脚本【英文标题】:PAYPAL PAYMENT Client Side REST SCRIPT 【发布时间】:2017-11-21 15:29:48 【问题描述】:

我已将 PayPal 客户端 REST 整合到我的网站。我使用了以下链接提供的示例代码: https://developer.paypal.com/demo/checkout/#/pattern/client

下面的代码工作了一个多月,但是今天它显示以下错误 错误:请求发布https://www.sandbox.paypal.com/v1/payments/payment

failed with 400 error


    "name": "MALFORMED_REQUEST",
    "message": "Incoming JSON request does not map to API request",
    "information_link": "https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST",
    "debug_id": "a26904ff6211a"

我的代码如下:

 <div id="paypal-button-container" class="info"></div><script src="https://www.paypalobjects.com/api/checkout.js"></script>
<script>

    // Render the PayPal button

    paypal.Button.render(

        // Set your environment

        env: 'production', // sandbox | production

        // PayPal Client IDs - replace with your own
        // Create a PayPal app: https://developer.paypal.com/developer/applications/create

        client: 
            sandbox:    '<?=SANDBOXPAYPAL?>',
            production: '<?=PAYPAL_TOKEN?>'
        ,

        // Set to 'Pay Now'

        commit: true,

        // Wait for the PayPal button to be clicked

        payment: function() 
$('#card').attr('checked',true);
            // Make a client-side call to the REST api to create the payment


            return paypal.rest.payment.create(this.props.env, this.props.client, 
                transactions: [
                    
                        amount:  total: '12.99', currency: 'GBP' 
                    
                ]
            );
        ,

        // Wait for the payment to be authorized by the customer

        onAuthorize: function(data, actions) 

           jQuery.ajax(
           type: "POST",
           url: "ipn.php",
           data: data,
           success: function(data) 
           
         );

            return actions.payment.execute().then(function() 
                document.querySelector('#paypal-button-container').innerText = 'Payment Complete!';
            );
        

    , '#paypal-button-container');

</script>

【问题讨论】:

【参考方案1】:

您的代码与示例不太匹配,但我也在使用我正在构建的 Laravel 购物车中的 client-sdie API,我使用 AJAX 以这种方式将支付数据推送回我的系统:

onAuthorize:function(data, actions)
    
        console.log("onAuthorize()");
        console.log("Dumping DATA");
        console.log(data);
        console.log("Dumping ACTIONS");
        console.log(actions);
        return actions.payment.execute().then(function(payment)
        
            console.log("payment.execute called");
            document.querySelector('#paypal-button-container').innerText = 'Payment Complete!';
            console.log("Dumping payment:");
            console.log("CART: "+payment['cart']);
            console.log(payment);

            var values = encodeURIComponent(JSON.stringify(payment));
            $.ajaxSetup(headers:'X-CSRF-TOKEN':' $token '   );
            ajaxRequest= $.ajax( url: "/ajax/payment", type: "post",data:values );
            ajaxRequest.done(function(response, textStatus, jqXHR)
            
                var result = $.parseJSON(response);
                console.log(result);
            );

        );
    ,

/ajax/payment 是我目前正在开发的捕获脚本,但它确实记录了购买的所有数据。希望对您有所帮助。

【讨论】:

以上是关于PAYPAL 支付客户端 REST 脚本的主要内容,如果未能解决你的问题,请参考以下文章

PayPal智能支付按钮与服务器端REST API集成

与 Paypal 的 PHP REST SDK 的支付协议

使用客户端 REST api 在 Laravel 中不显示 PayPal 按钮

使用 webhook 将客户发送到 PayPal 支付页面? C#

如何使用 PayPal Express Checkout 集成(客户端 REST)获取交易 ID

paypal rest sdk - 取消付款只提供令牌