使用PayPal-node-SDK(PayPal Express Checkout)进行付款授权和捕获未来的付款

4
我正在使用PayPal Express Checkout,我需要做的是分别授权和捕获付款,我的当前PayPal流程如下:
1)我使用以下代码创建付款:
var paypal = require('paypal-rest-sdk');

function createPayPal(req, res, itemsArray, redirectsTos) {
    var payment = {
        "intent": "sale",
        "payer": {},
        "transactions": [{
            "amount": {
                "currency": sails.config.currency,
                "total": itemsArray.totalArrayAmount,
                "details": {
                    "subtotal": itemsArray.totalArrayAmount,
                    "fee": sails.config.PayPalCreateFee
                }
            },
            "invoice_number": req.session.invNum,
            "item_list": {
                "items": itemsArray.itemsArray
            }
        }]
    };
    sails.log.info('payment obj :', JSON.stringify(payment))
    payment.payer.payment_method = sails.config.PayPalPaymentMethod;
    payment.redirect_urls = {
        "return_url": res.locals.return_url_buy,
        "cancel_url": res.locals.cancel_url_buy
    };

    paypal.payment.create(payment, function(error, payment) {
        if (error) {
            sails.log.error(error);
            redirectsTos(({
                message: 'failure',
                redirect: '/paypal/error'
            }), null);
        } else {
        sails.log.info('Payment ID = ', payment.id);
        sails.log.info('User ID = ', req.session.userSession);
        var redirectUrl;
        for (var i = 0; i < payment.links.length; i++) {
            var link = payment.links[i];
            if (link.method === 'REDIRECT') {
                redirectUrl = link.href;
                sails.log.info('goto:', redirectUrl)
                redirectsTos(null, ({
                    message: 'success',
                    redirect: redirectUrl
                }));
            }
        }
        }
    });
}

PayPal会返回订单信息和重定向URL,我将用户重定向到链接对象中的href。当Payflow返回我的网站时,它会发送给我...
{
    paymentId: 'PAY-5FB60654T5508144abcxyzZLQ',
    token: 'EC-26U68825EW2123428',
    PayerID: 'QSABTRW6AHYH6'
}

然后我使用以下代码执行了付款。
function executePayPal(req, paymentId, payerId, executedPayPal) {
    sails.log.info('in executedPayPal');
    var details = {
        "payer_id": payerId
    };
    var payment = paypal.payment.execute(paymentId, details, function(error, payment) {
        if (error) {
            sails.log.error('error in payment id in executePayPal function of paypal controller', error);
            var err = JSON.stringify(error);
            var errParsed = JSON.parse(err);
            crashHandlingService.appCrash(errParsed, 101202);
            executedPayPal(({
                message: 'failure',
                redirect: '/paypal/error/'
            }), null);
        } else {
            executedPayPal(({
                message: 'success',
                redirect: '/paypal/success/'
            }), null);
        }
    });
}

这段代码的基本功能是:
  1. 创建付款;
  2. 将用户重定向到PayPal页面C;
  3. 捕获付款。
我实际想要实现的是:
  1. 授权付款 →
  2. 捕获付款,以便我可以在稍后的某个cronJob或服务中捕获付款。并在上述流程的中途将用户重定向到PayPal页面,我真的不知道如何授权、重定向和捕获付款。
请在此方面指导我。
请注意:我已阅读了以下PayPal文档,但无法理解。请注意,我需要在PayPal页面上显示付款详细信息以及付款页面上的优惠券代码及其折扣。 https://developer.paypal.com/docs/integration/direct/capture-payment/#authorize-the-payment https://developer.paypal.com/docs/classic/express-checkout/ht_ec-singleAuthPayment-curl-etc/ 提前感谢 :) .

1
我认为我已经找到了解决方案,通过使用以下链接,我可以做到,但不确定,我将进行测试。 https://github.com/paypal/PayPal-node-SDK/blob/af279e3087e60c13ca65c5baf3faba5fab70ce48/samples/payment/execute.js - Ahsan Hussain
1个回答

12

最终我找到了解决方案,我认为应该在这里发布它,以便其他人可以受益。

因此,基本上有4个步骤,必须使用PayPal Node SDK

  • 创建付款。
  • 重定向用户到PayPal。
  • 执行已授权的付款。
  • 捕获已授权的付款。

要创建支付,您可以使用PayPal Node sdk的payment.create方法 这里

.Create方法将以“href”的形式返回url链接,以将用户重定向到PayPal页面,因此重定向用户到PayPal页面。

从PayPal页面返回后,您必须使用PayPal Node SDK的.execute方法这里,使用从PayPal收到的payer_id和paymentId。

当您需要捕获已授权交易的金额时,您必须使用PayPal Node SDK的.authorization.capture方法这里,提供在执行授权响应中接收的金额和17位数字授权ID。

如果将来链接无法使用,可以在以下提供的代码和响应示例中查看。

.create Code

var create_payment_json = {
    "intent": "authorize",
    "payer": {
        "payment_method": "paypal"
    },
    "redirect_urls": {
        "return_url": "http://return.url",
        "cancel_url": "http://cancel.url"
    },
    "transactions": [{
        "item_list": {
            "items": [{
                "name": "item",
                "sku": "item",
                "price": "1.00",
                "currency": "USD",
                "quantity": 1
            }]
        },
        "amount": {
            "currency": "USD",
            "total": "1.00"
        },
        "description": "This is the payment description."
    }]
};

paypal.payment.create(create_payment_json, function (error, payment) {
    if (error) {
        console.log(error.response);
        throw error;
    } else {
        for (var index = 0; index < payment.links.length; index++) {
        //Redirect user to this endpoint for redirect url
            if (payment.links[index].rel === 'approval_url') {
                console.log(payment.links[index].href);
            }
        }
        console.log(payment);
    }
});

响应示例

{
  "id": "PAY-17S8410768582940NKEE66EQ",
  "create_time": "2013-01-31T04:12:02Z",
  "update_time": "2013-01-31T04:12:04Z",
  "state": "approved",
  "intent": "authorize",
  "payer": {
    "payment_method": "credit_card",
    "funding_instruments": [
      {
        "credit_card": {
          "type": "visa",
          "number": "xxxxxxxxxxxx0331",
          "expire_month": "11",
          "expire_year": "2018",
          "first_name": "Betsy",
          "last_name": "Buyer",
          "billing_address": {
            "line1": "111 First Street",
            "city": "Saratoga",
            "state": "CA",
            "postal_code": "95070",
            "country_code": "US"
          }
        }
      }
    ]
  },
  "transactions": [
    {
      "amount": {
        "total": "7.47",
        "currency": "USD",
        "details": {
          "tax": "0.03",
          "shipping": "0.03"
        }
      },
      "description": "This is the payment transaction description.",
      "related_resources": [
        {
          "sale": {
            "id": "4RR959492F879224U",
            "create_time": "2013-01-31T04:12:02Z",
            "update_time": "2013-01-31T04:12:04Z",
            "state": "completed",
            "amount": {
              "total": "7.47",
              "currency": "USD"
            },
            "parent_payment": "PAY-17S8410768582940NKEE66EQ",
            "links": [
              {
                "href": "https://api.sandbox.paypal.com/v1/payments/sale/4RR959492F879224U",
                "rel": "self",
                "method": "GET"
              },
              {
                "href": "https://api.sandbox.paypal.com/v1/payments/sale/4RR959492F879224U/refund",
                "rel": "refund",
                "method": "POST"
              },
              {
                "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-17S8410768582940NKEE66EQ",
                "rel": "parent_payment",
                "method": "GET"
              }
            ]
          }
        }
      ]
    }
  ],
  "links": [
    {
      "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-17S8410768582940NKEE66EQ",
      "rel": "self",
      "method": "GET"
    }
  ]
}

.执行 代码

var paymentId = 'PAYMENT id created in previous step';

paypal.payment.execute(paymentId, execute_payment_json, function (error, payment) {
    if (error) {
        console.log(error.response);
        throw error;
    } else {
        console.log("Get Payment Response");
        console.log(JSON.stringify(payment));
    }
});

响应示例

{
  "id": "PAY-34629814WL663112AKEE3AWQ",
  "create_time": "2013-01-30T23:44:26Z",
  "update_time": "2013-01-30T23:44:28Z",
  "state": "approved",
  "intent": "aurthorize",
  "payer": {
    "payment_method": "paypal",
    "payer_info": {
      "email": "bbuyer@example.com",
      "first_name": "Betsy",
      "last_name": "Buyer",
      "payer_id": "CR87QHB7JTRSC"
    }
  },
  "transactions": [
    {
      "amount": {
        "total": "7.47",
        "currency": "USD",
        "details": {
          "tax": "0.04",
          "shipping": "0.06"
        }
      },
      "description": "This is the payment transaction description.",
      "related_resources": [
        {
          "sale": {
            "id": "1KE4800207592173L",
            "create_time": "2013-01-30T23:44:26Z",
            "update_time": "2013-01-30T23:44:28Z",
            "state": "completed",
            "amount": {
              "currency": "USD",
              "total": "7.47"
            },
            "transaction_fee": {
              "value": "0.50",
              "currency": "USD"
            },
            "parent_payment": "PAY-34629814WL663112AKEE3AWQ",
            "links": [
              {
                "href": "https://api.sandbox.paypal.com/v1/payments/sale/1KE4800207592173L",
                "rel": "self",
                "method": "GET"
              },
              {
                "href": "https://api.sandbox.paypal.com/v1/payments/sale/1KE4800207592173L/refund",
                "rel": "refund",
                "method": "POST"
              },
              {
                "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-34629814WL663112AKEE3AWQ",
                "rel": "parent_payment",
                "method": "GET"
              }
            ]
          }
        }
      ]
    }
  ],
  "links": [
    {
      "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-34629814WL663112AKEE3AWQ",
      "rel": "self",
      "method": "GET"
    }
  ]
}

.authorization.capture Code

var capture_details = {
    "amount": {
        "currency": "USD",
        "total": "4.54"
    },
    "is_final_capture": true
};

paypal.authorization.capture("5RA45624N3531924N", capture_details, function (error, capture) {
    if (error) {
        console.error(error);
    } else {
        console.log(capture);
    }
});

响应样例

{
  "id": "6BA17599X0950293U",
  "create_time": "2013-05-06T22:32:24Z",
  "update_time": "2013-05-06T22:32:25Z",
  "amount": {
    "total": "4.54",
    "currency": "USD"
  },
  "is_final_capture": true,
  "state": "completed",
  "parent_payment": "PAY-44664305570317015KGEC5DI",
  "links": [
    {
      "href": "https://api.sandbox.paypal.com/v1/payments/capture/6BA17599X0950293U",
      "rel": "self",
      "method": "GET"
    },
    {
      "href": "https://api.sandbox.paypal.com/v1/payments/capture/6BA17599X0950293U/refund",
      "rel": "refund",
      "method": "POST"
    },
    {
      "href": "https://api.sandbox.paypal.com/v1/payments/authorization/5RA45624N3531924N",
      "rel": "authorization",
      "method": "GET"
    },
    {
      "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-44664305570317015KGEC5DI",
      "rel": "parent_payment",
      "method": "GET"
    }
  ]
}

您可以从REST API参考PayPal-node-SDK获得更多信息。

如果响应示例因为我从PayPal网站复制而稍有变化,请谅解。

谢谢。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接