Stack Exchange
Stack Exchange

Reputation: 175

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?

Upvotes: 6

Views: 11853

Answers (1)

daygloink
daygloink

Reputation: 437

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.

Upvotes: 19

Related Questions