//
// Create, verify, and debit a bank account.
//
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 = 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();
Exemplo n.º 2
0
 public function actionAddBankAccount()
 {
     Yii::import('application.extensions.vendor.autoload', true);
     Httpful\Bootstrap::init();
     Balanced\Bootstrap::init();
     Balanced\Settings::$api_key = Yii::app()->params['balancedAPISecret'];
     $outcome = array('success' => 0);
     $transaction = Yii::app()->db->beginTransaction();
     try {
         if (isset($_POST['data']) && isset($_POST['merchantData'])) {
             $data = json_decode($_POST['data']);
             $merchantData = json_decode($_POST['merchantData']);
             $uri = $data->uri;
             $user = $this->getUser();
             if ($user->bankAccount != null) {
                 $bankAccount = $user->bankAccount;
                 $balancedBankAccount = Balanced\BankAccount::get($bankAccount->URI);
                 $balancedBankAccount->delete();
                 $bankAccount->Active = 0;
                 $bankAccount->save();
             }
             $account = new BankAccount();
             $account->User_ID = $user->User_ID;
             $account->URI = $uri;
             if ($account->save()) {
                 $balancedAccount = $this->getUserAccount();
                 $balancedAccount->addBankAccount($uri);
                 if (!in_array('merchant', $balancedAccount->roles)) {
                     $balancedAccount->promoteToMerchant($merchantData);
                 }
                 $outcome = array('success' => 1, 'BankAccount_ID' => $account->BankAccount_ID);
             } else {
                 $outcome = array('success' => 0, 'Errors' => $account->getErrors());
             }
         }
         $transaction->commit();
     } catch (Balanced\Errors\IdentityVerificationFailed $error) {
         $transaction->rollback();
         /* could not identify this account. */
         $outcome = array('success' => 0, 'Errors' => "redirect merchant to:" . $error->redirect_uri . "\n");
     } catch (Balanced\Errors\HTTPError $error) {
         $transaction->rollback();
         /* TODO: handle 400 and 409 exceptions as required */
         $outcome = array('success' => 0, 'Errors' => print_r($error, true));
     } catch (Exception $error) {
         $transaction->rollback();
         $outcome = array('success' => 0, 'Errors' => print_r($error, true));
     }
     echo CJSON::encode($outcome);
 }
Exemplo n.º 3
0
<?php

require __DIR__ . '/vendor/autoload.php';
Httpful\Bootstrap::init();
RESTful\Bootstrap::init();
Balanced\Bootstrap::init();
Balanced\Settings::$api_key = "ak-test-2eKlj1ZDfAcZSARMf3NMhBHywDej0avSY";
$order = Balanced\Order::get("/orders/OR3vURGwVtqDnnkRS9fgH41G");
$bank_account = Balanced\BankAccount::get("/bank_accounts/BA45anEaEr8g0lOhzhcE9VAN/credits");
$order->creditTo($bank_account, 5000);
<?php

require __DIR__ . '/vendor/autoload.php';
Httpful\Bootstrap::init();
RESTful\Bootstrap::init();
Balanced\Bootstrap::init();
Balanced\Settings::$api_key = "ak-test-22IOkhevjZlmRP2do6CZixkkDshTiOjTV";
$bank_account = Balanced\BankAccount::get("/bank_accounts/BA3PDwDCkdeC4OgPtPNwoCWl");
$bank_account->meta = array("facebook.user_id" => "0192837465", "my-own-customer-id" => "12345", "twitter.id" => "1234987650");
$bank_account->save();
<?php

require __DIR__ . '/vendor/autoload.php';
Httpful\Bootstrap::init();
RESTful\Bootstrap::init();
Balanced\Bootstrap::init();
Balanced\Settings::$api_key = "ak-test-22IOkhevjZlmRP2do6CZixkkDshTiOjTV";
$bank_account = Balanced\BankAccount::get("/bank_accounts/BA3Y63fK5STwlhKNMkE3Utmd");
$bank_account->credits->create(array("amount" => "5000"));
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");
Exemplo n.º 7
0
<?php

// $bank_account_href is the stored href for the BankAccount
// $order_href is the stored href for the Order
$bank_account = Balanced\BankAccount::get($bank_account_href);
$bank_account->credits->create(array("amount" => "100000", "description" => "Payout for order #1111", 'order' => $order_href));
Exemplo n.º 8
0
<?php

require __DIR__ . '/vendor/autoload.php';
Httpful\Bootstrap::init();
RESTful\Bootstrap::init();
Balanced\Bootstrap::init();
Balanced\Settings::$api_key = "ak-test-2eKlj1ZDfAcZSARMf3NMhBHywDej0avSY";
$bank_account = Balanced\BankAccount::get("/bank_accounts/BA3Ya2sAlEQE14O1iS17FN0Q");
$bank_account->meta = array("facebook.user_id" => "0192837465", "my-own-customer-id" => "12345", "twitter.id" => "1234987650");
$bank_account->save();
<?php

require __DIR__ . '/vendor/autoload.php';
Httpful\Bootstrap::init();
RESTful\Bootstrap::init();
Balanced\Bootstrap::init();
Balanced\Settings::$api_key = "ak-test-22IOkhevjZlmRP2do6CZixkkDshTiOjTV";
// NOTE: Bank account must be associated with a customer before initiating a verification
$bank_account = Balanced\BankAccount::get("/bank_accounts/BA3IhKG3bIN22cLHbaOIGtHb");
$verification = $bank_account->verify();
Exemplo n.º 10
0
<?php

require __DIR__ . '/vendor/autoload.php';
Httpful\Bootstrap::init();
RESTful\Bootstrap::init();
Balanced\Bootstrap::init();
Balanced\Settings::$api_key = "ak-test-2eKlj1ZDfAcZSARMf3NMhBHywDej0avSY";
// NOTE: Bank account must be associated with a customer before initiating a verification
$bank_account = Balanced\BankAccount::get("/bank_accounts/BA3LVXVgJLrzkmB3vUntKJ6t");
$verification = $bank_account->verify();
Exemplo n.º 11
0
 public static function BalancedCallback($post)
 {
     Yii::import('application.extensions.vendor.autoload', true);
     Httpful\Bootstrap::init();
     Balanced\Bootstrap::init();
     Balanced\Settings::$api_key = Yii::app()->params['balancedAPISecret'];
     $log = "Received callback... \n";
     try {
         $data = json_decode($post);
         if (!isset($data->type)) {
             $log .= "Error parsing data: \n";
             $log .= "Raw post: \n" . $post . "\n";
             $log .= "Parsed data: \n" . print_r($data, true) . "\n";
             throw new Exception("Error parsing data");
         }
         if ($data->type == 'debit.succeeded') {
             try {
                 $log .= "Customer debit succeeded. Paying the host...\n";
                 $paymentID = $data->entity->meta->Payment_ID;
                 $log .= "Payment_ID: {$paymentID}\n";
                 $payment = Payment::model()->findByPk($paymentID);
                 $hostAmount = $payment->Amount * Yii::app()->params['HostPercentage'];
                 $log .= "Host's share is \${$hostAmount}\n";
                 // Convert to cents
                 $hostAmount *= 100;
                 $hostBankAccount = Balanced\BankAccount::get($payment->bankAccount->URI);
                 $credit = $hostBankAccount->credit($hostAmount);
                 $log .= 'Sent credit, response is: ' . print_r($credit, true) . "\n";
             } catch (Exception $e) {
                 Mail::Instance()->Alert("Error crediting host account", print_r($e));
                 throw $e;
             }
         } else {
             $log .= print_r($data, true) . "\n";
         }
     } catch (Exception $e) {
         $log .= "\n" . print_r($e, true);
     }
     Yii::log($log, 'info', 'BalancedCallback');
 }
Exemplo n.º 12
0
<?php

// $bank_account_href_a is the stored href for the BankAccount for Person A
$bank_account_person_a = Balanced\BankAccount::get($bank_account_href_a);
$bank_account_person_a->credits->create(array("amount" => "50000", "description" => "Payout for order #1111"));
// $bank_account_href_b is the stored href for the BankAccount for Person B
$bank_account_person_b = Balanced\BankAccount::get($bank_account_href_b);
$bank_account_person_b->credits->create(array("amount" => "50000", "description" => "Payout for order #1111"));