Showing Posts From

Php stripe subscriptions

Stripe Subscriptions using php – Download Full Source Code

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 : "sk_test_456456546hgjhg", "publishable_key" => "pk_test_567hjhgjghgjhg" );\Stripe\Stripe::setApiKey($stripe['secret_key']); ?> 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 : .spacing { margin-top:20px; } .success { color: #fff; background-color: green; } .error { color: #fff; background-color: red; }</style> <meta name="viewport" content="width=device-width, initial-scale=1.0">* { box-sizing: border-box; } body { margin:50px 0; padding:0; text-align:center; }.content { margin:0 auto; text-align:left; padding:15px;}.columns { float: left; width: 33.3%; padding: 8px; }.price { list-style-type: none; border: 1px solid #eee; margin: 0; padding: 0; -webkit-transition: 0.3s; transition: 0.3s; }.price:hover { box-shadow: 0 8px 12px 0 rgba(0,0,0,0.2) }.price .header { background-color: #111; color: white; font-size: 25px; }.price li { border-bottom: 1px solid #eee; padding: 20px; text-align: center; }.price .grey { background-color: #eee; font-size: 20px; }.button { background-color: #4CAF50; border: none; color: white; padding: 10px 25px; text-align: center; text-decoration: none; font-size: 18px; }@media only screen and (max-width: 600px) { .columns { width: 100%; } }Stripe Subscriptions using php with Responsive Pricing Tables Basic $ 9.99 / year 10GB Storage 10 Emails 10 Domains 1GB Bandwidth <form action="" method="POST" class="spacing"> <input name="plan" type="hidden" value="Basic" /> <input name="interval" type="hidden" value="year" /> <input name="price" type="hidden" value="9.99" /> <input name="currency" type="hidden" value="usd" /> <script src="https://checkout.stripe.com/checkout.js" class="stripe-button" data-key="<?php echo $stripe['publishable_key']; ?>" data-image="https://www.w3school.info/wp-content/uploads/2017/08/w3school_logo.png" data-name="BASIC PLAN" data-description="Start with basic plan" data-panel-label="Subscribe Now" data-label="Subscribe Now" data-locale="auto"> </script> </form></li> Pro $ 24.99 / year 25GB Storage 25 Emails 25 Domains 2GB Bandwidth <input name="plan" type="hidden" value="Pro" /> <input name="interval" type="hidden" value="year" /> <input name="price" type="hidden" value="24.99" /> <input name="currency" type="hidden" value="usd" /> <script src="https://checkout.stripe.com/checkout.js" class="stripe-button" data-key="<?php echo $stripe['publishable_key']; ?>" data-image="https://www.w3school.info/wp-content/uploads/2017/08/w3school_logo.png" data-name="Pro PLAN" data-description="Start with Pro plan" data-panel-label="Subscribe Now" data-label="Subscribe Now" data-locale="auto"> </script> </form> </li> Premium $ 49.99 / year 50GB Storage 50 Emails 50 Domains 5GB Bandwidth " data-image="https://www.w3school.info/wp-content/uploads/2017/08/w3school_logo.png" data-name="Premium PLAN" data-description="Start with Premium plan" data-panel-label="Subscribe Now" data-label="Subscribe Now" data-locale="auto">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: $_POST['plan'], "id" => "plan-".$_POST['plan'], "interval" => $_POST['interval'], "currency" => $_POST['currency'], "amount" => $_POST['price']*100, )); $customer = \Stripe\Customer::create(array( "email" => $_POST['stripeEmail'], "source" => $_POST['stripeToken'], // The token submitted from Checkout )); \Stripe\Subscription::create(array( "customer" => $customer->id, "items" => array( array( "plan" => "plan-".$_POST['plan'], ), ), )); $success = "Thanks! You've subscribed to the " . $_POST['plan'] . " plan."; } catch(\Stripe\Error\Card $e) { // Since it's a decline, \Stripe\Error\Card will be caught $body = $e->getJsonBody(); $error = $body['error']['message']; } // Probably want to log all of these for later or send yourself a notification catch (\Stripe\Error\RateLimit $e) { $error = "Sorry, we weren't able to authorize your card. You have not been charged."; } catch (\Stripe\Error\InvalidRequest $e) { $error = "Sorry, we weren't able to authorize your card. You have not been charged."; } catch (\Stripe\Error\Authentication $e) { $error = "Sorry, we weren't able to authorize your card. You have not been charged."; } catch (\Stripe\Error\ApiConnection $e) { $error = "Sorry, we weren't able to authorize your card. You have not been charged."; } catch (\Stripe\Error\Base $e) { $error = "Sorry, we weren't able to authorize your card. You have not been charged."; } catch (Exception $e) { $error = "Sorry, we weren't able to authorize your card. You have not been charged."; } } ?> Now you are Ready it will look like this : stripe subscriptions preview and if we click on any subscription button it will prompt a stripe popup like this 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 [email protected] or ask query in comments below