Stripe:在创建时将元数据添加到订阅

Stripe: Add metadata to subscription on creation

所以我最近一直在尝试学习 Stripe API...我进展缓慢,但在使用这些元数据信息时遇到了一些障碍。

我希望实现的是在创建订阅和客户时向订阅添加元数据。

这是创建客户和订阅的原始脚本:

$customer = \Stripe\Customer::create(array(
               'email' => $_POST['stripeEmail'],
               'source'  => $_POST['stripeToken'],
               'customer' => $randomID,
               'plan' => $item
              ));

并且在文档中我可以看到有多个区域可以添加更多信息?示例:

Stripe\StripeObject JSON: {
  "id": "sub_9aZ6q72UQs7664",
  "object": "subscription",
  "application_fee_percent": null,
  "cancel_at_period_end": false,
  "canceled_at": null,
  "created": 1479520145,
  "current_period_end": 1482112145,
  "current_period_start": 1479520145,
  "customer": "XXXXXXX",
  "discount": null,
  "ended_at": null,
  "livemode": false,
  "metadata": {
  },
  "plan": {
    "id": "AdFree",
    "object": "plan",
    "amount": 700,
    "created": 1479261871,
    "currency": "gbp",
    "interval": "month",
    "interval_count": 1,
    "livemode": false,
    "metadata": {
    },
    "name": "AdFree Hosting",
    "statement_descriptor": "WEBSITE",
    "trial_period_days": null
  },
  "quantity": 1,
  "start": 1479520145,
  "status": "active",
  "tax_percent": null,
  "trial_end": null,
  "trial_start": null
}

我感兴趣的是两个metadata元素。我想出的第一个是 Customer metadata 可以这样添加...

$customer = \Stripe\Customer::create(array(
               'email' => $_POST['stripeEmail'],
               'source'  => $_POST['stripeToken'],
               'customer' => $randomID,
               'plan' => $item,
               'metadata' => array("test1" => "test2", "testa" => "testb")
              ));

虽然我希望将信息添加到第二个元数据标签,因此它会被添加到 "plan" (subscription)

我已经查过了,似乎找不到任何答案。我希望有人能够将我推向正确的方向。

我也阅读了文档,但在那里找不到任何相关帮助,尽管可以在此处找到文档:

https://stripe.com/docs/api#create_subscription

我最终通过创建两个单独的请求设法实现了这一点。看起来好像没有办法使用一个请求执行相同的操作,您必须将客户和订阅分开。

$randomID = mt_rand(10000000, 99999999);     

$customer = \Stripe\Customer::create(array(
      'email' => $_POST['stripeEmail'],
      'source'  => $_POST['stripeToken'],
      'customer' => $randomID
));

$subscription = \Stripe\Subscription::create(array(
      'customer' => $randomID,
      'plan' => $item,
      'metadata' => array("website_ref" => $website_ref, "user_id" => $user_id)
));