Magento Engineer
social
Journal

BrainTree PayPal Payment Gateway Integrating With core PHP

BrainTree API is cool enough for you to understand, but you need to setup the prerequisites first.

.

A Developer Sandbox Account – Create here (braintreegateway.com)
.

BrainTree Client Framework within your application – Download from :http://www.hurricanesoftwares.com/goto/https://github.com/braintree/braintree_php

Now, let's create a php file called config.php and place the following code in it


require_once('lib/Braintree.php');
Braintree_Configuration::environment('sandbox');
Braintree_Configuration::merchantId('---YOUR MERCHANT ID---');
Braintree_Configuration::publicKey('---YOUR PUBLIC KEY---');
Braintree_Configuration::privateKey('---YOUR PRIVATE KEY---');
//Get the Client Token
$clientToken = Braintree_ClientToken::generate();



We will use Braintree Transparent Redirect to accept one time transactions.
Create a file called OneTime_Transactions.php and place the following code in it.

<?php
require_once 'config.php';
 
function braintree_text_field($label, $name, $result) {
    echo('<div>' . $label . '</div>');
    $fieldValue = isset($result) ? $result->valueForHtmlField($name) : '';
    echo('<div><input type="text" name="' . $name .'" value="' . $fieldValue . '" /></div>');
    $errors = isset($result) ? $result->errors->onHtmlField($name) : array();
    foreach($errors as $error) {
        echo('<div style="color: red;">' . $error->message . '</div>');
    }
    echo("\n");
}
?>
 
<html>
    <head>
        <title>Braintree Transparent Redirect</title>
    </head>
    <body>
        <?php
        if (isset($_GET["id"])) {
            $result = Braintree_TransparentRedirect::confirm($_SERVER['QUERY_STRING']);
        }
        if (isset($result) && $result->success) { ?>
            <h1>Braintree redirect Response</h1>
            <?php $transaction = $result->transaction; ?>
            <table>
                <tr><td>transaction id</td><td><?php echo htmlentities($transaction->id); ?></td></tr>
                <tr><td>transaction status</td><td><?php echo htmlentities($transaction->status); ?></td></tr>
                <tr><td>transaction amount</td><td><?php echo htmlentities($transaction->amount); ?></td></tr>
                <tr><td>customer first name</td><td><?php echo htmlentities($transaction->customerDetails->firstName); ?></td></tr>
                <tr><td>customer last name</td><td><?php echo htmlentities($transaction->customerDetails->lastName); ?></td></tr>
                <tr><td>customer email</td><td><?php echo htmlentities($transaction->customerDetails->email); ?></td></tr>
                <tr><td>credit card number</td><td><?php echo htmlentities($transaction->creditCardDetails->maskedNumber); ?></td></tr>
                <tr><td>expiration date</td><td><?php echo htmlentities($transaction->creditCardDetails->expirationDate); ?></td></tr>
            </table>
        <?php
        } else {
            if (!isset($result)) { $result = null; } ?>
            <h1>Braintree Transparent Redirect Example</h1>
            <?php if (isset($result)) { ?>
                <div style="color: red;"><?php echo $result->errors->deepSize(); ?> error(s)</div>
            <?php } ?>
            <form method="POST" action="<?php echo Braintree_TransparentRedirect::url() ?>" autocomplete="off">
                <fieldset>
                    <legend>Customer</legend>
                    <?php braintree_text_field('First Name', 'transaction[customer][first_name]', $result); ?>
                    <?php braintree_text_field('Last Name', 'transaction[customer][last_name]', $result); ?>
                    <?php braintree_text_field('Email', 'transaction[customer][email]', $result); ?>
                </fieldset>
 
                <fieldset>
                    <legend>Payment Information</legend>
 
                    <?php braintree_text_field('Credit Card Number', 'transaction[credit_card][number]', $result); ?>
                    <?php braintree_text_field('Expiration Date (MM/YY)', 'transaction[credit_card][expiration_date]', $result); ?>
                    <?php braintree_text_field('CVV', 'transaction[credit_card][cvv]', $result); ?>
                </fieldset>
 
<?php
$trans_data = Braintree_TransparentRedirect::transactionData(
array(
'redirectUrl' => "http://" . $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH),
'transaction' => array(
'amount' => '50.00',
 'type' => 'sale'
)))
?>
                <input type="hidden" name="tr_data" value="<?php echo $trans_data ?>" />
 
                <br />
                <input type="submit" value="Submit" />
            </form>
        <?php } ?>
    </body>
</html>

$result holds the processor responses from BrainTree.

$transaction = $result->transaction;

Now, we have the full information in $transaction and you can display the transaction information like
echo htmlentities($transaction->id);
    
echo htmlentities($transaction->id);

This is how you can use BrainTree Payment Gateway’s Transparent Redirect Method to make one time transactions. In the next part we are going to explain how to subscribe customers for recurring payments.

Ad comes here
Comments
Leave A Comment
PHP blog
Update Cookies Preferences