<?php

// 更新用户会员状态
require_once ("../user_include.inc");

$unionId = ParamUtil::getRequestString("unionId", 0);
$openId = ParamUtil::getRequestString("openId", 0);
ErrorLogger::doOutput("Compass...ajax_update_user_member.php....Start.", 0);

ErrorLogger::doOutput("Compass...ajax_update_user_member.php....unionId=" . $unionId, 0);

//参数验证
if(empty($unionId) || empty($openId)) {
	responseNG("参数错误！");
}

//检索用户是否存在
$param['unionid'] = $unionId;
$param['openId'] = $openId;
$param['delete_flg'] = false;
$tmpUserMstList = UserMst::getList($param,'id','desc', 0, 1);
if(empty($tmpUserMstList)) {
	responseNG("参数错误！");
}

$userMst = $tmpUserMstList[0];

//调用支付
//生成临时订单
$orderNo = "M" . date("YmdHis") . "_" . $userMst->id;
$userBuyMemberDat = new UserBuyMemberDat();
$userBuyMemberDat->user_id = $userMst->id;
$userBuyMemberDat->money = MEMBER_FEE;
$userBuyMemberDat->order_no = $orderNo;
$userBuyMemberDat->status = "NEW";
$userBuyMemberDat->save();

//调用微信支付接口
$tools = new JsApiPay();
$input = new WxPayUnifiedOrder();
$input->SetBody("志愿者会员购买");
$input->SetAttach("志愿者会员购买");
$input->SetOut_trade_no($orderNo);
$input->SetTotal_fee(MEMBER_FEE * 100);//折算到分
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 800));
$input->SetGoods_tag("志愿者会员购买");
$input->SetNotify_url("https://" . SSL_PATH . "/api/api_wx_pay_notify.php");
$input->SetTrade_type("JSAPI");
$input->SetOpenid($userMst->openid);

$order = WxPayApi::unifiedOrder($input);

$jsApiParameters = $tools->GetJsApiParameters($order);
ErrorLogger::doOutput($jsApiParameters);
ErrorLogger::doOutput("Compass...ajax_set_buy_media.php.php....End.", 0);


ErrorLogger::doOutput("Compass...ajax_update_user_member.php....End.", 0);

responseOK($jsApiParameters);

function responseNG($message) {
	$result = array("status"=>"NG", "message"=>$message);
	print json_encode($result);
	exit;
}
function responseOK($result) {
	$result = array("status"=>"OK", "result"=>$result);
	print json_encode($result);
	exit;
}
?>