PayPal REST API - 购买后状态不会更改

时间:2016-02-05 05:30:30

标签: paypal

所以我注意到在检查付款状态时

GET /v1/payments/payment/<Payment-Id>
即使在PayPal用户购买该商品后,PayPal响应的“状态”也不会从“已创建”更改。直到我执行付款,状态才会变为“已批准”

这使得很难区分实际已被客户批准的付款与仍在处理中的付款之间的差异,因为它们都处于“已创建”状态。 json响应的唯一区别是,一旦付款实际获得批准,它似乎包含 shipping_address 。我不确定这是表示差异的标准方法。

在使用上述REST API调用时,判断客户端是否实际批准了PayPal事务的标准方法是什么?

注意:我已经设置并运行了PHP回调脚本。我正在开发一个故障安全系统,它不断检查数据库条目,以确保执行批准的付款(如果系统出现故障)。

我以为我可以使用我从CLASSIC API设置的IPN回调脚本,但似乎REST APICLASSIC API不兼容,因为IPN回调不包含事务ID或任何有用的必要信息。

似乎在使用REST API时,如果您错过了对您的网络服务器的重定向调用,那么该交易将永远丢失。

1 个答案:

答案 0 :(得分:3)

正确的做法是,您不会在查找API响应中的state字段中告知已批准的付款,而是在JSON正文中查找payer对象,并且信息将表明您已批准的付款资源。

以下是客户重定向(用户批准)之前/之后相同PAY-ID的JSON响应

  1. 查询付款/v1/payments/payment/PAY-9J02491382988403BK3BMC6I(在用户批准之前):

    {
       "id": "PAY-9J02491382988403BK3BMC6I",
       "intent": "sale",
       "state": "created",
       "cart": "07U14103P0008801U",
       "transactions": [   {
          "amount":       {
             "total": "80.00",
             "currency": "USD"
          },
          "payee": {"email": "USM@email.com"},
          "invoice_number": "55a460ff696br",
          "item_list":       {
             "items":          [
                            {
                   "name": "Test Ticket 1",
                   "sku": "55a460ff65f13",
                   "price": "10.00",
                   "currency": "USD",
                   "quantity": 1
                },
                            {
                   "name": "Test Ticket 2",
                   "sku": "55a460ff66c7a",
                   "price": "20.00",
                   "currency": "USD",
                   "quantity": 2
                },
                            {
                   "name": "Test Ticket 3",
                   "sku": "55a460ff66ce2",
                   "price": "10.00",
                   "currency": "USD",
                   "quantity": 3
                }
             ],
             "shipping_address":          {
                "recipient_name": "Test Name",
                "line1": "Main St 1",
                "city": "San Jose",
                "state": "CA",
                "postal_code": "95131",
                "country_code": "US"
             }
          },
          "related_resources": [],
          "notify_url": "https://bt-pduan-1.c9.io/ipn.php"
       }],
       "redirect_urls":    {
          "return_url": "http://localhost:80/getpaypal?paymentId=PAY-9J02491382988403BK3BMC6I",
          "cancel_url": "http://localhost:80/cancel"
       },
       "create_time": "2016-02-16T06:28:08Z",
       "update_time": "2016-02-16T06:28:08Z",
       "links":    [
                {
             "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-9J02491382988403BK3BMC6I",
             "rel": "self",
             "method": "GET"
          },
                {
             "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-9J02491382988403BK3BMC6I/execute",
             "rel": "execute",
             "method": "POST"
          },
                {
             "href": "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-07U14103P0008801U",
             "rel": "approval_url",
             "method": "REDIRECT"
          }
       ]
    }
    
  2. 在用户批准后查找付款(我正在移除一些JSON内容,例如事务/ url数组的可读性):

    {
       "id": "PAY-9J02491382988403BK3BMC6I",
       "intent": "sale",
       "state": "created",
       "cart": "07U14103P0008801U",
       "payer":    {
          "payment_method": "paypal",
          "status": "VERIFIED",
          "payer_info":       {
             "email": "USP@email.com",
             "first_name": "Payer",
             "last_name": "US",
             "payer_id": "8FMFQ2KVYYHTY",
             "shipping_address":          {
                "recipient_name": "Test Name",
                "line1": "Main St 1",
                "city": "San Jose",
                "state": "CA",
                "postal_code": "95131",
                "country_code": "US"
             },
             "phone": "408-743-9795",
             "country_code": "US",
             "billing_address":          {
                "line1": "1 Main St",
                "line2": "",
                "city": "San Jose",
                "state": "CA",
                "postal_code": "95131",
                "country_code": "US"
             }
          }
       },
       "transactions": [],
       "redirect_urls": {},
       "create_time": "2016-02-16T06:28:08Z",
       "update_time": "2016-02-16T06:28:08Z",
       "links": []
    }
    
  3. 通过在API响应中检查记录的PAY-ID查找payer对象,您将能够保存订单并继续执行调用,以防在客户返回重定向中丢失它。

    另外,IPN或webhooks在这种情况下都不会有帮助,因为它们是由事务级别事件触发的异步消息,这意味着在执行付款之前不会发出通知。