Showing posts with label Create Token. Show all posts
Showing posts with label Create Token. Show all posts

Friday, July 14, 2017

BrainTree: Create Payment Token | Store Credit Card Details | Create Customer | Create Credit Card Token | Create Payment Method

The very step to create an account in BrainTree. And then need to create an user and then application. 

1. Visit https://sandbox.braintreegateway.com
2. Click on "Settings" -> "Users and roles" and then click "New User" to create a new user.



3. Now click "Account" -> "My User" and then "View Authorizations".



4. Now click on "Generate New API Key" to create an API key. Copy "Public Key" and "Private Key".



5. What we need to communicate with BrainTree API in our hand now. Now its time to communicate with BrainTree API.

Below is a screenshot to show how you can collect your Merchant ID:




And below PHP script is full example how to create token in BrainTree portal:


<?php
require_once('CurlExecutor.php');
require_once "BrainTreeUtils.php";
define("MERCHANT_ID", "kxxxxnxxxxxxxx");
define("PUBLIC_KEY", "85dspxxxxxxxxxx");
define("PRIVATE_KEY", "510d5b5b672xxxxxxxxxxxxxxxxxxx");

$array = array(
    "customer" => array(
        'firstName' => htmlentities("Pritom"),
        'lastName' => htmlentities("Kumar"),
        'company' => htmlentities("My Company"),
        'email' => htmlentities("pritomkucse@gmail.com"),
        'phone' => htmlentities("33303030"),

        'creditCard' => array(
            'cardholderName' => htmlentities("Test"),
            'number' => htmlentities("4444333322221111"),
            'expirationMonth' => htmlentities("12"),
            'expirationYear' => htmlentities("18"),
            'cvv' => htmlentities("354"),
            'billingAddress' => array(
                'firstName' => htmlentities("Pritom"),
                'lastName' => htmlentities("Kumar")
            )
        )
    )
);

$test = "https://api.sandbox.braintreegateway.com:443/merchants/" . MERCHANT_ID . "/customers";
$live = "https://api.braintreegateway.com:443/merchants/" . MERCHANT_ID . "/customers";
$headers[] = "Accept: application/xml";
$headers[] = "Content-Type: application/xml";
$headers[] = "X-ApiVersion: 4";
$closure = function(&$curl) {
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($curl, CURLOPT_USERPWD, PUBLIC_KEY . ':' . PRIVATE_KEY);
};
$result = CurlExecutor::execute(
    $test, "POST", BrainTreeUtils::arrayToXml($array),
    null, $headers, $closure
);
CurlExecutor::prettyPrint(BrainTreeUtils::arrayFromXml($result["response"]));

And successful output of above script is below. From the response below you have to store customer id and token for further use of customer to update, add new payment method, create payment etc...


Array
(
    [customer] => Array
        (
            [id] => 608968099
            [merchantId] => k9yxxxxxxxxxxxx6
            [firstName] => Pritom
            [lastName] => Kumar
            [company] => My Company
            [email] => pritomkucse@gmail.com
            [phone] => 33303030
            [fax] => 
            [website] => 
            [createdAt] => DateTime Object
                (
                    [date] => 2017-07-14 16:44:33.000000
                    [timezone_type] => 3
                    [timezone] => UTC
                )

            [updatedAt] => DateTime Object
                (
                    [date] => 2017-07-14 16:44:33.000000
                    [timezone_type] => 3
                    [timezone] => UTC
                )

            [customFields] => 
            [creditCards] => Array
                (
                    [0] => Array
                        (
                            [billingAddress] => Array
                                (
                                    [id] => vt
                                    [customerId] => 608968099
                                    [firstName] => Pritom
                                    [lastName] => Kumar
                                    [company] => 
                                    [streetAddress] => 
                                    [extendedAddress] => 
                                    [locality] => 
                                    [region] => 
                                    [postalCode] => 
                                    [countryCodeAlpha2] => 
                                    [countryCodeAlpha3] => 
                                    [countryCodeNumeric] => 
                                    [countryName] => 
                                    [createdAt] => DateTime Object
                                        (
                                            [date] => 2017-07-14 16:44:33.000000
                                            [timezone_type] => 3
                                            [timezone] => UTC
                                        )

                                    [updatedAt] => DateTime Object
                                        (
                                            [date] => 2017-07-14 16:44:33.000000
                                            [timezone_type] => 3
                                            [timezone] => UTC
                                        )

                                )

                            [bin] => 444433
                            [cardType] => Visa
                            [cardholderName] => Test
                            [commercial] => Unknown
                            [countryOfIssuance] => Unknown
                            [createdAt] => DateTime Object
                                (
                                    [date] => 2017-07-14 16:44:33.000000
                                    [timezone_type] => 3
                                    [timezone] => UTC
                                )

                            [customerId] => 608968099
                            [customerLocation] => US
                            [debit] => Unknown
                            [default] => 1
                            [durbinRegulated] => Unknown
                            [expirationMonth] => 12
                            [expirationYear] => 2018
                            [expired] => 
                            [healthcare] => Unknown
                            [imageUrl] => https://assets.braintreegateway.com/payment_method_logo/visa.png?environment=sandbox
                            [issuingBank] => Unknown
                            [last4] => 1111
                            [payroll] => Unknown
                            [prepaid] => Unknown
                            [productId] => Unknown
                            [subscriptions] => Array
                                (
                                )

                            [token] => 6m76wy
                            [uniqueNumberIdentifier] => 8a0xxxxxxxxxxxxxxxxxxxxxxf06f
                            [updatedAt] => DateTime Object
                                (
                                    [date] => 2017-07-14 16:44:33.000000
                                    [timezone_type] => 3
                                    [timezone] => UTC
                                )

                            [venmoSdk] => 
                            [verifications] => Array
                                (
                                )

                        )

                )

            [addresses] => Array
                (
                    [0] => Array
                        (
                            [id] => vt
                            [customerId] => 608968099
                            [firstName] => Pritom
                            [lastName] => Kumar
                            [company] => 
                            [streetAddress] => 
                            [extendedAddress] => 
                            [locality] => 
                            [region] => 
                            [postalCode] => 
                            [countryCodeAlpha2] => 
                            [countryCodeAlpha3] => 
                            [countryCodeNumeric] => 
                            [countryName] => 
                            [createdAt] => DateTime Object
                                (
                                    [date] => 2017-07-14 16:44:33.000000
                                    [timezone_type] => 3
                                    [timezone] => UTC
                                )

                            [updatedAt] => DateTime Object
                                (
                                    [date] => 2017-07-14 16:44:33.000000
                                    [timezone_type] => 3
                                    [timezone] => UTC
                                )

                        )

                )

        )

)

And below is screenshot to show that token created on BrainTree end:





Saturday, June 10, 2017

PayWay Rest API: Create Payment Using Stored Credit Card Token

It's easy to store credit card in PayWay and create payment using that that credit card token.

PayWay Rest API: Check if Rest API credentials are valid

PayWay Rest API: Create Customer Create Token

Below is a PHP script to create payment using stored credit card token:


<?php
include_once "CurlExecutor.php";

define("BASE_URL", "https://api.payway.com.au/rest/v1");
define("MERCHANT_ID", "TEST");
define("PRIVATE_KEY", "T10487_SEC_...");
define("PUBLIC_KEY", "T10487_PUB_...");

createTokenPayment();

function createTokenPayment() {
    $headers[] = "Authorization: Basic " . base64_encode(PRIVATE_KEY . ":");
    $headers[] = "Content-Type: application/x-www-form-urlencoded";

    $post = array(
        "customerNumber" => 7,
        "merchantId" => MERCHANT_ID,
        "transactionType" => "payment",
        "principalAmount" => "23.45",
        "currency" => "aud",
        "orderNumber" => "Sale-Identification"
    );

    $result = CurlExecutor::execute(BASE_URL . "/transactions", "POST", $post, null, $headers);
    $result["response"] = json_decode($result["response"]);
    CurlExecutor::prettyPrint($result);
}
?>

Output as below:


Array
(
    [code] => 201
    [response] => stdClass Object
        (
            [transactionId] => 1957977083
            [receiptNumber] => 1957977083
            [status] => approved
            [responseCode] => 08
            [responseText] => Honour with identification
            [transactionType] => payment
            [customerNumber] => 7
            [customerName] => Pritom
            [customerEmail] => pritomkucse@gmail.com
            [orderNumber] => Sale-Identification
            [currency] => aud
            [principalAmount] => 23.45
            [surchargeAmount] => 0
            [paymentAmount] => 23.45
            [paymentMethod] => creditCard
            [creditCard] => stdClass Object
                (
                    [cardNumber] => 516320...008
                    [expiryDateMonth] => 08
                    [expiryDateYear] => 20
                    [cardScheme] => mastercard
                    [cardholderName] => My Mastercard
                )

            [merchant] => stdClass Object
                (
                    [merchantId] => TEST
                    [merchantName] => Test Merchant
                    [links] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [rel] => self
                                    [href] => https://api.payway.com.au/rest/v1/merchants/TEST
                                )

                        )

                )

            [transactionDateTime] => 10 Jun 2017 13:09 AEST
            [settlementDate] => 10 Jun 2017
            [isVoidable] => 1
            [isRefundable] => 
            [links] => Array
                (
                    [0] => stdClass Object
                        (
                            [rel] => self
                            [href] => https://api.payway.com.au/rest/v1/transactions/1957977083
                        )

                    [1] => stdClass Object
                        (
                            [rel] => void
                            [href] => https://api.payway.com.au/transactions/1957977083/void
                        )

                )

        )

)








PayWay Rest API: Create Customer Create Token

You can create token in PayWay portal using credit card details to use further that token to create payment.

PayWay Rest API: Check if Rest API credentials are valid

If you are interested more on PayWay Rest API you can visit the below link:

https://www.payway.com.au/docs/rest.html

Below is a PHP script to create token using credit card etails. Be aware that below code snippet used both private and public key in different section.



<?php
include_once "CurlExecutor.php";

define("BASE_URL", "https://api.payway.com.au/rest/v1");
define("MERCHANT_ID", "TEST");
define("PRIVATE_KEY", "T10487_SEC_...");
define("PUBLIC_KEY", "T10487_PUB_...");

createCustomer(getSingleUseToken());

function createCustomer($token) {
    $headers[] = "Authorization: Basic " . base64_encode(PRIVATE_KEY . ":");
    $headers[] = "Content-Type: application/x-www-form-urlencoded";

    $post = array(
        "singleUseTokenId" => $token,
        "merchantId" => MERCHANT_ID,
        "customerName" => "Pritom",
        "emailAddress" => "pritomkucse@gmail.com"
    );

    $result = CurlExecutor::execute(BASE_URL . "/customers", "POST", $post, null, $headers);
    $result["response"] = json_decode($result["response"]);
    CurlExecutor::prettyPrint($result);
}

function getSingleUseToken() {
    $headers[] = "Authorization: Basic " . base64_encode(PUBLIC_KEY . ":");
    $headers[] = "Content-Type: application/x-www-form-urlencoded";

    $post = array(
        "paymentMethod" => "creditCard",
        "cardNumber" => "5163200000000008",
        "cardholderName" => "My Mastercard",
        "cvn" => "070",
        "expiryDateMonth" => "08",
        "expiryDateYear" => "20",
    );

    $result = CurlExecutor::execute(BASE_URL . "/single-use-tokens", "POST", $post, null, $headers);
    $result["response"] = json_decode($result["response"]);
    if ($result["code"] == 200) {
        return $result["response"]->singleUseTokenId;
    }
    CurlExecutor::prettyPrint($result);
    die("Error");
}
?>

Output is below:


Array
(
    [code] => 201
    [response] => stdClass Object
        (
            [customerNumber] => 7
            [paymentSetup] => stdClass Object
                (
                    [paymentMethod] => creditCard
                    [stopped] => 
                    [creditCard] => stdClass Object
                        (
                            [cardNumber] => 516320...008
                            [expiryDateMonth] => 08
                            [expiryDateYear] => 20
                            [cardScheme] => mastercard
                            [cardholderName] => My Mastercard
                            [surchargePercentage] => 0
                        )

                    [merchant] => stdClass Object
                        (
                            [merchantId] => TEST
                            [merchantName] => Test Merchant
                            [links] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [rel] => self
                                            [href] => https://api.payway.com.au/rest/v1/merchants/TEST
                                        )

                                )

                        )

                )

            [contact] => stdClass Object
                (
                    [customerName] => Pritom
                    [emailAddress] => pritomkucse@gmail.com
                    [sendEmailReceipts] => 
                    [phoneNumber] => 
                    [address] => stdClass Object
                        (
                            [street1] => 
                            [street2] => 
                            [cityName] => 
                            [state] => 
                            [postalCode] => 
                        )

                )

            [customFields] => stdClass Object
                (
                )

            [links] => Array
                (
                    [0] => stdClass Object
                        (
                            [rel] => self
                            [href] => https://api.payway.com.au/rest/v1/customers/7
                        )

                    [1] => stdClass Object
                        (
                            [rel] => help
                            [href] => https://www.payway.com.au/docs/rest.html#customers
                        )

                    [2] => stdClass Object
                        (
                            [rel] => contact
                            [href] => https://api.payway.com.au/rest/v1/customers/7/contact
                        )

                    [3] => stdClass Object
                        (
                            [rel] => custom-fields
                            [href] => https://api.payway.com.au/rest/v1/customers/7/custom-fields
                        )

                    [4] => stdClass Object
                        (
                            [rel] => payment-setup
                            [href] => https://api.payway.com.au/rest/v1/customers/7/payment-setup
                        )

                    [5] => stdClass Object
                        (
                            [rel] => schedule
                            [href] => https://api.payway.com.au/rest/v1/customers/7/schedule
                        )

                    [6] => stdClass Object
                        (
                            [rel] => virtual-account
                            [href] => https://api.payway.com.au/rest/v1/customers/7/virtual-account
                        )

                    [7] => stdClass Object
                        (
                            [rel] => search-customer-transactions
                            [href] => https://api.payway.com.au/rest/v1/transactions/search-customer?customerNumber=7
                        )

                )

        )

)





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