2013-02-22 85 views
4

我有一個網站使用Stripe來處理訂閱付款。只有一種類型的訂閱。 我在NetTuts上按照this tutorial進行初始設置。 有一個表單處理罰款處理訂閱和一切工作。客戶請求優惠券代碼。 Stripe支持這一點,所以我着手嘗試將優惠券代碼添加到現有表單中。使用帶條形碼的優惠券代碼

我成立了優惠券代碼的條紋,設置我的測試鍵和切換的條紋測試模式。 我在我的代碼執行情侶檢查:

  1. 檢查,以確定是否進入了一個優惠券,如果沒有優惠券的選擇創建一個新的客戶對象
  2. 查看一下優惠券是否有效,如果不返回錯誤

如果已經輸入優惠券並且它是有效的,那麼在創建新客戶時作爲選項傳遞匹配的條帶優惠券對象。

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中填充表單字段正確地以同樣的方式其他所有字段填充,我三重檢查它是否以正確的。

當我嘗試提交表單沒有優惠券代碼,它的費用的全部金額,並通過條紋傳遞正確。

但是如果我輸入一個有效或無效的優惠券代碼,創建一個新的客戶對象,並通過條紋時收取的全部金額,當它不通過與客戶對象的優惠券對象。

我已經看了好幾個小時的代碼,似乎無法找出它爲什麼總是沒有認識到的優惠碼和優惠券匹配對象傳遞給條紋。

回答

4

這可能有點過時了,你找到了你的代碼的解決方案。但是您似乎只需將原始$ couponCode作爲Coupon的數組值傳遞給您。正如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) 
       ); 
     } 
+0

是的。我以爲我通過優惠券代碼,但我有一些變量名混合起來,所以它實際上並沒有通過。感謝您的答案,但我相信它會幫助很多來自谷歌的人。 – biggles 2014-03-19 15:47:06

2

望着文檔https://stripe.com/docs/api#create_customer,將Stripe_Customer:創建()調用僅查找優惠券代碼。看起來你正在傳遞整個優惠券對象。

除非您的第一次嘗試抓取失敗,否則$ couponCode已經有了您的優惠券代碼。此外,您還需要執行其他幾項檢查來確定優惠券是否有效。例如,如果優惠券 - > times_redeemed <優惠券 - > max_redemptions,或者優惠券 - > redeem_by已通過等。您可能還想檢查客戶是否已在使用優惠券,方法是檢查客戶折扣對象。

如果這些檢查失敗,只需設置$ using_discount = false;

相關問題