Friday, May 26, 2017

Stripe Payment API: Create Token Using Credit Card Details

What is need at first is to you have need an Stripe account. If you haven't that yet follow the link https://dashboard.stripe.com/register to create one. After that you need to collect API Key from Stripe portal. Below is a screenshot attached to describe how API Key collected.



Next step is to collect credit card details from end user and sent them to stripe to create a token to use further. Below is a full PHP Script that create and retrieve token from Stripe end.

Below are list of test credit cards for Stripe:
https://stripe.com/docs/testing



<?php
$params = array(
    "card" => array(
        "name" => "Card Name",
        "number" => "4242424242424242",
        "exp_month" => "12",
        "exp_year" =>  "19",
        "cvc" => "123"
    )
);

$create = CreateToken::create($params);
CreateToken::prettyPrint($create);

if ($create["code"] == 200) {
    $get = CreateToken::get($create["response"]->id);
    CreateToken::prettyPrint($get);
}

class CreateToken {
    private static $key = "sk_test_...................";

    static function create($params)
    {
        $url = "https://api.stripe.com/v1/tokens";

        $headers[] = "Authorization: Bearer " . self::$key;
        $headers[] = "Content-Type: application/x-www-form-urlencoded";

        return makeCurlCall($url, "POST", null, $params, $headers);
    }

    static function get($id)
    {
        $url = "https://api.stripe.com/v1/tokens/$id";

        $headers[] = "Authorization: Bearer " . self::$key;
        $headers[] = "Content-Type: application/x-www-form-urlencoded";

        return makeCurlCall($url, "GET", null, null, $headers);
    }

    static function prettyPrint($data)
    {
        echo "<pre>";
        print_r($data);
        echo "</pre>";
    }
}


Below is output for error response:
Array
(
    [code] => 402
    [response] => stdClass Object
        (
            [error] => stdClass Object
                (
                    [message] => Your card number is incorrect.
                    [type] => card_error
                    [param] => number
                    [code] => incorrect_number
                )
        )
)

Below is output for success response:

Array
(
    [code] => 200
    [response] => stdClass Object
        (
            [id] => tok_1ANblQFIwfarG3vBaSA6dNaX
            [object] => token
            [card] => stdClass Object
                (
                    [id] => card_1ANblQFIwfarG3vBdjdo6nd5
                    [object] => card
                    [address_city] =>
                    [address_country] =>
                    [address_line1] =>
                    [address_line1_check] =>
                    [address_line2] =>
                    [address_state] =>
                    [address_zip] =>
                    [address_zip_check] =>
                    [brand] => Visa
                    [country] => US
                    [cvc_check] => unchecked
                    [dynamic_last4] =>
                    [exp_month] => 12
                    [exp_year] => 2019
                    [fingerprint] => SmsX0otFJxkdwdrG
                    [funding] => credit
                    [last4] => 4242
                    [metadata] => stdClass Object
                        (
                        )
                    [name] => Card Name
                    [tokenization_method] =>
                )
            [client_ip] => 103.59.179.132
            [created] => 1495779452
            [livemode] =>
            [type] => card
            [used] =>
        )
)

No comments:

Post a Comment