Stripe List all coupons using php

In this post we will show you Stripe List all coupons using php, hear for Stripe List all coupons using php we will give you demo and example for implement.

hear we use curl for call Stripe List all coupons 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');



// create simple customer subscription with your credit card and simple plan
$set_subscription = new Stripe();
$set_subscription->url .= 'coupons?limit=30';

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

Stripe List all coupons using php output somthin like this ::

{
    "object": "list",
    "data": [
        {
            "id": "free-period",
            "object": "coupon",
            "amount_off": null,
            "created": 1499757121,
            "currency": null,
            "duration": "once",
            "duration_in_months": null,
            "livemode": false,
            "max_redemptions": null,
            "metadata": {},
            "percent_off": 100,
            "redeem_by": null,
            "times_redeemed": 24,
            "valid": true
        },
        {
            "id": "FREE6",
            "object": "coupon",
            "amount_off": null,
            "created": 1499237289,
            "currency": null,
            "duration": "repeating",
            "duration_in_months": 6,
            "livemode": false,
            "max_redemptions": null,
            "metadata": {},
            "percent_off": 100,
            "redeem_by": 1622347199,
            "times_redeemed": 15,
            "valid": true
        }
    ],
    "has_more": false,
    "url": "/v1/coupons"
}

Hope this code and post will helped you for implement Stripe List all coupons 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