使用Stripes Webhooks PHP

时间:2016-11-23 03:07:09

标签: api stripe-payments payment webhooks invoice

我无法使用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"
            }

所以我知道这笔付款涉及哪个帐户。希望有人知道我做错了什么。

1 个答案:

答案 0 :(得分:0)

您是否尝试过$ invoice-> lines-> data-> Metadata-> website_ref来获取您所追求的元数据?

您的发票包含订阅列表,在这种情况下只是1.每个订阅都是用户选择计划的结果。元数据存储在订阅级别,因为它特定于客户,而不是计划。