phpmetadatapaymentsubscription

Stripe: Add metadata to subscription on creation


So I've been trying to learn Stripe API recently... I'm getting there slowly but have come to a bit of a speed bump with this metadata information.

What I am looking to achieve is add metadata to the subscription when the subscription and customer are created.

This is the original script to create a customer and subscription:

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

And in the documentation I can see that there are multiple areas to add further information? Example:

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
}

What I am interested in is the two metadata elements. The first one I have figured out is the Customer metadata which can be added like so...

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

Although I am looking to add information to the second metadata tag so it will be added to the "plan" (subscription).

I have checked around and can't seem to find any answers. I'm hoping somebody might be able to push me in the right direction.

I've also read the documentation and can't find any relevant help there, although the docs can be found here:

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


Solution

  • I eventually managed to achieve this by creating two separate requests. It doesn't look as if there is a way to perform the same action using one request, you have to separate the customer and the 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)
    ));