Friday, May 26, 2017

Stripe Payment API: Create Get Edit Delete Customer

You can create Customer in Stripe using API. You can profile source when you create the Customer. Here source is payment source, Such you can create a Credit Card Token and provide reference as source. To create Credit Card Token follow below link:

http://pritomkumar.blogspot.com/2017/05/stripe-payment-api-create-token-using.html

And below link for API details:
https://stripe.com/docs/api#create_customer

Below is a PHP Script that will Create Get And Delete Customer From Stripe.



<?php
$params = array(
    "email" => "customer_3@pritom.com",
    "source" => "token_id_using_above_link" 
 );
$create = StripeCustomer::create($params);
StripeCustomer::prettyPrint($create);

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

    $delete = StripeCustomer::delete($create["response"]->id);
    StripeCustomer::prettyPrint($delete);
}

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

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

        $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/customers/$id";

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

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

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

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

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

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

Below is output of the Script above:



CREATE REQUEST RESPONSE:
Array
(
    [code] => 200
    [response] => stdClass Object
        (
            [id] => cus_Aj4vdApn2NQIbB
            [object] => customer
            [account_balance] => 0
            [created] => 1495783280
            [currency] => 
            [default_source] => card_1ANcktFIwfarG3vBDsNU6Bg9
            [delinquent] => 
            [description] => 
            [discount] => 
            [email] => customer_8@pritom.com
            [livemode] => 
            [metadata] => stdClass Object
                (
                )

            [shipping] => 
            [sources] => stdClass Object
                (
                    [object] => list
                    [data] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [id] => card_1ANcktFIwfarG3vBDsNU6Bg9
                                    [object] => card
                                    [address_city] => 
                                    [address_country] => 
                                    [address_line1] => 
                                    [address_line1_check] => 
                                    [address_line2] => 
                                    [address_state] => 
                                    [address_zip] => 
                                    [address_zip_check] => 
                                    [brand] => Visa
                                    [country] => US
                                    [customer] => cus_Aj4vdApn2NQIbB
                                    [cvc_check] => pass
                                    [dynamic_last4] => 
                                    [exp_month] => 12
                                    [exp_year] => 2019
                                    [fingerprint] => CjZNbbCtG5QSnuIS
                                    [funding] => credit
                                    [last4] => 4242
                                    [metadata] => stdClass Object
                                        (
                                        )

                                    [name] => Card Name
                                    [tokenization_method] => 
                                )

                        )

                    [has_more] => 
                    [total_count] => 1
                    [url] => /v1/customers/cus_Aj4vdApn2NQIbB/sources
                )

            [subscriptions] => stdClass Object
                (
                    [object] => list
                    [data] => Array
                        (
                        )

                    [has_more] => 
                    [total_count] => 0
                    [url] => /v1/customers/cus_Aj4vdApn2NQIbB/subscriptions
                )

        )

)
GET REQUEST RESPONSE
Array
(
    [code] => 200
    [response] => stdClass Object
        (
            [id] => cus_Aj4kNrfMs3ivE8
            [object] => customer
            [account_balance] => 0
            [created] => 1495782666
            [currency] =>
            [default_source] =>
            [delinquent] =>
            [description] =>
            [discount] =>
            [email] => customer_3@pritom.com
            [livemode] =>
            [metadata] => stdClass Object
                (
                )

            [shipping] =>
            [sources] => stdClass Object
                (
                    [object] => list
                    [data] => Array
                        (
                        )

                    [has_more] =>
                    [total_count] => 0
                    [url] => /v1/customers/cus_Aj4kNrfMs3ivE8/sources
                )

            [subscriptions] => stdClass Object
                (
                    [object] => list
                    [data] => Array
                        (
                        )

                    [has_more] =>
                    [total_count] => 0
                    [url] => /v1/customers/cus_Aj4kNrfMs3ivE8/subscriptions
                )

        )

)

DELETE REQUEST RESULT
Array
(
    [code] => 200
    [response] => stdClass Object
        (
            [deleted] => 1
            [id] => cus_Aj4kNrfMs3ivE8
        )

)

No comments:

Post a Comment