Basics Of Stripe API

Introduction 

 
In order for members to get familiar and avoid spending too much time, I thought about creating the document about Stripe API.
 
Note: If you are familiar with the Stripe API, You can ignore this information and for those who are not aware, I have listed out whatever knowledge I have about this API.
 
Here is the basic information about Stripe and Stripe API which will help you understand the process about it.
 
Strip connect provides 3 different types:
  1. Standard
  2. Express
  3. Custom
In short, Standard and Express types have mild differences and they are used when we want to charge the users and in order to do that, we send them to Stripe and Stripe handle all the payment related details. We do this by making an application in the stripe dashboard. Any marketplace that wishes to charge and also pay to different users then these methods are used. We can go with any option based on our familiarity with it.
 
For more, you can check this link: https://stripe.com/docs/connect/accounts
 
The 3rd option on which I’ll be focusing on is Custom. Using the custom API. In which we handle everything on our own using the keys in Stripe connect custom API.
 
Before we go any further, in order for us to connect to the API we need to create the API keys. Once we have created an account, we have to click on Developers -> API keys, and from that page, we can get the API keys, secret and publishable key.
 
For Test Data
 
The secret keys will precede with sk_test and for the live ones, the keys will precede with sk_live.
 
The publishable keys will precede with pk_test and the live ones will be pk_live.
 
We can connect to the Stripe API in two ways:
  1. GitHub code
  2. cURL. 
I will be explaining the payment transaction in both with code. Authentication will be done based on a secret key for an account.
 
GitHub
 
By downloading the code from GitHub https://github.com/stripe/stripe-php OR if we have a composer installed we can run this command and the code will be downloaded.
 
Command: composer require stripe/stripe-php
 
-> If you have directly downloaded the folder then we have to load the init.php
  1. require_once('/path/to/stripe-php/init.php');  
-> If we have downloaded the code with Composer then we have to load the autoload.php file.
  1. require_once ('vendor/autoload.php');  
--> Retrieve Balance
 
Using the balance object, we can retrieve the available balance in our stripe account.
  1. require_once 'vendor/autoload.php';  
  2.   
  3. \Stripe\Stripe::setApiKey("----SECRET----KEY----");  
  4.   
  5. $test=\Stripe\Balance::retrieve();  
  6.   
  7. echo $test;  
  8. $val= json_decode($test);  
-->Charge a customer
 
When supposing in our eCommerce site, we add stripe as out payment gateway then in order to charge the customer we use the Charge object in API. This object using the payment information of the card will deduct the amount and deposit in Stripe.
  1. require_once 'vendor/autoload.php';\  
  2. Stripe\ Stripe::setApiKey("----SECRET----KEY----");  
  3. $charge = \Stripe\ Charge::create(['amount' => 2000, 'currency' => 'usd''source' => array('object' => 'card''exp_month' => '12''exp_year' => '2021''number' => '4111111111111111''cvc' => '123'), 'description' => 'My First Test Charge', ]);   
In return, we get all the transaction-related details. Transaction Id, status, amount, etc.
 
cURL Request
 
Retrieve Balance:
  1. class Stripe {  
  2.     public $headers;  
  3.     public $url = 'https://api.stripe.com/v1/';  
  4.     public $fields = array();  
  5.     public $STRIPE_API_KEY = '--SECRET--KEY--';  
  6.   
  7.     function __construct() {  
  8.         $this - > headers = array('Authorization: Bearer '.  
  9.             'STRIPE_API_KEY'); // STRIPE_API_KEY = your stripe api key    
  10.     }  
  11.   
  12.     function initiate() {  
  13.         $ch = curl_init();  
  14.         curl_setopt($ch, CURLOPT_HTTPHEADER, $this - > headers);  
  15.         curl_setopt($ch, CURLOPT_URL, $this - > url);  
  16.         curl_setopt($ch, CURLOPT_POST, false);  
  17.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
  18.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
  19.         $output = curl_exec($ch);  
  20.         curl_close($ch);  
  21.         return json_decode($output, true); // return php array with api response    
  22.     }  
  23. }  
  24. $s = new Stripe();  
  25. $s - > url. = 'balance';  
  26. $balance = $s - > initiate();  
  27. print_r($balance);   
Creating a Charge
  1. class Stripe {  
  2.     public $headers;  
  3.     public $url = 'https://api.stripe.com/v1/';  
  4.     public $fields = array();  
  5.     public $STRIPE_API_KEY = '--SECRET--KEY--';  
  6.   
  7.     function __construct() {  
  8.         $this - > headers = array('Authorization: Bearer '.  
  9.             'STRIPE_API_KEY'); // STRIPE_API_KEY = your stripe api key    
  10.     }  
  11.   
  12.     function initiate() {  
  13.         $ch = curl_init();  
  14.         curl_setopt($ch, CURLOPT_HTTPHEADER, $this - > headers);  
  15.         curl_setopt($ch, CURLOPT_URL, $this - > url);  
  16.         curl_setopt($ch, CURLOPT_POST, true);  
  17.         curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($this - > fields));  
  18.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
  19.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
  20.         $output = curl_exec($ch);  
  21.         curl_close($ch);  
  22.         return json_decode($output, true); // return php array with api response    
  23.     }  
  24. }  
  25. $s = new Stripe();  
  26. $s - > url. = 'charges';  
  27. $s - > fields['amount'] = '1000';  
  28. $s - > fields['currency'] = 'usd';  
  29. $s - > fields['source'] = array('object' => 'card''exp_month' => '12''exp_year' => '2021''number' => '4111111111111111''cvc' => '123');  
  30. $s - > fields['description'] = 'test desc';  
  31. $payment = $s - > initiate();  
  32. print_r($payment);   
These are some of the code samples of getting the balance and making a payment transaction using the two different ways by downloading the files or by executing the request with cURL.
 
Similarly, we can create the customer, we can get the disputes, update, and delete them.
  
You can get the detailed information about the API from the documentation page of Stripe.
 
Here is the link.

Summary

 
In this article, we learned the basics of Stripe API.
 
I hope this will be useful. Thank you for reading!