2016-11-23 33 views
0

我一直无法找到任何有关PHP的细节,所以我希望有人能帮我完成这个脚本?用条纹Webhooks访问订阅详细信息PHP

我正在从Stripe API Webhook事件中搜索订阅详细信息。我正在处理的事件是invoice.payment_succeeded,尽管我正在努力从此访问订阅信息。这里是充满了此次试射活动:

{ 
    "id": "evt_19HdmRL346436RYAmvgxkr", 
    "object": "event", 
    "api_version": "2016-07-06", 
    "created": 1479580899, 
    "data": { 
    "object": { 
     "id": "in_19HdmRLniq434634643dO2gU", 
     "object": "invoice", 
     "amount_due": 700, 
     "application_fee": null, 
     "attempt_count": 1, 
     "attempted": true, 
     "charge": "ch_19Hdm3463464365IDDXX", 
     "closed": true, 
     "currency": "gbp", 
     "customer": "315464619", 
     "date": 1479580899, 
     "description": null, 
     "discount": null, 
     "ending_balance": 0, 
     "forgiven": false, 
     "lines": { 
     "object": "list", 
     "data": [ 
      { 
      "id": "sub_9apRC346346CMNg", 
      "object": "line_item", 
      "amount": 700, 
      "currency": "gbp", 
      "description": null, 
      "discountable": true, 
      "livemode": false, 
      "metadata": { 
       "website_ref": "Z8ckRo2x", 
       "user_id": "1" 
      }, 
      "period": { 
       "start": 1479580899, 
       "end": 1482172899 
      }, 
      "plan": { 
       "id": "AdFree", 
       "object": "plan", 
       "amount": 700, 
       "created": 1479261871, 
       "currency": "gbp", 
       "interval": "month", 
       "interval_count": 1, 
       "livemode": false, 
       "metadata": {}, 
       "name": "AdFree", 
       "statement_descriptor": "SNAPPYSITES ADFREE", 
       "trial_period_days": null 
      }, 
      "proration": false, 
      "quantity": 1, 
      "subscription": null, 
      "type": "subscription" 
      } 
     ], 
     "has_more": false, 
     "total_count": 1, 
     "url": "/v1/invoices/in_19HdmRLn34353465dO2gU/lines" 
     }, 
     "livemode": false, 
     "metadata": {}, 
     "next_payment_attempt": null, 
     "paid": true, 
     "period_end": 1479580899, 
     "period_start": 1479580899, 
     "receipt_number": null, 
     "starting_balance": 0, 
     "statement_descriptor": null, 
     "subscription": "sub_9a2552OA5553MNg", 
     "subtotal": 700, 
     "tax": null, 
     "tax_percent": null, 
     "total": 700, 
     "webhooks_delivered_at": null 
    } 
    }, 
    "livemode": false, 
    "pending_webhooks": 1, 
    "request": "req_9apRx9555ZVm55", 
    "type": "invoice.payment_succeeded" 
} 

我目前这个未完成的脚本听:

$input = @file_get_contents("php://input"); 
$event_json = json_decode($input); 

    $event_id = $event_json->id; 
    $event = \Stripe\Event::retrieve($event_id); 


     if($event->type == 'invoice.payment_succeeded'){ 

       $invoice = $event->data->object; 
       $subscription = $invoice->lines->data->plan; 
       $customer = \Stripe\Customer::retrieve($invoice->customer); 

       print_r($subscription); 
     } 

不幸的是我没有收到来自$subscription阵列任何回应。我曾尝试过各种方法,例如; $subscription = $invoice->plan;$subscription = $invoice->data->plan;等等

我为$invoice & $customer接收数据,所以我正确认识它们的作用都。我的主要重点是检索元数据信息:

"metadata": { 
       "website_ref": "Z8ckRo2x", 
       "user_id": "1" 
      } 

所以我知道这个支付与哪个帐户有关。希望有人可能知道我做错了什么。

回答

0

您是否尝试过$ invoice-> lines-> data-> Metadata-> website_ref以获取您之后的元数据?

您的发票由订阅列表组成,本例中仅为1.每个订阅都是用户选择计划的结果。元数据存储在订阅级别,因为它只针对客户,而不是计划。