无法捕获沙箱 PayPal 付款

时间:2021-06-15 20:17:30

标签: paypal paypal-sandbox

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

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

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

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

请求成功,响应正文:

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

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

当我尝试使用 rel:self 的链接检查订单时: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 的链接:POST https://api.sandbox.paypal.com/v2/checkout/orders/<random-id>/capture 与标题 Content Type: application/json 和空正文获取付款时,它说“付款人尚未批准付款订单”,尽管我之前从 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. 我是否正确使用了 Orders API?我是否错过了一些 HTTP 请求和/或一些关键步骤?
  2. 我为沙盒应用程序设置了返回 URL,为什么付款页面没有重定向我而是自行刷新?我之前错过了一些设置吗?
  3. 为什么我无法像上述那样获取付款?

附言经过一番挖掘,我想我可能会错过 authorize payment step 但我不知道该怎么做。 (客户端请求?服务器端请求?)

2 个答案:

答案 0 :(得分:1)

<块引用>

我继续访问 rel:approve 的链接 .. 当我按下“继续”按钮时,它尝试重定向到返回页面,但反而刷新了页面本身。

您没有指定 return_url ,因此无处可返回。刷新是所有可以做的。

您应该做的不是重定向到批准 URL,而是集成无重定向。为此,在您的服务器上创建两条路由,一条用于“创建订单”,另一条用于“捕获订单”,documented here。这些路由应该只返回 JSON 数据(没有 HTML 或文本)。后者应该(成功时)在返回之前将付款详细信息存储在您的数据库中(特别是 purchase_units[0].payments.captures[0].id,PayPal 交易 ID)

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

答案 1 :(得分:0)

我也遇到了这个问题,我通过扩展请求正文来解决它,就像@preston-phx 所说的那样,使用返回 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 人员将这些包含在文档中的正确位置,大多数开发人员必须深入研究大量文档才能创建一个广泛的、可用的请求正文。