PAYMENT.SALE.COMPLETED webhook 事件中的 PayPal 订阅 ID 和交易 ID 在哪里?如何获取他们的详细信息?

Posted

技术标签:

【中文标题】PAYMENT.SALE.COMPLETED webhook 事件中的 PayPal 订阅 ID 和交易 ID 在哪里?如何获取他们的详细信息?【英文标题】:Where is the PayPal Subscription ID and Transaction ID in a PAYMENT.SALE.COMPLETED webhook event? How to get their details? 【发布时间】:2021-06-08 19:35:45 【问题描述】:

我正在使用带有 php 的 PayPal 订阅 API。

到目前为止,我知道付款成功后会触发 PAYMENT.SALE.COMPLETED 事件。但该对象仅包含一个计费协议 ID,似乎与订阅无关。如何获得有关客户和付款的更多详细信息?

示例 webhook 的内容



    "id": "WH-1J525465EH157100Y-81K24252LY536784P",
    "create_time": "2021-03-10T18:22:33.292Z",
    "resource_type": "sale",
    "event_type": "PAYMENT.SALE.COMPLETED",
    "summary": "Payment completed for EUR 5.0 EUR",
    "resource": 
        "billing_agreement_id": "I-8XRLDA4MNEW3",
        "amount": 
            "total": "5.00",
            "currency": "EUR",
            "details": 
                "subtotal": "5.00"
            
        ,
        "payment_mode": "INSTANT_TRANSFER",
        "update_time": "2021-03-10T18:22:13Z",
        "create_time": "2021-03-10T18:22:13Z",
        "protection_eligibility_type": "ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE",
        "transaction_fee": 
            "currency": "EUR",
            "value": "0.45"
        ,
        "protection_eligibility": "ELIGIBLE",
        "links": [
            
                "method": "GET",
                "rel": "self",
                "href": "https://api.sandbox.paypal.com/v1/payments/sale/1P642136UK709905M"
            ,
            
                "method": "POST",
                "rel": "refund",
                "href": "https://api.sandbox.paypal.com/v1/payments/sale/1P642136UK709905M/refund"
            
        ],
        "id": "1P642136UK709905M",
        "state": "completed",
        "invoice_number": ""
    ,
    "status": "SUCCESS",
    "transmissions": [
        
            "webhook_url": "https://musily.de/payment/webhook/paypal.php",
            "http_status": 200,
            "reason_phrase": "HTTP/1.1 200 Connection established",
            "response_headers": 
                "Accept-Ranges": "none",
                "Server": "Apache/2.4.46 (Unix)",
                "Cache-Control": "max-age=172800",
                "Expires": "Fri, 12 Mar 2021 18:23:16 GMT",
                "Content-Length": "65",
                "Date": "Wed, 10 Mar 2021 18:23:16 GMT",
                "X-Powered-By": "PHP/7.3.27",
                "Content-Type": "text/html"
            ,
            "transmission_id": "a7472af0-81cd-11eb-aacd-47b3747d966f",
            "status": "SUCCESS",
            "timestamp": "2021-03-10T18:23:03Z"
        
    ],
    "links": [
        
            "href": "https://api.sandbox.paypal.com/v1/notifications/webhooks-events/WH-1J525465EH157100Y-81K24252LY536784P",
            "rel": "self",
            "method": "GET",
            "encType": "application/json"
        ,
        
            "href": "https://api.sandbox.paypal.com/v1/notifications/webhooks-events/WH-1J525465EH157100Y-81K24252LY536784P/resend",
            "rel": "resend",
            "method": "POST",
            "encType": "application/json"
        
    ],
    "event_version": "1.0"


【问题讨论】:

什么是结算协议 ID?查询那个。对于付款,您可以尝试developer.paypal.com/docs/api/payments/v2/#captures。不过,Webhook 应该已经包含了您需要的大部分信息 - 如果您记录问题的所有 Webhook 详细信息,将会有所帮助。 我添加了一个示例 webhook 的内容。但是没有像捕获ID这样的想法 【参考方案1】:
    "billing_agreement_id": "I-8XRLDA4MNEW3",

那是您订阅的 ID。

    "id": "1P642136UK709905M",

那是您的 PayPal 交易/销售/捕获 ID。

    "links": [
       
           "method": "GET",
           "rel": "self",
           "href": "https://api.sandbox.paypal.com/v1/payments/sale/1P642136UK709905M"
       ,
       

有如何获取有关销售的详细信息(v2/payments/captures API 也可能有效,不确定)

【讨论】:

【参考方案2】:

正如@Preston-PHX 所说 billing_agreement_id 是订阅 ID,但如果您需要获取订阅名称和类似详细信息:

    获取 billing_agreement_id 并调用订阅 api,回复将是客户的详细信息和订阅的小细节:

function getsubdetails($id)
                  
                $header = array(); 
                $header[] = 'Content-type: application/json';
                $header[] = 'Authorization: Bearer '.$this->token;
                $url = 'https://api-m.sandbox.paypal.com/v1/billing/subscriptions/'.$id;
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
                curl_setopt($ch, CURLOPT_URL,$url); 
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
                $server_output = curl_exec($ch);
                return $server_output;
                 curl_close ($ch);
                    
    获取plan_id 和调用计划api,回复将是订阅的所有详细信息:
   public function getplandetails($id)
    
     
    $header = array(); 
    $header[] = 'Content-type: application/json';
    $header[] = 'Authorization: Bearer '.$this->token;
    
    
        
    $url = 'https://api-m.sandbox.paypal.com/v1/billing/plans/'.$id;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_URL,$url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $server_output = curl_exec($ch);
    return $server_output;
    
    curl_close ($ch);
    
    
    

完整代码:

 include 'paypal.php'; //contains above code inside Paypal class
     $paypal = new Paypal;
     $paypal->setcredentials($clientid,$secret); //setting api credentials, create this function near above code
     $paypal->gettoken(); // initiating token, create this function near above code

     $subdetails = $paypal->getsubdetails($planid); //sending billing_agreement_id
     $subdetails2 = json_decode($subdetails,true);
     $planid = $subdetails2['plan_id']; //received plan_id
     $plandetails = $paypal->getplandetails($planid); // sending it to get subscription name
     $plandetails2 = json_decode($plandetails, true);
     $this->planname = $plandetails2['name']; 


【讨论】:

以上是关于PAYMENT.SALE.COMPLETED webhook 事件中的 PayPal 订阅 ID 和交易 ID 在哪里?如何获取他们的详细信息?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 php 在 PayPal 订阅中获取付款详细信息?

"verification_status":"FAILURE" 总是失败 - PayPal-Node-SDK

Sometimes we can pretend we have never been to some places.

We went through a period ______ communications we

微擎we7模块和模板安装方法

23we