<?php

date_default_timezone_set('asia/shanghai');
include('/../../inc/buysyspc.php');
require_once("lib/itppay.class.php");

// $_POST['amount'] = '1';
// $_POST['body'] = '商品描述';
// $_POST['mchntOrderNo'] = date("Y").date("m").date("d").rand(10000,100000);
// $_POST['notifyUrl'] = 'http://www.itppay.com';
// $_POST['payChannelId'] = "2100000001";
// $_POST['returnUrl'] = 'http://www.itppay.com';
// $_POST['subject'] = "商品名称";

if(!check_post_args($msg, array('amount', 'body', 'mchntOrderNo', 'notifyUrl', 'payChannelId', 'returnUrl', 'subject'))) {
	ajax_msg(1, $msg);
}

/* *
 * 配置信息
 */
$itppay_config["appid"]=zl_appid1;//交易发起所属app
$itppay_config["key"]=zl_key1;//合作密钥

/* *
 * 请求参数，参数须按字符升序排列
 */
$parameter=array(
	"amount"         =>$_POST['amount'],//[必填]订单总金额，单位(分)
	"appid"          =>$itppay_config["appid"],//[必填]//交易发起所属app
	"body"           =>$_POST['body'],//[必填]商品描述
	"clientIp"       =>ip(),//[必填]客户端IP
	"cpChannel"      =>"",//CP分发渠道
	"currency"       =>"",//币种，默认RMB
	"description"    =>"",//订单附加描述
	"expireMs"       =>"",//过期时间毫秒数，默认24小时过期
	"extra"          =>empty($_POST['extra']) ? '' : $_POST['extra'],//附加数据，以键值对形式存放，例如{"key":"value"}
	"mchntOrderNo"   =>$_POST['mchntOrderNo'],//[必填]商户订单号，必须唯一性
	"notifyUrl"      =>$_POST['notifyUrl'],//[必填]订单支付结果异步通知地址，用于接收订单支付结果通知，必须以http或https开头
	"payChannelId"   =>$_POST['payChannelId'],//支付渠道id
	"returnUrl"      =>$_POST['returnUrl'],//[必填]订单支付结果同步跳转地址，用于同步跳转到商户页面，必须以http或https开头
	"subject"        =>$_POST['subject'],//[必填]商品名称
	"version"        =>"h5_NoEncrypt",//接口版本号，值为pc_NoEncrypt时,则明天平台返回商户参数时，不进行RSA加密
);

/* *
 * 建立请求
 */
$itpPay = new itpPay($itppay_config);
$orderInfo=$itpPay->getOrderInfo($parameter);
$res = http_post('http://trans.palmf.cn/sdk/api/v1.0/cli/order_api/0', array('orderInfo' => $orderInfo));
print_r($res);

function check_post_args(&$msg, $fields) {
	foreach ($fields as $field) {
		if(empty($_POST[$field])) {
			$msg = $field . ' required';
			return false;
		}
	}

	return true;
}

function http_post($url, $fields) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}

function ajax_msg($error, $data = '') {
	$arr = array(
		'error' => $error,
	);

	if($data) {
		$arr['data'] = $data;
	}

	exit(json_encode($arr));
}

function ip() {
    if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
        $ip = getenv('HTTP_CLIENT_IP');
    } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
        $ip = getenv('HTTP_X_FORWARDED_FOR');
    } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
        $ip = getenv('REMOTE_ADDR');
    } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return preg_match ( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : '';
}
