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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么