<?php
define('IN_WEB', true);

define('SKEY', 'Qs2HtcqnMyIqf7Ey');//服务端密钥

$url = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"];

$sid = (isset($_REQUEST['sid']) == false) ? 1 : $_REQUEST['sid'];
$lid = (isset($_REQUEST['lid']) == false) ? 1 : $_REQUEST['lid'];

if ($sid <= 0) {
	die('no sid. in login.');
}

include_once dirname(__FILE__) . "/../com.php";
	
$ret = encrypt($url);
function encrypt($path){
	$path = trim($path);
	
	$start = strpos($path,'?')+1;
	$end = strrpos($path,'&') - strlen($path);
	
	//Generate encrypted string by init parameters
	$encryptStr = substr($path,$start,$end);
	$encryptStr = md5($encryptStr.'Qs2HtcqnMyIqf7Ey');
	
	//Get encrypted string from URL
	$encryptParameter = substr($path,-32);

	//oo::logs()->debug3($encryptStr, 'payment_bluepay.log');
	
	//Compare the result.
	if($encryptStr == $encryptParameter){
		return 1;
	}else{
		return 0;
	}
}

if ( $ret == 1 ) {
	//获取status, price,t_id, currency
	$matches = $param = array();
    preg_match_all("#.*&operator=(.*?)&paytype=.*&price=(.*?)&.*&status=(.*?).*&t_id=(.*?).*&currency=(.*?)&interfacetype=(.*?)&.*#iU", $url, $matches);
	if( empty($matches) ){
		oo::logs()->debug3($url,'payment_bluepay.log');		
	}
	//oo::logs()->debug3(array('m' =>$matches, 'line' => 50),'payment_bluepay.log');
	$status = $matches[3][0];
    $currency = $matches[5][0];
	$param['operator'] = $matches[1][0];//运营商
	$param['price'] = $matches[2][0];//价格
	$param['orderId'] = $matches[4][0];//订单ID
	$param['currency'] = $currency;//计费单位
	$param['interfacetype'] = $matches[6][0];//计费类型，sms（短信）/cashcard（充值卡）/bank（银行）/wallet（line）

	//oo::logs()->debug3(array('status' =>$status, 'line' => 56),'payment_bluepay.log');
	if( $status == 200 && $currency == 'THB' ){//扣费成功,开始发货
		$ret = oo::commonOprModel('paymentbluepay')->verifyBluePay($param);
		if ($ret) {
			return true;
		}
	}
	if( $status == 200 && $currency == 'VND') {//越南支付扣费成功，开始发货
        $ret = oo::commonOprModel('paymentvi_bluepay')->verifyBluePay($param);
        if ($ret) {
            return true;
        }
    }
}else{
	oo::logs()->debug3(array('ret' => $ret, 'url' => $url), 'payment_bluepay.log');
}