paypal checkout按钮在javascript中获取json响应

时间:2017-04-28 07:30:14

标签: javascript json paypal paypal-sandbox

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

enter image description here

EDIT2:

请求paypal框架和响应

request to framework when creating payment

response when creating payment

request to framework when executing

response when executing

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

2 个答案:

答案 0 :(得分:2)

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');

答案 1 :(得分:1)

设置似乎都很好,现在您只需要决定处理响应的内容和方式。

onAuthorize功能中,您可以对所有已批准的销售执行此类操作(github paypal checkout button)(检查data.statusapproved(信用卡)或created(使用paypal付款),那么您应该拥有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  */  });

或直接使用函数中的data json对象。