Add Balanced Payments API
Summary: Adds the Balanced PHP API to externals/. Ref T2787. Test Plan: Used in next diff. Reviewers: btrahan, chad Reviewed By: chad CC: aran, aurelijus Maniphest Tasks: T2787 Differential Revision: https://secure.phabricator.com/D5764
This commit is contained in:
54
externals/balanced-php/example/bank-account-debits.php
vendored
Normal file
54
externals/balanced-php/example/bank-account-debits.php
vendored
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
//
|
||||
// Learn how to authenticate a bank account so you can debit with it.
|
||||
//
|
||||
|
||||
require(__DIR__ . '/vendor/autoload.php');
|
||||
|
||||
Httpful\Bootstrap::init();
|
||||
RESTful\Bootstrap::init();
|
||||
Balanced\Bootstrap::init();
|
||||
|
||||
// create a new marketplace
|
||||
$key = new Balanced\APIKey();
|
||||
$key->save();
|
||||
Balanced\Settings::$api_key = $key->secret;
|
||||
$marketplace = new Balanced\Marketplace();
|
||||
$marketplace->save();
|
||||
|
||||
// create a bank account
|
||||
$bank_account = $marketplace->createBankAccount("Jack Q Merchant",
|
||||
"123123123",
|
||||
"123123123"
|
||||
);
|
||||
$buyer = $marketplace->createAccount("buyer@example.org");
|
||||
$buyer->addBankAccount($bank_account);
|
||||
|
||||
print("you can't debit from a bank account until you verify it\n");
|
||||
try {
|
||||
$buyer->debit(100);
|
||||
} catch (Exception $e) {
|
||||
printf("Debit failed, %s\n", $e->getMessage());
|
||||
}
|
||||
|
||||
// authenticate
|
||||
$verification = $bank_account->verify();
|
||||
|
||||
try {
|
||||
$verification->confirm(1, 2);
|
||||
} catch (Balanced\Errors\BankAccountVerificationFailure $e) {
|
||||
printf('Authentication error , %s\n', $e->getMessage());
|
||||
print("PROTIP: for TEST bank accounts the valid amount is always 1 and 1\n");
|
||||
|
||||
}
|
||||
|
||||
$verification->confirm(1, 1);
|
||||
|
||||
$debit = $buyer->debit(100);
|
||||
printf("debited the bank account %s for %d cents\n",
|
||||
$debit->source->uri,
|
||||
$debit->amount
|
||||
);
|
||||
print("and there you have it");
|
||||
|
||||
?>
|
||||
157
externals/balanced-php/example/buyer-example.php
vendored
Normal file
157
externals/balanced-php/example/buyer-example.php
vendored
Normal file
@@ -0,0 +1,157 @@
|
||||
<?php
|
||||
|
||||
require(__DIR__ . '/vendor/autoload.php');
|
||||
|
||||
Httpful\Bootstrap::init();
|
||||
RESTful\Bootstrap::init();
|
||||
Balanced\Bootstrap::init();
|
||||
|
||||
$API_KEY_SECRET = '5f4db668a5ec11e1b908026ba7e239a9';
|
||||
$page = $_SERVER['REQUEST_URI'];
|
||||
Balanced\Settings::$api_key = $API_KEY_SECRET;
|
||||
$marketplace = Balanced\Marketplace::mine();
|
||||
|
||||
if ($page == '/') {
|
||||
// do nothing
|
||||
} elseif ($page == '/buyer') {
|
||||
if (isset($_POST['uri']) and isset($_POST['email_address'])) {
|
||||
// create in balanced
|
||||
$email_address = $_POST['email_address'];
|
||||
$card_uri = $_POST['uri'];
|
||||
try {
|
||||
echo create_buyer($email_address, $card_uri)->uri;
|
||||
return;
|
||||
} catch (Balanced\Errors\Error $e) {
|
||||
echo $e->getMessage();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function create_buyer($email_address, $card_uri) {
|
||||
$marketplace = Balanced\Marketplace::mine();
|
||||
try {
|
||||
# new buyer
|
||||
$buyer = $marketplace->createBuyer(
|
||||
$email_address,
|
||||
$card_uri);
|
||||
}
|
||||
catch (Balanced\Errors\DuplicateAccountEmailAddress $e) {
|
||||
# oops, account for $email_address already exists so just add the card
|
||||
$buyer = Balanced\Account::get($e->extras->account_uri);
|
||||
$buyer->addCard($card_uri);
|
||||
}
|
||||
return $buyer;
|
||||
}
|
||||
|
||||
?>
|
||||
<html>
|
||||
<head>
|
||||
<link rel="stylesheet" href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css" type="text/css">
|
||||
<style type="text/css">
|
||||
[name="marketplace_eid"] {
|
||||
width: 300px;
|
||||
}
|
||||
[name^="expiration"] {
|
||||
width: 50px;
|
||||
}
|
||||
[name="security_code"] {
|
||||
width: 50px;
|
||||
}
|
||||
code { display: block; }
|
||||
pre { color: green; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Balanced Sample - Collect Credit Card Information</h1>
|
||||
<div class="row">
|
||||
<div class="span6">
|
||||
<form id="payment">
|
||||
<div>
|
||||
<label>Email Address</label>
|
||||
<input name="email_address" value="bob@example.com">
|
||||
</div>
|
||||
<div>
|
||||
<label>Card Number</label>
|
||||
<input name="card_number" value="4111111111111111" autocomplete="off">
|
||||
</div>
|
||||
<div>
|
||||
<label>Expiration</label>
|
||||
<input name="expiration_month" value="1"> / <input name="expiration_year" value="2020">
|
||||
</div>
|
||||
<div>
|
||||
<label>Security Code</label>
|
||||
<input name="security_code" value="123" autocomplete="off">
|
||||
</div>
|
||||
<button>Submit Payment Data</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
<div id="result"></div>
|
||||
<script type="text/javascript" src="https://js.balancedpayments.com/v1/balanced.js"></script>
|
||||
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
|
||||
<script type="text/javascript">
|
||||
var marketplaceUri = '<?php echo $marketplace->uri; ?>';
|
||||
|
||||
var debug = function (tag, content) {
|
||||
$('<' + tag + '>' + content + '</' + tag + '>').appendTo('#result');
|
||||
};
|
||||
|
||||
try {
|
||||
balanced.init(marketplaceUri);
|
||||
} catch (e) {
|
||||
debug('code', 'You need to set the marketplaceUri variable');
|
||||
}
|
||||
|
||||
function accountCreated(response) {
|
||||
debug('code', 'account create result: ' + response);
|
||||
}
|
||||
|
||||
function balancedCallback(response) {
|
||||
var tag = (response.status < 300) ? 'pre' : 'code';
|
||||
debug(tag, JSON.stringify(response));
|
||||
switch (response.status) {
|
||||
case 201:
|
||||
// response.data.uri == uri of the card resource, submit to your server
|
||||
$.post('/buyer', {
|
||||
uri: response.data.uri,
|
||||
email_address: $('[name="email_address"]').val()
|
||||
}, accountCreated);
|
||||
case 400:
|
||||
case 403:
|
||||
// missing/malformed data - check response.error for details
|
||||
break;
|
||||
case 402:
|
||||
// we couldn't authorize the buyer's credit card - check response.error for details
|
||||
break;
|
||||
case 404:
|
||||
// your marketplace URI is incorrect
|
||||
break;
|
||||
default:
|
||||
// we did something unexpected - check response.error for details
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var tokenizeCard = function(e) {
|
||||
e.preventDefault();
|
||||
|
||||
var $form = $('form#payment');
|
||||
var cardData = {
|
||||
card_number: $form.find('[name="card_number"]').val(),
|
||||
expiration_month: $form.find('[name="expiration_month"]').val(),
|
||||
expiration_year: $form.find('[name="expiration_year"]').val(),
|
||||
security_code: $form.find('[name="security_code"]').val()
|
||||
};
|
||||
|
||||
balanced.card.create(cardData, balancedCallback);
|
||||
};
|
||||
|
||||
$('#payment').submit(tokenizeCard);
|
||||
|
||||
if (window.location.protocol === 'file:') {
|
||||
alert("balanced.js does not work when included in pages served over file:// URLs. Try serving this page over a webserver. Contact support@balancedpayments.com if you need assistance.");
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
5
externals/balanced-php/example/composer.json
vendored
Normal file
5
externals/balanced-php/example/composer.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"require": {
|
||||
"balanced/balanced": "*"
|
||||
}
|
||||
}
|
||||
42
externals/balanced-php/example/debit-example.php
vendored
Normal file
42
externals/balanced-php/example/debit-example.php
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
require('vendor/autoload.php');
|
||||
|
||||
Httpful\Bootstrap::init();
|
||||
RESTful\Bootstrap::init();
|
||||
Balanced\Bootstrap::init();
|
||||
|
||||
$API_KEY_SECRET = '5f4db668a5ec11e1b908026ba7e239a9';
|
||||
Balanced\Settings::$api_key = $API_KEY_SECRET;
|
||||
$marketplace = Balanced\Marketplace::mine();
|
||||
|
||||
print "create a card\n";
|
||||
$card = $marketplace->cards->create(array(
|
||||
"card_number" => "5105105105105100",
|
||||
"expiration_month" => "12",
|
||||
"expiration_year" => "2015"
|
||||
));
|
||||
print "our card: " . $card->uri . "\n";
|
||||
|
||||
print "create a **buyer** account with that card\n";
|
||||
$buyer = $marketplace->createBuyer(null, $card->uri);
|
||||
print "our buyer account: " . $buyer->uri . "\n";
|
||||
|
||||
print "debit our buyer, let's say $15\n";
|
||||
try {
|
||||
$debit = $buyer->debit(1500);
|
||||
print "our buyer debit: " . $debit->uri . "\n";
|
||||
}
|
||||
catch (Balanced\Errors\Declined $e) {
|
||||
print "oh no, the processor declined the debit!\n";
|
||||
}
|
||||
catch (Balanced\Errors\NoFundingSource $e) {
|
||||
print "oh no, the buyer has not active funding sources!\n";
|
||||
}
|
||||
catch (Balanced\Errors\CannotDebit $e) {
|
||||
print "oh no, the buyer has no debitable funding sources!\n";
|
||||
}
|
||||
|
||||
print "and there you have it 8)\n";
|
||||
|
||||
?>
|
||||
59
externals/balanced-php/example/events-and-callbacks.php
vendored
Normal file
59
externals/balanced-php/example/events-and-callbacks.php
vendored
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
/*
|
||||
* Welcome weary traveller. Sick of polling for state changes? Well today have
|
||||
* I got good news for you. Run this example below to see how to get yourself
|
||||
* some callback goodness and to understand how events work.
|
||||
*/
|
||||
require(__DIR__ . "/vendor/autoload.php");
|
||||
|
||||
Httpful\Bootstrap::init();
|
||||
RESTful\Bootstrap::init();
|
||||
Balanced\Bootstrap::init();
|
||||
|
||||
// create a new marketplace
|
||||
$key = new Balanced\APIKey();
|
||||
$key->save();
|
||||
Balanced\Settings::$api_key = $key->secret;
|
||||
$marketplace = new Balanced\Marketplace();
|
||||
$marketplace->save();
|
||||
|
||||
// let"s create a requestb.in
|
||||
$ch = curl_init("http://requestb.in/api/v1/bins");
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
|
||||
'Content-Type: application/json',
|
||||
'Content-Length: ' . 0)
|
||||
);
|
||||
$result = json_decode(curl_exec($ch));
|
||||
$bin_name = $result->name;
|
||||
$callback_url = "http://requestb.in/" . $bin_name;
|
||||
$requests_url = "http://requestb.in/api/v1/bins/" . $bin_name . "/requests";
|
||||
|
||||
printf("let's create a callback\n");
|
||||
$marketplace->createCallback($callback_url);
|
||||
|
||||
printf("let's create a card and associate it with a new account\n");
|
||||
$card = $marketplace->cards->create(array(
|
||||
"card_number" => "5105105105105100",
|
||||
"expiration_month" => "12",
|
||||
"expiration_year" => "2015"
|
||||
));
|
||||
$buyer = $marketplace->createBuyer("buyer@example.org", $card->uri);
|
||||
|
||||
printf("generate a debit (which implicitly creates and captures a hold)\n");
|
||||
$buyer->debit(100);
|
||||
|
||||
foreach ($marketplace->events as $event) {
|
||||
printf("this was a %s event, it occurred at %s\n",
|
||||
$event->type,
|
||||
$event->occurred_at
|
||||
);
|
||||
}
|
||||
|
||||
printf("ok, let's check with requestb.in to see if our callbacks fired at %s\n", $callback_url);
|
||||
printf("we received callbacks, you can view them at http://requestb.in/%s?inspect\n",
|
||||
$bin_name
|
||||
);
|
||||
|
||||
?>
|
||||
120
externals/balanced-php/example/example.php
vendored
Normal file
120
externals/balanced-php/example/example.php
vendored
Normal file
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
require('vendor/autoload.php');
|
||||
|
||||
Httpful\Bootstrap::init();
|
||||
RESTful\Bootstrap::init();
|
||||
Balanced\Bootstrap::init();
|
||||
|
||||
print "create our new api key\n";
|
||||
$key = new Balanced\APIKey();
|
||||
$key->save();
|
||||
print "Our secret is " . $key->secret . "\n";
|
||||
|
||||
print "configure with our secret " . $key->secret . "\n";
|
||||
Balanced\Settings::$api_key = $key->secret;
|
||||
|
||||
print "create our marketplace";
|
||||
$marketplace = new Balanced\Marketplace();
|
||||
$marketplace->save();
|
||||
|
||||
if (Balanced\Merchant::me() == null) {
|
||||
throw new Exception("Balanced\Merchant::me() should not be null");
|
||||
}
|
||||
|
||||
print "What's my merchant? Easy: Balanced\Merchant::me(): " . Balanced\Merchant::me()->uri . "\n";
|
||||
|
||||
if (Balanced\Marketplace::mine() == null) {
|
||||
throw new Exception("Balanced\Marketplace::mine() should never be null");
|
||||
}
|
||||
|
||||
print "What's my marketplace? Easy: Balanced\Marketplace::mine(): " .Balanced\Marketplace::mine()->uri . "\n";
|
||||
|
||||
print "My marketplace's name is " . $marketplace->name . "\n";
|
||||
print "Changing it to TestFooey\n";
|
||||
$marketplace->name = "TestFooey";
|
||||
$marketplace->save();
|
||||
print "My marketplace name is now " . $marketplace->name . "\n";
|
||||
|
||||
if ($marketplace->name != "TestFooey") {
|
||||
throw new Exception("Marketplace name is NOT TestFooey");
|
||||
}
|
||||
|
||||
print "Cool, let's create a card\n";
|
||||
$card = $marketplace->cards->create(array(
|
||||
"card_number" => "5105105105105100",
|
||||
"expiration_month" => "12",
|
||||
"expiration_year" => "2015"
|
||||
));
|
||||
|
||||
print "Our card: " . $card->uri . "\n";
|
||||
|
||||
print "Create out **buyer** account\n";
|
||||
$buyer = $marketplace->createBuyer("buyer@example.org", $card->uri);
|
||||
print "our buyer account: " . $buyer->uri . "\n";
|
||||
|
||||
print "hold some amount of funds on the buyer, let's say $15\n";
|
||||
$the_hold = $buyer->hold(1500);
|
||||
|
||||
print "ok, no more holds! let's capture it (for the full amount)\n";
|
||||
$debit = $the_hold->capture();
|
||||
|
||||
print "hmm, ho much money do i have in escrow? it should equal the debit amount\n";
|
||||
$marketplace = Balanced\Marketplace::mine();
|
||||
if ($marketplace->in_escrow != 1500) {
|
||||
throw new Exception("1500 is not in escrow! This is wrong");
|
||||
}
|
||||
print "I have " . $marketplace->in_escrow . " in escrow!\n";
|
||||
|
||||
print "Cool. now let me refund the full amount";
|
||||
$refund = $debit->refund();
|
||||
|
||||
print "ok, we have a merchant that's signing up, let's create an account for them first, let's create their bank account\n";
|
||||
|
||||
$bank_account = $marketplace->createBankAccount("Jack Q Merchant",
|
||||
"123123123", /* account_number */
|
||||
"123123123" /* bank_code (routing number is USA)*/
|
||||
);
|
||||
|
||||
$identity = array(
|
||||
"type" => "person",
|
||||
"name" => "Billy Jones",
|
||||
"street_address" => "801 High St",
|
||||
"postal_code" => "94301",
|
||||
"country" => "USA",
|
||||
"dob" => "1979-02",
|
||||
"phone_number" => "+16505551234"
|
||||
);
|
||||
|
||||
$merchant = $marketplace->createMerchant('merchant@example.org',
|
||||
$identity,
|
||||
$bank_account->uri
|
||||
);
|
||||
|
||||
print "our buyer is interested in buying something for $130\n";
|
||||
$another_debit = $buyer->debit(13000, "MARKETPLACE.COM");
|
||||
|
||||
print "let's credit our merchant $110\n";
|
||||
$credit = $merchant->credit(11000, "Buyer purchase something on Marketplace.com");
|
||||
|
||||
print "let's assume the marketplace charges 15%, so it earned $20\n";
|
||||
$mp_credit = $marketplace->owner_account->credit(2000,
|
||||
"Commission from MARKETPLACE.COM");
|
||||
|
||||
print "ok, let's invalidate the card used so it cannot be used again\n";
|
||||
$card->is_valid = false;
|
||||
$card->save();
|
||||
|
||||
print "how do we look up an existing object from the URI?\n";
|
||||
$the_buyer = Balanced\Account::get($buyer->uri);
|
||||
print "we got the buyer " . $the_buyer->email_address . "\n";
|
||||
|
||||
$the_debit = Balanced\Debit::get($debit->uri);
|
||||
print "we got the debit: " . $the_debit->uri . "\n";
|
||||
|
||||
$the_credit = Balanced\Credit::get($credit->uri);
|
||||
print "we got the credit: " . $the_credit->uri . "\n";
|
||||
|
||||
print "and there you have it :)\n";
|
||||
|
||||
?>
|
||||
71
externals/balanced-php/example/iterate-example.php
vendored
Normal file
71
externals/balanced-php/example/iterate-example.php
vendored
Normal file
@@ -0,0 +1,71 @@
|
||||
<?
|
||||
require('vendor/autoload.php');
|
||||
|
||||
Httpful\Bootstrap::init();
|
||||
RESTful\Bootstrap::init();
|
||||
Balanced\Bootstrap::init();
|
||||
|
||||
$key = new Balanced\APIKey();
|
||||
$key->save();
|
||||
Balanced\Settings::$api_key = $key->secret;
|
||||
$marketplace = new Balanced\Marketplace();
|
||||
$marketplace->save();
|
||||
|
||||
$card = $marketplace->cards->create(array(
|
||||
"card_number" => "5105105105105100",
|
||||
"expiration_month" => "12",
|
||||
"expiration_year" => "2015"
|
||||
));
|
||||
|
||||
$buyer = $marketplace->createBuyer("buyer@example.com", $card->uri);
|
||||
|
||||
$debit = $buyer->debit(1500);
|
||||
$debit->refund(100);
|
||||
$debit->refund(100);
|
||||
$debit->refund(100);
|
||||
|
||||
echo $debit->refunds->total() . " refunds" . "\n";
|
||||
|
||||
$total = 0;
|
||||
|
||||
foreach ($debit->refunds as $r) {
|
||||
$total += $r->amount;
|
||||
print "refund = " . $r->amount . "\n";
|
||||
}
|
||||
|
||||
print $total . "\n";
|
||||
|
||||
# bigger pagination example
|
||||
|
||||
print "Create 60 **buyer** with cards accounts\n";
|
||||
|
||||
for ($i = 0; $i < 60; $i++) {
|
||||
$card = $marketplace->cards->create(array(
|
||||
"card_number" => "5105105105105100",
|
||||
"expiration_month" => "12",
|
||||
"expiration_year" => "2015"
|
||||
));
|
||||
$buyer = $marketplace->createBuyer("buyer" . $i . "@example.org", $card->uri);
|
||||
print '.';
|
||||
}
|
||||
|
||||
print "\n";
|
||||
|
||||
$cards = $marketplace->cards;
|
||||
|
||||
print $cards->total() . " cards in Marketplace\n";
|
||||
|
||||
foreach ($cards as $c) {
|
||||
print "card " . $c->uri . "\n";
|
||||
}
|
||||
|
||||
# let's iterate through cards for just a single account
|
||||
|
||||
foreach ($buyer->cards as $c) {
|
||||
print "buyer's card " . $c->uri . "\n";
|
||||
}
|
||||
|
||||
print "and there you have it :)\n";
|
||||
|
||||
|
||||
?>
|
||||
14
externals/balanced-php/example/test-composer.php
vendored
Normal file
14
externals/balanced-php/example/test-composer.php
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
// run this file to test your composer install of Balanced
|
||||
|
||||
require(__DIR__ . '/vendor/autoload.php');
|
||||
|
||||
\Httpful\Bootstrap::init();
|
||||
\RESTful\Bootstrap::init();
|
||||
\Balanced\Bootstrap::init();
|
||||
|
||||
echo "[ OK ]\n";
|
||||
echo "balanced version -- " . \Balanced\Settings::VERSION . " \n";
|
||||
echo "restful version -- " . \RESTful\Settings::VERSION . " \n";
|
||||
echo "httpful version -- " . \Httpful\Httpful::VERSION . " \n";
|
||||
12
externals/balanced-php/example/test-phar.php
vendored
Normal file
12
externals/balanced-php/example/test-phar.php
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
// run this file to test your phar install of Balanced
|
||||
|
||||
include(__DIR__ . '/httpful.phar');
|
||||
include(__DIR__ . '/restful.phar');
|
||||
include(__DIR__ . '/balanced.phar');
|
||||
|
||||
echo "[ OK ]\n";
|
||||
echo "balanced version -- " . \Balanced\Settings::VERSION . " \n";
|
||||
echo "restful version -- " . \RESTful\Settings::VERSION . " \n";
|
||||
echo "httpful version -- " . \Httpful\Httpful::VERSION . " \n";
|
||||
12
externals/balanced-php/example/test-source.php
vendored
Normal file
12
externals/balanced-php/example/test-source.php
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
// run this file to test your source install of Balanced
|
||||
|
||||
require(__DIR__ . "/httpful/bootstrap.php");
|
||||
require(__DIR__ . "/restful/bootstrap.php");
|
||||
require(__DIR__ . "/balanced/bootstrap.php");
|
||||
|
||||
echo "[ OK ]\n";
|
||||
echo "balanced version -- " . \Balanced\Settings::VERSION . " \n";
|
||||
echo "restful version -- " . \RESTful\Settings::VERSION . " \n";
|
||||
echo "httpful version -- " . \Httpful\Httpful::VERSION . " \n";
|
||||
Reference in New Issue
Block a user