简体   繁体   中英

Check If Stripe Customer Was Charged Upon Creation

So I have two ways I am creating charges in Stripe in PHP, one when I create a customer object, the other when I just use the charge object.

The latter is working perfectly, the former is breaking when I check if the customer was charged, which should happen right away because I subscribe said customer to a plan.

Here is the code that works:

    try {
$charge = \Stripe\Charge::create(array(
  "source" => $token,
  "description" => "Example charge",
  "amount" => $amount,
  "receipt_email" => $email,
 "currency" => "usd")
);
} catch(\Stripe\Error\Card $e) {
  // The card has been declined
}
if ($charge->paid == true)
{
  save_stripe_customer_id($token,$amount, $today, $type, $email);
   echo "Thank you for your donation!";
}

Notice the $charge->paid == true which is what I am checking for. I want to use this same property when creating and charging a customer, but it doesn't seem to have this method when I do this:

try {
$customer = \Stripe\Customer::create(array(
  "description" => "Customer for APCAN",
  "source" => $token,
  "plan" => $plan,
  "quantity" => $amount,
  "email" => $email
  ));
}catch(\Stripe\Error\Card $e) {
  // The card has been declined or something
}
if ($customer->paid == true)
{
  save_stripe_customer_id($customer->id,$amount, $today, $type, $email);
  echo "Thank you for your donation!";
}

Notice a similar code, $customer->paid == true but it doesn't fire. I see the charge go through in the dashboard when the customer gets created this way, but I don't get the proper echo back and the function save_stripe_customer_id() doesn't fire.

So how do I check if the customer created that way and charged using the plan was actually successfully charged?

The Stripe Customer object does not have a paid attribute you can check like the Charge object. Instead, when you create a customer and subscribe them to a plan using the Stripe API, Stripe will make sure the credit card is valid and able to be charged before returning. See Stripe doc here: https://stripe.com/docs/subscriptions

I believe the actual Invoice for the subscription is created but not necessarily charged right away. As per Stripe's doc here: https://stripe.com/docs/api#invoices

Once an invoice is created, payment is automatically attempted. Note that the payment, while automatic, does not happen exactly at the time of invoice creation. If you have configured webhooks, the invoice will wait until one hour after the last webhook is successfully sent (or the last webhook times out after failing).

I also recommend contacting Stripe Support directly with any questions as they are very quick to respond in my experience, and have been helpful in the past.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM