2017-11-07 5 views
0

스트립 지급 방식으로 맞춤 기준 금액 기반 계획 코드를 관리하는 방법은 무엇입니까?Stripe php : 맞춤형 금액 기반 요금제를 관리하는 방법은 무엇입니까?

플랜 ID : basic- {간격} - {양}

1) 계획의 종료를 확인하거나하지?

if exit : 구독자에게 할당

아니요 - 새 요금제를 만듭니다.

if(!empty($recurring_duration)){ 
           try { 
            $plan = \Stripe\Plan::retrieve($planname); 
           } catch (Error\InvalidRequest $exception) { 
            $plan = \Stripe\Plan::create(array(
             "name" => "Basic Plan", 
             "id" => $planname,  
             "interval" => "$recurring_duration", 
             "currency" => strtolower($currency), 
             "amount" => $amount, 
            )); 
           } 

           $plan = \Stripe\Plan::create(array(
             "name" => "Basic Plan", 
             "id" => $planname,  
             "interval" => "$recurring_duration", 
             "currency" => strtolower($currency), 
             "amount" => $amount, 
           )); 
          } 

$customer = \Stripe\Customer::create(array(
           'email'  => $email, 
           'source' => $token 
          )); 

          if(!empty($recurring_duration)){ 

           $charge = \Stripe\Subscription::create(array(
            "customer" => $customer->id, 
            "items" => array(
             array(
             "plan" => $planname, 
            ), 
            ), 
           )); 

          }else{ 

           $charge = \Stripe\Charge::create(array(
               'customer' => $customer->id, 
               'amount'  => $amount, 
               'currency' => strtolower($currency), 
               'description' => '', 
             ) 
           ); 

          } 

          $val = BSP_add_form_data($charge); 
+0

어떤 코드를 공유 할 수 있습니까? – samiles

답변

0

먼저 모든 계획을 수립해야합니다. & 값을 확인하십시오.

<?php 
$recurring_duration = $_POST['recurring_duration']; 
$planname = "plan value"; 
$last_customer = NULL; 
while (true) { 
    $plan_list = \Stripe\Plan::all(array("limit" => 100, "starting_after" => $last_plan)); 
    foreach ($plan_list->autoPagingIterator() as $plan) { 
     if ($plan->id == $planname) { 
      $planlookfor = $plan; 
      break 2; 
     } 
    } 
    if (!$plan->has_more) { 
     break; 
    } 
    $last_plan = end($plan_list->data); 
} 

if(!empty($recurring_duration)){ 
    if(isset($planlookfor) && $planlookfor->id==$planname){ 

    }else{ 
     $plan = \Stripe\Plan::create(array(
      "name" => 'Basic Plan'.' '.$recurring_duration_text.' '.$amount/100, 
      "id" => $planname,  
      "interval" => "$recurring_duration", 
      "currency" => strtolower($currency), 
      "amount" => $amount, 
     )); 
    } 
} 
1

나는 계획 작성을 너무 많이하고 있다고 생각합니다. 귀하의 코드가 있지만 한 번 계획을 가지고 그때 당신은 그것을 다시 만들 필요가 없습니다. 다음은 간단한 단계입니다. 계획이 검색 할 수있는 경우

  1. 고객에게
  2. 확인을 만듭니다. 가능한 경우 $ plan에 저장하십시오.
  3. 기간을 반복하는 것은 다음
  4. 그렇지 후 사용자 코드에서 고객
  5. 이제

에 금액을 충전 고객에 계획을 할당 비어 있지

  • 선택하면 새로운 계획을 작성하고 $ 계획에 저장하지 않은 경우 예외를 확인했으면 계획을 세우고 그 후에 다시 만들 필요는 없습니다.

    $customer = \Stripe\Customer::create(array(
              'email'  => $email, 
              'source' => $token 
             )); 
    if(!empty($recurring_duration)){ 
              try { 
               $plan = \Stripe\Plan::retrieve($planname); 
               //got plan 
              } catch (Error\InvalidRequest $exception) { 
               //create new plan 
               $plan = \Stripe\Plan::create(array(
                "name" => "Basic Plan", 
                "id" => $planname,  
                "interval" => "$recurring_duration", 
                "currency" => strtolower($currency), 
                "amount" => $amount, 
               )); 
              } 
              $charge = \Stripe\Subscription::create(array(
               "customer" => $customer->id, 
               "items" => array(
                array(
                "plan" => $planname, 
               ), 
               ), 
              )); 
            }else{ 
              $charge = \Stripe\Charge::create(array(
                  'customer' => $customer->id, 
                  'amount'  => $amount, 
                  'currency' => strtolower($currency), 
                  'description' => '', 
                ) 
              ); 
            } 
    $val = BSP_add_form_data($charge); 
    

    항상 고객이 먼저 만들어야합니다.

  • +0

    네, 그렇지만 그 시간에 문제가있는 계획을 수확하십시오. 메시지를 반환하십시오. –