/**
  * 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();
 }
예제 #2
0
 /**
  * Set amount.
  */
 public function setAmount($denomination, $amount)
 {
     if ($amount <= 0) {
         throw new Exception("Negative or zero amount for transaction: " . $amount . " " . $denomination);
     }
     $this->amount = BitcoinUtil::toSatoshi($denomination, $amount);
 }
예제 #3
0
 /**
  * Withdraw funds.
  * If withdraw processing is manual, the transaction will
  * be stored as scheduled. If withdraw processing is automatic
  * the transaction will be performed immediately.
  */
 public function withdraw($denomination, $address, $amount)
 {
     if ($amount < 0 || $amount > $this->getBalance($denomination)) {
         throw new Exception("Insufficient funds on account.");
     }
     if ($this->hasConfirming()) {
         throw new Exception("There are unconfirmed transactions for the account.");
     }
     if ($this->entity_type != "user") {
         throw new Exception("Can only withdraw from user accounts.");
     }
     $this->balance -= BitcoinUtil::toSatoshi($denomination, $amount);
     $t = new Transaction();
     $t->withdrawAddress = $address;
     $t->fromAccountId = $this->id;
     $t->fromAccountBalance = $this->balance;
     $t->setAmount($denomination, $amount);
     $t->state = Transaction::SCHEDULED;
     $t->notice = "Withdraw";
     $t->save();
     $this->save();
     if (get_option("blockchainaccounts_withdraw_processing") == "auto") {
         $t->performWithdraw();
     }
     return $t;
 }