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 = new \Balanced\BankAccount(array("account_number" => "9900000001", "name" => "Johann Bernoulli", "routing_number" => "121000358", "type" => "checking"));
$bank_account->save();
print "The BankAccount: " . $bank_account->uri;
// Create a Customer
$customer = new \Balanced\Customer(array("name" => "William Henry Cavendish III", "email" => "*****@*****.**"));
$customer->save();
// Add the bank account to the Customer
$customer->addBankAccount($bank_account);
print "You can't debit from a bank account until you verify it\n";
try {
    $customer->debit(100);
} catch (Exception $e) {
    printf("Debit failed, %s\n", $e->description);
}
// Initiate a bank account verification
$verification = $bank_account->verify();
// Verify the bank account
try {
    $verification->confirm(1, 2);
} catch (Balanced\Errors\BankAccountVerificationFailure $e) {
    printf("Verification error , %s\n", $e->description);
    print "PROTIP: for TEST bank accounts the valid amount is always 1 and 1\n";
}
print "The customer: " . $customer->uri . "\n";
// Add Card to Customer
$customer->addCard($card->uri);
// Hold some funds
print "Create a Hold for some funds, \$15\n";
$hold = $marketplace->holds->create(array("amount" => 1500, "description" => "Some descriptive text for the debit in the dashboard", "source_uri" => $card->uri));
// Capture the hold
print "Capture the Hold (for the full amount)\n";
$debit = $hold->capture();
// Check escrow for new funds from debit
$marketplace = Balanced\Marketplace::mine();
if ($marketplace->in_escrow != 1500) {
    throw new Exception("1500 is not in escrow! This is wrong");
}
print "Escrow amount: " . $marketplace->in_escrow . " \n";
print "Refund the full amount\n";
$refund = $debit->refund();
// Create a bank account
$bank_account = new \Balanced\BankAccount(array("account_number" => "9900000001", "name" => "Johann Bernoulli", "routing_number" => "121000358", "type" => "checking"));
$bank_account->save();
// Create a Customer who is the seller
$seller = new \Balanced\Customer(array("name" => "Billy Jones", "email" => "*****@*****.**", "street_address" => "801 High St", "postal_code" => "94301", "country" => "USA", "dob" => "1979-02"));
$seller->save();
// Add bank account to seller
$seller->addBankAccount($bank_account->uri);
print "Debit the customer for \$130\n";
$debit = $customer->debit(13000, "MARKETPLACE.COM");
print "Credit the seller \$110\n";
$credit = $seller->credit(11000, "Buyer purchased something on Marketplace.com");
print "The marketplace charges 15%, so it earned \$20\n";
$mp_credit = $marketplace->owner_customer->credit(2000, "Commission from MARKETPLACE.COM");