Showing posts with label credit card. Show all posts
Showing posts with label credit card. Show all posts

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] =>
        )
)

Tuesday, November 19, 2013

Test Credit Card Account Numbers

The table below contains a number of credit card numbers that can be used to test credit card handling software.

4111111111111111   (VISA)
4222222222222    (VISA)
4444333322221111   (VISA)
378282246310005   (AMEX)
378734493671000   (AMEX)
371449635398431   (AMEX)
6011111111111117   (DISCOVER)
6011000990139424   (DISCOVER)
5555555555554444   (MASTERCARD)
5105105105105100   (MASTERCARD)

Wednesday, November 13, 2013

Validate credit card and get card type by java code

import org.apache.commons.validator.GenericValidator

def getCreditCardType(String cardNumber) throws AutobillException {
    cardNumber = "" + cardNumber;
    cardNumber = cardNumber.trim();
    cardNumber = cardNumber.replaceAll("\\D+", "");
    if(!GenericValidator.isCreditCard(cardNumber)) {
        throw new InvalidPropertyException("cardNumber");
    }
    if(cardNumber.matches(("^4[0-9]{12}(?:[0-9]{3})?\$"))) {
        return "VISA";
    } else if(cardNumber.matches(("^5[1-5][0-9]{14}\$"))) {
        return "MASTERCARD";
    } else if(cardNumber.matches(("^3[47][0-9]{13}\$"))) {
        return "AMEX";
    } else if(cardNumber.matches(("^3(?:0[0-5]|[68][0-9])[0-9]{11}\$"))) {
        return "DINERS";
    } else if(cardNumber.matches(("^6(?:011|5[0-9]{2})[0-9]{12}\$"))) {
        return "DISCOVER";
    } else if(cardNumber.matches(("^(?:2131|1800|35\\d{3})\\d{11}\$"))) {
        return "JCB";
    }
    return null;
}

Monday, July 15, 2013

Credit card validation using php


<?php
define("CARD_TYPE_MASTERCARD","CARD_TYPE_MASTERCARD");
define("CARD_TYPE_VISA","CARD_TYPE_VISA");
define("CARD_TYPE_AEX","CARD_TYPE_AEX");
define("CARD_TYPE_DINERS","CARD_TYPE_DINERS");
define("CARD_TYPE_DISCOVER","CARD_TYPE_DISCOVER");
define("CARD_TYPE_JCB","CARD_TYPE_JCB");

function getCardType($cardNumber){
    $cardNumber = preg_replace('/\D/', '', $cardNumber);
    $len = strlen($cardNumber);
    if ($len < 15 || $len > 16) {
        return null;
    } else {
        switch($cardNumber) {
            case(preg_match ('/^4/', $cardNumber) >= 1):
                return CARD_TYPE_VISA;
            case(preg_match ('/^5[1-5]/', $cardNumber) >= 1):
                return CARD_TYPE_MASTERCARD;
            case(preg_match ('/^3[47]/', $cardNumber) >= 1):
                return CARD_TYPE_AEX;
            case(preg_match ('/^3(?:0[0-5]|[68])/', $cardNumber) >= 1):
                return CARD_TYPE_DINERS;
            case(preg_match ('/^6(?:011|5)/', $cardNumber) >= 1):
                return CARD_TYPE_DISCOVER;
            case(preg_match ('/^(?:2131|1800|35\d{3})/', $cardNumber) >= 1):
                return CARD_TYPE_JCB;
            default:
                return null;
                break;
        }
    }
    return null;
}
?>