Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stripe create a customer and recurring charge in PHP

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 and monthly_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 the monthly_recurring_setupfee plan at the time of registration and if the payment is successful change the plan of the user to monthly_recurring, is this possible ?

like image 349
Criesto Avatar asked Jul 23 '15 06:07

Criesto


2 Answers

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

like image 163
Criesto Avatar answered Sep 29 '22 08:09

Criesto


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:

  1. Create the customer using the API.
  2. Create the $50 Invoice Item through the API.
  3. 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.

like image 33
koopajah Avatar answered Sep 29 '22 08:09

koopajah