javascript 在 Stripe 中使用优惠券代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15032918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Coupon Codes with Stripe
提问by biggles
I have a site that is using Stripeto process a subscription payments. There is only one type of subscription. I followed this tutorialon NetTuts to do the initial setup. Had a form working fine processing subscriptions and everything worked. Client requested a coupon code. Stripe supports this so I set out trying to add a coupon code to the existing form.
我有一个使用Stripe处理订阅付款的网站。只有一种订阅类型。我按照NetTuts 上的本教程进行了初始设置。有一个表单可以很好地处理订阅,一切正常。客户请求优惠券代码。Stripe 支持这一点,因此我开始尝试向现有表单添加优惠券代码。
I set up coupon codes in Stripe, set my testing keys and switched to test mode in stripe. I'm performing a couple of checks in my code:
我在 Stripe 中设置了优惠券代码,设置了我的测试键并在 Stripe 中切换到测试模式。我正在我的代码中执行一些检查:
- Check to see whether a coupon was entered, if not create a new customer object without a coupon option
- Check to see whether the Coupon is valid, if not return an error
- 检查是否输入了优惠券,如果没有则创建一个没有优惠券选项的新客户对象
- 检查Coupon是否有效,如果无效则返回错误
If there has been a coupon entered and it is valid, then pass the matching Stripe coupon object as an option when creating a new customer.
如果已输入优惠券且有效,则在创建新客户时将匹配的 Stripe 优惠券对象作为选项传递。
if(isset($couponCode) && strlen($couponCode) > 0) {
$using_discount = true;
try {
$coupon = Stripe_Coupon::retrieve($couponCode);
if($coupon !== NULL) {
$cCode = $coupon;
}
// if we got here, the coupon is valid
} catch (Exception $e) {
// an exception was caught, so the code is invalid
$message = $e->getMessage();
returnErrorWithMessage($message);
}
}
try
{
if($using_discount == true) {
$customer = Stripe_Customer::create(array(
"card" => $token,
"plan" => "basic_plan",
"email" => $email,
"coupon" => $cCode
));
}
else {
$customer = Stripe_Customer::create(array(
"card" => $token,
"plan" => "basic_plan",
"email" => $email
));
}
$couponCode is populated with the form field correctly the same way all other fields are populated, I've triple checked that it is being pulled correctly.
$couponCode 以与填充所有其他字段相同的方式正确填充表单字段,我已经三重检查它是否被正确拉出。
When I try to submit the form without a coupon code, it charges the full amount and passes through Stripe correctly.
当我尝试在没有优惠券代码的情况下提交表单时,它会收取全额费用并正确通过 Stripe。
However if I enter either a valid OR invalid coupon code, it does not pass a coupon object with the customer object when creating a new customer object and charges the full amount when passing through Stripe.
但是,如果我输入有效或无效的优惠券代码,则在创建新客户对象时不会将优惠券对象与客户对象一起传递,并在通过 Stripe 时收取全额费用。
I've looked at the code for hours and can't seem to figure out why it is always failing to recognize the discount code and pass the matching coupon object to Stripe.
我已经看了几个小时的代码,似乎无法弄清楚为什么它总是无法识别折扣代码并将匹配的优惠券对象传递给 Stripe。
采纳答案by Rob Cullen
This is probably a little out dated and you found a solution for your code. But it would seem all you need to do is pass through your original $couponCode as the array value for Coupon. As stated by codasaurus you are just getting an array back from stripe of the coupon, which you don't need unless you did $cCode->id and pass the ID back to your array to create a customer.
这可能有点过时了,您找到了代码的解决方案。但看起来你需要做的就是传递你的原始 $couponCode 作为 Coupon 的数组值。正如 codasaurus 所说,您只是从优惠券的条纹中取回一个数组,除非您执行 $cCode->id 并将 ID 传递回您的数组以创建客户,否则您不需要它。
I changed when you set the $using_discount to true, as this would trigger to send a coupon code if the coupon was valid or not.
当您将 $using_discount 设置为 true 时,我进行了更改,因为如果优惠券有效与否,这将触发发送优惠券代码。
Once the coupon is actually valid we then send the coupon. You only need the value of your submitted state so $coupon is the reference to the discount in there system. Or you could use the $coupon->id if you wanted to create it that way.
一旦优惠券实际有效,我们就会发送优惠券。您只需要提交状态的值,因此 $coupon 是那里系统中折扣的参考。或者,如果您想以这种方式创建它,则可以使用 $coupon->id。
Here is my take on a solution based on your code, it could be better, but I hope it helps others looking for a solution like me.
这是我根据您的代码对解决方案的看法,它可能会更好,但我希望它可以帮助其他人像我一样寻找解决方案。
if($coupon!='' && strlen($coupon) > 0) {
try {
$coupon = Stripe_Coupon::retrieve($coupon); //check coupon exists
if($coupon !== NULL) {
$using_discount = true; //set to true our coupon exists or take the coupon id if you wanted to.
}
// if we got here, the coupon is valid
} catch (Exception $e) {
// an exception was caught, so the code is invalid
$message = $e->getMessage();
returnErrorWithMessage($message);
}
}
if($using_discount==true)
{
$customer = Stripe_Customer::create(array(
"card" => $token,
"plan" => $plan,
"email" => $id,
"coupon"=>$coupon) //original value input example: "75OFFMEMBER" stripe should be doing the rest.
);
}
else
{
$customer = Stripe_Customer::create(array(
"card" => $token,
"plan" => $plan,
"email" => $id)
);
}
回答by Codasaurus
Looking at the documentation https://stripe.com/docs/api#create_customer, the Stripe_Customer::create() call is looking for just the coupon code. It looks like you're passing in the entire coupon object.
查看文档https://stripe.com/docs/api#create_customer, Stripe_Customer::create() 调用仅查找优惠券代码。看起来您正在传递整个优惠券对象。
Unless your first try catch fails, $couponCode already has your coupon code. Also, there are several other checks you need to perform to determine if the coupon is valid. For example, if the coupon->times_redeemed < coupon->max_redemptions, or if the coupon->redeem_by has passed, etc. You might also want to check if the customer is already using the coupon by checking the customer discount object.
除非您第一次尝试捕获失败,否则 $couponCode 已经拥有您的优惠券代码。此外,您还需要执行其他几项检查来确定优惠券是否有效。例如,如果coupon->times_redeemed <coupon->max_redemptions,或者coupon->redeem_by 已通过等。您可能还想通过检查客户折扣对象来检查客户是否已经在使用该优惠券。
If any of these checks fail, just set your $using_discount = false;
如果这些检查中的任何一个失败,只需设置您的 $using_discount = false;