【问题标题】:paypal checkout button get json response in javascriptpaypal结帐按钮在javascript中获取json响应
【发布时间】:2017-09-26 05:38:27
【问题描述】:

paypal API reference page 中,它说创建付款后会返回响应。我想在支付完成后使用交易信息,但是不知道如何从基础集成场景here获取这个响应json。浏览完文档后,我看不到在哪里可以获得此响应。我在这里错过了一个选项吗?提前致谢。

以下是我的代码

paypal.Button.render({

    // Set your environment

    env: 'sandbox', // sandbox | production

    // Pass the client ids to use to create your transaction on sandbox and production environments

    client: {
        sandbox:    'removed for security', // from https://developer.paypal.com/developer/applications/
        production: 'removed for security'  // from https://developer.paypal.com/developer/applications/
    },

    // Pass the payment details for your transaction
    // See https://developer.paypal.com/docs/api/payments/#payment_create for the expected json parameters

    payment: function() {
        return paypal.rest.payment.create(this.props.env, this.props.client, {
            transactions: [
                {
                    amount: {
                        total:    total,
                        currency: 'USD'
                    },
                    custom: purchaseOrderNumber
                }
            ]
        });
    },

    // Display a "Pay Now" button rather than a "Continue" button

    commit: true,

    // Pass a function to be called when the customer completes the payment

    onAuthorize: function(data, actions) {
        console.log("data", data);
        console.log("actions", actions);
        return actions.payment.execute().then(function() {
            console.log('The payment was completed!');

            //use transaction information from json response here
        });
    },

    // Pass a function to be called when the customer cancels the payment

    onCancel: function(data) {
        console.log('The payment was cancelled!');
    }

}, '#paypal-button');

编辑:

数据的控制台结果

{

"paymentToken":"EC-8T213183GM917341N",
"payerID":"784ARKSZGWBPG",
"paymentID":"PAY-00M809553A479072XLEBN4RI",
"intent":"sale",
"returnUrl":"https://www.sandbox.paypal.com/?paymentId=PAY-00M809553A479072XLEBN4RI&token=EC-8T213183GM917341N&PayerID=784ARKSZGWBPG"

}

操作结果的控制台

{
    "payment":{}
}
//see below for console screenshot

EDIT2:

对paypal框架的请求和响应

看起来最后一张图片是我真正需要的响应。有没有办法从基本的 paypal 按钮获取这些数据?

【问题讨论】:

  • 您是否已将您的贝宝开发帐户设置为使用沙盒?为了清楚起见......你按下贝宝结帐按钮,然后你登陆贝宝,你在那里做所有的事情,最后当它把你重定向回你的页面时它不起作用。或者你没有进入贝宝沙盒?
  • 是的,我已将我的帐户设置为使用沙盒,一切正常。我不需要重定向到另一个页面。我只是想使用响应中的信息来做一些其他的事情,仅此而已。如果某处有一个包含响应 json 的变量,那就太好了。
  • 你在dataactions看到了什么?
  • @Edwin 并澄清一下,单击贝宝结帐按钮会打开一个弹出窗口,显示在同一页面中。我认为这是因为我正在使用基本集成,如 here
  • 您能否也将您的请求发布到 paypal 框架?

标签: javascript json paypal paypal-sandbox


【解决方案1】:

actions.payment.execute().then()添加一个参数来捕捉响应

paypal.Button.render({

    // Set your environment

    env: 'sandbox', // sandbox | production

    // Pass the client ids to use to create your transaction on sandbox and production environments

    client: {
        sandbox:    'removed for security', // from https://developer.paypal.com/developer/applications/
        production: 'removed for security'  // from https://developer.paypal.com/developer/applications/
    },

    // Pass the payment details for your transaction
    // See https://developer.paypal.com/docs/api/payments/#payment_create for the expected json parameters

    payment: function() {
        return paypal.rest.payment.create(this.props.env, this.props.client, {
            transactions: [
                {
                    amount: {
                        total:    total,
                        currency: 'USD'
                    },
                    custom: purchaseOrderNumber
                }
            ]
        });
    },

    // Display a "Pay Now" button rather than a "Continue" button

    commit: true,

    // Pass a function to be called when the customer completes the payment

    onAuthorize: function(data, actions) {

        return actions.payment.execute().then(function(param) {
            console.log('The payment was completed!');

            //param is the json response
        });
    },

    // Pass a function to be called when the customer cancels the payment

    onCancel: function(data) {
        console.log('The payment was cancelled!');
    }

}, '#paypal-button');

【讨论】:

    【解决方案2】:

    设置看起来一切都很好,您现在所需要的只是决定您想要什么以及如何处理您的响应。

    onAuthorize 函数中,您可以对所有已批准的销售执行类似 (github paypal checkout button) 的操作(检查 data.status 是否为 approved(信用卡)或 created(使用贝宝付款) ), 那么你应该有一个data.payer 包含所有信息):

    jQuery.post('/my-api/execute-payment', { paymentID: data.paymentID, payerID: data.payerID });
                .done(function(data) { /* Go to a success page */ })
                .fail(function(err)  { /* Go to an error page  */  });
    

    或者直接在函数中使用datajson对象。

    【讨论】:

    • 我终于想通了!如果将参数传递给actions.payment.execute().then(function(response){}),那将是响应。我将发布整个代码并将其标记为解决方案,但 Edwin 非常感谢您的输入!另外,我想我会在 github 上创建一个 PR 以向其他人说明这一点。
    猜你喜欢
    • 2021-12-14
    • 2018-03-01
    • 2017-07-15
    • 1970-01-01
    • 2018-04-09
    • 2017-07-29
    • 1970-01-01
    • 2021-04-06
    • 2015-01-06
    相关资源
    最近更新 更多