paypal订单api如何指定收款人?

How to specify a payee with paypal orders api?

这是我正在学习的教程:

问题出在我的 post 请求上。 让我们把它分解成碎片。

  1. Headers
headers: {
  "Authorization": `Bearer ${accessToken}`,
  "Content-Type": "application/json"
}

我也通过 paypal api 获得访问令牌,即 https://api.sandbox.paypal.com/v1/oauth2/token

  1. Body
body: {
  intent: 'CAPTURE',
  purchase_units: [{
    amount: {
      currency_code: 'USD',
      value: '2.00'
    },
    payee: {
      email_address: "myanothersandboxaccount@gmail.com"
    }
  }]
}

但是,它不起作用,因为我得到一个错误

{
  name: 'INVALID_REQUEST',
  message: 'Request is not well-formed, syntactically incorrect, or violates schema.',
  debug_id: '2884e1b5eccee',
  details: [
    {
      field: '/',
      location: 'body',
      issue: 'INVALID_SYNTAX',
      description: 'MALFORMED_REQUEST_JSON'
    }
  ],
  links: [
    {
      href: 'https://developer.paypal.com/docs/api/orders/v2/#error-INVALID_SYNTAX',
      rel: 'information_link',
      encType: 'application/json'
    }
  ]
}

卷曲请求

curl -v -X POST https://api.sandbox.paypal.com/v2/checkout/orders \
-H "Content-Type: application/json" \
-H "Authorization: Bearer A23AALAej8Yg-4iKJBcWckiv5-ZlhYWlkmBsPuWaVngJcMigU7P-6f8P02vnOpIo8QlOJ-P3hd3K86vKo_lpSlu0-bZBj98eg" \
-d '{
  "intent": "CAPTURE",
  "purchase_units": [
    {
      "amount": {
        "currency_code": "USD",
        "value": "100.00"
      },
      "payee": {
        "email": "myanothersandboxaccount@gmail.com"
      }
    }
  ]
}'

卷曲响应

Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying 173.0.82.78...
* TCP_NODELAY set
* Connected to api.sandbox.paypal.com (173.0.82.78) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/cert.pem
  CApath: none
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Request CERT (13):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS handshake, Certificate (11):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN, server did not agree to a protocol
* Server certificate:
*  subject: C=US; ST=California; L=San Jose; O=PayPal, Inc.; OU=PayPal Production; CN=api.sandbox.paypal.com
*  start date: Jul 27 00:00:00 2020 GMT
*  expire date: Aug  1 12:00:00 2022 GMT
*  subjectAltName: host "api.sandbox.paypal.com" matched cert's "api.sandbox.paypal.com"
*  issuer: C=US; O=DigiCert Inc; OU=www.digicert.com; CN=DigiCert SHA2 High Assurance Server CA
*  SSL certificate verify ok.
> POST /v2/checkout/orders HTTP/1.1
> Host: api.sandbox.paypal.com
> User-Agent: curl/7.64.1
> Accept: */*
> Content-Type: application/json
> Authorization: Bearer A23AALAej8Yg-4iKJBcWckiv5-ZlhYWlkmBsPuWaVngJcMigU7P-6f8P02vnOpIo8QlOJ-P3hd3K86vKo_lpSlu0-bZBj98eg
> Content-Length: 212
> 
* upload completely sent off: 212 out of 212 bytes
< HTTP/1.1 201 Created
< Cache-Control: max-age=0, no-cache, no-store, must-revalidate
< Content-Length: 501
< Content-Type: application/json
< Date: Sat, 05 Dec 2020 10:30:13 GMT
< Paypal-Debug-Id: 6bd069526af1c
< 
* Connection #0 to host api.sandbox.paypal.com left intact
{"id":"674004650C383744Y","status":"CREATED","links":[{"href":"https://api.sandbox.paypal.com/v2/checkout/orders/674004650C383744Y","rel":"self","method":"GET"},{"href":"https://www.sandbox.paypal.com/checkoutnow?token=674004650C383744Y","rel":"approve","method":"GET"},{"href":"https://api.sandbox.paypal.com/v2/checkout/orders/674004650C383744Y","rel":"update","method":"PATCH"},{"href":"https://api.sandbox.paypal.com/v2/checkout/orders/674004650C383744Y/capture","rel":"capture","method":"POST"}]}* Closing connection 0

并且,通过 curl 我得到了成功的响应。我可以从我的贝宝沙箱仪表板看到一切顺利。

会是什么?

该错误基本上意味着 PayPal 无法将其收到的正文对象解析为 JSON,因此似乎 transmitted/received 不正确。

您应该首先使用命令行 curl 进行测试,以验证功能和您的 JSON 的正确性,这应该有效。

然后要获得有关您发送的内容实际出了什么问题的更多详细信息,您将需要记录发送的实际数据(不是您的代码,而是代码在执行时发送的实际数据)

一种方法是不将您的请求发送到 https://api-m.paypal.com ,而是将其发送到通过 https://requestbin.com/

提供的请求箱服务

我正在使用 node-fetch 调用 paypal api 来创建订单。尝试使用 axios,效果很好。