<?php
ini_set('date.timezone','Asia/Shanghai');
//error_reporting(E_ERROR);
require_once "ourphpapi.php";
require_once "lib/WxPay.Api.php";
require_once "WxPay.JsApiPay.php";
require_once 'log.php';

//初始化日志
$logHandler= new CLogFileHandler("logs/".date('Y-m-d').'.log');
$log = Log::Init($logHandler, 15);

//打印输出数组信息
function printf_info($data)
{
    foreach($data as $key=>$value){
        echo "<font color='#00ff55;'>$key</font> : $value <br/>";
    }
}

//①、获取用户openid
$tools = new JsApiPay();
$openId = $tools->GetOpenid($api[2],$api[5]);
$att = explode("|",$_GET['att']);
//验证码|订单ID|用户ID|用户校验码|优惠券ID|优惠券价格|团长ID|总金额（分）|用户账号


//②、统一下单
$timeStamp = date("YmdHis");
$input = new WxPayUnifiedOrder();
$input->SetBody("商品购买小程序付款");
$input->SetAttach($att[0]."|".$att[1]."|".$att[2]."|".$att[3]."|".$att[4]."|".$att[5]."|".$att[6]);
$input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis"));
$input->SetTotal_fee($_GET['money']);
$input->SetTime_start($timeStamp);
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetGoods_tag("ourphp");
$input->SetNotify_url("https://".$web['weburl']."/function/api/appletspay/notify.php");
$input->SetTrade_type("JSAPI");
$input->SetOpenid($openId);
$order = WxPayApi::unifiedOrder($input);
//printf_info($order);
//print_r($order);
//exit;

$nonceStr = $order['nonce_str'];
$package = $order['prepay_id'];
$sign = $order['sign'];
$paySign = MD5("appId=".$api[2]."&nonceStr=".$nonceStr."&package=prepay_id=".$package."&signType=MD5&timeStamp=".$timeStamp."&key=".$api[4]);

$pay = array(
			"timeStamp" => $timeStamp,
			"nonceStr" => $nonceStr,
			"package" => $package,
			"paySign" => strtoupper($paySign),
);

echo json_encode($pay);
exit;
?>