<?php 
$ref = "SPCS-LEVEL-2";
$amount = 400;
$wallet_type = 'BTC';
//
$sql4="SELECT count(*) as sum from j_pays WHERE j_ref='".$ref."' AND j_user_id='".$id."'"; //check 
$result4 = runSQL($sql4);
$data4 = mysqli_fetch_array($result4);
$sum4 = $data4['sum']; 
if($sum4 < 1){
	//
	$wallet_type_sm = strtolower($wallet_type);
	$wallet_db = "j_wallet_$wallet_type_sm";
	//
	$wallet = ${"wallet_".$wallet_type_sm.""}; $wallet = TWO_DEC($wallet); $bal = $wallet;
	//
	$rate_single =crypto_usd($wallet_type,$currency_in_use);
	$amount_crypto = TWO_DEC_CRYPTO($amount/$rate_single,$wallet_type); 
	//
	if($amount_crypto > 0){
		$timex = time();
		$sql4="SELECT count(*) as sum from j_pays"; //check 
		$result4 = runSQL($sql4);
		$data4 = mysqli_fetch_array($result4);
		$sum4 = $data4['sum']; $sum4 = $sum4+1;
		$transact_id = "$acr-$sum4-$timex";
		//record transaction as unpaid
		$ip = getIPAddress();  
		//
		$paid = 0; 
		$type = "credit";
		$kind = "A-Credit";
		$reason = "Credit alert from $company : $transact_id";
		$gateway = "Wallet"; //Paystack Rave
		//insert 
		$sql = "INSERT INTO j_pays 
		(j_transact_id,j_ref,j_user_id,j_currency,j_amount,j_bal,j_crypto,j_amount_crypto,j_type,j_kind,j_desc,j_gateway,j_paid,j_date,j_ip
		)
		VALUES
		('".$transact_id."','".$ref."','".$id."','".$currency_in_use."','".$amount."','".$bal."','".$wallet_type."','".$amount_crypto."','".$type."','".$kind."','".$reason."','".$gateway."','".$paid."','".$CurrentDate."','".$ip."'
		)
		";
		$result = runSQL($sql);
		if(!$result){
		  // die("<div class='alert alert-danger'> Error. Code 104. Try again.</div>"); 
		}
		//credit the user wallet
		$bal = TWO_DEC($wallet+$amount);
		//
		$sql = "UPDATE j_users SET
		$wallet_db ='".$bal."'
		WHERE j_id='".$id."'";
		$result = runSQL($sql);
		if(!$result){
			//die("Error. Code 103.");
		}
		//update record
		$sql = "UPDATE j_pays SET
		j_paid = '1', j_disburse = '1',j_bal ='".$bal."'
		WHERE j_transact_id='".$transact_id."' AND j_deleted < 1
		";
		$result = runSQL($sql);
		if(!$result){
			//die("<div class='alert alert-danger'> Error. Try again.</div>");
		}

		//send a mail for user
		$u_email = $email;
		$u_subject ="$ref CREDIT";
		$u_subject_m ="$u_subject";
		$u_message ="<p>Dear $name;</p> <p>We are glad to inform you that 
		your $wallet_type wallet with us has been credited with <b>".amount_w_c($amount,$currency_in_use)." (".amount_w_c($amount_crypto,$wallet_type).")</b>
		for completing <b>$p1_name ($ref)</b> and your new $wallet_type wallet balance is <b>".amount_w_c($bal,$currency_in_use)."</b>
		<br/><br/>
		Thank you for choosing <b>$company</b>.
		</p>";
		$uu_message = $u_message;
		require("../j_incs/mail.php");
		//send notify
		send_notify("",$id,$u_subject_m,$u_message);
	}
}
?>