Skip to content

Instantly share code, notes, and snippets.

@mjallday
Created March 14, 2013 23:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjallday/5166040 to your computer and use it in GitHub Desktop.
Save mjallday/5166040 to your computer and use it in GitHub Desktop.
Balanced PHP client meta filtering example
<?php
require('vendor/autoload.php');
Httpful\Bootstrap::init();
RESTful\Bootstrap::init();
Balanced\Bootstrap::init();
use Balanced\Debit;
use Balanced\Credit;
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();
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 "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
);
/* OK LET THE SHOW BEGIN
Here we debit the buyer for $110 ten times and on each debit we
add the sellers account URI in the meta field so that we know
which transactions belong to him.
Next we create a credit to the seller and also tag the meta field.
Finally we query the credits and debits using that meta data and
subtract the total åmount of the credits by the total amount of
the debits to get the current amount owed.
*/
print "create 10 debits on behalf of this merchant\n";
for ($i = 0; $i < 10; $i++) {
$another_debit = $buyer->debit(
13000,
"MARKETPLACE.COM",
null,
array(
"on_behalf_of_uri" => $merchant->uri
)
);
}
print "let's credit our merchant $220\n";
$credit = $merchant->credit(
2200,
"Buyer purchase something on Marketplace.com",
array(
"on_behalf_of_uri" => $merchant->uri
)
);
#
$debits = $marketplace->debits->query()->filter(Debit::$f->meta->on_behalf_of_uri->eq($merchant->uri))->all();
$credits = $marketplace->credits->query()->filter(Credit::$f->meta->on_behalf_of_uri->eq($merchant->uri))->all();
$total_paid = 0;
$total_debited = 0;
foreach($debits as $debit) {
$total_debited += $debit->amount;
}
foreach($credits as $credit) {
$total_paid += $credit->amount;
}
print "total_debited " . $total_debited . "\n";
print "total_credited " . $total_paid . "\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment