<?php

// 用户募捐支付接口
require_once ("../user_include.inc");

ErrorLogger::doOutput("Compass...ajax_set_user_donation_dat.php.php....Start.", 0);

//获取参数
$unionId = ParamUtil::getRequestString("unionId");
$donationId = ParamUtil::getRequestNumber("donationId", 0);
$comment = ParamUtil::getRequestString("comment");
$money = ParamUtil::getRequestNumber("money", 0);

$result = array();

//参数检查
if(empty($unionId) || empty($donationId)|| empty($comment)|| empty($money)) {
	$result["message"] = "参数错误！";
	responseNG($result);
}

$result = array();

//判断用户是否存在
$param = array();
$param['unionid'] = $unionId;
$param['delete_flg'] = false;
$userList = UserMst::getList($param,'id','desc', 0, 1);
if(empty($userList)) {
	$result["message"] = "用户不存在！";
	responseNG($result);
}
$userMst = $userList[0];

//募捐项目是否存在
$donationEventDat = DonationsEventDat::getById($donationId);
if(empty($donationEventDat)) {
	$result["message"] = "参数错误！";
	responseNG($result);
}

//调用支付
//生成临时订单
$orderNo = "D" . date("YmdHis") . "_" . $userMst->id;
$userDonationDat = new UserDonationDat();
$userDonationDat->user_id = $userMst->id;
$userDonationDat->donation_id = $donationEventDat->id;
$userDonationDat->comment = $comment;
$userDonationDat->money = $money;
$userDonationDat->order_no = $orderNo;
$userDonationDat->status = "NEW";
$userDonationDat->save();

//调用微信支付接口
$tools = new JsApiPay();
$input = new WxPayUnifiedOrder();
$input->SetBody("慈善募捐");
$input->SetAttach("慈善募捐");
$input->SetOut_trade_no($orderNo);
$input->SetTotal_fee($money * 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_user_donation_dat.php....End.", 0);

responseOK($jsApiParameters);

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