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

Stripe php:如何管理基于金额的定制计划?,php,stripe-payments,Php,Stripe Payments,如何在条带支付中管理基于自定义金额的计划代码 计划id:basic-{interval}-{amount} 1是否检查计划出口 如果:退出,则分配给订阅服务器 不-创建新计划 if(!empty($recurring_duration)){ try { $plan = \Stripe\Plan::retrieve($planname);

如何在条带支付中管理基于自定义金额的计划代码

计划id:basic-{interval}-{amount}

1是否检查计划出口

如果:退出,则分配给订阅服务器

不-创建新计划

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);

我认为你做的计划太多了。你的代码在那里,但是一旦你有了计划,你就不需要重新创建它了。 以下是简单的步骤

创建客户 检查是否可以检索计划。如果可以,将其存储在$plan中。 如果没有,请创建新计划并将其存储在$plan中 检查定期持续时间是否为空,然后将计划分配给客户 否则,向客户收取金额 现在,在完成异常检查后,从代码中可以看出,您已经创建了计划,之后就不需要再创建它了

$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);

我已经创建了客户至上,因为这是始终需要的。

首先需要获取所有计划并检查您的价值观

<?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,
        ));
    }
}

您可以共享任何代码吗?是的,但当选中“检索当时发布的计划”时。返回消息。