<?php

if (!defined('IN_OELOVE')) {
exit('OElove[OEPHP] Access Denied');
}
$weixin_incfile = BASE_ROOT."source/conf/weixin.inc.php";
if (file_exists($weixin_incfile)) {
require_once($weixin_incfile);
}
require_once(BASE_ROOT."api/netpay/weixinpay/lib/WxPay.Config.php");
require_once(BASE_ROOT."api/netpay/weixinpay/lib/WxPay.Api.php");
require_once(BASE_ROOT."api/netpay/weixinpay/WxPay.JsApiPay.php");
require_once(BASE_ROOT."api/netpay/weixinpay/WxPay.NativePay.php");
require_once(BASE_ROOT."api/netpay/weixinpay/lib/WxPay.Notify.php");
class weixinpayWModel extends OE {
private $call_back_url = "";
private $notify_url = "";
public function submit($orderdata) {
if (empty($orderdata)) {
die("order error");
}
$tools = new JsApiPay();
$openId = isset($orderdata['wxopenid']) ?$orderdata['wxopenid'] : "";
if (empty($openId)) {
if (isset(parent::$wrap_user['weixinid'])) {
$openId = parent::$wrap_user['weixinid'];
}
}
if (empty($openId)){
$backurl = parent::$cfg['siteurl']."index.php?m=wap&c=pay&a=submit&paynum=".$orderdata['paynum'];
$openId = $tools->GetOpenid($backurl);
}
$input = new WxPayUnifiedOrder();
$input->SetBody($orderdata['title']);
$input->SetAttach($orderdata['title']);
$input->SetOut_trade_no($orderdata['paynum']);
$input->SetTotal_fee($orderdata['amount']*100);
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis",time() +6000));
$input->SetGoods_tag($orderdata['title']);
$input->SetNotify_url(parent::$cfg['siteurl']."notify/weixin_notify.php");
$input->SetTrade_type("JSAPI");
$input->SetOpenid($openId);
$order = WxPayApi::unifiedOrder($input);
$jsApiParameters = $tools->GetJsApiParameters($order);
return $jsApiParameters;
}
public function nativeCallBack() {
$notify = new NativeNotifyCallBack();
$notify_xml = $notify->Handle(true,false);
return $notify_xml;
}
public function jsApiCallBack() {
$notify = new JsApiNotifyCallBack();
$notify_xml = $notify->Handle(false,false);
return $notify_xml;
}
}
class NativeNotifyCallBack extends WxPayNotify {
public function unifiedorder($openId,$order_data) {
$input = new WxPayUnifiedOrder();
$input->SetBody($order_data['title']);
$input->SetAttach($order_data['title']);
$input->SetOut_trade_no($order_data['paynum']);
$input->SetTotal_fee($order_data['amount']*100);
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis",time() +6000));
$input->SetNotify_url(OE::$cfg['siteurl']."notifiy/weixin_native.php");
$input->SetTrade_type("NATIVE");
$input->SetOpenid($openId);
$input->SetProduct_id($order_data['paynum']);
$result = WxPayApi::unifiedOrder($input);
XLogic::writeTxtLog(json_encode($result)." unified","wxpay_native");
return $result;
}
public function NotifyProcess($data,&$msg) {
if (is_array($data)) {
$log_array = array(
"out_trade_no"=>$data['out_trade_no'],
"transaction_id"=>$data['transaction_id'],
"result_code"=>$data['result_code'],
"return_code"=>$data['return_code'],
);
$log_cont = "callback:".json_encode($log_array);
XLogic::writeTxtLog($log_cont,"wxpay_native");
}
if (!array_key_exists("openid",$data) ||!array_key_exists("out_trade_no",$data)) {
$msg = "回调数据异常";
XLogic::writeTxtLog($msg,"wxpay_native");
return false;
}
$openid = $data["openid"];
$out_trade_no = $data["out_trade_no"];
$transaction_id = $data['transaction_id'];
$result_code = $data['result_code'];
$return_code = $data['return_code'];
if (empty($out_trade_no)) {
$msg = "订单号错误";
XLogic::writeTxtLog($msg,"wxpay_native");
return false;
}
if ($result_code == "SUCCESS"&&$return_code['SUCCESS'] &&true === XValid::isSpChar($out_trade_no)) {
$payment_data = M("payorder","im")->getOrderData($out_trade_no);
if (empty($payment_data)) {
$msg = "订单信息错误";
XLogic::writeTxtLog($msg,"wxpay_native");
return false;
}
else {
if ($payment_data['paystatus'] == 0 ||$payment_data['paystatus'] == 11) {
$notic_array = array(
"sdkid"=>998,
"dealnum"=>$transaction_id,
"flag"=>1,
"bankid"=>"微信支付",
);
$notic_res = M("payorder","im")->doCallBack($out_trade_no,$notic_array);
return true;
}
else {
XLogic::writeTxtLog("回调执行：订单重复执行{$out_trade_no}","wxpay_jsapi");
return true;
}
}
}
else {
$msg = "回调数据异常";
XLogic::writeTxtLog($msg,"wxpay_native");
return false;
}
}
}
class JsApiNotifyCallBack extends WxPayNotify {
public function Queryorder($transaction_id) {
$input = new WxPayOrderQuery();
$input->SetTransaction_id($transaction_id);
$result = WxPayApi::orderQuery($input);
if (is_array($result)) {
$log_array = array(
"out_trade_no"=>$result['out_trade_no'],
"transaction_id"=>$result['transaction_id'],
"return_code"=>$result['return_code'],
"result_code"=>$result['result_code'],
);
$log_cont = "query:".json_encode($log_array);
XLogic::writeTxtLog($log_cont,"wxpay_jsapi");
}
if (is_array($result) &&$result["return_code"] == "SUCCESS"&&$result["result_code"] == "SUCCESS") {
$out_trade_no = $result["out_trade_no"];
$transaction_id = $result['transaction_id'];
if (empty($out_trade_no) ||false === XValid::isSpChar($out_trade_no)) {
$msg = "订单号错误";
XLogic::writeTxtLog($msg,"wxpay_jsapi");
return false;
}
if (true === XValid::isSpChar($out_trade_no)) {
$payment_data = M("payorder","im")->getFullPayOrder($out_trade_no);
if (empty($payment_data)) {
$msg = "订单信息错误";
XLogic::writeTxtLog($msg,"wxpay_jsapi");
return false;
}
else {
if ($payment_data['paystatus'] == 0 ||$payment_data['paystatus'] == 11) {
$notice_args = array(
"sdkid"=>998,
"dealnum"=>$transaction_id,
"flag"=>1,
"bankid"=>"微信支付",
);
$notic_res = M("payorder","im")->doCallBack($out_trade_no,$notice_args);
return true;
}
else {
XLogic::writeTxtLog("回调执行：订单重复执行{$out_trade_no}","wxpay_jsapi");
return true;
}
}
}
}
else {
XLogic::writeTxtLog("回调执行：微信支付失败{$result['return_code']}","wxpay_jsapi");
}
return false;
}
public function NotifyProcess($data,&$msg) {
if(!array_key_exists("transaction_id",$data)){
$msg = "输入参数不正确";
return false;
}
if (!$this->Queryorder($data["transaction_id"])){
$msg = "订单查询失败";
return false;
}
return true;
}
}
?>
