当前位置: 首页 > 知识库问答 >
问题:

付款授权

徐子石
2023-03-14

我正在使用PayPal快速结帐,我需要做的是稍后单独授权和捕获付款,我目前的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对象。然后,当支付流返回到我的网站时,它会发送给我

{
    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. 创建付款,

我真正想要实现的是

>

  • 授权付款-

    并将用户重定向到上面流程中间的paypal页面,我真的不知道如何授权、重定向和捕获付款。

    所以请在这方面指导我。

    注意:我已经阅读了以下paypal文档,但无法理解。请记住,我需要在paypal页面上显示付款详细信息,并在付款页面上显示优惠券代码及其折扣。

    https://developer.paypal.com/docs/integration/direct/capture-payment/#authorize-the-paymenthttps://developer.paypal.com/docs/classic/express-checkout/ht_ec-singleAuthPayment-curl-etc/

    提前感谢:)。


  • 共有1个答案

    吉泰宁
    2023-03-14

    最后,我找到了解决方案,我想我应该把它贴在这里,这样可以对其他人有所帮助。

    因此,基本上有以下4个步骤,其中一个步骤必须使用PayPal Node SDK

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

    要创建付款,可以使用Payment。在这里创建PayPal Node sdk的方法。

    .Create方法将返回“href”中的url链接,以将用户重定向到PayPal页面,从而将用户重定向到PayPal页面。

    从PayPal页面返回后,您必须运行。在这里执行PayPal Node SDK的方法,使用从PayPal接收的payer_idpaymentId

    authorization.capture节点SDK的PayPal方法,通过提供金额和在执行授权响应中接收的17位授权id。

    密码

    。创建代码

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

    。批准捕获代码

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

    您可以从RESTAPI参考和PayPal节点SDK获得更多信息。

    如果回复样本有点变化,请原谅,因为我是从PayPal网站复制的。

    谢谢。

     类似资料:
    • 我有问题让贝宝接受从我的应用程序的实时支付。我正在使用一个Azure服务器,用C#编写代码,带有一个基于Xamarin的Android客户端应用程序。我可以得到所有工作与贝宝沙箱,但它失败时,我切换到实时凭据。下面是我的付款流程: 1)在我的Android应用程序中,启动PayPal应用程序授权支付。这返回一个授权代码。该代码类似于C21AAHHVXYJKTRK82SOMDGL6SSQD85LLM

    • Docs提供HTTP请求作为“捕获授权付款”的示例:https://developer.paypal.com/docs/archive/payments/authorize-and-capture-payments/ Checkout Java SDK只有捕获订单的示例:https://github.com/paypal/Checkout-Java-SDK/blob/develop/checkou

    • 我已经创建了一个授权的网络ARB会员。我可以在运行ARB脚本时创建任何成员。但在第二次付款后我怎么知道呢。除了电子邮件之外,还有其他方式吗?在ARB期间的每笔交易发生后,授权的网络将直接从授权的网络直接抛出一个url,我网站上的会员资格将在我的网站上自动更新。 这是我下载ARB代码并实现订阅的URL。 http://developer.authorize.net/downloads/samplec

    • EasyWeChat 4.0.7+ 该模块需要用到双向证书,请参考:https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=4_3 企业付款到用户零钱 $app->transfer->toBalance([ 'partner_trade_no' => '1233455', // 商户订单号,需保持唯一性(只能是字母或

    • 说明 用于门店扫客人的付款码进行支付结账,支持微信支付、支付宝支付 请求地址 http://api.dc78.cn/Api/cash_code_pay 请求方式 POST 请求参数 参数 参数名称 必填 描述 范例 table 桌台 否 桌台号 2288 bzid 结算业务单号 否 建议提供,请确保本门店内唯一 201805180018 amt 结账金额 是 208.80 auth_code 付款

    • 用于门店扫客人的付款码进行支付结账,支持微信支付、支付宝支付 此协议的返回有两种情况:一是扫码后无需密码直接完成交易,则协议马上返回。二是需要用户输入密码,返回status=0,请根据state的内容分别进行后续处理。 如果为WAIT表示用户正在输入密码,请调用协议2.7继续查询支付状态。 如果state为RETRY为服务器未能返回结果,也请继续调用2.7继续查询支付状态。 建议5秒一次重试,直到