コード例 #1
0
 public function trade($key = null)
 {
     if (!$this->request->data) {
         return $this->render(array('json' => array('success' => 0, 'now' => time(), 'error' => "Not submitted through POST.")));
     }
     if ($key == null) {
         return $this->render(array('json' => array('success' => 0, 'now' => time(), 'error' => "Key not specified. Please get your key from your settings page under security.")));
     } else {
         $details = Details::find('first', array('conditions' => array('key' => $key)));
         print_r($details['username']);
         if (count($details) == 0) {
             return $this->render(array('json' => array('success' => 0, 'now' => time(), 'error' => "Incorrect Key! Please get your key from your settings page under security.")));
         } else {
             if (!$this->requestAPI("trade", $details['username'])) {
                 return $this->render(array('json' => array('success' => 0, 'now' => time(), 'error' => "Too many requests from your IP. Try after some time.")));
             }
             $type = $this->request->data['type'];
             $pair = $this->request->data['pair'];
             $amount = $this->request->data['amount'];
             $price = $this->request->data['price'];
             if (!($type == "Buy" || $type == "Sell")) {
                 return $this->render(array('json' => array('success' => 0, 'now' => time(), 'error' => "Type is incorrect! Should be 'Buy' or 'Sell'." . $type)));
             }
             /*				if(!($pair=='BTC_USD' || $pair=='BTC_GBP' || $pair=='BTC_EUR' || $pair=='BTC_LTC' || $pair=='LTC_USD' || $pair=='LTC_GBP' || $pair=='LTC_EUR')){
             					return $this->render(array('json' => array('success'=>0,
             					'now'=>time(),
             					'error'=>"Pair is incorrect! Should be 'BTC_USD','BTC_GBP','BTC_EUR','BTC_LTC','LTC_USD','LTC_GBP','LTC_EUR'.".$pair
             					)));
             				}
             */
             if ((double) $amount <= 0) {
                 return $this->render(array('json' => array('success' => 0, 'now' => time(), 'error' => "Amount less than or equal to ZERO! Should be greater than ZERO." . $amount)));
             }
             if ((double) $price <= 0) {
                 return $this->render(array('json' => array('success' => 0, 'now' => time(), 'error' => "Price less than or equal to ZERO! Should be greater than ZERO." . $price)));
             }
             $first_curr = strtoupper(substr($pair, 0, 3));
             $second_curr = strtoupper(substr($pair, 4, 3));
             //======================Trade========================================================
             $data = array('page.refresh' => true);
             Details::find('all')->save($data);
             $details = Details::find('first', array('conditions' => array('key' => $key)));
             $id = $details['user_id'];
             $user = Users::find('first', array('conditions' => array('_id' => $id)));
             $Action = $type;
             $commission = Parameters::find('first');
             $commissionRate = $commission['value'];
             if ($Action == "Buy") {
                 $PendingAction = 'Sell';
                 $FirstCurrency = $first_curr;
                 $SecondCurrency = $second_curr;
                 $Commission = (double) $commissionRate;
                 $CommissionAmount = number_format((double) $commissionRate * (double) $amount / 100, 8);
                 $CommissionCurrency = $first_curr;
                 $Amount = (double) $amount;
                 $PerPrice = (double) $price;
                 $BalanceAmount = $details['balance'][$second_curr];
                 if ($amount * $price >= $BalanceAmount) {
                     return $this->render(array('json' => array('success' => 0, 'now' => time(), 'error' => "Amount exceeds your balance! Balance: " . $BalanceAmount)));
                 }
                 $NewBalanceAmount = round($BalanceAmount - $amount * $price, 8);
                 $Currency = 'balance.' . $second_curr;
                 // Update balance of user with NewBalance Amount
                 $data = array('balance.' . $second_curr => (double) $NewBalanceAmount);
                 $details = Details::find('first', array('conditions' => array('user_id' => $id)))->save($data);
                 print_r("Buy-Save");
             }
             if ($Action == "Sell") {
                 $PendingAction = 'Buy';
                 $FirstCurrency = $first_curr;
                 $SecondCurrency = $second_curr;
                 $Commission = (double) $commissionRate;
                 $CommissionAmount = number_format((double) $commissionRate * (double) $amount * (double) $price / 100, 8);
                 $CommissionCurrency = $second_curr;
                 $Amount = (double) $amount;
                 $PerPrice = (double) $price;
                 $BalanceAmount = $details['balance'][$first_curr];
                 if ($amount >= $BalanceAmount) {
                     return $this->render(array('json' => array('success' => 0, 'now' => time(), 'error' => "Amount exceeds your balance! Balance: " . $BalanceAmount)));
                 }
                 $NewBalanceAmount = round($BalanceAmount - $amount, 8);
                 $Currency = 'balance.' . $first_curr;
                 // Update balance of user with NewBalance Amount
                 $data = array('balance.' . $first_curr => (double) $NewBalanceAmount);
                 $details = Details::find('first', array('conditions' => array('user_id' => $id)))->save($data);
                 print_r("Sell-Save");
             }
             $data = array('Action' => $Action, 'FirstCurrency' => $first_curr, 'SecondCurrency' => $second_curr, 'CommissionPercent' => (double) $Commission, 'Commission.Amount' => (double) $CommissionAmount, 'Commission.Currency' => $CommissionCurrency, 'Amount' => (double) $Amount, 'PerPrice' => (double) $PerPrice, 'DateTime' => new \MongoDate(), 'Completed' => 'N', 'IP' => $_SERVER['REMOTE_ADDR'], 'username' => $user['username'], 'user_id' => (string) $user['_id']);
             print_r($data);
             // Create Order for the user
             $orders = Orders::create();
             $orders->save($data);
             $order_id = $orders->_id;
             $data = array('refresh' => true);
             Trades::find('all', array('conditions' => array('trade' => $title)))->save($data);
             $ex = new ExController();
             $ex->SendEmails($order_id, $user['_id']);
             //	=Pending Orders=================================================================================
             $FirstCurrency = $first_curr;
             $SecondCurrency = $second_curr;
             $query = array('conditions' => array('Action' => $PendingAction, 'FirstCurrency' => $FirstCurrency, 'SecondCurrency' => $SecondCurrency, 'Completed' => 'N', 'user_id' => array('$ne' => $user['_id']), 'PerPrice' => (double) $PerPrice), 'order' => array('DateTime' => 'ASC'));
             $PendingOrders = Orders::find('all', $query);
             $i = 0;
             foreach ($PendingOrders as $PO) {
                 if ((double) $PO['Amount'] == (double) $Amount) {
                     $data = array('Completed' => 'Y', 'Transact.id' => $order_id, 'Transact.username' => $user['username'], 'Transact.user_id' => $user['_id'], 'Transact.DateTime' => new \MongoDate());
                     $orders = Orders::find('first', array('conditions' => array('_id' => $PO['_id'])))->save($data);
                     $orders = Orders::find('first', array('conditions' => array('_id' => $PO['_id'])));
                     $data = array('Completed' => 'Y', 'Transact.id' => $PO['_id'], 'Transact.username' => $PO['username'], 'Transact.user_id' => $PO['user_id'], 'Transact.DateTime' => new \MongoDate());
                     $orders = Orders::find('first', array('conditions' => array('_id' => $order_id)))->save($data);
                     $ex->updateBalance($order_id);
                     $ex->updateBalance($PO['_id']);
                     $ex->SendOrderCompleteEmails($order_id, $user['_id']);
                     $ex->SendOrderCompleteEmails($PO['_id'], $PO['user_id']);
                     break;
                 }
                 if ((double) $PO['Amount'] > (double) $Amount) {
                     // Update Previous Order with New Order Amount and New Commission and Transact User
                     if ($PO['Action'] == "Buy") {
                         $PrevCommAmount = round($PO['CommissionPercent'] * $Amount / 100, 8);
                         $CurrCommAmount = round($PO['CommissionPercent'] * ($PO['Amount'] - $Amount) * $PO['PrePrice'] / 100, 8);
                         $PrevCommCurr = $PO['FirstCurrency'];
                         $CurrCommCurr = $PO['SecondCurrency'];
                     } else {
                         $PrevCommAmount = round((double) $PO['CommissionPercent'] * (double) $Amount * (double) $PerPrice / 100, 8);
                         $CurrCommAmount = round($PO['CommissionPercent'] * $Amount / 100, 8);
                         $PrevCommCurr = $PO['SecondCurrency'];
                         $CurrCommCurr = $PO['FirstCurrency'];
                     }
                     $data = array('Commission.Amount' => (double) $PrevCommAmount, 'Amount' => (double) $Amount, 'Completed' => 'Y', 'Transact.id' => $order_id, 'Transact.username' => $user['username'], 'Transact.user_id' => $user['_id'], 'Transact.DateTime' => new \MongoDate(), 'Order' => 'P>C: Update Previous Commission and Amount and Complete Order');
                     $orders = Orders::find('first', array('conditions' => array('_id' => $PO['_id'])))->save($data);
                     // --------------------Complete
                     // Create new Order for Previous Order so that the order tallies
                     if ($PO['Action'] == "Buy") {
                         $PrevCommAmount = round((double) round((double) $PO['Amount'] - (double) $Amount, 8) * (double) $PO['CommissionPercent'] / 100, 8);
                     } else {
                         $PrevCommAmount = round((double) round((double) $PO['Amount'] - (double) $Amount, 8) * (double) $PO['PerPrice'] * (double) $PO['CommissionPercent'] / 100, 8);
                     }
                     $data = array('Amount' => (double) round((double) $PO['Amount'] - (double) $Amount, 8), 'Action' => $PO['Action'], 'FirstCurrency' => $PO['FirstCurrency'], 'SecondCurrency' => $PO['SecondCurrency'], 'CommissionPercent' => (double) $PO['CommissionPercent'], 'Commission.Amount' => (double) $PrevCommAmount, 'Commission.Currency' => $PrevCommCurr, 'PerPrice' => (double) $PO['PerPrice'], 'DateTime' => $PO['DateTime'], 'Completed' => 'N', 'IP' => $PO['IP'], 'username' => $PO['username'], 'user_id' => $PO['user_id'], 'Order' => 'P>C: Create New Previous Order with Balance details');
                     $orders = Orders::create();
                     $orders->save($data);
                     //-------------------Complete
                     //Update New order with Transact User
                     $data = array('Completed' => 'Y', 'Transact.id' => $PO['_id'], 'Transact.username' => $PO['username'], 'Transact.user_id' => $PO['user_id'], 'Transact.DateTime' => new \MongoDate(), 'Order' => 'P>C: Update current order no change in commission or amount');
                     $orders = Orders::find('first', array('conditions' => array('_id' => $order_id)))->save($data);
                     //---------------------Complete
                     //To update Balance
                     $ex->updateBalance($order_id);
                     $ex->updateBalance($PO['_id']);
                     $ex->SendOrderCompleteEmails($order_id, $user['_id']);
                     $ex->SendOrderCompleteEmails($PO['_id'], $PO['user_id']);
                     break;
                 }
                 if ((double) $PO['Amount'] < (double) $Amount) {
                     // Update Previous Order with New Order Amount and New Commission and Transact User
                     if ($PO['Action'] == "Buy") {
                         $PrevCommAmount = round($PO['CommissionPercent'] * ($PO['Amount'] - $Amount) / 100, 8);
                         $CurrCommAmount = round($PO['CommissionPercent'] * ($PO['Amount'] - $Amount) * $PO['PrePrice'] / 100, 8);
                         $PrevCommCurr = $PO['FirstCurrency'];
                         $CurrCommCurr = $PO['SecondCurrency'];
                     } else {
                         $PrevCommAmount = round($PO['CommissionPercent'] * ($PO['Amount'] - $Amount) * $PO['PerPrice'] / 100, 8);
                         $CurrCommAmount = round($PO['CommissionPercent'] * ($PO['Amount'] - $Amount) / 100, 8);
                         $PrevCommCurr = $PO['SecondCurrency'];
                         $CurrCommCurr = $PO['FirstCurrency'];
                     }
                     if ($PO['Action'] == "Buy") {
                         $PrevCommAmount = round($PO['CommissionPercent'] * $PO['Amount'] / 100, 8);
                     } else {
                         $PrevCommAmount = round($PO['CommissionPercent'] * $PO['Amount'] * $PO['PerPrice'] / 100, 8);
                     }
                     $data = array('Commission.Amount' => (double) $PrevCommAmount, 'Amount' => (double) $PO['Amount'], 'Completed' => 'Y', 'Transact.id' => $order_id, 'Transact.username' => $user['username'], 'Transact.user_id' => $user['_id'], 'Transact.DateTime' => new \MongoDate(), 'Order' => 'P<C: Update Previous Record');
                     $orders = Orders::find('first', array('conditions' => array('_id' => $PO['_id'])))->save($data);
                     //--------------------Complete
                     // Update current order with new commission and amount
                     if ($PO['Action'] == "Buy") {
                         $CurrCommAmount = round($PO['CommissionPercent'] * $PO['Amount'] * $PO['PerPrice'] / 100, 8);
                     } else {
                         $CurrCommAmount = round($PO['CommissionPercent'] * $PO['Amount'] / 100, 8);
                     }
                     $data = array('Commission.Amount' => (double) $CurrCommAmount, 'Amount' => (double) $PO['Amount'], 'Completed' => 'Y', 'Transact.id' => $PO['_id'], 'Transact.username' => $PO['username'], 'Transact.user_id' => $PO['user_id'], 'Transact.DateTime' => new \MongoDate(), 'Order' => 'P<C: Update current record');
                     $orders = Orders::find('first', array('conditions' => array('_id' => $order_id)))->save($data);
                     //--------------------Complete
                     //Create a new order of pending amount
                     if ($PO['Action'] == 'Buy') {
                         $CurrCommAmount = round($PO['CommissionPercent'] * (double) round((double) $Amount - (double) $PO['Amount'], 8) * $PerPrice / 100, 8);
                     } else {
                         $CurrCommAmount = round($PO['CommissionPercent'] * (double) round((double) $Amount - (double) $PO['Amount'], 8) / 100, 8);
                     }
                     $data = array('Action' => $Action, 'FirstCurrency' => $FirstCurrency, 'SecondCurrency' => $SecondCurrency, 'CommissionPercent' => (double) $Commission, 'Commission.Amount' => (double) $CurrCommAmount, 'Commission.Currency' => $CurrCommCurr, 'Amount' => (double) round((double) $Amount - (double) $PO['Amount'], 8), 'PerPrice' => (double) $PerPrice, 'DateTime' => new \MongoDate(), 'Completed' => 'N', 'IP' => $_SERVER['REMOTE_ADDR'], 'username' => $user['username'], 'user_id' => $user['_id'], 'Order' => 'P<C: Create New Previous Order with Balance details');
                     $orders = Orders::create();
                     $orders->save($data);
                     $ex->updateBalance($order_id);
                     $ex->updateBalance($PO['_id']);
                     $ex->SendOrderCompleteEmails($order_id, $user['_id']);
                     $ex->SendOrderCompleteEmails($PO['_id'], $PO['user_id']);
                     break;
                 }
             }
             //  =Pending orders=================================================================================
             //======================Trade========================================================
             $Order = Orders::find('first', array('conditions' => array('_id' => $order_id)));
             $result = array('Order_id' => String::hash($Order['_id']), 'pair' => $pair, 'type' => $type, 'Commission.Amount' => $Order['Commission']['Amount'], 'Commission.Currency' => $Order['Commission']['Currency'], 'amount' => $Order['Amount'], 'price' => $Order['PerPrice'], 'time' => $Order['DateTime']->sec, 'Completed' => $Order['Completed'], 'username' => $Order['username']);
             return $this->render(array('json' => array('success' => 1, 'now' => time(), 'result' => $result)));
         }
     }
 }
コード例 #2
0
ファイル: ExController.php プロジェクト: nilamdoc/msx
 public function ChangeCurrentOrder($order_id, $PO_id, $PO_Amount, $user)
 {
     $PO = Orders::find('first', array('conditions' => array('_id' => $PO_id)));
     $CO = Orders::find('first', array('conditions' => array('_id' => $order_id)));
     if ($CO['Action'] == "Buy") {
         $CurrCommAmount = round((double) $CO['CommissionPercent'] * (double) $PO_Amount / 100, 8);
         $NewCommAmount = round((double) $CO['CommissionPercent'] * ((double) $CO['Amount'] - (double) $PO_Amount) / 100, 8);
     } else {
         $CurrCommAmount = round((double) $CO['CommissionPercent'] * (double) $PO_Amount * (double) $CO['PrePrice'] / 100, 8);
         $NewCommAmount = round((double) $CO['CommissionPercent'] * ((double) $CO['Amount'] - (double) $PO_Amount) * (double) $CO['PrePrice'] / 100, 8);
     }
     $COData = array('Action' => $CO['Action'], 'FirstCurrency' => $CO['FirstCurrency'], 'SecondCurrency' => $CO['SecondCurrency'], 'CommissionPercent' => (double) $CO['CommissionPercent'], 'Commission.Amount' => (double) $CurrCommAmount, 'Commission.Currency' => $CO['Commission']['Currency'], 'Amount' => (double) $PO_Amount, 'PerPrice' => (double) $CO['PerPrice'], 'DateTime' => $CO['DateTime'], 'username' => $CO['username'], 'IP' => $CO['IP'], 'user_id' => $CO['user_id'], 'Completed' => 'Y', 'Transact.id' => $PO_id, 'Transact.username' => $PO['username'], 'Transact.user_id' => $PO['_id'], 'Transact.DateTime' => new \MongoDate(), 'Order' => 'P>C: Update Current Commission and Amount and Complete Order');
     //	print_r($COData)		;
     $data = array('Action' => $CO['Action'], 'FirstCurrency' => $CO['FirstCurrency'], 'SecondCurrency' => $CO['SecondCurrency'], 'CommissionPercent' => (double) $CO['CommissionPercent'], 'Commission.Amount' => (double) $NewCommAmount, 'Commission.Currency' => $CO['Commission']['Currency'], 'Amount' => (double) ($CO['Amount'] - $PO_Amount), 'PerPrice' => (double) $CO['PerPrice'], 'DateTime' => new \MongoDate(), 'Completed' => 'N', 'IP' => $_SERVER['REMOTE_ADDR'], 'username' => $user['username'], 'user_id' => $user['_id']);
     $CO = Orders::find('all', array('conditions' => array('_id' => $order_id)))->save($COData);
     // Create Order for the user
     if ((double) ($CO['Amount'] - $PO_Amount) > 0) {
         $orders = Orders::create();
         $orders->save($data);
         $order_id = $orders->_id;
         $this->SendEmails($order_id, $user['_id']);
         return $order_id;
     }
     return false;
 }