简体   繁体   English

检查是否在创建时对Stripe客户收费

[英]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. 因此,我有两种在PHP的Stripe中创建费用的方式,一种是在创建客户对象时创建的,另一种是在我仅使用计费对象时创建的。

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. 注意$charge->paid == true ,这是我要检查的内容。 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. 注意类似的代码, $customer->paid == true但不会触发。 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. 我看到,当顾客被创建这种方式在仪表板的充电经历,但我没有得到正确的echo背部和功能save_stripe_customer_id()不火。

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. Stripe Customer对象没有像Charge对象一样可以检查的paid属性。 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. 相反,当您创建客户并使用Stripe API订阅他们的计划时,Stripe将确保信用卡有效并且可以在退货前进行收费。 See Stripe doc here: https://stripe.com/docs/subscriptions 请在此处查看Stripe文档: https//stripe.com/docs/subscriptions

I believe the actual Invoice for the subscription is created but not necessarily charged right away. 我相信会创建实际的订阅Invoice ,但不一定立即收费。 As per Stripe's doc here: https://stripe.com/docs/api#invoices 根据Stripe的文档,在这里: 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). 如果您已经配置了webhook,则发票将一直等到成功发送完最后一个webhook之后的一小时(或者失败后最后一个webhook超时)。

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. 我还建议您对任何问题直接联系Stripe支持,因为根据我的经验它们可以很快地做出响应,并且过去对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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