Пример #1
0
 public function execute(array $matches, $url, $rest)
 {
     $this->stopScanner();
     $transferResourceBase64 = $matches['address'];
     $transfer = Transfer::findByResourceUrl(base64_decode($transferResourceBase64));
     $addr = new BitcoinAddress(Transfer::COINEE_DEFAULT_BTC_ADDRESS);
     $admin = AdminConfig::volatileLoad();
     $cfg = $admin->getConfig();
     $ticket = Purchase::load($cfg, Container::dispense('DB'), intval($matches['ticket']));
     if (strval($addr->get()) !== strval($ticket->getCustomerAddress()->get())) {
         error_log("Tampering detected on ticket #" . $ticket->getId() . ' at ' . time());
         return false;
     }
     $isZero = $ticket->getCurrencyAmount()->isEqualTo(new Amount("0"));
     if ($ticket->getStatus() !== Purchase::PENDING || $isZero === false) {
         header('HTTP/1.1 303 See Other');
         header('Location: /start');
         header('Cache-Control: no-cache, no-store, must-revalidate');
         header('Pragma: no-cache');
         header('Expires: 0');
         return false;
     }
     header('Cache-Control: no-cache, no-store, must-revalidate');
     header('Pragma: no-cache');
     header('Expires: 0');
     $tmpl = new Template('coinee/purchase');
     $tmpl->render(['ticket' => $ticket, 'config' => $cfg, 'transfer' => $transfer->getInfo()]);
     return true;
 }
Пример #2
0
 public function execute(array $matches, $url, $rest)
 {
     $get = Container::dispense('Environment\\Get');
     $cfg = AdminConfig::volatileLoad()->getConfig();
     $db = Container::dispense('DB');
     $p = Purchase::load($cfg, $db, $matches['ticket']);
     $scanner = Container::dispense('BillScannerDriver');
     $scanner->stop();
     $error = false;
     if (!empty($get['error'])) {
         $error = true;
     }
     $sent = false;
     if (!empty($get['sent'])) {
         $sent = true;
     }
     $transferResourceBase64 = $matches['transfer'];
     $transfer = Transfer::findByResourceUrl(base64_decode($transferResourceBase64));
     $coineeConfig = $cfg->getCoinee();
     $userEmail = $coineeConfig['email'];
     $apiKey = $coineeConfig['apiKey'];
     $orderId = $matches['order'];
     $transfer->orderPaid($orderId, $userEmail, $apiKey);
     $tmpl = new Template('coinee/receipt');
     $tmpl->render(['purchase' => $p, 'config' => $cfg, 'error' => $error, 'sent' => $sent]);
     return true;
 }
Пример #3
0
 public function execute(array $matches, $url, $rest)
 {
     $resourceBase64 = $matches['address'];
     Transfer::findByResourceUrl(base64_decode($resourceBase64))->validate();
     //		$addr = new BitcoinAddress($matches['address']);
     $addr = new BitcoinAddress(Transfer::COINEE_DEFAULT_BTC_ADDRESS);
     $admin = AdminConfig::volatileLoad();
     $cfg = $admin->getConfig();
     $db = Container::dispense('DB');
     $ticket = Purchase::create($cfg, $db, $addr);
     $wallet = $cfg->getWalletProvider();
     $i18n = Localization::getTranslator();
     $btcBalance = $wallet->getBalance();
     $balance = $btcBalance->multiplyBy($ticket->getBitcoinPrice());
     $threshhold = Math::max([$cfg->getMaxTransactionValue(), Math::max($cfg->getCurrencyMeta()->getDenominations())]);
     if ($balance->isLessThan($threshhold)) {
         MachineStatusEmail::reportError($cfg, $db, $i18n->_('Low balance: ') . $btcBalance->get() . ' ' . $i18n->_('bitcoin'));
     }
     header('HTTP/1.1 303 See Other');
     //		header('Location: /purchase/' . $addr->get() . '/' . $ticket->getId());
     header('Location: /coinee/purchase/' . $resourceBase64 . '/' . $ticket->getId());
     header('Cache-Control: no-cache, no-store, must-revalidate');
     header('Pragma: no-cache');
     header('Expires: 0');
     return true;
 }
Пример #4
0
 public function execute(array $matches, $url, $rest)
 {
     $address = parse_url($matches['address'], PHP_URL_PATH);
     $address = base64_decode($address);
     try {
         Transfer::findByResourceUrl($address)->validate();
         echo JSON::encode(['valid' => true]);
     } catch (Exception $e) {
         echo JSON::encode(['valid' => false, 'error' => $e->getMessage()]);
     }
     return true;
 }
Пример #5
0
 public function execute(array $matches, $url, $rest)
 {
     $admin = Admin::volatileLoad();
     $cfg = $admin->getConfig();
     $db = Container::dispense('DB');
     $scanner = new BillScannerDriver();
     $ticket = Purchase::load($cfg, $db, intval($matches['ticket']));
     $response = [];
     $currencyEntered = $ticket->getCurrencyAmount();
     $zero = new Amount("0");
     if ($currencyEntered->isEqualTo($zero) || $currencyEntered->isLessThan($zero)) {
         echo JSON::encode(['cancelTransaction' => true]);
         return true;
     }
     if ($ticket->getStatus() !== Purchase::PENDING) {
         header('Cache-Control: no-cache, no-store, must-revalidate');
         header('Pragma: no-cache');
         header('Expires: 0');
         echo JSON::encode(['redirect' => '/start']);
         return true;
     }
     $scanner->stop();
     try {
         $coineeConfig = $cfg->getCoinee();
         $userEmail = $coineeConfig['email'];
         $apiKey = $coineeConfig['apiKey'];
         // get current btc amount
         $btcAmount = $ticket->recalculateBitcoinAmount()->get();
         // get address for given btc amount
         $transferResourceBase64 = $matches['transfer'];
         $transfer = Transfer::findByResourceUrl(base64_decode($transferResourceBase64));
         $order = $transfer->createOrder($btcAmount, $userEmail, $apiKey);
         // finish the transaction
         CoineePurchase::completeCoineeTransaction($cfg, $db, $ticket, $order['payToAddress'], $order['btcAmount']);
         $response['orderId'] = $order['id'];
         $this->notifyBalanceChange();
         $response['proceed'] = true;
     } catch (Exception $e) {
         //			throw $e;
         if ($e instanceof InsufficientFundsException) {
             $response['insufficient'] = true;
         }
         file_put_contents('/home/pi/phplog/purchase_errors.' . intval($matches['ticket']) . '.log', $e, FILE_APPEND);
         $response['error'] = true;
         JobManager::enqueue($db, 'PurchaseError', ['purchase_id' => $ticket->getId()]);
         $this->notifyBalanceChange();
     }
     echo JSON::encode($response);
     return true;
 }
Пример #6
0
 private function getTotals()
 {
     $result = ['event' => 'totalsUpdated', 'bills' => $this->currencyMeta->format($this->balance, false), 'btc' => $this->purchase->getBitcoinAmount()->get()];
     $diff = $this->maxFiat->subtract($this->balance);
     if ($diff->isLessThan($this->threshhold)) {
         $result['diff'] = intval($this->disableBills($diff)->get());
     }
     if ($diff->isLessThan($this->lowest)) {
         $this->driver->stop();
         $result['diff'] = 0;
     }
     $result['estimatedExitAmount'] = 0;
     if ((int) $result['bills'] > 0) {
         try {
             $transfer = Transfer::findByResourceUrl($this->transferResource);
             $result['estimatedExitAmount'] = $transfer->getInfo($result['btc'])['btcQuote']['totalDelivered'];
         } catch (Exception $e) {
             if ($e->getCode() === Transfer::EXCEPTION_METHOD_NOT_AVAILABLE) {
                 $result['estimatedExitAmount'] = 'EXCEPTION_METHOD_NOT_AVAILABLE';
             } elseif ($e->getCode() === Transfer::EXCEPTION_ZERO_AMOUNT) {
                 $result['estimatedExitAmount'] = 'EXCEPTION_ZERO_AMOUNT';
             } else {
                 throw $e;
             }
         }
     }
     return $result;
 }