付款授权书使用PayPal

付款授权书使用PayPal

本文介绍了付款授权书使用PayPal-node-SDK(PayPal Express结帐)获取付款(未来)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PayPal Express Checkout,我需要做的是稍后分别授权和捕获付款,我目前的PayPal流程是

I'm working with PayPal Express Checkout and and what i need to do is Authorize and Capture the payment later separately, my current PayPal flow is,

1)我使用以下代码创建付款:

1) I create a payment using following code:

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向我返回订单信息和重定向urls,我将用户重定向到links对象中的href.然后当付款流返回我的网站时,它会向我发送

and Paypal returns me the order information and the redirect urls and i redirected the user to the href in links object. then when the payflow returns to me website it sends me

{
    paymentId: 'PAY-5FB60654T5508144abcxyzZLQ',
    token: 'EC-26U68825EW2123428',
    PayerID: 'QSABTRW6AHYH6'
}

然后我使用以下代码执行付款.

and then i executed the payment using following code.

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

现在这段代码基本上是做什么的

Now what this code basically do is

  1. 创建付款,
  2. 将用户重定向到Paypal页面C
  3. 获取付款.

我真正想要实现的是

  1. 授权付款->
  2. 捕获付款,以便以后可以在某些cronJob或服务中捕获付款.

  1. authorize the payment ->
  2. capturing the payment so that i can capture the payment some later time in some cronJob or service.

并在上述流程的中间将用户重定向到贝宝页面,我真的不知道如何授权,重定向然后捕获付款.

And redirect the user to paypal page in the middle of above flow, i really don't know how to authorize, redirect and then capture payment.

所以请在这方面指导我.

So please guide me in this regards.

注意:我已经阅读了以下Paypal文档,但无法理解.并且请记住,我需要在贝宝页面上显示付款明细,并在付款页面上显示优惠券代码及其折扣.

Note: i've read the following paypal documentation but not able to understand. and please keep in mind that i need to show payment detail on paypal page and also the coupon code and its discount on the payment page.

https://developer.paypal.com /docs/integration/direct/capture-payment/#authorize-the-payment https://developer.paypal.com/docs/经典/快速结帐/ht_ec-singleAuthPayment-curl-etc/

预先感谢:).

推荐答案

最后,我找到了解决方案,我认为我应该在此处发布它,以便对其他人有所帮助.

Finally I've found the solution and i thought i should post it here so that it could be helpful for other.

因此,基本上有以下4个步骤,您必须使用 PayPal节点SDK

So there are basically 4 following steps and one have to you use PayPal Node SDK

  • 创建付款.
  • 将用户重定向到PayPal.
  • 执行授权付款.
  • 获取授权付款.

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

To Create Payment you can use payment.create method of PayPal Node sdk here.

.Create方法将返回"href"中的URL链接以将用户重定向到PayPal页面,因此将用户重定向到PayPal页面.

.Create Method will return url links in "href" to redirect user to PayPal page so redirect the user to PayPal page.

从Pay Payal页面返回后,您必须运行PayPal Node SDK的.execute方法,使用从PayPal获取的payer_idpaymentId.

After returning from pay PayPal page you have to run .execute method of PayPal Node SDK here, using payer_id and paymentId recived from PayPal.

最后,当您需要捕获授权交易的金额时,您必须使用PayPal Node SDK .authorization.capture方法,方法是提供金额和执行授权响应中获得的17位授权ID.

and finally when you need to capture the amount of the authorized transaction you had to .authorization.capture method of PayPal Node SDK here, by providing amount and the 17 digits authorization id recived in execute authorize response.

代码和下面提供了响应示例,以防将来链接失效.

Code & Response sample are provided below in case if the links doesn't works in future.

.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);
    }
});

Response Sample

{
  "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"
    }
  ]
}

.execute Code

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

Response Sample

{
  "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": "[email protected]",
      "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);
    }
});

Response Sample

{
  "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网站复制了响应样本,但由于某种原因样本有所变化.

Forgive me if the response sample are somehow a little bit change because i've copied it from PayPal web.

谢谢.

这篇关于付款授权书使用PayPal-node-SDK(PayPal Express结帐)获取付款(未来)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 13:46