Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am using Stripe as a payment gateway. Now there's a big problem bothers me.

I used code below to create a subscription:

<?php
require_once('lib/Stripe.php');

Stripe::setApiKey(API_KEY);

$token = $_POST['stripeToken'];

$customer = Stripe_Customer::create(array(
    "card"  => $token,
    "plan"  => $_POST['plan'],
    "email" => "[email protected]",
));

This works fine, but I can not get Charge ID from $customer, 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.

like image 544
david30xie Avatar asked Aug 20 '13 15:08

david30xie


People also ask

How do I get my Stripe charge ID?

1 Answer. Show activity on this post. $charge = \Stripe\Charge::create(array( "amount" => $price, "currency" => "usd", "customer" => $custID, // obtained with Stripe.

How do I deal with failed subscription payments on Stripe?

If a payment fails for a subscription invoice, your customers can use the Stripe-hosted page to: View the details and amounts for the failed invoice and the associated subscription. Add a new card payment method for their subscription for the payment that's due and for future subscription payments.

How do Subscriptions work within stripe?

Learn how subscriptions work within Stripe. With Subscriptions, customers make recurring payments for access to a product. Subscriptions require you to retain more information about your customers than one-time purchases do because you need to charge customers in the future. Use the following core API resources to build and manage subscriptions:

What do you need to know about stripe customer objects?

What your business offers — whether that’s a good or a service. How much and how often to charge for products, including how much the product costs, what currency to use, and the interval if the price is for subscriptions. Stripe Customer objects allow you to perform recurring charges for the same customer, and to track multiple charges.

How do I manage recurring charges with stripe?

Notify your customer that they must authenticate. Complete authentication using stripe.ConfirmCardPayment. You can see an example of this in the fixed-price guide. Stripe handles recurring charges for you automatically. This includes: Automatically invoicing customers and attempting payments when new billing cycles start.

Do I charge my customers immediately when I create a subscription?

This means you don’t immediately charge your customer when you create the subscription. Even though you don’t charge customers for the first invoice, authenticating and authorizing their card is often beneficial as it can increase the chance that the first non-zero payment completes successfully.


4 Answers

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.

like image 159
colinm Avatar answered Oct 07 '22 10:10

colinm


I just ran into the same issue myself. I'm using the python library but the answer is more about Stripe's API than what language the client is in.

Ultimately, since I'm creating a new customer with each subscription, I was able to look up the invoice against the customer_id and grab its charge id. Here's what the python code for that looks like:

stripe_api.Invoice.all(customer=subscribe_result['customer'])['data'][0]['charge']

Again, note that this method would not work if you re-use customers, only if creating new customers with each subscription create.

It's certainly not ideal. It would be far better if the charge id were included in the return. Even knowing the Invoice ID would at least solve the issue of re-using customers though it would still require an unnecessary API call to fetch the invoice.

like image 34
Jason Lantz Avatar answered Oct 07 '22 09:10

Jason Lantz


Another option you can use if you need the charge id right away and can't wait for a webhook is use the latest_invoice field on the returned subscription object.

A python example:

inv_id = subscription.latest_invoice
inv = stripe.Invoice.retrieve(inv_id)
charge_id = inv.charge
like image 20
RoHS4U Avatar answered Oct 07 '22 10:10

RoHS4U


Well there is no straight-forward way to do this. There is however a hack to get charge_id for that subscription with out waiting for invoice.payment_succeeded callback.

This how I did in Ruby, you can treat this as a pseudo code. May be you can do this using PHP APIs

  # Lets not retrieve all the invoices
  #  use filter
  lower_limit_date = DateTime.strptime(that_subscription.start.to_s, '%s') - 1.hour
  upper_limit_date = 2.hours.from_now

  list_object_of_all_invoices_in_range = Stripe::Invoice.all(
      {
          customer: customer_id,
          date: {
              gt: lower_limit_date.to_i, # Start TimeStamp
              lt: upper_limit_date.to_i # End TimeStamp
          }
      })

  particular_invoice = list_object_of_all_invoices_in_range.data.
      keep_if { |s| s[:subscription] == that_subscription.id }.first

  stripe_charge_id = particular_invoice.charge # gives charge_id

See structure of ListObject for Invoices

like image 45
illusionist Avatar answered Oct 07 '22 10:10

illusionist