<?php
include("bigcommerce_unified_biling.php");
include_once 'inc/functions.php';
include_once 'inc/database.php';

if(isset($_GET["store_hash"]))
{
	$store_hash=$_GET["store_hash"];
	//$channel_id=$_GET["channel_id"];


	$checkout_id=$db->getSingleColumn( "bc_ubSubscription", "bc_subcription_checkout_id", array( "store_id" =>$store_hash));

	if(!empty($checkout_id)){

		//echo "not empty";
		//sleep(3);

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

		if(!empty($bc_subscription_id))
		{
			/*$bc_data=["bc_subscripion_id"=>$bc_subscription_id,"subscription_status"=>"active"];
			$db->update("bc_ubSubscription", $bc_data, array("store_id" => $store_hash));*/

			mysqli_query($Conn_db,"UPDATE bc_ubSubscription SET `bc_subscripion_id`='$bc_subscription_id',`subscription_status`='active' WHERE bc_subcription_checkout_url!=''  AND `store_id`='$user_store_hash'");
			

		}

		$redirecturl="https://store-".$store_hash.".mybigcommerce.com/manage/app/".APP_ID;

		echo'<script>window.location.href="'.$redirecturl.'"</script>';
	}
	else{
		//echo " empty";
		//sleep(3);
	}


}


?>