<?php



class ControllerDhpayCallback extends Controller {







	var $error = array();







	function index() {



/**



 * 回调方法会接受如下参数



 *



 * mechant_id: 商户号，对应商户提交的商户号



 * invoice_id: 交易号，对应商户提交的交易号



 * order_no: 订单号，对应商户提交的订单号



 * currency: 交易币种



 * amount: 交易金额



 * status: 交易状态(00处理中, 01成功, 02失败)



 * failure_reason: 如果交易状态为“失败”，则会有相应的失败原因



 * trans_date: 交易日期



 * trans_time: 交易时间



 * hash: 返回参数的签名



 */







$mechant_id=$_REQUEST['mechant_id'];



$invoice_id=$_REQUEST['invoice_id'];



$order_no=trim($_REQUEST['order_no']);



$currency=$_REQUEST['currency'];



$amount=$_REQUEST['amount'];



$status=$_REQUEST['status'];



$failure_reason=$_REQUEST['failure_reason'];



$trans_date=$_REQUEST['trans_date'];



$trans_time=$_REQUEST['trans_time'];



$hash=$_REQUEST['hash'];



// 验证签名==============================================================================================================================================



		// 密钥



		$private_key = 'sV60GHJYFOIU67865432Fr63sJHGFDG295jhg6rt67S1AY734Dcr68558743';



		// 签名的表单字段名



		$hash_key = array('amount','currency','invoice_id','merchant_id');



		// 按key名进行顺序排序



		sort($hash_key);



		foreach ($hash_key as $key) {



		    $hash_src .= $_REQUEST[$key];



		}



		// 密钥放最前面



		$hash_src = $private_key . $hash_src;



		// sha256算法



		$verify_hash = hash('sha256', $hash_src);



//==================================================================================================================================================







$dhpay_log="      商户订单号 = ".$order_no.", 支付金额 = ".$amount.", 支付币种 = ".$currency.", 支付状态 = ".$status."\n";



$dhpay_log.="      返回的签名串 = ".$hash."\n"."      验证的签名串 = ".$verify_hash."\n";







$fp = fopen("dhpay_log.txt","a");



	flock($fp, LOCK_EX) ;







	switch ($status) {



    case '00':



        $dhpay_log.="      支付结果 = 处理中\n";



        break;



    case '01':



        $dhpay_log.="      支付结果 = 成功\n";



        break;



    case '02':



        $dhpay_log.="      支付结果 = 失败(".$failure_reason.")\n";



        break;



    default:



        break;



}







if ($hash == $verify_hash)



{



	$dhpay_log.="      验证签名结果相等\n";



}



else //验证不通过 支付失败



{



	$dhpay_log.="      验证签名结果不相等\n";



}







 date_default_timezone_set('Asia/Hong_Kong');



 fwrite($fp,"回调时间：".date('Y-m-d H:i:s',time())."\n".$dhpay_log."\n");



		flock($fp, LOCK_UN);



		fclose($fp);







// 如果需要跳转的话



$http_server = "https://www.beststoreitems.com/";	







	if($status=='01')



	{



		//收到通知 返回success



		echo "Payment succeed, you will be redirected to your account after 5 seconds, please do not close the window.";		//请不要修改或删除



		



		// 支付成功，转到相应网处理页



			$url=$http_server.'index.php?controller=potential_order&action=ProcessPending&order_id='.$order_no;



			 echo '



	    	       <form action="'.$url.'" method="post"  id="form" name="form">



				      <input type="hidden" name="dhpay" value="1" />



	    	          <input type="hidden" name="order_id" value="'.$order_no.'" />



	    	          <input type="hidden" name="amount" value="'.$amount.'" />



					  <input type="hidden" name="currency" value="'.$currency.'" />



	    	          <script language="javascript">



						   form.submit();



					  </script>



				   </form>



	        	';      	



		}



		else



		{



			echo "Payment failed, the possible reason is incorrect card details or your issuing bank refused the foreign transaction, please contact them to get the payment permission, and then try to pay again.";	



			$url=$http_server.'index.php?controller=potential_order&order_id='.$order_no;



			echo "<meta http-equiv=\"refresh\" content=\"5;url=$url\">";



		}



 	} 



}







?>



