可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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" => "fakeuser@gmail.com", ));
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
.
回答1:
This is exactly what Stripe's webhooks are for. After creating a customer with an initial subscription, you'll get six webhook notifications:
customer.created
, with the customer data (which you already have if you're saving what the API returns) charge.succeeded
(or charge.failed
), which contains the initial charge data you're looking for invoice.created
, which is the associated invoice invoice.payment_succeeded
(or invoice.payment_failed
), also telling you the status of the charge customer.card.created
, with the details of the new card 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.
回答2:
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.
回答3:
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
API
s
# 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
回答4:
Came across this question while searching for how to extract subscription ID in Java. Turns out the new version of the API (1.7.1 for Java) has ID field built right into the subscription object. The same is probably true for the PHP API.