Stripe Subscriptions using php – Download Full Source Code

Hi Guys,

In This blog article i am going to explain about STRIPE SUBSCRIPTIONS USING PHP and making available the source code also. by extending this code you can make a complete subscription management script.

Here in this article about

stripe subscriptions using php ,

i am going to create a pricing table where 3 type of plans are defined that plan will create on stripe if plan not exists on stripe panel and then create a customer and create a subscription in that customers reference.

Note: the Stripe account is the pre requisite for you , where we will require stripe publishable key and secret key.

and the stripe php library will be require for including in config file so dont worry about the stripe php library it will be available in full source code download of this article.

Step 1: Create a config file and put following code :

Note : you need to change the publishable key and secret key in this config file

Step 2:  Create a file index.php where pricing table and stripe client side code will come as following :

Note : here you have 3 plans listed with different subscription plan and data(like plan name,plan price,plan interval [month/year/week/day] and currency)  which are defined in hidden fields in every stripe form tag inside every pricing options.

Step 3: Now the Stripe Server Side code where after inputing the card details Actyual payment need to be done

So create a file create-subscription.php which is already included in index.php code in top lines, and this file wil have following Code:

Now you are Ready  it will look like this :

stripe subscriptions preview
stripe subscriptions preview

and if we click on any subscription button it will prompt a stripe popup like this

stripe subscriptions credit card promt by stripe
stripe subscriptions credit card prompt by stripe

Click HERE to download full source code 

Note: this is completely Free script.

if you have any trouble in download this then please contact me at jeetendra.apps@gmail.com or ask query in comments below

 

 

 

  • Nathan

    Hi I have downloaded and trying your example code but I seem to always end up going in the catch statement saying invalid request I am using the test cards Stripe give what am I doing wrong?

    • w3school

      Hi Nathan,
      It seems that stripe rejected your request with a specific reason, can you just try to print the original msg return by the stripe or just vardump that $e or exception

      • Nathan

        hi found the error from doing what you said
        Fatal error: Uncaught exception ‘StripeErrorInvalidRequest’ with message ‘No such customer: cus_BG5EUSGxjv8xs0’ in /Applications/XAMPP/xamppfiles/htdocs/projects/flowersondemandv2/php/stripe-php/lib/ApiRequestor.php:110 from API request ‘req_X5dMRM3EojjEWM’ Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/projects/flowersondemandv2/php/stripe-php/lib/ApiRequestor.php(275): StripeApiRequestor->handleApiError(‘{n “error”: {n…’, 404, Array, Array) #1 /Applications/XAMPP/xamppfiles/htdocs/projects/flowersondemandv2/php/stripe-php/lib/ApiRequestor.php(65): StripeApiRequestor->_interpretResponse(‘{n “error”: {n…’, 404, Array) #2 /Applications/XAMPP/xamppfiles/htdocs/projects/flowersondemandv2/php/stripe-php/lib/ApiResource.php(120): StripeApiRequestor->request(‘post’, ‘/v1/subscriptio…’, Array, Array) #3 /Applications/XAMPP/xamppfiles/htdocs/projects/flowersondemandv2/php/stripe-php/lib/ApiResource.php(159): StripeApiResource::_staticRequest(‘post’, ‘/v1/subscriptio…’, Array, NULL) #4 /Applications/XAMP in /Applications/XAMPP/xamppfiles/htdocs/projects/flowersondemandv2/php/stripe-php/lib/ApiRequestor.php on line 110

        Do you guys have an example code to create a customer using stripe?

        • w3school

          It represents that you customer is not created. Or might be it doesn’t work fully on localhost , are you using local dev environment or any server environment

          • Nathan

            local dev I changed “customer” => ‘cus_BG5EUSGxjv8xs0’, to “customer” => $customer->id, and that fixed it

            • w3school

              Oh yes Nathan I had written this for demonstration purposes, we.need to put actual customer I’d return by the stripe server, I appreciate your efforts and let me do the line change,it may create a confusion

              • Nathan

                Was line 21 in create-subscription.php