<?php

pay();

function connectToDatabase(){
    require_once('db.php');
    $connection = new database();
    return $connection;

}

function pay(){
    
    $connection = connectToDatabase();
    $Key = $_REQUEST['Key'];
    $ID = $_REQUEST['ID'];
    $Currency = $_REQUEST['Currency'];
    $Timee = date("Y/m/d G:i");
    $Price = 0 ;
    $AddressCallBack = "https://yourdomain.com/YourFolder/pay_btc_end.php";

    if($Key!="" && $ID!=""){
        
        $check_hash = $connection->q("select ID from users where ID='$ID'");
        $check_hash_r = $connection->a($check_hash);
        if($check_hash_r[0]!=""){
			
			if($Key == "VIP"){
				
				$Check_Shop = $connection->q("select * from valuesBase where Name='VIPSHOP_Usdt'");
				$Check_Shop_r = $connection->a($Check_Shop);
				if($Check_Shop_r['Value']!=""){
					
					$Price = $Check_Shop_r['Value'] ;
					
					$CallbackURL = $AddressCallBack.'?Key='.$Key.'&ID='.$ID.'&Amount='.$Price;
					
					$postdata = array(
                        'price_amount' => $Price,
                        'price_currency' => 'usd',
                        'pay_currency' => $Currency,
                        'order_id' => $ID.'-'.$Key,
                        'ipn_callback_url' => $CallbackURL,
                        'success_url' => $CallbackURL,
                        'cancel_url' => $CallbackURL,
                        'order_description' => 'buy coin'
                    );
                    $result = httpPost('https://api.nowpayments.io/v1/invoice',$postdata);
                    
                    if(isset($result->invoice_url)){
                        $result = $connection->q("insert into pay_btc (User,ID_Pay) values('$ID','".$result->id."') ");
                        Header('Location: '.$result->invoice_url);
                    }else {
                        print "ERROR,".$result;
                    }
					
				}else {
					print "NO,shop" ;
				}
				
			}else {
				
				$Check_Shop = $connection->q("select * from shop where KeyMarket='$Key'");
				$Check_Shop_r = $connection->a($Check_Shop);
				if($Check_Shop_r['USD']!=""){
					
					$Price = $Check_Shop_r['USD'] ;
					
					$CallbackURL = $AddressCallBack.'?Key='.$Key.'&ID='.$ID.'&Amount='.$Price;
					
					$postdata = array(
                        'price_amount' => $Price,
                        'price_currency' => 'usd',
                        'pay_currency' => $Currency,
                        'order_id' => $ID.'-'.$Key,
                        'ipn_callback_url' => $CallbackURL,
                        'success_url' => $CallbackURL,
                        'cancel_url' => $CallbackURL,
                        'order_description' => 'buy coin'
                    );
                    $result = httpPost('https://api.nowpayments.io/v1/invoice',$postdata);
                    
                    if(isset($result->invoice_url)){
                        $result = $connection->q("insert into pay_btc (User,ID_Pay) values('$ID','".$result->id."') ");
                        Header('Location: '.$result->invoice_url);
                    }else {
                        print "ERROR,".$result;
                    }
					
				}else {
					print "NO,shop" ;
				}
				
			}
            
        }else {
            print "wrong Hash";
        }
        
    }else {
        print "NULL,0";
    }
    
}

function httpPost($url, $data)
{
    $MerchantID = 'MTE687V-X5347GS-MDR934F-DXPSFHK';
    $postdata = json_encode($data);
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'x-api-key: '.$MerchantID
    ));
    $response = curl_exec($curl);
    curl_close($curl);
    return json_decode($response);
}

?>