[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 require(__DIR__ . '/vendor/autoload.php'); 4 5 Httpful\Bootstrap::init(); 6 RESTful\Bootstrap::init(); 7 Balanced\Bootstrap::init(); 8 9 $API_KEY_SECRET = '5f4db668a5ec11e1b908026ba7e239a9'; 10 $page = $_SERVER['REQUEST_URI']; 11 Balanced\Settings::$api_key = $API_KEY_SECRET; 12 $marketplace = Balanced\Marketplace::mine(); 13 14 if ($page == '/') { 15 // do nothing 16 } elseif ($page == '/buyer') { 17 if (isset($_POST['uri']) and isset($_POST['email_address'])) { 18 // create in balanced 19 $email_address = $_POST['email_address']; 20 $card_uri = $_POST['uri']; 21 try { 22 echo create_buyer($email_address, $card_uri)->uri; 23 return; 24 } catch (Balanced\Errors\Error $e) { 25 echo $e->getMessage(); 26 return; 27 } 28 } 29 } 30 31 function create_buyer($email_address, $card_uri) { 32 $marketplace = Balanced\Marketplace::mine(); 33 try { 34 # new buyer 35 $buyer = $marketplace->createBuyer( 36 $email_address, 37 $card_uri); 38 } 39 catch (Balanced\Errors\DuplicateAccountEmailAddress $e) { 40 # oops, account for $email_address already exists so just add the card 41 $buyer = Balanced\Account::get($e->extras->account_uri); 42 $buyer->addCard($card_uri); 43 } 44 return $buyer; 45 } 46 47 ?> 48 <html> 49 <head> 50 <link rel="stylesheet" href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css" type="text/css"> 51 <style type="text/css"> 52 [name="marketplace_eid"] { 53 width: 300px; 54 } 55 [name^="expiration"] { 56 width: 50px; 57 } 58 [name="security_code"] { 59 width: 50px; 60 } 61 code { display: block; } 62 pre { color: green; } 63 </style> 64 </head> 65 <body> 66 <h1>Balanced Sample - Collect Credit Card Information</h1> 67 <div class="row"> 68 <div class="span6"> 69 <form id="payment"> 70 <div> 71 <label>Email Address</label> 72 <input name="email_address" value="[email protected]"> 73 </div> 74 <div> 75 <label>Card Number</label> 76 <input name="card_number" value="4111111111111111" autocomplete="off"> 77 </div> 78 <div> 79 <label>Expiration</label> 80 <input name="expiration_month" value="1"> / <input name="expiration_year" value="2020"> 81 </div> 82 <div> 83 <label>Security Code</label> 84 <input name="security_code" value="123" autocomplete="off"> 85 </div> 86 <button>Submit Payment Data</button> 87 </form> 88 </div> 89 </div> 90 <div id="result"></div> 91 <script type="text/javascript" src="https://js.balancedpayments.com/v1/balanced.js"></script> 92 <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 93 <script type="text/javascript"> 94 var marketplaceUri = '<?php echo $marketplace->uri; ?>'; 95 96 var debug = function (tag, content) { 97 $('<' + tag + '>' + content + '</' + tag + '>').appendTo('#result'); 98 }; 99 100 try { 101 balanced.init(marketplaceUri); 102 } catch (e) { 103 debug('code', 'You need to set the marketplaceUri variable'); 104 } 105 106 function accountCreated(response) { 107 debug('code', 'account create result: ' + response); 108 } 109 110 function balancedCallback(response) { 111 var tag = (response.status < 300) ? 'pre' : 'code'; 112 debug(tag, JSON.stringify(response)); 113 switch (response.status) { 114 case 201: 115 // response.data.uri == uri of the card resource, submit to your server 116 $.post('/buyer', { 117 uri: response.data.uri, 118 email_address: $('[name="email_address"]').val() 119 }, accountCreated); 120 case 400: 121 case 403: 122 // missing/malformed data - check response.error for details 123 break; 124 case 402: 125 // we couldn't authorize the buyer's credit card - check response.error for details 126 break; 127 case 404: 128 // your marketplace URI is incorrect 129 break; 130 default: 131 // we did something unexpected - check response.error for details 132 break; 133 } 134 } 135 136 var tokenizeCard = function(e) { 137 e.preventDefault(); 138 139 var $form = $('form#payment'); 140 var cardData = { 141 card_number: $form.find('[name="card_number"]').val(), 142 expiration_month: $form.find('[name="expiration_month"]').val(), 143 expiration_year: $form.find('[name="expiration_year"]').val(), 144 security_code: $form.find('[name="security_code"]').val() 145 }; 146 147 balanced.card.create(cardData, balancedCallback); 148 }; 149 150 $('#payment').submit(tokenizeCard); 151 152 if (window.location.protocol === 'file:') { 153 alert("balanced.js does not work when included in pages served over file:// URLs. Try serving this page over a webserver. Contact [email protected] if you need assistance."); 154 } 155 </script> 156 </body> 157 </html>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |