<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Api extends CI_Controller {

	public function __construct() 
	{
		parent::__construct();
		
		$this->load->model('member_model');
    }
	
	public function crypto_deposit()
	{
		set_time_limit(0);
		
		$post = file_get_contents('php://input');
		
		if( ! empty($post))
		{
			$output = array(
				'errorCode' => 100, 
				'errorMessage' => '',
			);
				
			$arr = json_decode($post, TRUE);
			
			$secret_key = PG_TRONGRID_KEY;
			$merchant_id = (isset($arr['merchant_id']) ? trim($arr['merchant_id']) : '');
			$signature = (isset($arr['signature']) ? trim($arr['signature']) : '');
			$username = (isset($arr['username']) ? trim($arr['username']) : '');
			$coin_id = (isset($arr['coin_id']) ? trim($arr['coin_id']) : '');
			$amount = (isset($arr['amount']) ? trim($arr['amount']) : '');
			
			//Verify merchant
			if($merchant_id == PG_TRONGRID_AGENT_ID) {
				//Verify signature
				if( ! empty($signature)) {
					$verify_sign = md5($merchant_id . $username . $coin_id . $amount . $secret_key);
						
					if($signature == $verify_sign) {
						if($coin_id == 1 || $coin_id == 2) {
							if($amount > 0) {
								$query = $this->db->query("SELECT a.member_id, a.username, b.v_unit2 FROM sw_members a, sw_member_wallets b WHERE (a.member_id = b.member_id) AND a.username = ? LIMIT 1", array($username));
								if($query->num_rows() > 0) {
									$row = $query->row();
									
									$currency = $this->member_model->get_currency_rate();
									
									switch($coin_id) {
										case 1: $actual_coin_id = 5; $payment_method = 4; break;
									}
									
									$actual_amount = ($amount / $currency[$actual_coin_id]['in']);
									
									#Populate submitted data
									$DBdata = array(
										'transaction_type' => 2,
										'payment_method' => $payment_method,
										'to_member_id' => $row->member_id,
										'to_member_username' => $row->username,
										'from_debit_balance' => $row->v_unit2,
										'debit_amount' => $actual_amount,
										'to_debit_balance' => ($row->v_unit2 + $actual_amount),
										'transaction_amount' => $amount,
										'order_no' => date("Ymdhms").sprintf("%05d", rand(1, 99999)),
										'currency_id' => $actual_coin_id,
										'currency_rate' => $currency[$actual_coin_id]['in'],
										'status' => 1,
										'transaction_date' => time()
									);
									
									$this->db->trans_start();
									
									$this->db->insert('sw_member_topup', $DBdata);
									
									$this->db->query("UPDATE sw_member_wallets SET v_unit2 = (v_unit2 + ?) WHERE member_id = ? LIMIT 1", array($DBdata['debit_amount'], $DBdata['to_member_id']));
									
									$this->member_model->insert_member_log($row->member_id, LOG_TOPUP, json_encode($DBdata));
									
									$this->db->trans_complete();
									
									$rs = $this->db->trans_status();
									
									if($rs) {
										$output['errorCode'] = 0;
										$output['errorMessage'] = 'success';
									}
									else {
										$output['errorCode'] = 105;
										$output['errorMessage'] = 'failed';
									}
								}
								else {
									$output['errorCode'] = 103;
									$output['errorMessage'] = 'Invalid username';
								}
							}
							else {
								$output['errorCode'] = 104;
								$output['errorMessage'] = 'Invalid amount';
							}	
						}
						else {
							$output['errorCode'] = 107;
							$output['errorMessage'] = 'Invalid coin id';
						}
					}
					else {
						$output['errorCode'] = 102;
						$output['errorMessage'] = 'Invalid signature';
					}
				}
				else {
					$output['errorCode'] = 101;
					$output['errorMessage'] = 'Signature is empty';
				}
			}
			else {
				$output['errorCode'] = 106;
				$output['errorMessage'] = 'Invalid merchant';
			}	
			
			//Output
			$this->output
					->set_status_header(200)
					->set_content_type('application/json', 'utf-8')
					->set_output(json_encode($output))
					->_display();
		}
		else
		{
			echo show_404();
		}
		
		exit();
	}
}

/* End of file api.php */
/* Location: ./application/controllers/api.php */