david-xie david-xie - 2 years ago 95
PHP Question

How to get charge id after creating a subscription using Stripe?

I am using

as a payment gateway. Now there's a big problem bothers me.

I used code below to create a subscription:



$token = $_POST['stripeToken'];

$customer = Stripe_Customer::create(array(
"card" => $token,
"plan" => $_POST['plan'],
"email" => "fakeuser@gmail.com",

This works fine, but I can not get
Charge ID
, and I found out there's no way in
Stripe API
to get it.

How to get it when creating a subscription? I really need
Charge ID

Answer Source

This is exactly what Stripe's webhooks are for. After creating a customer with an initial subscription, you'll get six webhook notifications:

  1. customer.created, with the customer data (which you already have if you're saving what the API returns)
  2. charge.succeeded (or charge.failed), which contains the initial charge data you're looking for
  3. invoice.created, which is the associated invoice
  4. invoice.payment_succeeded (or invoice.payment_failed), also telling you the status of the charge
  5. customer.card.created, with the details of the new card
  6. customer.subscription.created, with the details of the customer's subscription.

Stripe's API, like many APIs and many payment solutions, is built to be used with webhooks. If you're not taking advantage of webhooks, you're going to be missing functionality, and you're probably working too hard for what can be done without webhooks.

Stripe works to deliver the data to you. If you're writing code to poll Stripe, you're working way, way too hard.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download