Quantcast
Viewing latest article 1
Browse Latest Browse All 2

Answer by daygloink for Executing Curl in PHP to do a Stripe subscription

I ran into the same issue. I wanted to use PHP's CURL functions instead of using the official stripe API because singletons make me nauseous.

I wrote my own very simple Stripe class which utilizes their API via PHP and CURL.

class Stripe {    public $headers;    public $url = 'https://api.stripe.com/v1/';    public $fields = array();    function __construct () {        $this->headers = array('Authorization: Bearer '.STRIPE_API_KEY); // STRIPE_API_KEY = your stripe api key    }    function call () {        $ch = curl_init();        curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);        curl_setopt($ch, CURLOPT_URL, $this->url);        curl_setopt($ch, CURLOPT_POST, true);        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($this->fields));        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);        $output = curl_exec($ch);        curl_close($ch);        return json_decode($output, true); // return php array with api response    }}// create customer and use email to identify them in stripe$s = new Stripe();$s->url .= 'customers';$s->fields['email'] = $_POST['email'];$customer = $s->call();// create customer subscription with credit card and plan$s = new Stripe();$s->url .= 'customers/'.$customer['id'].'/subscriptions';$s->fields['plan'] = $_POST['plan']; // name of the stripe plan i.e. my_stripe_plan// credit card details$s->fields['source'] = array('object' => 'card','exp_month' => $_POST['card_exp_month'],'exp_year' => $_POST['card_exp_year'],'number' => $_POST['card_number'],'cvc' => $_POST['card_cvc']);$subscription = $s->call();

You can dump $customer and $subscription via print_r to see the response arrays if you want to manipulate the data further.


Viewing latest article 1
Browse Latest Browse All 2

Trending Articles