Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing Curl in PHP to do a Stripe subscription

The Stripe API allows for Curl calls to be made. For example, the command:

curl https://api.stripe.com//v1/customers/cus_5ucsCmNxF3jsSY/subscriptions    -u sk_test_REDACTED:

returns the subscription of customer cus_5ucsCmNxF3jsSY.

How can I use PHP to call this curl command (I am trying to avoid using the PHP Stripe libraries).

I am trying the following:

<?php 
        // create curl resource 
        $ch = curl_init(); 

        // set url 
        curl_setopt($ch, CURLOPT_URL, "https://api.stripe.com//v1/customers/cus_5ucsCmNxF3jsSY/subscriptions    -u sk_test_REDACTED:"); 

        //return the transfer as a string 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

        // $output contains the output string 
        $output = curl_exec($ch); 
        print($output);

        // close curl resource to free up system resources 
        curl_close($ch);      
?>

However, it seems that curl does not take the -u parameter of the URL. I get the following error:

{ "error": { "type": "invalid_request_error", "message": "You did not provide an API key. You need to provide your API key in the Authorization header, using Bearer auth (e.g. 'Authorization: Bearer YOUR_SECRET_KEY'). See https://stripe.com/docs/api#authentication for details, or we can help at https://support.stripe.com/." } 

How can I pass the -u sk_test_REDACTED: parameter to my curl call?

like image 915
Stack Exchange Avatar asked Mar 28 '15 15:03

Stack Exchange


People also ask

Can I use cURL in PHP?

cURL is a PHP extension that allows you to use the URL syntax to receive and submit data. cURL makes it simple to connect between various websites and domains.


Video Answer


1 Answers

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 $method = null;
    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);

        switch ($this->method){
           case "POST":
              curl_setopt($ch, CURLOPT_POST, 1);
              if ($this->fields)
                 curl_setopt($ch, CURLOPT_POSTFIELDS, $this->fields);
              break;
           case "PUT":
              curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
              if ($this->fields)
                 curl_setopt($ch, CURLOPT_POSTFIELDS, $this->fields);
              break;
           default:
              if ($this->fields)
                 $this->url = sprintf("%s?%s", $this->url, http_build_query($this->fields));
        }

        curl_setopt($ch, CURLOPT_URL, $this->url);
        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->method = "POST";
$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->method = "POST";
$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.

like image 60
daygloink Avatar answered Oct 16 '22 16:10

daygloink