Stripe create a customer and recurring charge in PHP
Solution 1
I found a way to create a customer, register them to a plan and charge them a one-time setup fee. Here is the code that I have used:
$customer = \Stripe\Customer::create(array(
'source' => $token,
'email' => $billing_email,
'plan' => $stripePlan,
'account_balance' => $setupFee,
'description' => "Charge with one time setup fee"
));
This will charge them a one time set up fee 'account_balance' => $setupFee,
, register them to a plan and charge them the amount of the plan.
Stripe one-time subscription fee
Solution 2
To charge a customer for the same price each month you need to use Stripe's subscriptions. If you subscribe a customer to a $50 monthly plan he will be charged automatically each month for $50 without any manual work from you.
As for the setup fee of $50 for the first month, what you need is too look at Invoice Items.
Here's the flow you would follow:
- Create the customer using the API.
- Create the $50 Invoice Item through the API.
- Create the subscription to the $50 monthly plan using the API.
The last step automatically creates the first invoice for the subscription and adds the invoice item to it. The customer is then charged $50 for the plan and $50 for the invoice item so $100 as expected. Then next month on the same day the customer will be charged $50 only and each month from then.
![Criesto](https://i.stack.imgur.com/WRwgL.jpg?s=256&g=1)
Criesto
I want to learn everything about programming (i know that is difficult to know everything but i try)... #SOreadytohelp
Updated on June 05, 2022Comments
-
Criesto about 2 years
I have a website where I want to integrate the Stripe payment gateway, when a user registers I want to create a customer on Stripe and charge them the first month fee for example $100 and from the next month I want to charge them $50.
How do I create the customer and then charge them simultaneously and setup recurring payment, so far I could only reach find out about one time payment system:
$charge = \Stripe\Charge::create( array( "amount" => $amount, "currency" => "usd", "source" => $token, "description" => $email ) );
For recurring payments, will I have to run this code in a cron or is there any better way ?
Thanks in advance.
EDIT:
I used the following code to create a customer first and then charge that customer with their charge id:
//Create Customer: $customer = \Stripe\Customer::create(array( 'source' => $token, 'email' => $_POST['email'], 'plan' => "monthly_recurring_setupfee", )); // Charge the order: $charge = \Stripe\Charge::create(array( 'customer' => $customer->id, "amount" => $amount, "currency" => "usd", "description" => "monthly payment", ) );
this seems to be working.
Another question: I have created two plans
monthly_recurring_setupfee
andmonthly_recurring
, the previous plan contains the amount that I want to charge plus the one time setup fee and the later plan contains the regular amount that I will be charging from the second month, I was thinking of assigning the user themonthly_recurring_setupfee
plan at the time of registration and if the payment is successful change the plan of the user tomonthly_recurring
, is this possible ?