
ilovephp.net
Are you looking for Payment gateway which suites for your website as well as mobile app platform?
Then braintree makes your work much easier. It’s very simple, easier and quick to integrate using Braintree SDK.
Few important point need to consider while proceed to start on Braintree integration-
- PHP version of your server
- Braintree offer 2 ways API integration
- Drop-in UI
- Custom Integration
1. Depending on you server PHP version you can choise Braintree SDK –
Currently availble latest braintree SDK is (braintree-php-3.9.0) which requires PHP version >= 5.4.0 (https://developers.braintreepayments.com/client_libraries/php/braintree-php-3.9.0.tgz)
If your server runs on PHP version which is < 5.4.0?, don’t worry we have work arround for this issue, you can use (braintree_php-2.40.0) Braintree SDK. (https://codeload.github.com/braintree/braintree_php/zip/2.40.0)
2.a Drop-in UI
– Braintree’s Drop-in UI offers a complete, ready-made payment UI for a quick and easy way to accept payments. It’s equally as easy to maintain PCI compliance with Drop-in; it is eligible for SAQ A since Braintree hosts the form that captures customer payment information.
The UI includes a card entry form and, if enabled, PayPal/Venmo buttons (Venmo only on mobile). When a user completes the UI, your client code obtains a payment method nonce for use on your server.
The Drop-in UI is the quickest way to start accepting payments. Considering security of customer credit card data handing at server end, the Drop-in UI is best choise. In drop in UI you will deal with only a single token (payment_method_nonce) which communicate from website client to website server and then website server to Brintree server.
2.b Custom Integration
In Custom integration, you can create your own payment form with custom colors and layout and collect the customer information before submit to payment gateway. If you need payment form with multilingual support then you can use custom Brintree API integration. In custom Braintree integration you need more developer efferts compare to Drop in UI.
Ref. Link : https://developers.braintreepayments.com/guides/drop-in/javascript/v2
Before proceed for Braintree API integration, we need to sign up for Braintree SDK. Which is free of cost for everyone.
Follow the below link to Sign up to Braintree SDK.
https://www.braintreepayments.com/get-started
After successfull signup we will get –
Merchant ID: xxxxxxx
Public Key: xxxxxxxx
Private Key: xxxxxxxxxxxxxxx
Below are the 3 demo script of Drop-in UI, Hosted field and custom integration of Braintree Gateway API’s –
- Braintree – Payment Gateway integration (Drop-in UI) example :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
<?php // Braintree library require 'braintree/lib/Braintree.php'; $params = array( "testmode" => "on", "merchantid" => "xxxxxxx", "publickey" => "xxxxxxx", "privatekey" => "xxxxxxxxxxxxxxxxxxxxx", ); if ($params['testmode'] == "on") { Braintree_Configuration::environment('sandbox'); } else { Braintree_Configuration::environment('production'); } Braintree_Configuration::merchantId($params["merchantid"]); Braintree_Configuration::publicKey($params["publickey"]); Braintree_Configuration::privateKey($params["privatekey"]); if(isset($_POST['payment_method_nonce'])) { // Customer details $customer_firstname = $_POST['c_firstname']; $customer_lastname = $_POST['c_lastname']; $customer_email = $_POST['c_email']; $customer_phonenumber = $_POST['c_phonenumber']; // EOF Customer details // Customer billing details $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $email = $_POST['c_email']; $address1 = $_POST['address1']; $address2 = $_POST['address2']; $city = $_POST['city']; $state = $_POST['state']; $postcode = $_POST['postcode']; $country = $_POST['country']; $phone = $_POST['c_phonenumber']; // EOF Customer billing details $sale = array( 'amount' => $_POST['amount'], 'orderId' => $_POST['invoiceid'], 'paymentMethodNonce' => $_POST['payment_method_nonce'], // Autogenerated field from braintree 'customer' => array( 'firstName' => $customer_firstname, 'lastName' => $customer_lastname, 'phone' => $customer_phonenumber, 'email' => $customer_email ), 'billing' => array( 'firstName' => $firstname, 'lastName' => $lastname, 'streetAddress' => $address1, 'extendedAddress' => $address2, 'locality' => $city, 'region' => $state, 'postalCode' => $postcode, 'countryCodeAlpha2' => $country ), 'options' => array( 'submitForSettlement' => true, 'storeInVaultOnSuccess' => true ) ); $result = Braintree_Transaction::sale($sale); if ($result->success) { echo "Braintree_cust_id : ".$braintree_cust_id = $result->transaction->_attributes['customer']['id']; // After first successfull transaction, save this Braintree_cust_id in DB and use for future transactions } else { echo "Error : ".$result->_attributes['message']; } print_r($result); exit; } else if (isset($_POST['braintree_cust_id'])) { $sale = array( 'customerId' => $braintree_cust_id, 'amount' => $_POST['amount'], 'orderId' => $_POST['invoiceid'], // This field is get back in responce to track this transaction 'options' => array( 'submitForSettlement' => true ) ); } else if (isset($_POST['action']) && $_POST['action'] == 'generateclienttoken') { //$braintree_cust_id = "31904842"; // Generate the nonce and send it back try { $clientToken = Braintree_ClientToken::generate(array( // use customerId to get a previous customer from the vault // 'customerId' => $braintree_cust_id // $braintree_cust_id is Fetch from DB )); } catch(Exception $e) { // cannot get the customer from the vault!! $clientToken = Braintree_ClientToken::generate(); } echo $clientToken; exit; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Checkout</title> </head> <body> <link href="style.css" type="text/css" rel="stylesheet" /> <h1 class="bt_title">Braintree Drop-in</h1> <div class="dropin-page"> <form id="checkout" method="post" action=""> <h4 class="bt_title">Customer Information</h4> <input type="hidden" name="invoiceid" value="123456"> <fieldset class="one_off_firstname"> <label class="input-label" for="firstname"> <span class="field-name">First Name</span> <input id="c_firstname" name="c_firstname" class="input-field card-field" type="text" placeholder="First Name" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_lastname"> <label class="input-label" for="lastname"> <span class="field-name">Last Name</span> <input id="c_lastname" name="c_lastname" class="input-field card-field" type="text" placeholder="Last Name" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_lastname"> <label class="input-label" for="email"> <span class="field-name">Email</span> <input id="c_email" name="c_email" class="input-field card-field" type="text" placeholder="Email" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_phonenumber"> <label class="input-label" for="phonenumber"> <span class="field-name">Phone Number</span> <input id="c_phonenumber" name="c_phonenumber" class="input-field card-field" type="text"placeholder="Phone Number" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <h4 class="bt_title">Customer Billing Information</h4> <fieldset class="one_off_firstname"> <label class="input-label" for="firstname"> <span class="field-name">First Name</span> <input id="firstname" name="firstname" class="input-field card-field" type="text" placeholder="First Name" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_lastname"> <label class="input-label" for="lastname"> <span class="field-name">Last Name</span> <input id="lastname" name="lastname" class="input-field card-field" type="text" placeholder="Last Name" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_address1"> <label class="input-label" for="address1"> <span class="field-name">Address1</span> <input id="address1" name="address1" class="input-field card-field" type="text" placeholder="Address" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_address2"> <label class="input-label" for="address2"> <span class="field-name">Address2</span> <input id="address2" name="address2" class="input-field card-field" type="text" placeholder="Address" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_city"> <label class="input-label" for="city"> <span class="field-name">City/Town</span> <input id="city" name="city" class="input-field card-field" type="text" placeholder="City/Town" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_state"> <label class="input-label" for="state"> <span class="field-name">State/Region</span> <input id="state" name="state" class="input-field card-field" type="text" placeholder="State/Region" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_postcode"> <label class="input-label" for="postcode"> <span class="field-name">Post Code</span> <input id="postcode" name="postcode" class="input-field card-field" type="text" placeholder="Post Code" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_country"> <label class="input-label" for="country"> <span class="field-name">Country</span> <input id="country" name="country" class="input-field card-field" type="text" placeholder="Country" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <h4 class="bt_title">Credit Card Details</h4> <div id="dropin"> <div class="loader_container"> <div>Loading...</div> </div> </div> <fieldset class="one_off_amount"> <?php if(isset($_GET['amt'])) { $amt = number_format((float)$_GET['amt'], 2, '.', ''); ?> <h3>Your bill is for an amount of $ <?php echo $amt; ?></h3> <input type="hidden" name="amount" step="any" id="amount" value="<?php echo $amt; ?>" /> <?php }else{ ?> <label class="input-label" for="amount"> <span class="field-name">Amount</span> <input id="amount" name="amount" class="input-field card-field" type="number" inputmode="numeric" placeholder="Amount" autocomplete="off" step="any"> <div class="invalid-bottom-bar"></div> </label> <?php } ?> </fieldset> <div class="btn_container"> <input type="submit" value="Make Payment" class="pay-btn"> <span class="loader_img"></span> </div> </form> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <script src="https://js.braintreegateway.com/v2/braintree.js"></script> <!-- TO DO : Place below JS code in js file and include that JS file --> <script type="text/javascript"> (function() { var BTFn = {}; BTFn.sendJSON = function($pay_btn) { $.ajax({ dataType: "text", type: "POST", data: { action: "generateclienttoken"}, url: "braintree_dropin_demo.php", success: function (req) { BTFn.initBT(req, $pay_btn); }, error: function() { } }); }; BTFn.initBT = function(req, $pay_btn) { braintree.setup( req, 'dropin', { container: 'dropin', onReady:function(){ $('.loader_container').remove(); }, onError: function(error) { $pay_btn.show().closest('.btn_container').find('.loader_img').hide(); } }); }; BTFn.formValidate = function($form, $submit, $amount, $pay_btn) { var THIS = this; $submit.on('click', function(e) { $('.input-label .invalid-bottom-bar').removeClass('invalid'); $(this).hide().closest('.btn_container').find('.loader_img').css('display', 'inline-block'); }); }; BTFn.updateForm = function($form, link) { $form.attr('action', link); $('.one_off_amount, .monthly_amount').toggleClass('hide'); }; BTFn.appendTo = function($cont, childSelector, options) { var input = document.createElement(childSelector); input.type = options.type; input.name = options.name; input.value = options.value; $cont.appendChild(input); }; $(document).ready(function() { $('.loader_container').find("div").show(); var $form = $('#checkout'), $submit = $('#checkout input[type="submit"]'), $amount = $('input[name="amount"]'), $pay_btn = $('.pay-btn'); BTFn.sendJSON($pay_btn); BTFn.formValidate($form, $submit, $amount, $pay_btn); }); })(); </script> </body> </html> |
2. Braintree – Payment Gateway integration (Hosted Fields) example :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
<?php // Braintree library require 'braintree/lib/Braintree.php'; $params = array( "testmode" => "on", "merchantid" => "xxxxxxx", "publickey" => "xxxxxxx", "privatekey" => "xxxxxxxxxxxxxxxxxxxxx", ); if ($params['testmode'] == "on") { Braintree_Configuration::environment('sandbox'); } else { Braintree_Configuration::environment('production'); } Braintree_Configuration::merchantId($params["merchantid"]); Braintree_Configuration::publicKey($params["publickey"]); Braintree_Configuration::privateKey($params["privatekey"]); if(isset($_POST['payment_method_nonce'])) { // Customer details $customer_firstname = $_POST['c_firstname']; $customer_lastname = $_POST['c_lastname']; $customer_email = $_POST['c_email']; $customer_phonenumber = $_POST['c_phonenumber']; // EOF Customer details // Customer billing details $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $email = $_POST['c_email']; $address1 = $_POST['address1']; $address2 = $_POST['address2']; $city = $_POST['city']; $state = $_POST['state']; $postcode = $_POST['postcode']; $country = $_POST['country']; $phone = $_POST['c_phonenumber']; // EOF Customer billing details $sale = array( 'amount' => $_POST['amount'], 'orderId' => $_POST['invoiceid'], 'paymentMethodNonce' => $_POST['payment_method_nonce'], // Autogenerated field from braintree 'customer' => array( 'firstName' => $customer_firstname, 'lastName' => $customer_lastname, 'phone' => $customer_phonenumber, 'email' => $customer_email ), 'billing' => array( 'firstName' => $firstname, 'lastName' => $lastname, 'streetAddress' => $address1, 'extendedAddress' => $address2, 'locality' => $city, 'region' => $state, 'postalCode' => $postcode, 'countryCodeAlpha2' => $country ), 'options' => array( 'submitForSettlement' => true, 'storeInVaultOnSuccess' => true ) ); $result = Braintree_Transaction::sale($sale); if ($result->success) { echo "Braintree_cust_id : ".$braintree_cust_id = $result->transaction->_attributes['customer']['id']; // After first successfull transaction, save this Braintree_cust_id in DB and use for future transactions } else { echo "Error : ".$result->_attributes['message']; } print_r($result); exit; } else if (isset($_POST['braintree_cust_id'])) { $sale = array( 'customerId' => $braintree_cust_id, 'amount' => $_POST['amount'], 'orderId' => $_POST['invoiceid'], // This field is get back in responce to track this transaction 'options' => array( 'submitForSettlement' => true ) ); } else if (isset($_POST['action']) && $_POST['action'] == 'generateclienttoken') { //$braintree_cust_id = "31904842"; // Generate the nonce and send it back try { $clientToken = Braintree_ClientToken::generate(array( // use customerId to get a previous customer from the vault // 'customerId' => $braintree_cust_id // $braintree_cust_id is Fetch from DB )); } catch(Exception $e) { // cannot get the customer from the vault!! $clientToken = Braintree_ClientToken::generate(); } echo $clientToken; exit; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Braintree Hosted Fields Demo</title> <link href="style.css" type="text/css" rel="stylesheet" /> </head> <body> <h1 class="bt_title">Braintree Hosted Fields Demo</h1> <form id="bt_custom_form" name="bt_custom_form" method="post" action=""> <div class="dropin-page"> <h4 class="bt_title">Customer Information</h4> <input type="hidden" name="invoiceid" value="123456"> <fieldset class="one_off_firstname"> <label class="input-label" for="firstname"> <span class="field-name">First Name</span> <input id="c_firstname" name="c_firstname" class="input-field card-field" type="text" placeholder="First Name" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_lastname"> <label class="input-label" for="lastname"> <span class="field-name">Last Name</span> <input id="c_lastname" name="c_lastname" class="input-field card-field" type="text" placeholder="Last Name" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_lastname"> <label class="input-label" for="email"> <span class="field-name">Email</span> <input id="c_email" name="c_email" class="input-field card-field" type="text" placeholder="Email" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_phonenumber"> <label class="input-label" for="phonenumber"> <span class="field-name">Phone Number</span> <input id="c_phonenumber" name="c_phonenumber" class="input-field card-field" type="text"placeholder="Phone Number" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <h4 class="bt_title">Customer Billing Information</h4> <fieldset class="one_off_firstname"> <label class="input-label" for="firstname"> <span class="field-name">First Name</span> <input id="firstname" name="firstname" class="input-field card-field" type="text" placeholder="First Name" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_lastname"> <label class="input-label" for="lastname"> <span class="field-name">Last Name</span> <input id="lastname" name="lastname" class="input-field card-field" type="text" placeholder="Last Name" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_address1"> <label class="input-label" for="address1"> <span class="field-name">Address1</span> <input id="address1" name="address1" class="input-field card-field" type="text" placeholder="Address" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_address2"> <label class="input-label" for="address2"> <span class="field-name">Address2</span> <input id="address2" name="address2" class="input-field card-field" type="text" placeholder="Address" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_city"> <label class="input-label" for="city"> <span class="field-name">City/Town</span> <input id="city" name="city" class="input-field card-field" type="text" placeholder="City/Town" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_state"> <label class="input-label" for="state"> <span class="field-name">State/Region</span> <input id="state" name="state" class="input-field card-field" type="text" placeholder="State/Region" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_postcode"> <label class="input-label" for="postcode"> <span class="field-name">Post Code</span> <input id="postcode" name="postcode" class="input-field card-field" type="text" placeholder="Post Code" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_country"> <label class="input-label" for="country"> <span class="field-name">Country</span> <input id="country" name="country" class="input-field card-field" type="text" placeholder="Country" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <h4 class="bt_title">Credit Card Details</h4> </div> <div class="loader_container"> <div class="loader_img loader"></div> </div> <div class="bt_form_wrap hide invisible"> <fieldset> <p class="amountDeclare">Amount to be paid 10.00 ($)</p> <input type="hidden" name="amount" id="amount" value="10" /> </fieldset> <fieldset> <label for="bt_card_number">Card number</label> <div id="bt_card_number" class="inputFields"></div> </fieldset> <fieldset> <label for="bt_cvv">CVV</label> <div id="bt_cvv" class="inputFields"></div> </fieldset> <fieldset> <label for="bt_exp_date">Expiration date (MM/YY)</label> <div id="bt_exp_date" class="inputFields"></div> </fieldset> <div id="bt_paypal_container"></div> <div class="btn_container"> <button class="pay-btn">Make Payment</button> <span class="loader_img"></span> </div> </div> </form> <script type="text/javascript" src="https://js.braintreegateway.com/js/beta/braintree-hosted-fields-beta.18.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <!-- TO DO : Place below JS code in js file and include that JS file --> <script type="text/javascript"> (function() { var BTFn = {}; BTFn.sendJSON = function($obj) { $.ajax({ dataType: "text", type: "POST", data: { action: "generateclienttoken"}, url: "braintree_hosted_fields_demo.php", success: function (req) { BTFn.initBT(req, $obj); }, error: function() { } }); }; BTFn.initBT = function(req, $obj) { // we're on the custom fields tab braintree.setup(req, 'custom', { id: 'bt_custom_form', hostedFields: { number: { selector: '#bt_card_number' }, cvv: { selector: '#bt_cvv' }, expirationDate: { selector: '#bt_exp_date' } }, onReady: function() { BTFn.showBtForm(); }, onError: function(error) { BTFn.showFormErrors(error); $obj.show().closest('.btn_container').find('.loader_img').hide(); }, paypal:{ container:'bt_paypal_container', } }); }; BTFn.showBtForm = function() { var eAnimation = this.detectAnimationEvent(); $('.loader_container').addClass('fadeOut'); $('.loader_container').one(eAnimation, function(event) { $('.loader_container').remove(); $('.bt_form_wrap').removeClass('hide'); $('.bt_form_wrap').addClass('fadeIn').removeClass('invisible'); }); }; BTFn.detectAnimationEvent = function() { var t, el = document.createElement("fakeelement"); var animations = { "animation" : "animationend", "OAnimation" : "oAnimationEnd", "MozAnimation" : "animationend", "WebkitAnimation": "webkitAnimationEnd" } for (t in animations){ if (el.style[t] !== undefined){ return animations[t]; } } }; BTFn.showFormErrors = function(error) { var inputMap = { 'number' : '#bt_card_number', 'cvv' : '#bt_cvv', 'expirationDate' : '#bt_exp_date' }; if(!error.details) { $('.inputFields').addClass('inputError'); }else{ var i, errorLength = error.details.invalidFieldKeys.length; $('.inputFields').removeClass('inputError'); for(var i=0; i < errorLength; i++) { $(inputMap[error.details.invalidFieldKeys[i]]).addClass('inputError'); } } }; $(document).ready(function() { var $pay_btn = $('.pay-btn'); BTFn.sendJSON($pay_btn); $pay_btn.on('click', function() { $(this).hide().closest('.btn_container').find('.loader_img').css('display', 'inline-block'); }); }); })(); </script> </body> </html> |
3. Braintree – Payment Gateway Custom integration example :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
<?php // Braintree library require 'braintree/lib/Braintree.php'; $params = array( "testmode" => "on", "merchantid" => "xxxxxxx", "publickey" => "xxxxxxx", "privatekey" => "xxxxxxxxxxxxxxxxxxxxx", ); if ($params['testmode'] == "on") { Braintree_Configuration::environment('sandbox'); } else { Braintree_Configuration::environment('production'); } Braintree_Configuration::merchantId($params["merchantid"]); Braintree_Configuration::publicKey($params["publickey"]); Braintree_Configuration::privateKey($params["privatekey"]); if(isset($_POST['make_payment'])) { // Customer details $customer_firstname = $_POST['c_firstname']; $customer_lastname = $_POST['c_lastname']; $customer_email = $_POST['c_email']; $customer_phonenumber = $_POST['c_phonenumber']; // EOF Customer details // Customer billing details $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $email = $_POST['c_email']; $address1 = $_POST['address1']; $address2 = $_POST['address2']; $city = $_POST['city']; $state = $_POST['state']; $postcode = $_POST['postcode']; $country = $_POST['country']; $phone = $_POST['c_phonenumber']; // EOF Customer billing details // Credit Card Details $card_number = $_POST['card_number']; $cvv = $_POST['cvv']; $exp_date = explode("/",$_POST['exp_date']); // EOF Credit Card Details // Create customer in braintree Vault $result = Braintree_Customer::create(array( 'firstName' => $customer_firstname, 'lastName' => $customer_lastname, 'phone' => $customer_phonenumber, 'email' => $customer_email, 'creditCard' => array( 'number' => $card_number, 'cardholderName' => $firstname . " " . $lastname, 'expirationMonth' => $exp_date[0], 'expirationYear' => $exp_date[1], 'cvv' => $cvv, 'billingAddress' => array( 'postalCode' => $postcode, 'streetAddress' => $address1, 'extendedAddress' => $address2, 'locality' => $city, 'region' => $state, 'countryCodeAlpha2' => $country ) ) )); if ($result->success) { // Save this Braintree_cust_id in DB and use for future transactions too $braintree_cust_id = $result->customer->id; } else { die("Error : ".$result->message); } // EOF Create customer in braintree Vault $sale = array( 'customerId' => $braintree_cust_id, 'amount' => $_POST['amount'], 'orderId' => $_POST['invoiceid'], 'options' => array('submitForSettlement' => true) ); $result = Braintree_Transaction::sale($sale); if ($result->success) { // Execute on payment success event at here } else { echo "Error : ".$result->_attributes['message']; } print_r($result); exit; } else if (isset($_POST['braintree_cust_id'])) { $sale = array( 'customerId' => $braintree_cust_id, 'amount' => $_POST['amount'], 'orderId' => $_POST['invoiceid'], // This field is get back in responce to track this transaction 'options' => array('submitForSettlement' => true) ); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Checkout</title> </head> <body> <link href="style.css" type="text/css" rel="stylesheet" /> <h1 class="bt_title">Braintree Custom Integration</h1> <div class="dropin-page"> <form id="checkout" method="post" action=""> <h4 class="bt_title">Customer Information</h4> <input type="hidden" name="invoiceid" value="123456"> <fieldset class="one_off_firstname"> <label class="input-label" for="firstname"> <span class="field-name">First Name</span> <input id="c_firstname" name="c_firstname" class="input-field card-field" type="text" placeholder="First Name" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_lastname"> <label class="input-label" for="lastname"> <span class="field-name">Last Name</span> <input id="c_lastname" name="c_lastname" class="input-field card-field" type="text" placeholder="Last Name" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_lastname"> <label class="input-label" for="email"> <span class="field-name">Email</span> <input id="c_email" name="c_email" class="input-field card-field" type="text" placeholder="Email" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_phonenumber"> <label class="input-label" for="phonenumber"> <span class="field-name">Phone Number</span> <input id="c_phonenumber" name="c_phonenumber" class="input-field card-field" type="text"placeholder="Phone Number" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <h4 class="bt_title">Customer Billing Information</h4> <fieldset class="one_off_firstname"> <label class="input-label" for="firstname"> <span class="field-name">First Name</span> <input id="firstname" name="firstname" class="input-field card-field" type="text" placeholder="First Name" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_lastname"> <label class="input-label" for="lastname"> <span class="field-name">Last Name</span> <input id="lastname" name="lastname" class="input-field card-field" type="text" placeholder="Last Name" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_address1"> <label class="input-label" for="address1"> <span class="field-name">Address1</span> <input id="address1" name="address1" class="input-field card-field" type="text" placeholder="Address" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_address2"> <label class="input-label" for="address2"> <span class="field-name">Address2</span> <input id="address2" name="address2" class="input-field card-field" type="text" placeholder="Address" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_city"> <label class="input-label" for="city"> <span class="field-name">City/Town</span> <input id="city" name="city" class="input-field card-field" type="text" placeholder="City/Town" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_state"> <label class="input-label" for="state"> <span class="field-name">State/Region</span> <input id="state" name="state" class="input-field card-field" type="text" placeholder="State/Region" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_postcode"> <label class="input-label" for="postcode"> <span class="field-name">Post Code</span> <input id="postcode" name="postcode" class="input-field card-field" type="text" placeholder="Post Code" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_country"> <label class="input-label" for="country"> <span class="field-name">Country</span> <input id="country" name="country" class="input-field card-field" type="text" placeholder="Country" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <h4 class="bt_title">Credit Card Details</h4> <fieldset class="one_off_country"> <label class="input-label" for="country"> <span class="field-name">Card number</span> <input id="card_number" name="card_number" class="input-field card-field" type="text" placeholder="Card number" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_country"> <label class="input-label" for="country"> <span class="field-name">CVV</span> <input id="CVV" name="cvv" class="input-field card-field" type="text" placeholder="CVV" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_country"> <label class="input-label" for="country"> <span class="field-name">Expiration date (MM/YY)</span> <input id="exp_date" name="exp_date" class="input-field card-field" type="text" placeholder="Expiration date (MM/YY)" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_amount"> <label class="input-label" for="amount"> <span class="field-name">Amount</span> <input id="amount" name="amount" class="input-field card-field" type="number" inputmode="numeric" placeholder="Amount" autocomplete="off" step="any"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <div class="btn_container"> <input type="submit" name="make_payment" value="Make Payment" class="pay-btn"> <span class="loader_img"></span> </div> </form> </div> </body> </html> |
Braintree Payment Gateway integration with 3D secure in Drop in-UI Example (Demo) :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
<?php // Braintree library require 'braintree/lib/Braintree.php'; $params = array( "testmode" => "on", "merchantid" => "xxxxxxx", "publickey" => "xxxxxxx", "privatekey" => "xxxxxxxxxxxxxxxxxxxxx", ); if ($params['testmode'] == "on") { Braintree_Configuration::environment('sandbox'); } else { Braintree_Configuration::environment('production'); } Braintree_Configuration::merchantId($params["merchantid"]); Braintree_Configuration::publicKey($params["publickey"]); Braintree_Configuration::privateKey($params["privatekey"]); if(isset($_POST['payment_method_nonce'])) { // Customer details $customer_firstname = $_POST['c_firstname']; $customer_lastname = $_POST['c_lastname']; $customer_email = $_POST['c_email']; $customer_phonenumber = $_POST['c_phonenumber']; // EOF Customer details // Customer billing details $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $email = $_POST['c_email']; $address1 = $_POST['address1']; $address2 = $_POST['address2']; $city = $_POST['city']; $state = $_POST['state']; $postcode = $_POST['postcode']; $country = $_POST['country']; $phone = $_POST['c_phonenumber']; // EOF Customer billing details $sale = array( 'amount' => $_POST['amount'], 'orderId' => $_POST['invoiceid'], 'paymentMethodNonce' => $_POST['payment_method_nonce'], // Autogenerated field from braintree 'customer' => array( 'firstName' => $customer_firstname, 'lastName' => $customer_lastname, 'phone' => $customer_phonenumber, 'email' => $customer_email ), 'billing' => array( 'firstName' => $firstname, 'lastName' => $lastname, 'streetAddress' => $address1, 'extendedAddress' => $address2, 'locality' => $city, 'region' => $state, 'postalCode' => $postcode, 'countryCodeAlpha2' => $country ), 'options' => array( 'submitForSettlement' => true, 'storeInVaultOnSuccess' => true, 'three_d_secure' => array('required' => true) ) ); $result = Braintree_Transaction::sale($sale); if ($result->success) { echo "Braintree_cust_id : ".$braintree_cust_id = $result->transaction->_attributes['customer']['id']; // After first successfull transaction, save this Braintree_cust_id in DB and use for future transactions } else { echo "Error : ".$result->_attributes['message']; } print_r($result); exit; } else if (isset($_POST['braintree_cust_id'])) { $sale = array( 'customerId' => $braintree_cust_id, 'amount' => $_POST['amount'], 'orderId' => $_POST['invoiceid'], // This field is get back in responce to track this transaction 'options' => array( 'submitForSettlement' => true ) ); } else if (isset($_POST['action']) && $_POST['action'] == 'generateclienttoken') { //$braintree_cust_id = "31904842"; // Generate the nonce and send it back try { $clientToken = Braintree_ClientToken::generate(array( // use customerId to get a previous customer from the vault // 'customerId' => $braintree_cust_id // $braintree_cust_id is Fetch from DB )); } catch(Exception $e) { // cannot get the customer from the vault!! $clientToken = Braintree_ClientToken::generate(); } echo $clientToken; exit; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Checkout</title> </head> <body> <link href="style.css" type="text/css" rel="stylesheet" /> <h1 class="bt_title">Braintree Drop-in</h1> <div class="dropin-page"> <form id="checkout" method="post" action=""> <h4 class="bt_title">Customer Information</h4> <input type="hidden" name="invoiceid" value="123456"> <fieldset class="one_off_firstname"> <label class="input-label" for="firstname"> <span class="field-name">First Name</span> <input id="c_firstname" name="c_firstname" class="input-field card-field" type="text" placeholder="First Name" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_lastname"> <label class="input-label" for="lastname"> <span class="field-name">Last Name</span> <input id="c_lastname" name="c_lastname" class="input-field card-field" type="text" placeholder="Last Name" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_lastname"> <label class="input-label" for="email"> <span class="field-name">Email</span> <input id="c_email" name="c_email" class="input-field card-field" type="text" placeholder="Email" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_phonenumber"> <label class="input-label" for="phonenumber"> <span class="field-name">Phone Number</span> <input id="c_phonenumber" name="c_phonenumber" class="input-field card-field" type="text"placeholder="Phone Number" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <h4 class="bt_title">Customer Billing Information</h4> <fieldset class="one_off_firstname"> <label class="input-label" for="firstname"> <span class="field-name">First Name</span> <input id="firstname" name="firstname" class="input-field card-field" type="text" placeholder="First Name" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_lastname"> <label class="input-label" for="lastname"> <span class="field-name">Last Name</span> <input id="lastname" name="lastname" class="input-field card-field" type="text" placeholder="Last Name" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_address1"> <label class="input-label" for="address1"> <span class="field-name">Address1</span> <input id="address1" name="address1" class="input-field card-field" type="text" placeholder="Address" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_address2"> <label class="input-label" for="address2"> <span class="field-name">Address2</span> <input id="address2" name="address2" class="input-field card-field" type="text" placeholder="Address" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_city"> <label class="input-label" for="city"> <span class="field-name">City/Town</span> <input id="city" name="city" class="input-field card-field" type="text" placeholder="City/Town" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_state"> <label class="input-label" for="state"> <span class="field-name">State/Region</span> <input id="state" name="state" class="input-field card-field" type="text" placeholder="State/Region" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_postcode"> <label class="input-label" for="postcode"> <span class="field-name">Post Code</span> <input id="postcode" name="postcode" class="input-field card-field" type="text" placeholder="Post Code" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_country"> <label class="input-label" for="country"> <span class="field-name">Country</span> <input id="country" name="country" class="input-field card-field" type="text" placeholder="Country" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <h4 class="bt_title">Credit Card Details</h4> <div id="dropin"> <div class="loader_container"> <div>Loading...</div> </div> </div> <fieldset class="one_off_amount"> <?php if(isset($_GET['amt'])) { $amt = number_format((float)$_GET['amt'], 2, '.', ''); ?> <h3>Your bill is for an amount of $ <?php echo $amt; ?></h3> <input type="hidden" name="amount" step="any" id="amount" value="<?php echo $amt; ?>" /> <?php }else{ ?> <label class="input-label" for="amount"> <span class="field-name">Amount</span> <input id="amount" name="amount" class="input-field card-field" type="number" inputmode="numeric" placeholder="Amount" autocomplete="off" step="any"> <div class="invalid-bottom-bar"></div> </label> <?php } ?> </fieldset> <div class="btn_container"> <input type="submit" value="Make Payment" class="pay-btn"> <span class="loader_img"></span> </div> </form> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <script src="https://js.braintreegateway.com/v2/braintree.js"></script> <!-- TO DO : Place below JS code in js file and include that JS file --> <script type="text/javascript"> (function() { var BTFn = {}; var bt_client_token = ''; BTFn.sendJSON = function($pay_btn) { $.ajax({ dataType: "text", type: "POST", data: { action: "generateclienttoken"}, url: "braintree_dropin_with_3d_secure_demo.php", success: function (req) { bt_client_token = req; BTFn.initBT(req, $pay_btn); }, error: function() { } }); }; BTFn.initBT = function(req, $pay_btn) { braintree.setup( req, 'dropin', { container: 'dropin', onPaymentMethodReceived:function(obj){ var client = new braintree.api.Client({ clientToken: bt_client_token }); client.verify3DS({ amount: $('#amount').val(), creditCard: obj.nonce }, function (error, response) { if (!error) { // 3D Secure finished. send the response.nonce to server for further processing BTFn.appendTo(document.forms.checkout, 'input', {name: 'payment_method_nonce', type: 'hidden', value: response.nonce}); document.forms.checkout.submit(); } else { // Handle errors alert("Error : "+error.message); } }); }, onReady:function(){ $('.loader_container').remove(); }, onError: function(error) { $pay_btn.show().closest('.btn_container').find('.loader_img').hide(); } }); }; BTFn.formValidate = function($form, $submit, $amount, $pay_btn) { var THIS = this; $submit.on('click', function(e) { $('.input-label .invalid-bottom-bar').removeClass('invalid'); $(this).hide().closest('.btn_container').find('.loader_img').css('display', 'inline-block'); }); }; BTFn.updateForm = function($form, link) { $form.attr('action', link); $('.one_off_amount, .monthly_amount').toggleClass('hide'); }; BTFn.appendTo = function($cont, childSelector, options) { var input = document.createElement(childSelector); input.type = options.type; input.name = options.name; input.value = options.value; $cont.appendChild(input); }; $(document).ready(function() { $('.loader_container').find("div").show(); var $form = $('#checkout'), $submit = $('#checkout input[type="submit"]'), $amount = $('input[name="amount"]'), $pay_btn = $('.pay-btn'); BTFn.sendJSON($pay_btn); BTFn.formValidate($form, $submit, $amount, $pay_btn); }); })(); </script> </body> </html> |
Braintree – Payment Gateway integration with 3D secure (Hosted Fields) example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
<?php // Braintree library require 'braintree/lib/Braintree.php'; $params = array( "testmode" => "on", "merchantid" => "xxxxxxx", "publickey" => "xxxxxxx", "privatekey" => "xxxxxxxxxxxxxxxxxxxxx", ); if ($params['testmode'] == "on") { Braintree_Configuration::environment('sandbox'); } else { Braintree_Configuration::environment('production'); } Braintree_Configuration::merchantId($params["merchantid"]); Braintree_Configuration::publicKey($params["publickey"]); Braintree_Configuration::privateKey($params["privatekey"]); if(isset($_POST['payment_method_nonce'])) { // Customer details $customer_firstname = $_POST['c_firstname']; $customer_lastname = $_POST['c_lastname']; $customer_email = $_POST['c_email']; $customer_phonenumber = $_POST['c_phonenumber']; // EOF Customer details // Customer billing details $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $email = $_POST['c_email']; $address1 = $_POST['address1']; $address2 = $_POST['address2']; $city = $_POST['city']; $state = $_POST['state']; $postcode = $_POST['postcode']; $country = $_POST['country']; $phone = $_POST['c_phonenumber']; // EOF Customer billing details $sale = array( 'amount' => $_POST['amount'], 'orderId' => $_POST['invoiceid'], 'paymentMethodNonce' => $_POST['payment_method_nonce'], // Autogenerated field from braintree 'customer' => array( 'firstName' => $customer_firstname, 'lastName' => $customer_lastname, 'phone' => $customer_phonenumber, 'email' => $customer_email ), 'billing' => array( 'firstName' => $firstname, 'lastName' => $lastname, 'streetAddress' => $address1, 'extendedAddress' => $address2, 'locality' => $city, 'region' => $state, 'postalCode' => $postcode, 'countryCodeAlpha2' => $country ), 'options' => array( 'submitForSettlement' => true, 'storeInVaultOnSuccess' => true, 'three_d_secure' => array('required' => true) ) ); $result = Braintree_Transaction::sale($sale); if ($result->success) { echo "Braintree_cust_id : ".$braintree_cust_id = $result->transaction->_attributes['customer']['id']; // After first successfull transaction, save this Braintree_cust_id in DB and use for future transactions } else { echo "Error : ".$result->_attributes['message']; } print_r($result); exit; } else if (isset($_POST['braintree_cust_id'])) { $sale = array( 'customerId' => $braintree_cust_id, 'amount' => $_POST['amount'], 'orderId' => $_POST['invoiceid'], // This field is get back in responce to track this transaction 'options' => array( 'submitForSettlement' => true ) ); } else if (isset($_POST['action']) && $_POST['action'] == 'generateclienttoken') { //$braintree_cust_id = "60033487"; // Generate the nonce and send it back try { $clientToken = Braintree_ClientToken::generate(array( // use customerId to get a previous customer from the vault // 'customerId' => $braintree_cust_id // $braintree_cust_id is Fetch from DB )); } catch(Exception $e) { // cannot get the customer from the vault!! $clientToken = Braintree_ClientToken::generate(); } echo $clientToken; exit; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Braintree Hosted Fields Demo</title> <link href="style.css" type="text/css" rel="stylesheet" /> </head> <body> <h1 class="bt_title">Braintree Hosted Fields Demo</h1> <form id="bt_custom_form" name="bt_custom_form" method="post" action=""> <div class="dropin-page"> <h4 class="bt_title">Customer Information</h4> <input type="hidden" name="invoiceid" value="123456"> <fieldset class="one_off_firstname"> <label class="input-label" for="firstname"> <span class="field-name">First Name</span> <input id="c_firstname" name="c_firstname" class="input-field card-field" type="text" placeholder="First Name" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_lastname"> <label class="input-label" for="lastname"> <span class="field-name">Last Name</span> <input id="c_lastname" name="c_lastname" class="input-field card-field" type="text" placeholder="Last Name" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_lastname"> <label class="input-label" for="email"> <span class="field-name">Email</span> <input id="c_email" name="c_email" class="input-field card-field" type="text" placeholder="Email" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_phonenumber"> <label class="input-label" for="phonenumber"> <span class="field-name">Phone Number</span> <input id="c_phonenumber" name="c_phonenumber" class="input-field card-field" type="text"placeholder="Phone Number" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <h4 class="bt_title">Customer Billing Information</h4> <fieldset class="one_off_firstname"> <label class="input-label" for="firstname"> <span class="field-name">First Name</span> <input id="firstname" name="firstname" class="input-field card-field" type="text" placeholder="First Name" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_lastname"> <label class="input-label" for="lastname"> <span class="field-name">Last Name</span> <input id="lastname" name="lastname" class="input-field card-field" type="text" placeholder="Last Name" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_address1"> <label class="input-label" for="address1"> <span class="field-name">Address1</span> <input id="address1" name="address1" class="input-field card-field" type="text" placeholder="Address" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_address2"> <label class="input-label" for="address2"> <span class="field-name">Address2</span> <input id="address2" name="address2" class="input-field card-field" type="text" placeholder="Address" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_city"> <label class="input-label" for="city"> <span class="field-name">City/Town</span> <input id="city" name="city" class="input-field card-field" type="text" placeholder="City/Town" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_state"> <label class="input-label" for="state"> <span class="field-name">State/Region</span> <input id="state" name="state" class="input-field card-field" type="text" placeholder="State/Region" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_postcode"> <label class="input-label" for="postcode"> <span class="field-name">Post Code</span> <input id="postcode" name="postcode" class="input-field card-field" type="text" placeholder="Post Code" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <fieldset class="one_off_country"> <label class="input-label" for="country"> <span class="field-name">Country</span> <input id="country" name="country" class="input-field card-field" type="text" placeholder="Country" autocomplete="off"> <div class="invalid-bottom-bar"></div> </label> </fieldset> <h4 class="bt_title">Credit Card Details</h4> </div> <div class="loader_container"> <div class="loader_img loader"></div> </div> <div class="bt_form_wrap hide invisible"> <fieldset> <p class="amountDeclare">Amount to be paid 10.00 ($)</p> <input type="hidden" name="amount" id="amount" value="10" /> </fieldset> <fieldset> <label for="bt_card_number">Card number</label> <div id="bt_card_number" class="inputFields"></div> </fieldset> <fieldset> <label for="bt_cvv">CVV</label> <div id="bt_cvv" class="inputFields"></div> </fieldset> <fieldset> <label for="bt_exp_date">Expiration date (MM/YY)</label> <div id="bt_exp_date" class="inputFields"></div> </fieldset> <div id="bt_paypal_container"></div> <div class="btn_container"> <button class="pay-btn">Make Payment</button> <span class="loader_img"></span> </div> </div> </form> <script type="text/javascript" src="https://js.braintreegateway.com/js/beta/braintree-hosted-fields-beta.18.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <!-- TO DO : Place below JS code in js file and include that JS file --> <script type="text/javascript"> (function() { var BTFn = {}; var bt_client_token = ''; BTFn.sendJSON = function($obj) { $.ajax({ dataType: "text", type: "POST", data: { action: "generateclienttoken"}, url: "braintree_hosted_fields_with_3d_secure_demo.php", success: function (req) { bt_client_token = req; BTFn.initBT(req, $obj); }, error: function() { } }); }; BTFn.initBT = function(req, $obj) { // we're on the custom fields tab braintree.setup(req, 'custom', { id: 'bt_custom_form', hostedFields: { number: { selector: '#bt_card_number' }, cvv: { selector: '#bt_cvv' }, expirationDate: { selector: '#bt_exp_date' } }, onPaymentMethodReceived:function(obj){ var client = new braintree.api.Client({ clientToken: bt_client_token }); client.verify3DS({ amount: $('#amount').val(), creditCard: obj.nonce }, function (error, response) { if (!error) { // 3D Secure finished. send the response.nonce to server for further processing BTFn.appendTo(document.forms.bt_custom_form, 'input', {name: 'payment_method_nonce', type: 'hidden', value: response.nonce}); document.forms.bt_custom_form.submit(); } else { // Handle errors alert("Error : "+error.message); } }); }, onReady: function() { BTFn.showBtForm(); }, onError: function(error) { BTFn.showFormErrors(error); $obj.show().closest('.btn_container').find('.loader_img').hide(); }, paypal:{ container:'bt_paypal_container', } }); }; BTFn.showBtForm = function() { var eAnimation = this.detectAnimationEvent(); $('.loader_container').addClass('fadeOut'); $('.loader_container').one(eAnimation, function(event) { $('.loader_container').remove(); $('.bt_form_wrap').removeClass('hide'); $('.bt_form_wrap').addClass('fadeIn').removeClass('invisible'); }); }; BTFn.detectAnimationEvent = function() { var t, el = document.createElement("fakeelement"); var animations = { "animation" : "animationend", "OAnimation" : "oAnimationEnd", "MozAnimation" : "animationend", "WebkitAnimation": "webkitAnimationEnd" } for (t in animations){ if (el.style[t] !== undefined){ return animations[t]; } } }; BTFn.showFormErrors = function(error) { var inputMap = { 'number' : '#bt_card_number', 'cvv' : '#bt_cvv', 'expirationDate' : '#bt_exp_date' }; if(!error.details) { $('.inputFields').addClass('inputError'); }else{ var i, errorLength = error.details.invalidFieldKeys.length; $('.inputFields').removeClass('inputError'); for(var i=0; i < errorLength; i++) { $(inputMap[error.details.invalidFieldKeys[i]]).addClass('inputError'); } } }; BTFn.appendTo = function($cont, childSelector, options) { var input = document.createElement(childSelector); input.type = options.type; input.name = options.name; input.value = options.value; $cont.appendChild(input); }; $(document).ready(function() { var $pay_btn = $('.pay-btn'); BTFn.sendJSON($pay_btn); $pay_btn.on('click', function() { $(this).hide().closest('.btn_container').find('.loader_img').css('display', 'inline-block'); }); }); })(); </script> </body> </html> |
You can download all above demos from below link :
Download Source Code
Or
Browse source code in github : https://github.com/sagarsdeshmukh/Braintree_gateway_integration_demo_with_PHP
If you have any additional questions Or queries, don’t hesitate to let me know!, Just put comment below or contact @ my mail id sagarsdeshmukh@gmail.com
Hope you enjoy my post 🙂
) )
( (
……..
[| |
\ /
`—-‘
Happy coding!
I have a Sandbox Account, add the merchant id, public key, and private key using the drop in demo. On text, the Card Fails, and I have more than enough to test with on that card. Am I missing something here?
Hi Scott,
You are testing Braintree Sandbox account, hope you try test card details.
Here are available test card details from Braintree :
https://developers.braintreepayments.com/reference/general/testing/php#no-credit-card-errors
Please try with above test cards details.
It saved a lot of time for me. Thanks for your code snippets
I it really helpful to me..
Thank you.. 🙂
hi im just wondering if i can use ur codings for commercial use? is there anything i need to do to avoid violating?
Thank you. I was really confused about it.
Now I guess things are going to be much easier.
Thank you!