<?php
include_once 'bigcommerce_unified_biling.php';

include_once 'inc/database.php';
include_once 'requestFunctions.php';
include_once 'inc/functions.php';


function decode3t($str){
    for($i=0; $i<5;$i++){
        $str    = base64_decode(strrev($str));
    }
    return $str;
}
if(isset($_GET["token"]))
{
	$store_hash=decode3t($_GET["token"]);
	//$channel_id=$_GET["channel_id"];
	$bc_subscription_id=$db->getSingleColumn( "bc_ubSubscription", "bc_subscripion_id", array( "store_id" =>$store_hash));

	if(!empty($bc_subscription_id)){

		 $store_info = $db->select("stores", "*", array( "store_id" =>$store_hash));
		 $user_account_id= isset($store_info[0])?$store_info[0]["merchnat_account_uuid"]:$store_info["merchnat_account_uuid"];
		
		 $bc_ub_apiUrl = "https://api.bigcommerce.com/accounts/" .$user_account_id. "/graphql";


		$is_cancel=UB_cancel_subscription($bc_subscription_id,$store_hash);


		if($is_cancel)
		{
			$message="Your subscription has been cancelled successfully.";
			

			
		}else{
			$message="We were unable to cancel your subscription.";
			
		}

		$redirect_url=BASE_PATH."subscription/index.php?store_hash={$store_hash}&menu=subscription&message={$message}";

		 echo '<script>window.location.href="'.$redirect_url.'"</script>';

	}

}

?>