Usually when we create the Stripe token thats used to send credit card information to Stripe’s servers we are using Stripe.js. But what if we are doing an API for example and there is no front end to utilize Stripe.js?

We can use the stripe/stripe-php package and easily create the token based on the POST parameters sent to the API.

Lets say we have an application that takes credit card payments and they are utilizing our API to process these payments.  They may have a very simple form like this.

<form id="testform" action="http://sample-api.example.com/api/payments/" method="post">
    First Name<input type="text" name="first_name" id="first_name">
    Last Name<input type="text" name="last_name" id="last_name">
    Card Number <input type="text" name="card_number" id="card_number" maxlength="16">
    Exp Month <input type="text" name="exp_month" id="exp_month">
    Exp Year <input type="text" name="exp_year" id="exp_year">
    CVC <input type="text" name="cvc" id="cvc" maxlength="3">
    <input type="submit" id="submit">
</form>

We are sending some basic information that might be used in a credit card payment. We pass it to the API using an ajax method

$("#testform").on('submit', function(ev) {
    var frm = $('#testform');
        ev.preventDefault();
        $.ajax({
            type: "POST",
            url: 'http://sample-api.example.com/api/payments/',
            dataType: "html",
            //Authentication
            beforeSend: function (xhr) {
                xhr.setRequestHeader('apikey', 'APIKEY');
                xhr.setRequestHeader('email', 'EMAIL');
            },
            data: frm.serialize(),
            success: function(data) {
                //
           }
    });
});

This information has now been sent to our controller and we need to do something with it. We can use Laravel’s Request to grab the information from the POST request. With that we can use the following code to create the token.

//Create Stripe Token
\Stripe\Stripe::setApiKey('STRIPESECRETKEY');
$response = \Stripe\Token::create(array(
  "card" => array(
    "number"    => $request->input('card_number'),
    "exp_month" => $request->input('exp_month'),
    "exp_year"  => $request->input('exp_year'),
    "cvc"       => $request->input('cvc'),
    "name"      => $request->input('first_name') . " " . $request->input('last_name')
)));
{% endhighlight %}

This will return JSON like this.

{% highlight json %}
Stripe\Token JSON: {
  "id": "tok_17DU6DDkqQb3z2jwJPBInlXV",
  "object": "token",
  "card": {
    "id": "card_17DU6DDkqQb3z2jwwx6Sgsl9",
    "object": "card",
    "address_city": null,
    "address_country": null,
    "address_line1": null,
    "address_line1_check": null,
    "address_line2": null,
    "address_state": null,
    "address_zip": null,
    "address_zip_check": null,
    "brand": "Visa",
    "country": "US",
    "cvc_check": null,
    "dynamic_last4": null,
    "exp_month": 8,
    "exp_year": 2016,
    "funding": "credit",
    "last4": "4242",
    "metadata": {
    },
    "name": null,
    "tokenization_method": null
  },
  "client_ip": null,
  "created": 1449037701,
  "livemode": false,
  "type": "card",
  "used": false
}

Now all we have to do is grab the ID from the returned JSON and we can utilize our token.

$response_array = $response->__toArray(true);
//Credit card one time use token
$token = $response_array['id'];