如何在使用 PayPal rest API 执行付款之前更改运费

Posted

技术标签:

【中文标题】如何在使用 PayPal rest API 执行付款之前更改运费【英文标题】:How can I change shipping amount before execute payment using PayPal restAPI 【发布时间】:2014-12-08 15:44:58 【问题描述】:

我尝试将我们基于 PayPal 经典 API 的旧 express checkout 流程迁移到新的 RestAPI ,但是在新流程中出现了一个问题:

如何在创建付款后和使用 PayPal restAPI 执行付款之前更改运费?

创建付款后的请求:


  "intent": "sale",
  "payer": 
    "payment_method": "paypal"
  ,
  "transactions": [
    
      "amount": 
        "currency": "AUD",
        "total": "100",
        "details": 
          "shipping": "10",
          "subtotal": "75",
          "tax": "15"
        
      ,
      "description": "Transaction description.",
      "item_list": 
        "items": [
          
            "quantity": "5",
            "name": "Item Name",
            "price": "15",
            "currency": "AUD",
            "sku": "sku"
          
        ]
      
    
  ],
  "redirect_urls": 
    "return_url": "http://localhost:56508/PaymentWithPayPal.aspx?guid=77096",
    "cancel_url": "http://localhost:56508/PaymentWithPayPal.aspx?guid=77096"
  

    

执行付款前的请求:


  "id": "PAY-12X5617352981972MKQ64KKA",
  "transactions": [
    
      "amount": 
        "currency": "AUD",
        "total": "101",
        "details": 
          "shipping": "11",
          "subtotal": "75",
          "tax": "15"
        
      
    
  ]

执行付款后 PayPal 回复:


  "id": "PAY-12X5617352981972MKQ64KKA",
  "create_time": "2014-10-15T00:51:52Z",
  "update_time": "2014-10-15T00:52:43Z",
  "intent": "sale",
  "payer": 
    "payment_method": "paypal",
    "payer_info": 
      --------------------
-----------------------------
----------------------
      
    
  ,
  "transactions": [
    
      "amount": 
        "currency": "AUD",
        "total": "100.00",
        "details": 
          "shipping": "10.00",
          "subtotal": "75.00",
          "tax": "15.00"
        
      ,
      "description": "Transaction description.",
      "item_list": 
        "items": [
          
            "quantity": "5",
            "name": "Item Name",
            "price": "15.00",
            "currency": "AUD",
            "sku": "sku"
          
        ],
        "shipping_address": 
         ------------------------------
        
      ,
      "related_resources": [
        
          "sale": 
            "id": "5KX17441UU9021742",
            "create_time": "2014-10-15T00:51:52Z",
            "update_time": "2014-10-15T00:52:43Z",
            "amount": 
              "currency": "AUD",
              "total": "100.00"
            ,
            "state": "completed",
            "parent_payment": "PAY-12X5617352981972MKQ64KKA",
            "links": [
              
                "href": "https://api.sandbox.paypal.com/v1/payments/sale/5KX17441UU9021742",
                "rel": "self",
                "method": "GET"
              ,
              
                "href": "https://api.sandbox.paypal.com/v1/payments/sale/5KX17441UU9021742/refund",
                "rel": "refund",
                "method": "POST"
              ,
              
                "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-12X5617352981972MKQ64KKA",
                "rel": "parent_payment",
                "method": "GET"
              
            ]
          
        
      ]
    
  ],
  "state": "approved",
  "links": [
    
      "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-12X5617352981972MKQ64KKA",
      "rel": "self",
      "method": "GET"
    
  ]

【问题讨论】:

【参考方案1】:

您可以在创建付款后更改运费。您需要在执行付款调用中传递运费金额,它将覆盖您在创建付款时设置的运费。

创建付款时传入的请求:


  "intent": "sale",
  "payer": 
    "payment_method": "paypal"
  ,
  "redirect_urls": 
    "return_url": "http://www.fff.com",
    "cancel_url": "http://www.fff.com"
  ,
  "transactions": [
    
      "amount": 
        "total": "20.00",
        "currency": "USD",
        "details": 
          "subtotal": "18.00",
          "tax": "1.00",
          "shipping": "1.00"
        
      ,
      "description": "This is payment description.",
      "item_list": 
        "items": [
          
            "quantity": "3",
            "name": "Hat",
            "price": "2.00",
            "sku": "product12345",
            "description": "This is desc",
            "currency": "USD"
          ,
          
            "quantity": "3",
            "name": "Hat",
            "price": "2.00",
            "sku": "product12345",
            "description": "This is desc",
            "currency": "USD"
          ,
          
            "quantity": "3",
            "name": "Hat",
            "price": "2.00",
            "sku": "product12345",
            "description": "This is desc",
            "currency": "USD"
          
        ]
      
    
  ]

执行付款时:

https://api.sandbox.paypal.com/v1/payments/payment/PAY-2NX408505D489885FKQ6M7NA/execute/


  "payer_id": "KQMQZ9Y7XZYBA",
  "transactions": [
    
      "amount": 
        "total": "28.00",
        "currency": "USD",
        "details": 
          "subtotal": "18.00",
          "tax": "5.00",
          "shipping": "5.00"
        
      ,
      "description": "This is payment description.",
      "item_list": 
        "items": [
          
            "quantity": "3",
            "name": "Hat",
            "price": "2.00",
            "sku": "product12345",
            "description": "This is desc",
            "currency": "USD"
          ,
          
            "quantity": "3",
            "name": "Hat",
            "price": "2.00",
            "sku": "product12345",
            "description": "This is desc",
            "currency": "USD"
          ,
          
            "quantity": "3",
            "name": "Hat",
            "price": "2.00",
            "sku": "product12345",
            "description": "This is desc",
            "currency": "USD"
          
        ]
      
    
  ]

回应:


  "id": "PAY-2NX408505DYT5005FKQ6M7NA",
  "create_time": "2014-10-14T07:24:36Z",
  "update_time": "2014-10-14T07:29:09Z",
  "state": "approved",
  "intent": "sale",
  "payer": 
    "payment_method": "paypal",
    "payer_info": 
      "email": "XXXXXXXXXXX",
      "first_name": "Eshan Personal Test",
      "last_name": "Account",
      "payer_id": "XXXXXXXXXXXX",
      "shipping_address": 
        "line1": "cxas",
        "line2": "asa",
        "city": "FL",
        "state": "FL",
        "postal_code": "95616",
        "country_code": "US",
        "recipient_name": "Eshan Personal Test Account"
      
    
  ,
  "transactions": [
    
      "amount": 
        "total": "28.00",
        "currency": "USD",
        "details": 
          "subtotal": "18.00",
          "tax": "5.00",
          "shipping": "5.00"
        
      ,
      "description": "This is payment description.",
      "item_list": 
        "items": [
          
            "name": "Hat",
            "sku": "product12345",
            "price": "2.00",
            "currency": "USD",
            "quantity": "3",
            "description": "This is desc"
          ,
          
            "name": "Hat",
            "sku": "product12345",
            "price": "2.00",
            "currency": "USD",
            "quantity": "3",
            "description": "This is desc"
          ,
          
            "name": "Hat",
            "sku": "product12345",
            "price": "2.00",
            "currency": "USD",
            "quantity": "3",
            "description": "This is desc"
          
        ],
        "shipping_address": 
          "recipient_name": "Eshan Personal Test Account",
          "line1": "cxas",
          "line2": "asa",
          "city": "FL",
          "state": "FL",
          "postal_code": "95616",
          "country_code": "US"
        
      ,
      "related_resources": [
        
          "sale": 
            "id": "04B04851PF2563348",
            "create_time": "2014-10-14T07:24:36Z",
            "update_time": "2014-10-14T07:29:09Z",
            "amount": 
              "total": "28.00",
              "currency": "USD"
            ,
            "payment_mode": "INSTANT_TRANSFER",
            "state": "completed",
            "protection_eligibility": "ELIGIBLE",
            "protection_eligibility_type": "ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE",
            "parent_payment": "PAY-2NX408505D485005FKQ6M7NA",
            "links": [
              
                "href": "https://api.sandbox.paypal.com/v1/payments/sale/04B04851PF2563348",
                "rel": "self",
                "method": "GET"
              ,
              
                "href": "https://api.sandbox.paypal.com/v1/payments/sale/04B04851PF2563348/refund",
                "rel": "refund",
                "method": "POST"
              ,
              
                "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-2NX408505D485005FKQ6M7NA",
                "rel": "parent_payment",
                "method": "GET"
              
            ]
          
        
      ]
    
  ],
  "links": [
    
      "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-2NX408505D485005FKQ6M7NA",
      "rel": "self",
      "method": "GET"
    
  ]

如您所见,我在执行付款时已通过新的运费金额,这将是最终金额。

【讨论】:

感谢您的回复,这是我在第一步中的想法,但是当我通过执行方法传递新的运费时,PayPal 回复以批准状态和旧的运费,这意味着金额没有被覆盖在执行后检查 PayPal 响应 是的,我检查了它,这是我在执行 api 中设置的。 不确定我是否会在回复中粘贴运费。你能发布你对这两个 api 的请求吗? 我已经粘贴了我的回复,您可以看到我的新运费已应用。我在您的执行请求中看到的唯一区别是您没有像在我的请求中那样传递 item_list 对象。您可以尝试在执行付款时也传递 item_list 对象吗? 我尝试了 item_list 以及所有其他参数,但在没有应用新的运输的情况下仍然有相同的响应,这真的很奇怪..【参考方案2】:

REST 似乎不支持 Express Checkout Callback API,后者用于在 Express Checkout 中更新运费和税金总额。这并不奇怪,因为 REST 是针对移动开发人员的,他们不一定想要花费额外的带宽。

【讨论】:

没有人愿意花费额外的带宽,但即使在像我这样的移动环境中,每个人仍然需要修改机制

以上是关于如何在使用 PayPal rest API 执行付款之前更改运费的主要内容,如果未能解决你的问题,请参考以下文章

PayPal Rest API - 使用更新的交易信息执行付款

paypal Java Rest API 在付款执行前获取运输信息

paypal rest api执行错误

如何在 c# 中使用 rest api 进行 paypal 卡存在(刷卡)付款?

执行付款后调用的 PayPal REST API 取消 URL

如何使用 PayPal REST API 和 PayPal PHP SDK 保存 PayPal 帐户以备将来付款