使用带条纹的优惠券代码

时间:2013-02-22 20:47:55

标签: php javascript stripe-payments

我有一个使用Stripe来处理订阅付款的网站。只有一种类型的订阅。 我在NetTuts上关注this tutorial进行初始设置。 有一个表单正常处理订阅和一切工作。客户要求优惠券代码。 Stripe支持这一点,因此我开始尝试将优惠券代码添加到现有表单中。

我在Stripe中设置优惠券代码,设置我的测试键并切换到条带中的测试模式。 我在代码中执行了几项检查:

  1. 检查是否输入了优惠券,如果没有优惠券选项则创建新的客户对象
  2. 检查优惠券是否有效,如果不是则返回错误
  3. 如果输入了优惠券并且有效,则在创建新客户时将匹配的条纹优惠券对象作为选项传递。

     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正确填充表单字段,就像填充所有其他字段一样,我已经三次检查它是否被正确拉出。

    当我尝试在没有优惠券代码的情况下提交表单时,它会收取全部金额并正确地通过Stripe。

    但是,如果我输入有效的OR无效优惠券代码,则在创建新的客户对象时,它不会传递带有客户对象的优惠券对象,并在通过Stripe时收取全部金额。

    我看了几个小时的代码,似乎无法弄清楚为什么总是无法识别折扣代码并将匹配的优惠券对象传递给Stripe。

2 个答案:

答案 0 :(得分:4)

这可能有点过时,您找到了代码的解决方案。但看起来您需要做的就是将原始$ couponCode作为优惠券的数组值传递。正如codasaurus所说,你只是从优惠券的条纹中取回一个数组,除非你输入$ cCode-> id并将ID传回你的数组以创建客户,否则你不需要这个数组。

我将$ using_discount设置为true时更改了,因为如果优惠券有效,这会触发优惠券代码。

优惠券实际有效后,我们会发送优惠券。您只需要提交状态的值,因此$ coupon是系统中折扣的参考。或者你可以使用$ coupon-> id,如果你想以这种方式创建它。

以下是基于您的代码的解决方案,它可能会更好,但我希望它可以帮助其他人寻找像我这样的解决方案。

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)
                );
        }

答案 1 :(得分:2)

查看文档https://stripe.com/docs/api#create_customer,Stripe_Customer :: create()调用仅查找优惠券代码。看起来你正在传递整个优惠券对象。

除非您的第一次尝试捕获失败,否则$ couponCode已经有您的优惠券代码。此外,还需要执行其他几项检查以确定优惠券是否有效。例如,如果优惠券 - > times_redeemed<优惠券 - > max_redemptions,或者如果优惠券 - > redeem_by已经过,等等。您可能还想通过检查客户折扣对象来检查客户是否已经在使用优惠券。

如果其中任何一项检查失败,只需设置$ using_discount = false;

相关问题