<?php
$voguepay_merchant_id = get_option('voguepay_merchant_id', 'payments');

if (!isset($data['transaction_id'])) {
    return;
}


$transaction_id = $data['transaction_id'];

$check = "https://voguepay.com/?v_transaction_id={$transaction_id}&type=json";
if (trim(strtolower($voguepay_merchant_id)) == 'demo') {
    $check .= "&demo=true";
}


$request = @mw()->http->url($check)->get();
$response = @json_decode($request, true);

if (!isset($response['merchant_id']) or !$voguepay_merchant_id){
	return;
}
if ($response['merchant_id'] != $voguepay_merchant_id){
	return;
} 

if (isset($response['merchant_ref']) 
and isset($response['transaction_id'])
and isset($response['status'])
and isset($response['total_paid_by_buyer'])
) {
 
    $query = array();
    $query['payment_verify_token'] = $response['merchant_ref'];
    $query['single'] = true;
    $order = mw()->shop_manager->get_orders($query);
 
    if($order){
        $update_order['transaction_id'] = $response['transaction_id']; // a reference generated by the payment gateway
		$update_order['payment_amount'] = $response['total_paid_by_buyer']; 
		$update_order['payment_status'] = $response['status']; 

		if (isset($response['email']) ){
					$update_order['payment_email'] = $response['email']; 
		}
        $update_order['success'] = 'Your payment was successful! Transaction id: ' . $response['transaction_id'];
        $update_order['is_paid'] = 1;
        $update_order['order_completed'] = 1;
		
    }

} 