Node.js 如何使用paypal orders api指定收款人?

Node.js 如何使用paypal orders api指定收款人?,node.js,paypal,Node.js,Paypal,以下是我正在学习的教程: 问题在于我的发帖请求。 让我们把它分成几部分 标题 我也通过paypal api获得访问令牌 身体 但是,它不工作,因为我得到一个错误 { name: 'INVALID_REQUEST', message: 'Request is not well-formed, syntactically incorrect, or violates schema.', debug_id: '2884e1b5eccee', details: [ {

以下是我正在学习的教程:

问题在于我的发帖请求。 让我们把它分成几部分

  • 标题
  • 我也通过paypal api获得访问令牌

  • 身体
  • 但是,它不工作,因为我得到一个错误

    {
      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
    
    注意:不必要地使用-X或--request,POST已经推断出来。
    *正在尝试173.0.82.78。。。
    *TCP_节点集
    *已连接到api.sandbox.paypal.com(173.0.82.78)端口443(#0)
    *阿尔卑斯山,提供h2
    *ALPN,提供http/1.1
    *已成功设置证书验证位置:
    *CAfile:/etc/ssl/cert.pem
    卡帕斯:没有
    *TLSv1.2(输出),TLS握手,客户端问候(1):
    *TLSv1.2(IN)、TLS握手、服务器hello(2):
    *TLSv1.2(IN),TLS握手,证书(11):
    *TLSv1.2(IN)、TLS握手、服务器密钥交换(12):
    *TLSv1.2(IN),TLS握手,请求证书(13):
    *TLSv1.2(IN),TLS握手,服务器完成(14):
    *TLSv1.2(输出),TLS握手,证书(11):
    *TLSv1.2(输出)、TLS握手、客户端密钥交换(16):
    *TLSv1.2(OUT),TLS更改密码,更改密码规范(1):
    *TLSv1.2(输出),TLS握手,完成(20):
    *TLSv1.2(IN),TLS更改密码,更改密码规范(1):
    *TLSv1.2(IN),TLS握手,完成(20):
    *使用TLSv1.2/ECDHE-RSA-AES128-GCM-SHA256的SSL连接
    *ALPN,服务器不同意协议
    *服务器证书:
    *主题:C=美国;ST=加利福尼亚州;L=圣何塞;O=贝宝公司。;OU=贝宝生产;CN=api.sandbox.paypal.com
    *开始日期:格林威治标准时间2020年7月27日00:00:00
    *过期日期:8月1日12:00:00 2022 GMT
    *subjectAltName:主机“api.sandbox.paypal.com”匹配证书的“api.sandbox.paypal.com”
    *发行人:C=美国;O=DigiCert公司;OU=www.digicert.com;CN=DigiCert SHA2高保证服务器CA
    *SSL证书验证正常。
    >POST/v2/checkout/orders HTTP/1.1
    >主持人:api.sandbox.paypal.com
    >用户代理:curl/7.64.1
    >接受:*/*
    >内容类型:application/json
    >授权:持有人A23ALAEJ8YG-4iKJBcWckiv5-ZLHYWLKMBSUWAVNGJCMIG7P-6f8P02vnOpIo8QlOJ-P3hd3K86vKo_lpSlu0-bZBj98eg
    >内容长度:212
    > 
    *上传已完全发送:212字节中的212字节
    
    而且,通过curl,我得到了一个成功的回复。我可以从我的paypal沙盒仪表板上看到一切都进展顺利


    这可能是什么?

    该错误基本上意味着PayPal无法解析它作为JSON接收的body对象,因此似乎有什么东西被错误地传输/接收

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

    然后,为了获得更多关于您发送的内容中实际出错的细节,您需要记录正在发送的实际数据(不是您的代码,而是代码在执行时发送的实际数据)


    一种方法是将请求发送到
    https://api-m.paypal.com
    ,而是将其发送到请求bin服务,如通过提供的请求bin服务

    我使用node fetch调用paypal api来创建订单。使用axios进行了尝试,效果很好。

    您的身体是正确的,您应该能够通过命令行c进行验证url或邮递员。因此,您需要显示完整的API响应(否“…”),并可能记录您的请求,以便您可以查看实际传输的内容,因为有些错误,但我们无法从您问题中的信息中分辨出错误。感谢您的反馈。刚刚用完整的API响应更新了问题
    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