<?php

Class MercadoPago {

    function __construct() {
		
        # Get Classes
        # ---------------
		# Database Class
		$this->dB = Connection::Database('MuOnline');

    }

    public function GetPayID($id){
        $Result = $this->dB->query_fetch("SELECT buy_id FROM WEBENGINE_MERCADOPAGO_TRANSACTIONS WHERE buy_id = '$id'");
        if(!$Result) return false;
        else return true;
    }

    public function GetUserID($username){
        $Result = $this->dB->query_fetch("SELECT memb_guid FROM MEMB_INFO WHERE memb___id = '$username'");
        return $Result;
    }

    public function GetStatusBuy($buy_id){
        $Result = $this->dB->query_fetch("SELECT buy_status FROM WEBENGINE_MERCADOPAGO_TRANSACTIONS WHERE buy_id = '$buy_id'");
        if($result['buy_status'] == "approved") return true;
        else return false;
    }   

    public function SetCoins($UserID, $Credits, $Type_Money){
        try {
            # user id
            if(!Validator::UnsignedNumber($UserID)) throw new Exception("invalid userid");
            // common class
            $common = new common();
            $accountInfo = $common->accountInformation($UserID);
            if(!is_array($accountInfo)) throw new Exception("invalid account");
            $creditSystem = new CreditSystem();
            $creditSystem->setConfigId($Type_Money);
            $configSettings = $creditSystem->showConfigs(true);
            switch($configSettings['config_user_col_id']) {
                case 'userid':
                    $creditSystem->setIdentifier($accountInfo[_CLMN_MEMBID_]);
                    break;
                case 'username':
                    $creditSystem->setIdentifier($accountInfo[_CLMN_USERNM_]);
                    break;
                case 'email':
                    $creditSystem->setIdentifier($accountInfo[_CLMN_EMAIL_]);
                    break;
                default:
                    throw new Exception("invalid identifier");
            }
            $creditSystem->addCredits($Credits);
            
        } catch (Exception $ex) {
            die();
        }
    }  

    public function SetVIP($username, $Credits, $type_vip){
        $Query = "UPDATE MEMB_INFO SET AccountLevel = ". $type_vip .", AccountExpireDate = GETDATE() + ". $Credits ." WHERE memb___id = '". $username ."'";
        $Result = $this->dB->query($Query);
    }  

    public function InsertLOGS($data){                   
        $Query =    "INSERT INTO WEBENGINE_MERCADOPAGO_TRANSACTIONS (
                    ip_payed, userID, buy_id, username, credits, description, method, method_payed, date_create, amount, type_money, buy_status, buy_detail, approved_date) "
                    ."VALUES ". "(:ip_payed, :userID, :buy_id, :username, :credits, :description, :method, :method_payed, :date_create, :amount, :type_money, :buy_status,
                    :buy_detail, :approved_date)";
        $InsertLog = $this->dB->query($Query, $data);
    }

}