218 lines
5.5 KiB
PHP
218 lines
5.5 KiB
PHP
<?php
|
||
|
||
use phprs\ezsql\Sql;
|
||
|
||
require_once "lib/WxPay.Api.php";
|
||
require_once 'lib/WxPay.Notify.php';
|
||
require_once 'log.php';
|
||
|
||
class PayNotifyCallBack extends WxPayNotify
|
||
{
|
||
// 查询订单
|
||
public function Queryorder($transaction_id)
|
||
{
|
||
/*$input = new WxPayOrderQuery();
|
||
$input->SetTransaction_id($transaction_id);
|
||
$result = WxPayApi::orderQuery($input);
|
||
Log::DEBUG("query:" . json_encode($result));
|
||
if(array_key_exists("return_code", $result)
|
||
&& array_key_exists("result_code", $result)
|
||
&& $result["return_code"] == "SUCCESS"
|
||
&& $result["result_code"] == "SUCCESS") {
|
||
return true;
|
||
}
|
||
return false;*/
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* @note 转换数据编码
|
||
* @param $data
|
||
* @param string $charset
|
||
* @return string
|
||
* @auther 应俊
|
||
*/
|
||
function Characet($data, $charset = 'utf-8')
|
||
{
|
||
if (!empty ($data))
|
||
{
|
||
$encoding = mb_detect_encoding($data, array('ASCII', 'UTF-8', 'GBK', 'GB2312', 'LATIN1', 'BIG5',));
|
||
if (0 != strcasecmp($encoding, $charset))
|
||
{
|
||
$data = mb_convert_encoding($data, $charset, $encoding);
|
||
}
|
||
}
|
||
return $data;
|
||
}
|
||
|
||
|
||
// 重写回调处理函数
|
||
public function NotifyProcess($data, &$msg, $db = null)
|
||
{
|
||
$app_id = 0; // 开发者应用ID
|
||
$business_id = 0; // 商户ID
|
||
if (!array_key_exists("transaction_id", $data))
|
||
{
|
||
$msg = "输入参数不正确,必须包含transaction_id参数";
|
||
return false;
|
||
}
|
||
|
||
// 查询订单,判断订单真实性
|
||
if (!$this->Queryorder($data["transaction_id"]))
|
||
{
|
||
$msg = "订单查询失败";
|
||
return false;
|
||
}
|
||
|
||
if (!array_key_exists("out_trade_no", $data))
|
||
{
|
||
$msg = "输入参数不正确,不能缺少out_trade_no参数。";
|
||
return false;
|
||
}
|
||
|
||
if (!array_key_exists("attach", $data))
|
||
{
|
||
$msg = "输入参数不正确";
|
||
return false;
|
||
}
|
||
|
||
$attach = $data['attach'];
|
||
|
||
if (!empty($attach))
|
||
{
|
||
$attach = explode('======', $attach);
|
||
if (count($attach) == 2)
|
||
{
|
||
$app_id = (int)$attach[0];
|
||
$business_id = (int)$attach[1];
|
||
|
||
$businessList = Sql::select('syweb_business.*')
|
||
->from('syweb_business')
|
||
->where('syweb_business.id=?', $business_id)
|
||
->get($db, null);
|
||
if (!empty($businessList) && count($businessList) > 0)
|
||
{
|
||
$signkey = $businessList[0]["signkey"];
|
||
}
|
||
else
|
||
{
|
||
$msg = "找不到的商户信息。";
|
||
return false;
|
||
}
|
||
|
||
}
|
||
else
|
||
{
|
||
$msg = "attach参数格式不正确。";
|
||
return false;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
$msg = "attach参数不能为空";
|
||
return false;
|
||
}
|
||
|
||
$log = Sql::select('syweb_core_paylog.*')
|
||
->from('syweb_core_paylog')
|
||
->where('uniontid=?', $data['out_trade_no'])
|
||
->get($db, null);
|
||
if (!empty($log) && count($log) > 0)
|
||
{
|
||
$log = $log[0];
|
||
}
|
||
else
|
||
{
|
||
// 当指定的订单不存在时,则直接返回true
|
||
return true;
|
||
}
|
||
|
||
if (!empty($log) && $log['status'] == '0')
|
||
{
|
||
// 提前设置为已经支付成功状态
|
||
$recordTemp = array();
|
||
$recordTemp['status'] = '1';
|
||
$pdo = $db;
|
||
$pdo->beginTransaction();
|
||
Sql::update('syweb_core_paylog')->setArgs($recordTemp)->where('plid = ?', $log['plid'])->exec($pdo);
|
||
$pdo->commit();
|
||
|
||
$log['tag'] = iunserializer($log['tag']);
|
||
$callbackUrl = $log['tag']['notice_url']; // 回调地址
|
||
$param_mask = $log['tag']['param_mask']; // 回调参数格式
|
||
// 验证客户端逻辑处理
|
||
if (!empty($callbackUrl))
|
||
{
|
||
if (!empty($param_mask))
|
||
{
|
||
$callback_data = str_replace("%orderNo%", $log["tid"], $param_mask);
|
||
$callback_data = str_replace("%money%", $log["fee"], $callback_data);
|
||
|
||
$callback_data = str_replace("%outtradeNo%", $log["uniontid"], $callback_data);
|
||
$callback_data = str_replace("%transactionid%", $log['tag']['transaction_id'], $callback_data);
|
||
$callback_data = str_replace("%signkey%", $signkey, $callback_data);
|
||
|
||
$callback_response = ihttp_request($callbackUrl . "?" . $callback_data, "", false);
|
||
|
||
$callback_response = mb_convert_encoding($callback_response, "UTF-8");
|
||
|
||
//$begin_position = stripos($callback_response,"\r\n\r\n");
|
||
$begin_position = strstr($callback_response, "\r\n");
|
||
|
||
|
||
if ($begin_position >= 0)
|
||
{
|
||
$callback_response = substr($callback_response, $begin_position + 17);
|
||
}
|
||
else
|
||
{
|
||
$callback_response = "";
|
||
}
|
||
}
|
||
else
|
||
{
|
||
$callback_data['orderNo'] = $log["tid"];
|
||
$callback_data['out_trade_no'] = $log["uniontid"]; // 商家订单号
|
||
$callback_data['transaction_id'] = $log['tag']['transaction_id']; // 微信订单号
|
||
$callback_data['signkey'] = $signkey;
|
||
$callback_data = json_encode($callback_data);
|
||
$callback_response = ihttp_request($callbackUrl, $callback_data, false);
|
||
}
|
||
// 发送模板消息接口地址
|
||
|
||
|
||
if (!empty($callback_response) && !is_null(json_decode($callback_response)))
|
||
{
|
||
$callback_result = @json_decode($callback_response, true);
|
||
if ($callback_result['error'] != '0')
|
||
{
|
||
// 假如客户端返回非0,则表示逻辑处理失败,则将再次发起
|
||
$msg = "订单处理出错。";
|
||
return false;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
// 假如客户端返回非0,则表示支付失败
|
||
$msg = "订单处理出错。";
|
||
return false;
|
||
}
|
||
}
|
||
|
||
$log['tag']['transaction_id'] = $data['transaction_id'];
|
||
$log['transaction_id'] = $data['transaction_id'];
|
||
|
||
$record = array();
|
||
$record['status'] = '1';
|
||
$record['tag'] = iserializer($log['tag']);
|
||
|
||
$pdo = $db;
|
||
$pdo->beginTransaction();
|
||
Sql::update('syweb_core_paylog')->setArgs($record)->where('plid = ?', $log['plid'])->exec($pdo);
|
||
$pdo->commit();
|
||
}
|
||
|
||
return true;
|
||
}
|
||
}
|