php - 在 Stripe 中使用优惠券代码

标签 php javascript stripe-payments

我有一个网站正在使用 Stripe处理订阅付款。只有一种订阅类型。 我关注了this tutorial在 NetTuts 上进行初始设置。 有一个表格可以很好地处理订阅,一切正常。客户要求优惠券代码。 Stripe 支持这一点,所以我开始尝试向现有表格添加优惠券代码。

我在 Stripe 中设置优惠券代码,设置我的测试 key 并切换到 Stripe 中的测试模式。 我正在我的代码中执行一些检查:

  1. 检查是否输入了优惠券,如果没有则创建一个没有优惠券选项的新客户对象
  2. 检查优惠券是否有效,如果无效则返回错误

如果输入了优惠券并且有效,则在创建新客户时将匹配的 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 以与填充所有其他字段相同的方式正确地填充了表单字段,我已经三次检查它是否被正确提取。

当我尝试提交没有优惠券代码的表单时,它会收取全额费用并正确通过 Stripe。

但是,如果我输入有效或无效的优惠券代码,它不会在创建新客户对象时将优惠券对象与客户对象一起传递,并在通过 Stripe 时收取全额费用。

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

最佳答案

这可能有点过时了,您已经为您的代码找到了解决方案。但看起来您需要做的就是将您的原始 $couponCode 作为 Coupon 的数组值传递。正如 codasaurus 所述,您只是从优惠券的 strip 中取回一个数组,除非您执行 $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)
                );
        }

关于php - 在 Stripe 中使用优惠券代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15032918/

相关文章:

javascript - 将多行 PHP 字符串插入 Javascript 变量

php - 如何用逻辑表达式初始化 bool?

javascript - 使用搜索栏动画移动图像时出现问题

ruby-on-rails - 对如何正确使用 key 感到困惑

php - 直接从数据库显示图像

javascript - gulp 网络服务器和 livereload

javascript - 所有集合的 Firestore 列表

php - Stripe 结帐不会接受元数据

javascript - AJAX请求与stripe结合

php - Memcached 没有出现在 phpinfo() 中