Stripe Payment Integration & Development in Codeigniter

This blog gives an overview on integration of stripe payment service in Codeigniter.

There are two types of payment services we can integrate using stripe.

  • Recurring Payment.
  • One Time Payment.

First step is to create a stripe account. Use the below link to create an account.

https://dashboard.stripe.com/register

Once registered, confirm your email address using the activation link received in email. It will redirect you to a login page of stripe website. After login, you will be redirected to dashboard.

Recurring Payment

For this, we need to create a new subscription plan which will be used for recurring payment. To create a plan, click on Subscriptions from left menu -> Plans -> New. It will open a popup to create new plan as shown below.

CreateNewPlan

We need to provide details for the below fields.

ID: Used for recurring payment which can be numeric or alpha numeric.

Name: Name of your subscription plan.

Currency: Select currency in which we want to accept payments.

Amount: Amount for the subscription.

Interval: Select subscription period. There are seven options available like daily, monthly, yearly, weekly, every 3 months, every 6 months and custom. In custom, we can set our own subscription period by entering number and selecting period from drop down like month(s), week(s) or day(s).

Trial period days: This field is optional and it is used if we want to give subscription as a trial for mentioned day(s).

Statement description: A description of the payment plan which optional and appear on statement.

When someone pays for the subscription or does a recurring payment, it is mandatory to create a customer. We can create customer through a php code.

 

$card_data = array(

            ‘number’ => $this->input->post(‘card_number’),

            ‘cvc’ => $this->input->post(‘cvv’),

            ‘exp_month’ => $this->input->post(‘exp_month’),

            ‘exp_year’ => $this->input->post(‘exp_year’),

            ‘name’ => $this->input->post(’email’),

 );

 $dollars = str_replace(‘$’, ”, $this->input->post(‘amount’));

$cents = number_format((float)$dollars*100., 0, ‘.’, ”);   

$tokenArray = json_decode($this->stripe->card_token_create($card_data,$cent));

$stripeToken = $tokenArray->id;

$card = $stripeToken;

$email = $this->input->post(’email’);

$desc = $this->input->post(‘service_name’);

$plan = $this->input->post(‘plan_name’);

$customerCreateArray = json_decode($this->stripe->customer_create( $card, $email, $desc, $plan));

$customer_id = $customerCreateArray->id; //return inserted customer id

 

How to integrate stripe payment gateway in codeigniter?

We need to create a basic view file which will accept the required data from the user like card number, card expiry month, card expiry year, card CVV number etc.

We need to mention controllerMethodName in the action of the form as shown below.

<form action=”controllerMethodName” method=”post”>

         

                  

Stripe Payment

                   $ 11.50

                  

                            

                                     

Card Number

                                     

                                     

                            

                            

                                     

Email Address

                                     

                            

                  

                            

                                     

Exp. Month

                                     

                            

                            

                                     

Exp. Year

                                     

                            

                            

                                     

CVV

                                     

                            

                            

                                      Pay Now

                            

                   </div>

          </div> 

</form>

To integrate stripe payment gateway, we need two files. One is the library file and another is the configuration file. Both these files can be downloaded from the below URL.

https://github.com/chrismackie28/php-stripe

After downloading the zip file from the above URL, we will get two files: one is README.md and another is Stripe.php

We need to create one configuration file and put it in codeigniter’s application/config/stripe.php and put the following configurations.

// Configuration options

$config[‘stripe_key_test_public’]         = ”;

$config[‘stripe_key_test_secret’]         = ”;

$config[‘stripe_key_live_public’]         = ”;

$config[‘stripe_key_live_secret’]         = ”;

$config[‘stripe_test_mode’]               = TRUE;

$stripe = new Stripe( $config ); // Create the library object

Copy the above code and save it as stripe.php in config folder so the path of this file will be like: <PROJECT_FOLDER>/application/config/stripe.php

Stripe.php file will be placed in our codeigniter’s application/libraries folder, so the path of the Stripe.php file is like: <PROJECT_FOLDER>/application/libraries/Stripe.php

The basic functions are defined in the Stripe.php.

Steps to integrate stripe in Codeigniter

Include library file

$this->load->library(‘stripe’);

Recurring Payment Code

Below piece of code is used to create a card token

$card_data = array(

          ‘number’ => $this->input->post(‘card_number’),

          ‘cvc’ => $this->input->post(‘cvv’),

          ‘exp_month’ => $this->input->post(‘exp_month’),

          ‘exp_year’ => $this->input->post(‘exp_year’),

          ‘name’ => $this->input->post(’email’),

);

$dollars = $this->input->post(‘amount’);

$cents = number_format((float)$dollars*100., 0, ‘.’, ”); // convert dollar to cent

$tokenArray = json_decode($this->stripe->card_token_create($card_data,$cents));

$stripeToken = $tokenArray->id;

NOTE: We need to convert the amount into cents because stripe accept payment in cents. (e.g. $2.5 = 250cents)

We need to create a customer using below piece of code.

$card = $stripeToken;

$email = $this->input->post(’email’);

$desc = $this->input->post(‘description’);

$plan = $this->input->post(‘plan_id’);

$customerCreateArray = json_decode($this->stripe->customer_create( $card, $email, $desc, $plan));

$customer_id = $customerCreateArray->id;

$amount =  $cents;

$card = $card_data;

$desc = $this->input->post(‘description’);

$payWithStripe = json_Decode($this->stripe->charge_card($amount, $card, $desc));

We need to store the data into database and get the transaction id.

$transaction_id = $payWithStripe->id;

To cancel the subscription, use the below code.

$customer_id = $this->input->post(‘customer_id’);

$result = json_decode($this->stripe->customer_unsubscribe($customer_id,true));

$cancel_date = $result->current_period_end;

In the above function if we pass true then the subscription will be cancel at the end of the subscription period and if we pass it false then subscription will cancel immediately.

One Time Payment

Code is same as the recurring payment code. The only change is that it is not necessary to create a customer for onetime payment in stripe.

One Time Payment Code

Below piece of code is used to create a card token

$card_data = array(

          ‘number’ => $this->input->post(‘card_number’),

          ‘cvc’ => $this->input->post(‘cvv’),

          ‘exp_month’ => $this->input->post(‘exp_month’),

          ‘exp_year’ => $this->input->post(‘exp_year’),

          ‘name’ => $this->input->post(’email’),

);

$dollars = $this->input->post(‘amount’);

$cents = number_format((float)$dollars*100., 0, ‘.’, ”); // convert dollar to cent

$tokenArray = json_decode($this->stripe->card_token_create($card_data,$cents));

$stripeToken = $tokenArray->id;

$amount =  $cents;

$card = $card_data;

$desc = $this->input->post(‘description’);

$payWithStripe = json_Decode($this->stripe->charge_card($amount, $card, $desc));

 

We need to store the data into database and get the transaction id.

 

$transaction_id = $payWithStripe->id;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s