first commit

This commit is contained in:
编码猿
2024-09-27 01:32:49 +08:00
commit 28ebe05a64
7399 changed files with 1174529 additions and 0 deletions

View File

@@ -0,0 +1,184 @@
<?php
namespace wxpayNew\lib;
use app\common\service\PayService;
use think\Exception;
use think\facade\Config;
use think\facade\Log;
use wxpayNew\WxPayConfig;
require_once dirname(__FILE__).'/WxPay.Data.php';
/**
*
* 回调基础类
* @author widyhu
*
*/
class WxPayNotify extends \WxPayNotifyReply
{
private $config = null;
/**
*
* 回调入口
* @param bool $needSign 是否需要签名返回
*/
final public function Handle($config, $needSign = true)
{
$this->config = $config;
$msg = "OK";
//当返回false的时候表示notify中调用NotifyCallBack回调失败获取签名校验失败此时直接回复失败
$result = WxPayApi::notify($config, array($this, 'NotifyCallBack'), $msg);
if($result == false){
$this->SetReturn_code("FAIL");
$this->SetReturn_msg($msg);
$this->ReplyNotify(false);
return;
} else {
//该分支在成功回调到NotifyCallBack方法处理完成之后流程
$this->SetReturn_code("SUCCESS");
$this->SetReturn_msg("OK");
}
$this->ReplyNotify($needSign);
}
/**
*
* 回调方法入口,子类可重写该方法
//TODO 1、进行参数校验
//TODO 2、进行签名验证
//TODO 3、处理业务逻辑
* 注意:
* 1、微信回调超时时间为2s建议用户使用异步处理流程确认成功之后立刻回复微信服务器
* 2、微信服务器在调用失败或者接到回包为非确认包的时候会发起重试需确保你的回调是可以重入
* @param WxPayNotifyResults $objData 回调解释出的参数
* @param WxPayConfigInterface $config
* @param string $msg 如果回调处理失败,可以将错误信息输出到该方法
* @return true回调出来完成不需要继续回调false回调处理未完成需要继续回调
*/
public function NotifyProcess($objData, $config, &$msg)
{
//TODO 用户基础该类之后需要重写该方法成功的时候返回true失败返回false
$data = $objData->GetValues();
//TODO 1、进行参数校验
if(!array_key_exists("return_code", $data)
||(array_key_exists("return_code", $data) && $data['return_code'] != "SUCCESS")) {
//TODO失败,不是支付成功的通知
//如果有需要可以做失败时候的一些清理处理,并且做一些监控
$msg = "异常异常";
return false;
}
if(!array_key_exists("transaction_id", $data)){
$msg = "输入参数不正确";
return false;
}
//TODO 2、进行签名验证
try {
$checkResult = $objData->CheckSign($config);
if($checkResult == false){
//签名错误
Log::ERROR("签名错误...");
return false;
}
} catch(Exception $e) {
Log::ERROR(json_encode($e));
}
//通过商户订单号out_trade_no获取订单详情
//查询订单,判断订单真实性
if(!$this->Queryorder($data["transaction_id"])){
$msg = "订单查询失败";
return false;
}
//TODO 3、处理业务逻辑
Log::info("业务处理逻辑开始:" . json_encode($data));
//商户订单号
$out_trade_no = $data['out_trade_no'];
//微信交易号
$trade_no = $data['transaction_id'];
//接受订单支付金额
$money = $data['total_fee'] / 100;
//如果支付出错,则做退款信息返回
$returnData = [];
$returnData['out_trade_no'] = $out_trade_no;
$returnData['trade_no'] = $trade_no;
$returnData['money'] = $money;
return model(PayService::class)->wxPayCallback($returnData);
}
/**
*
* 业务可以继承该方法打印XML方便定位.
* @param string $xmlData 返回的xml参数
*
**/
public function LogAfterProcess($xmlData)
{
return;
}
/**
*
* notify回调方法该方法中需要赋值需要输出的参数,不可重写
* @param array $data
* @return true回调出来完成不需要继续回调false回调处理未完成需要继续回调
*/
final public function NotifyCallBack($data)
{
$msg = "OK";
$result = $this->NotifyProcess($data, $this->config, $msg);
if($result == true){
$this->SetReturn_code("SUCCESS");
$this->SetReturn_msg("OK");
} else {
$this->SetReturn_code("FAIL");
$this->SetReturn_msg($msg);
}
return $result;
}
/**
*
* 回复通知
* @param bool $needSign 是否需要签名输出
*/
final private function ReplyNotify($needSign = true)
{
//如果需要签名
if($needSign == true &&
$this->GetReturn_code() == "SUCCESS")
{
$this->SetSign($this->config);
}
$xml = $this->ToXml();
$this->LogAfterProcess($xml);
WxpayApi::replyNotify($xml);
}
//查询订单
public function Queryorder($transaction_id)
{
$input = new \WxPayOrderQuery();
$input->SetTransaction_id($transaction_id);
$config = Config::pull('wxpay');
$wxPayConfig = new WxPayConfig($config);
$result = WxPayApi::orderQuery($wxPayConfig, $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;
}
}