Stripe: Add metadata to subscription on creation
Asked Answered
A

2

13

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

Artifice answered 19/11, 2016 at 2:21 Comment(0)
A
13

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)
));
Artifice answered 19/11, 2016 at 18:53 Comment(1)
Nice, thanks. Rather than generating a random number for the customer ID, you can let Stripe handle this for you - don't specify an ID when creating the customer, and then when creating the subscription you can access the ID that Stripe assigns in $customer['id']Hold
T
0

It actually works with ONE call to create the Customer record and setup the metadata:

 $stripe = new \Stripe\StripeClient(getenv('STRIPE_SECRET_KEY'));
 
   $newCustomer = $stripe->customers->create([
    'name' => 'Customer Name',
    'email' => '[email protected]',
    'metadata' => ['metadata_key'=>'metadata_value']
  ]);
Tamekia answered 24/3, 2024 at 19:21 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.