Hello Coders, we all know about Stripe payment gateway which is very secure and easy to implement in any language. If you want to integrate any payment gateway on very short steps than Stripe is best.
Today we will learn about how we integrate Stripe Payment Gateway in CodeIgniter and how to add customer’s or user’s card details to the stripe account and also how we provide access to the user how they delete the card details from the stripe.
Download Stripe Library
First, we download the CodeIgniter framework from www.codeigniter.com than download stripe library from link: https://github.com/stripe/stripe-php.
Setting API key on Code
On Codeigniter, you have to set API key on the path: application/config/config.php
1 2 3 4 |
$config['stripe_key'] = 'pk_test_reFxwbsm9cdCKASd'; $config['stripe_secret'] = 'sk_test_oQMFWteJiPd4w'; |
You get those code from here, create an account on the stripe and open dashboard go to below picture path info.
After setup the API credentials you have to create route of stripe
application/config/routes.php
1 2 3 |
$route['pstripe']="StripeController"; $route['pstripe']['post']="StripeController/StripeBlog"; |
After that, you use below code on your controller for the further process where we save card details of particular user and also delete, check code below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public function stripePost() { require_once('application/libraries/stripe-php/init.php'); \Stripe\Stripe::setApiKey($this->config->item('stripe_secret')); \Stripe\Charge::create ([ "amount" => 100 * 100, "currency" => "usd", "source" => $this->input->post('stripeToken'), "description" => "Test payment codingtasks.net" ]); } |
Main Points for Card detail process:
-
- First thing for saving new card details on stripe account of particular user you have to use CUSTOMER ID, I give you details about after the first payment of any user you can retrieve customer id using your database, on the time of first payment you store the payment data so from there you can retrieve the CUSTOMER ID.
- After getting customer ID, you can save the particular customer data using below code,
123456789101112131415161718public function stripeCard(){require_once('application/libraries/stripe-php/init.php');\Stripe\Stripe::setApiKey($this->config->item('stripe_secret'));<strong> //Customer ID parameter</strong>$customer = \Stripe\Customer::retrieve("cus_Ea3TyitGGHfNE0");<strong>//these are posted data which you can get from your card adding form</strong>$number = <span class="token this">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">input</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">post<span class="token punctuation">(</span></span><span class="token string">'cardnumber'</span><span class="token punctuation">);</span> $exp_month = <span class="token this">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">input</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">post<span class="token punctuation">(</span></span><span class="token string">'exp_month'</span><span class="token punctuation">);</span> $exp_year = <span class="token this">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">input</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">post<span class="token punctuation">(</span></span><span class="token string">'exp_year'</span><span class="token punctuation">);</span>$cvv = <span class="token this">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">input</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">post<span class="token punctuation">(</span></span><span class="token string">'cvv'</span><span class="token punctuation">);</span><strong>//here you have to give all parameters or posted value of card which user enter on the form</strong>$customer->sources->create(array("source" =>array("object"=>"card","number"=>$number, "exp_month"=>$exp_month, "exp_year"=>$exp_year,"cvc"=>$cvv )));}
- On the above code we save the card details using PHP library of stripe and now we create a controller for how can any user can delete the card details from sripe accunt using this API. See the code below,
123456789101112public function stripeCardDelete(){require_once('application/libraries/stripe-php/init.php');\Stripe\Stripe::setApiKey($this->config->item('stripe_secret'));<strong> //Customer ID parameter for retrieving all deails about customer</strong>$customer = \Stripe\Customer::retrieve("cus_Ea3TyitGGHfNE0");<strong>//you get the value on form of aaray and from that array you can get the CARD id</strong>//<strong>from above $cusomer variable you get the <span style="color: #008000">card number.</span></strong> <strong>And delete using below API</strong>$customer->sources->retrieve("<span style="color: #008000"><strong>card_1E74CH2eZvKYlo2C0ESsqVTJ</strong></span>")->delete();}
And now we completed both operations with Stripe API. I hope you will find useful thing on that post. If you have any query about programming thing please visit question panel for asking anything about your programming problem.
Happy Coding..!
[…] Check Out: Stripe Payment API integration and Card save and delete operation in Codeigniter […]
Howdy! Someone in my Facebook group shared this site with us so I came to
check it out. I’m definitely loving the information. I’m book-marking
and will be tweeting this to my followers! Superb blog and great style and design. Hello would you mind letting me know which web host you’re utilizing?
I’ve loaded your blog in 3 different web browsers and I must say this blog loads
a lot quicker then most. Can you suggest a good internet hosting provider at a reasonable price?
Many thanks, I appreciate it! You’ve made some decent points there.
I looked on the internet for more information about the issue and
found most individuals will go along with your views on this site.
Thanks Jason for your consideration.
We use Hostinger website hosting service (business plan).
I really like what you guys tend to be up too. This sort of clever work and exposure!
Keep up the fantastic works guys I’ve included you guys to my blogroll.
I’ll immediately snatch your rss as I can’t in finding your e-mail subscription link or
e-newsletter service. Do you’ve any? Kindly let me understand in order that I could subscribe.
Thanks. I couldn’t resist commenting. Exceptionally well written! http://foxnews.net
When someone writes an article he/she maintains the thought of
a user in his/her brain that how a user can understand it.
Therefore that’s why this paragraph is outstdanding.
Thanks!
When someone writes an article he/she keeps the idea of a user in his/her mind that how a user can know it.
Therefore that’s why this paragraph is perfect.
Thanks!
Its really very helpful. thanks for sharing and writing this wonderful article. Really appreciate.
[…] Stripe Payment API integration and Card save and delete operation in Codeigniter […]