Stripe Integration for Plans
There are many payment integrations but the Stripe payment gateway is one of the easiest and most powerful solutions.
GitHub repository: Click on the repository
Before Stripe implements, You must have a Stripe account and when you log in with that account then stripe will provide you, API Keys, as below.
- Test API Keys
-
Live API Keys
1. Test API Keys
These Test API Keys use for developing purposes only so before go to live it will help to test.
Let’s get started with the steps bellow
Step 1: Create a Stripe account (https://stripe.com/) and log in to the dashboard.
Step 2: Navigate to the Developers ->API keys page
Here are two types of standard API keys named secret key and publishable key. To show the Secret key, click on the Reveal test key token button.
Step 3: Go to the constant file in a code and set API keys
Step 4: Now Create the Product from Stripe navigation as mentioned below images
Step 5: Now go to a particular product that was created in Stripe and create a plan for that product as many as you needed with different prices and names.
Step 6: Now you can see ID
of plan in the above image and we need to copy that and use it in the subscription plan.
Step 7: Include <script src="https://js.stripe.com/v3/"></script>
in your page from https://stripe.com/docs/stripe-js. and setup code
Step 8: Download the Stripe page with a composer or direct folder from this current repository Repository (application/third_party/stripe/).
Step 9: include the Stripe PHP library in the controller. and set secret key
. and include bellow link
require_once APPPATH."third_party/stripe/init.php"
;
now write the code for subscribing plans as given bellow code.
if(!empty($_POST['stripeToken'])) { //include Stripe PHP library require_once APPPATH."third_party/stripe/init.php"; StripeStripe::setApiKey(SECRET_KEY); $customer = StripeCustomer::create(array( 'email' => trim($this->input->post('email')), 'source' => $this->input->post('stripeToken') )); // Get selected plan id $selected_plan = null; $plans = StripePlan::all(); foreach ($plans->data as $p) { if($p['nickname'] == strtolower(trim($this->input->post('plan')))) { $selected_plan = $p; break; } } $subscription = StripeSubscription::create(array( 'customer' => $customer->id, "items" => array( array("plan" => $selected_plan['id'],), ), 'trial_period_days' => 14, )); if (isset($subscription)) { $dataArr['payment_status'] = 1; $dataArr['subscription_status'] = 1; $dataArr['stripe_customer_id'] = $customer->id; $dataArr['subscription_id'] = $subscription->id; } $inserted_id = $this->users_model->common_insert_update('insert', TBL_USERS, $dataArr); $result = $this->users_model->get_user_detail(['id' => $inserted_id]); $this->session->set_userdata('stripeInCI', $result); $this->session->set_flashdata('success', 'User has been created successfully!'); redirect('home'); }
Step 10: Go to stripe account and check the customer & subscription
menu from the navigation bar
Note: This is stripe API documentation https://stripe.com/docs/api where you can check the code and parameter for particular API
If you want to implement any technology then you must need to follow the above steps
2. Live API Keys
Go to stripe account and make test mode off
so you will get live Keys and implement that keys.
Note: Live key will use for the original transaction so be careful to make testing with a minimum amount for testing.