无法捕获沙盒 PayPal 付款

Cannot capture sandbox PayPal payment

我目前正在尝试使用 Postman 的 PayPal Orders API,但无法捕获任何付款。

现在,我可以获得访问令牌,将其设置为 collection 变量,然后使用(注意访问令牌在授权选项卡中设置)创建订单:

POST https://api-m.sandbox.paypal.com/v2/checkout/orders

Body:
{
  "intent": "CAPTURE",
  "purchase_units": [
    {
      "amount": {
        "currency_code": "USD",
        "value": "10.00"
      }
    }
  ]
}

请求成功并得到响应 body:

{
    "id": "<random-id>",
    "status": "CREATED",
    "links": [
        {
            "href": "https://api.sandbox.paypal.com/v2/checkout/orders/<random-id>",
            "rel": "self",
            "method": "GET"
        },
        {
            "href": "https://www.sandbox.paypal.com/checkoutnow?token=<random-id>",
            "rel": "approve",
            "method": "GET"
        },
        {
            "href": "https://api.sandbox.paypal.com/v2/checkout/orders/<random-id>",
            "rel": "update",
            "method": "PATCH"
        },
        {
            "href": "https://api.sandbox.paypal.com/v2/checkout/orders/<random-id>/capture",
            "rel": "capture",
            "method": "POST"
        }
    ]
}

然后我使用浏览器 https://www.sandbox.paypal.com/checkoutnow?token=<random-id> 继续 rel:approve 的 link 并使用我的沙盒帐户登录。它向我显示了通常的付款 window 但是当我按下“继续”按钮时,它试图重定向到 return 页面但相反,刷新了页面本身。

当我尝试使用 rel:self 的 link 检查订单时:GET https://api.sandbox.paypal.com/v2/checkout/orders/<random-id>。它正确显示了沙盒帐户的运输详细信息(名称和地址),但 status 仍然是 CREATED(不是 APPROVEDCOMPLETED):

{
    "id": "<random-id>",
    "intent": "CAPTURE",
    "status": "CREATED",
    "purchase_units": [
        {
            "reference_id": "default",
            "amount": {
                "currency_code": "USD",
                "value": "10.00"
            },
            "payee": {
                "email_address": "<payee-email>",
                "merchant_id": "<payee-id>"
            },
            "shipping": {
                "name": {
                    "full_name": "<payer-name>"
                },
                "address": {
                    "address_line_1": "<payer-address-1>",
                    "address_line_2": "<payer-address-2>",
                    "admin_area_2": "<payer-address-3>",
                    "admin_area_1": "<payer-address-4>",
                    "postal_code": "<payer-address-5>",
                    "country_code": "<payer-address-6>"
                }
            }
        }
    ],
    "create_time": "<time-of-post-request>",
    "links": [
        {
            "href": "https://api.sandbox.paypal.com/v2/checkout/orders/<random-id>",
            "rel": "self",
            "method": "GET"
        },
        {
            "href": "https://www.sandbox.paypal.com/checkoutnow?token=<random-id>",
            "rel": "approve",
            "method": "GET"
        },
        {
            "href": "https://api.sandbox.paypal.com/v2/checkout/orders/<random-id>",
            "rel": "update",
            "method": "PATCH"
        },
        {
            "href": "https://api.sandbox.paypal.com/v2/checkout/orders/<random-id>/capture",
            "rel": "capture",
            "method": "POST"
        }
    ]
}

当我尝试使用 rel:caputure 的 link 获取付款时:POST https://api.sandbox.paypal.com/v2/checkout/orders/<random-id>/capture with header Content Type: application/json 和空 body,它说“付款人尚未批准付款订单”,尽管我之前从 GET 请求中获取了运输详细信息:

{
    "name": "UNPROCESSABLE_ENTITY",
    "details": [
        {
            "issue": "ORDER_NOT_APPROVED",
            "description": "Payer has not yet approved the Order for payment. Please redirect the payer to the 'rel':'approve' url returned as part of the HATEOAS links within the Create Order call or provide a valid payment_source in the request."
        }
    ],
    "message": "The requested action could not be performed, semantically incorrect, or failed business validation.",
    "debug_id": "6a10ea489ffce",
    "links": [
        {
            "href": "https://developer.paypal.com/docs/api/orders/v2/#error-ORDER_NOT_APPROVED",
            "rel": "information_link",
            "method": "GET"
        }
    ]
}

我有三个问题:

  1. 我是否正确使用了指令 API?我是否错过了一些 HTTP 请求 and/or 一些关键步骤?
  2. 我为我的沙盒应用程序设置了 return URL,为什么支付页面没有重定向我而是自行刷新?我是不是错过了一些事先的设置?
  3. 为什么我没有像上面那样捕获付款?

P.S。经过一番挖掘,我想我可能会错过 authorize payment step 但我不知道该怎么做。 (Client-side请求?Server-side请求?)

I proceeded to rel:approve's link .. when I pressed the "Continue" button, it tried to redirect to the return page but instead, refreshed the page itself.

您没有指定 return_url ,所以 return 无处可去。提神就够了

你应该做的是不重定向到批准 URL,并集成 没有重定向 。为此,请在您的服务器上创建两条路由,一条用于 'Create Order',一条用于 'Capture Order'、documented here。这些路由应该 return 只有 JSON 数据(没有 HTML 或文本)。后者应该(成功时)在执行 return(特别是 purchase_units[0].payments.captures[0].id、PayPal 交易 ID)

之前将付款详细信息存储在您的数据库中

将这两条路线与以下批准流程配对:https://developer.paypal.com/demo/checkout/#/pattern/server

我也遇到了这个问题,我通过扩展请求正文解决了它,就像@preston-phx 说的那样,使用 return URL,它看起来像这样:

{
    "intent": "CAPTURE",
    "payer": {
      "email_address": requestBody.payer_email
    },
    "purchase_units": [{
      "amount": {
        "currency_code": "USD",
        "value": requestBody.amount
      },
      "payee": {
        "email_address": requestBody.payee_email
      },
      "payment_instruction": {
        "disbursement_mode": "INSTANT",  // can be INSTANT or DELAYED
        "platform_fees": [
          {
            "amount": {
              "currency_code": "USD",
              "value": calculateFeesFromAmount(requestBody.amount)
            }
          }
        ]
      }
    }],
    "redirect_urls": {
      "return_url": "https://example.com/paypalpay/order/approved",
      "cancel_url": "https://example.com/paypalpay/order/cancelled"
    },
    "application_context": {
      "brand_name": "Header for payment page",
      "locale": "en-US",
      "landing_page": "BILLING", // can be NO_PREFERENCE, LOGIN, BILLING
      "shipping_preference": "NO_SHIPPING" // because I didn't want shipping info on the page,
      "user_action": "PAY_NOW",  // Button name, can be PAY_NOW or CONTINUE
      "return_url": "https://example.com/paypalpay/order/approved",
      "cancel_url": "https://example.com/paypalpay/order/cancelled"
    }
  }

这也帮助我在一定程度上定制了支付页面。我希望 Paypal 人员将这些内容包含在正确位置的文档中,大多数开发人员必须挖掘大量文档才能创建广泛的可用请求正文。