Exemple #1
0
 public function create_pending_session($wallet_id, $product_id = 0, $amount = 0, $currency = 'btc')
 {
     // Initialize
     global $config, $template;
     $userid = LOGIN === true ? $GLOBALS['userid'] : 0;
     $expire_time = time() + $config['payment_expire_seconds'];
     // Get hash
     do {
         $hash = generate_random_string(120);
         if ($row = DB::queryFirstRow("SELECT * FROM coin_pending_payment WHERE pay_hash = %s", hash('sha512', 120))) {
             $exists = 1;
         } else {
             $exists = 0;
         }
     } while ($exists > 0);
     // Get product, if needed
     if ($product_id > 0) {
         if (!($prow = DB::queryFirstRow("SELECT * FROM products WHERE id = %d", $product_id))) {
             trigger_error("Product does not exist, ID# {$product_id}", E_USER_ERROR);
         }
         $amount = $prow['amount'];
         $currency = $prow['currency'];
         $item_name = $prow['display_name'];
     } else {
         $item_name = '';
     }
     // Get amount
     if ($currency == 'fiat') {
         $amount_btc = $amount / $config['exchange_rate'];
     } else {
         $amount_btc = $amount;
         $amount = $amount_btc * $config['exchange_rate'];
     }
     // Get payment address
     if ($userid > 0) {
         $client = new bip32();
         $payment_address = $client->get_user_address($wallet_id, $userid);
         // Delete any existing pending payments
         DB::query("DELETE FROM coin_pending_payment WHERE payment_address = %s AND status = 'pending'", $payment_address);
     } else {
         $payment_address = '';
     }
     // Add to db
     DB::insert('coin_pending_payment', array('wallet_id' => $wallet_id, 'pay_hash' => $hash, 'userid' => $userid, 'item_id' => $product_id, 'amount' => $amount, 'amount_btc' => $amount_btc, 'expire_time' => $expire_time, 'payment_address' => $payment_address));
     // Template variables
     $template->assign('payment_address', $payment_address);
     $template->assign('currency', $currency);
     $template->assign('amount', fmoney_coin($amount_btc));
     $template->assign('amount_fiat', fmoney($amount));
     $template->assign('product_id', $product_id);
     $template->assign('product_name', $item_name);
     // Return hash
     return $hash;
 }