Coupon Codes with Stripe using php

In this post we will show you Coupon Codes with Stripe using php, hear for Coupon Codes with Stripe using php we will give you demo and example for implement.

hear we use curl for call Coupon Codes with Stripe using php, we need to pass YOUR_STRIPE_API_KEY, email, coupon

Stripe.php

this is Stripe class file and we need to include it in you main php file.

<?php 
// create class for Stripe
class Stripe 
{
    public $stripe_headers;
	// set api url
    public $url = 'https://api.stripe.com/v1/';
	
	// set all array for fields data
    public $fields = array();

    function __construct () 
	{
        $this->stripe_headers = array('Authorization: Bearer YOUR_STRIPE_API_KEY'); 
		// ADD HERE YOUR_STRIPE_API_KEY = add application YOUR STRIPE API KEY
    }

	// set PHP functions for curl call
    function call () 
	{
		$get_output = "";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, $this->stripe_headers);
		curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_URL, $this->url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($this->fields));
		// for https 	
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $get_output = curl_exec($ch);
        curl_close($ch);
		// stripe api response return here php array
        return json_decode($get_output, true); 
    }
}
?>

index.php

this is main file for execute Stripe api, Coupon Codes with Stripe.

<?php 
// include Stripe.php file
require_once('Stripe.php');

$set_subscription = new Stripe();
$set_subscription->url .= 'customers';
$set_subscription->fields['email'] = 'testadmin98@gmail.com';
$customer = $set_subscription->call();



// create simple customer subscription with your credit card and simple plan
$set_subscription = new Stripe();
$set_subscription->url .= 'customers/'.$customer['id'].'/subscriptions';

//add your plan i.e. 'ULTIMATE'
$set_subscription->fields['plan'] = 'ULTIMATE'; 

//starter plan name of the simple stripe plan 
//add your coupon i.e. 'ADD-YOUR-COUPON'

$set_subscription->fields['coupon'] = 'ADD-YOUR-COUPON'; 

//add trial end time 
$set_trial_end  = date(strtotime("+28 day"));
$set_subscription->fields['trial_end'] = $set_trial_end;


$subscription_add = $set_subscription->call();
echo "Coupon Codes with Stripe using php <br><br>";
echo "<pre>";
print_r($subscription_add);
echo "</pre>";
?>

Hope this code and post will helped you for implement Coupon Codes with Stripe using php. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve onlincode. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs onlincode.org