Magento Engineer
social
Journal

Programming Blogs to Improve Your Coding Skills!

The industry's top wizards and other experts share their advice and research findings.

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

Difference b/w Numeric Array and Associative Array?

Numeric Array:
These arrays can store numbers, strings and any object but their index will be represented by numbers.
By default array index starts from zero.

<?php
         /* First method to create array. */
         $numbers = array( 1, 2, 3);
         
         foreach( $numbers as $value ) {
            echo "Value is $value <br />";
         }
?>

Associative Arrays:

The associative arrays are very similar to Numeric arrays in term of functionality but they are different in terms of their index.
Associative array will have their index as string so that you can establish a strong association between key and values.

$numbers = array( "a"=>1, "b"=>2, "c"=>3);

Ad comes here

How to run custom MySql query in Magento ?

$db = Mage::getSingleton(‘core/resource’)->getConnection(‘core_write’);
$result=$db->query(“SELECT * FROM PCDSTable”);


$connect = Mage::getSingleton('core/resource')->getConnection('core_read');
$result=$db->query(“SELECT id FROM `customer_wishlists` WHERE `type`='wishlist'");

Ad comes here

Difference between break and continue in PHP?

Break ends a loop completely and continue just shortcuts of a current iteration and moves on to the next iteration.

while ($condition) {   <--------------------+
    continue;          --- goes back here --+
    break;             ----- jumps here ----+
}                                     |
                       <--------------------+

Ad comes here

array_map function

The array_map() function sends each value of an array to a user-made function, and returns an array with new values, given by the user-made function.

Example:

function myfunction($num) {

return($num*$num);

}

$a=array(1,2,3,4,5);

print_r(array_map("myfunction",$a));

Ad comes here
Error's and solution's
Update Cookies Preferences