Reputation: 5231
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);
Upvotes: 0
Views: 670
Reputation: 5231
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,
));
}
}
Upvotes: 0
Reputation: 2038
I think you are doing too much plan creation. Your code is there but once you have the plan then you dont need to recreate it. Here are simple steps.
Now from your code after you have done exception check you have created plan then you dont need to create it again after it.
$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);
I have created customer first as that would always be needed.
Upvotes: 1