Showing posts with label BrainTree Token API. Show all posts
Showing posts with label BrainTree Token API. Show all posts

Saturday, July 15, 2017

BrainTree API | Create Payment | Create Payment Using Stored Token | Token Payment

For merchant ID, public key and private key you can have a look at below link:

http://pritomkumar.blogspot.com/2017/07/braintree-create-payment-token-store.html

Below is a full PHP script:

You can also specify "customerId" instead of "paymentMethodToken" in the below variable $array (filled red color) as:

"customerId" => "brain_tree_customer_id" and then default payment method of the selected customer will be used.

'submitForSettlement' => true|false

The option that determines whether an authorized transaction is submitted for settlement.


<?php
require_once('CurlExecutor.php');
require_once "BrainTreeUtils.php";

define("MERCHANT_ID", "k9yxxxxxxj6");
define("PUBLIC_KEY", "85xxxxxxrhh7");
define("PRIVATE_KEY", "510xxxxxxxxxxxxxxxxxxxx8a00");

$array = array(
    "transaction" => array(
        "type" => "sale",
        'amount' => "1.00",
        'paymentMethodToken' => "6m76wy",
        'orderId' => htmlentities("ORDER_ID_" . time()),
        'options' => array(
            'submitForSettlement' => true
        )
) ); $test = "https://api.sandbox.braintreegateway.com:443/merchants/" . MERCHANT_ID . "/transactions"; $live = "https://api.braintreegateway.com:443/merchants/" . MERCHANT_ID . "/transactions";
$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 output/response from BrainTree API is below:

Screenshot:







Array
(
    [transaction] => Array
        (
            [id] => jztnv4pr
            [status] => Submitted For Settlement
            [type] => sale
            [currencyIsoCode] => USD
            [amount] => 1.00
            [merchantAccountId] => fj3............z2
            [subMerchantAccountId] => 
            [masterMerchantAccountId] => 
            [orderId] => ORDER_ID_1500087430
            [createdAt] => DateTime Object
                (
                    [date] => 2017-07-15 02:57:06.000000
                    [timezone_type] => 3
                    [timezone] => UTC
                )

            [updatedAt] => DateTime Object
                (
                    [date] => 2017-07-15 02:57:06.000000
                    [timezone_type] => 3
                    [timezone] => UTC
                )

            [customer] => Array
                (
                    [id] => 608968099
                    [firstName] => Pritom
                    [lastName] => Kumar
                    [company] => My Company
                    [email] => pritomkucse@gmail.com
                    [website] => 
                    [phone] => 33303030
                    [fax] => 
                )

            [billing] => Array
                (
                    [id] => vt
                    [firstName] => Pritom
                    [lastName] => Kumar
                    [company] => 
                    [streetAddress] => 
                    [extendedAddress] => 
                    [locality] => 
                    [region] => 
                    [postalCode] => 
                    [countryName] => 
                    [countryCodeAlpha2] => 
                    [countryCodeAlpha3] => 
                    [countryCodeNumeric] => 
                )

            [refundId] => 
            [refundIds] => Array
                (
                )

            [refundedTransactionId] => 
            [partialSettlementTransactionIds] => Array
                (
                )

            [authorizedTransactionId] => 
            [settlementBatchId] => 
            [shipping] => Array
                (
                    [id] => 
                    [firstName] => 
                    [lastName] => 
                    [company] => 
                    [streetAddress] => 
                    [extendedAddress] => 
                    [locality] => 
                    [region] => 
                    [postalCode] => 
                    [countryName] => 
                    [countryCodeAlpha2] => 
                    [countryCodeAlpha3] => 
                    [countryCodeNumeric] => 
                )

            [customFields] => 
            [avsErrorResponseCode] => 
            [avsPostalCodeResponseCode] => I
            [avsStreetAddressResponseCode] => I
            [cvvResponseCode] => I
            [gatewayRejectionReason] => 
            [processorAuthorizationCode] => 6CWKNZ
            [processorResponseCode] => 1000
            [processorResponseText] => Approved
            [additionalProcessorResponse] => 
            [voiceReferralNumber] => 
            [purchaseOrderNumber] => 
            [taxAmount] => 
            [taxExempt] => 
            [creditCard] => Array
                (
                    [token] => 6m76wy
                    [bin] => 444433
                    [last4] => 1111
                    [cardType] => Visa
                    [expirationMonth] => 12
                    [expirationYear] => 2018
                    [customerLocation] => US
                    [cardholderName] => Test
                    [imageUrl] => https://assets.braintreegateway.com/payment_method_logo/visa.png?environment=sandbox
                    [prepaid] => Unknown
                    [healthcare] => Unknown
                    [debit] => Unknown
                    [durbinRegulated] => Unknown
                    [commercial] => Unknown
                    [payroll] => Unknown
                    [issuingBank] => Unknown
                    [countryOfIssuance] => Unknown
                    [productId] => Unknown
                    [uniqueNumberIdentifier] => 8a0a266..............faf06f
                    [venmoSdk] => 
                )

            [statusHistory] => Array
                (
                    [0] => Array
                        (
                            [timestamp] => DateTime Object
                                (
                                    [date] => 2017-07-15 02:57:06.000000
                                    [timezone_type] => 3
                                    [timezone] => UTC
                                )

                            [status] => authorized
                            [amount] => 1.00
                            [user] => j234lk2342342j3l4
                            [transactionSource] => api
                        )

                )

            [planId] => 
            [subscriptionId] => 
            [subscription] => Array
                (
                    [billingPeriodEndDate] => 
                    [billingPeriodStartDate] => 
                )

            [addOns] => Array
                (
                )

            [discounts] => Array
                (
                )

            [descriptor] => Array
                (
                    [name] => 
                    [phone] => 
                    [url] => 
                )

            [recurring] => 
            [channel] => 
            [serviceFeeAmount] => 
            [escrowStatus] => 
            [disbursementDetails] => Array
                (
                    [disbursementDate] => 
                    [settlementAmount] => 
                    [settlementCurrencyIsoCode] => 
                    [settlementCurrencyExchangeRate] => 
                    [fundsHeld] => 
                    [success] => 
                )

            [disputes] => Array
                (
                )

            [authorizationAdjustments] => Array
                (
                )

            [paymentInstrumentType] => credit_card
            [processorSettlementResponseCode] => 
            [processorSettlementResponseText] => 
            [threeDSecureInfo] => 
        )

)




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: