dongyingdao8867 2017-11-07 11:33
浏览 37
已采纳

Stripe php:如何管理基于自定义金额的计划?

How to managed custom amount based plans code in stripe payment?

Plan id : basic-{interval}-{amount}

1) Check plan exits or not ?

if : exits then assign to subscriber

not - Create a new plan.

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);
  • 写回答

2条回答 默认 最新

  • dongyong8098 2017-11-13 13:24
    关注

    First Need to get all plans & check with your values.

    <?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条)

报告相同问题?

悬赏问题

  • ¥15 halcon联合c#遇到了问题不能解决
  • ¥15 xshell无法连接提示ssh服务器拒绝密码
  • ¥15 AT89C52单片机C语言关于串口通信的位操作
  • ¥20 需要步骤截图(标签-服务器|关键词-map)
  • ¥50 gki vendor hook
  • ¥15 灰狼算法和蚁群算法如何结合
  • ¥15 这是一个利用ESP32自带按键和LED控制的录像代码,编译过程出现问题,请解决并且指出错误,指导如何处理 ,协助完成代码并上传代码
  • ¥20 stm32f103,hal库 hal_usart_receive函数接收不到数据。
  • ¥20 求结果和代码,sas利用OPTEX程序和D-efficiency生成正交集
  • ¥50 adb连接不到手机是怎么回事?