/** * Process incomming request. */ public function process($payload) { if ($payload["type"] != "address") { return; } $data = $payload["data"]; if (!$data["txid"]) { throw new Exception("No transaction id"); } if (!$data["address"]) { throw new Exception("No address"); } if (!$data["balance_change"]) { throw new Exception("No amount data"); } $transaction = Transaction::findOneBy("transactionHash", $data["txid"]); if (!$transaction) { $account = Account::findOneBy("depositAddress", $data["address"]); if (!$account) { throw new Exception("No matching account."); } $transaction = new Transaction(); $transaction->notice = "Deposit"; $transaction->transactionHash = $data["txid"]; $transaction->toAccountId = $account->id; $transaction->state = Transaction::CONFIRMING; $transaction->amount = BitcoinUtil::toSatoshi("btc", $data["balance_change"]); $transaction->save(); $account->getPubSub()->publish(); } if ($transaction->getState() == Transaction::COMPLETE) { return; } $transaction->confirmations = intval($data["confirmations"]); $account = $transaction->getToAccount(); if (!$account) { throw new Exception("unable to find account"); } if ($transaction->confirmations >= get_option("blockchainaccounts_notifications")) { $account->balance += $transaction->amount; $account->save(); $transaction->toAccountBalance = $account->balance; $transaction->timestamp = time(); $transaction->state = Transaction::COMPLETE; $transaction->save(); } $transaction->save(); $account->getPubSub()->publish(); }
/** * Make sure a new transaction is created if no one exists. */ public function testTransactionCreated() { $user_id = $this->factory->user->create(); $account = bca_user_account($user_id); $address = $account->getDepositAddress(); $this->assertTrue(strlen($address) > 4); $data = array("type" => "address", "data" => array("address" => $address, "txid" => "12345678", "balance_change" => "0.05000000", "confirmations" => 0)); BlockIoController::instance()->process($data); $transaction = Transaction::findOneBy("transactionHash", "12345678"); $this->assertEquals(0.05, $transaction->getAmount("btc")); $this->assertEquals($transaction->getState(), Transaction::CONFIRMING); $account = bca_user_account($user_id); $this->assertEquals(0, $account->getBalance("btc")); $this->assertEquals(0.05, $account->getConfirmingBalance("btc")); $data = array("type" => "address", "data" => array("address" => $address, "txid" => "12345678", "balance_change" => "0.05000000", "confirmations" => "3")); BlockIoController::instance()->process($data); $account = bca_user_account($user_id); $this->assertEquals(0.05, $account->getBalance("btc")); }
<?php require_once __DIR__ . "/src/utils/WpUtil.php"; require_once __DIR__ . "/src/model/Transaction.php"; require_once __DIR__ . "/src/model/Account.php"; use wpblockchainaccounts\WpUtil; use wpblockchainaccounts\Transaction; use wpblockchainaccounts\Account; require_once WpUtil::getWpLoadPath(); if ($_REQUEST["key"] != get_option("blockchainaccounts_notification_key")) { exit("Wrong key"); } $transaction = Transaction::findOneBy("transactionHash", $_REQUEST["transaction_hash"]); if (!$_REQUEST["input_address"]) { exit("expected input address"); } if (!$_REQUEST["transaction_hash"]) { exit("expected transaction_hash"); } if ($_REQUEST["value"] < 0) { exit("*ok*"); } if (!$transaction) { $account = Account::findOneBy("depositAddress", $_REQUEST["input_address"]); if (!$account) { exit("no associated account or transaction"); } $transaction = new Transaction(); $transaction->notice = "Deposit"; $transaction->transactionHash = $_REQUEST["transaction_hash"]; $transaction->toAccountId = $account->id;