|
|
<?php
|
|
|
|
|
|
namespace app\money\controller\api;
|
|
|
|
|
|
use app\mall\model\MallOrder;
|
|
|
use app\money\model\MoneyOrder;
|
|
|
|
|
|
use EasyWeChat\Factory;
|
|
|
use think\facade\Db;
|
|
|
use think\facade\Request;
|
|
|
|
|
|
class Callback extends Base
|
|
|
{
|
|
|
|
|
|
/**
|
|
|
* 微信支付回调 商城订单
|
|
|
* @date 2022-10-28
|
|
|
*/
|
|
|
public function weixinNotify()
|
|
|
{
|
|
|
$uid = input('param.uid');
|
|
|
defined('UID') || define('UID', $uid);
|
|
|
$user_agent = input('param.agent');
|
|
|
|
|
|
$wxpay_param = get_wxpay_config_with_weixin($user_agent);
|
|
|
$wxpay_class = new \tencent\wechat\pay\Wxpay($wxpay_param);
|
|
|
|
|
|
// 获得类
|
|
|
$app = $wxpay_class->getPayClass();
|
|
|
|
|
|
$response = $app->handlePaidNotify(function ($message, $fail) use ($wxpay_class) {
|
|
|
// 记录日志
|
|
|
platformLog($message, true, 'easywechat_wxpay_notify_uid_' . UID);
|
|
|
|
|
|
$order_model = new MoneyOrder();
|
|
|
$order_service = new \app\money\service\Order();
|
|
|
|
|
|
// 商户订单号
|
|
|
$order_number = $message['out_trade_no'];
|
|
|
|
|
|
// 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
|
|
|
$order = $order_model->getOneData([
|
|
|
['order_number', '=', $order_number]
|
|
|
], 'id,order_number,is_pay,pay_money');
|
|
|
|
|
|
// 如果订单不存在 或者 订单已经支付过了
|
|
|
if (empty($order) || $order['is_pay'] == 1) {
|
|
|
// 告诉微信,我已经处理完了,订单没找到,别再通知我了
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
// 在这里调用微信的【订单查询】接口查一下该笔订单的情况,确认是已经支付
|
|
|
$result = $wxpay_class->queryByOutTradeNumber($order_number);
|
|
|
if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') {
|
|
|
if ($result['trade_state'] !== 'SUCCESS') {
|
|
|
return $fail('订单还未支付');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// return_code 表示通信状态,不代表支付状态
|
|
|
if ($message['return_code'] === 'SUCCESS' && $message['result_code'] === 'SUCCESS') {
|
|
|
Db::startTrans();
|
|
|
// 订单支付成功之后
|
|
|
$result = $order_service->afterOrderPaySuccess($order['id'], $message['transaction_id']);
|
|
|
if ($result['code'] != 0) {
|
|
|
Db::rollback();
|
|
|
return $fail($result['msg']);
|
|
|
}
|
|
|
Db::commit();
|
|
|
|
|
|
// 统计系统 - 新增交易流水
|
|
|
$statistics_class = new \jucheng\tongji\Statistics();
|
|
|
$statistics_class->updateTurnoverMoney($order['pay_money']);
|
|
|
|
|
|
// 告诉微信,我已经处理完了,订单没找到,别再通知我了
|
|
|
return true;
|
|
|
} else {
|
|
|
return $fail('通信失败,请稍后再通知我');
|
|
|
}
|
|
|
});
|
|
|
|
|
|
// return $response;
|
|
|
$response->send();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 支付宝支付回调 商城订单
|
|
|
* @date 2022-11-01
|
|
|
*/
|
|
|
public function alipayNotify()
|
|
|
{
|
|
|
$uid = input('param.uid');
|
|
|
defined('UID') || define('UID', $uid);
|
|
|
$user_agent = input('param.agent');
|
|
|
|
|
|
$data = input('post.');
|
|
|
|
|
|
$order_model = new MoneyOrder();
|
|
|
$order_service = new \app\money\service\Order();
|
|
|
|
|
|
switch ($user_agent) {
|
|
|
// APP项目
|
|
|
case 'app':
|
|
|
$ali_pay_param = get_app_alipay_config();
|
|
|
break;
|
|
|
// 小程序项目
|
|
|
case 'mp_alipay':
|
|
|
$ali_pay_param = get_mp_alipay_config();
|
|
|
break;
|
|
|
// 其他
|
|
|
default:
|
|
|
$ali_pay_param = [];
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
$ali_pay_class = new \ali\alipay\pay\AliPay($ali_pay_param);
|
|
|
|
|
|
// 异步通知验签
|
|
|
$result = $ali_pay_class->verifyNotify($data);
|
|
|
|
|
|
// 验签失败
|
|
|
if (!$result) {
|
|
|
exit;
|
|
|
}
|
|
|
|
|
|
// TODO 校验通知中的 seller_id(或者 seller_email ) 是否为 out_trade_no 这笔单据的对应的操作方(有的时候,一个商户可能有多个seller_id/seller_email)
|
|
|
|
|
|
// 在支付宝的业务通知中,只有交易通知状态为 TRADE_SUCCESS(支付成功) 或 TRADE_FINISHED(交易完成) 时,支付宝才会认定为买家付款成功。
|
|
|
if ($data['trade_status'] != 'TRADE_SUCCESS' && $data['trade_status'] != 'TRADE_FINISHED') {
|
|
|
exit;
|
|
|
}
|
|
|
|
|
|
// 订单编号
|
|
|
$order_number = $data['out_trade_no'];
|
|
|
|
|
|
// 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
|
|
|
$order = $order_model->getOneData([
|
|
|
['order_number', '=', $order_number]
|
|
|
],'id,order_number');
|
|
|
// 商户需要验证该通知数据中的 out_trade_no 是否为商户系统中创建的订单号
|
|
|
if (empty($order)) {
|
|
|
exit;
|
|
|
}
|
|
|
// 订单已经支付过了
|
|
|
if ($order['is_pay'] == 1) {
|
|
|
// 告诉支付宝,我已经处理完了,别再通知我了
|
|
|
echo "success";
|
|
|
exit;
|
|
|
}
|
|
|
|
|
|
Db::startTrans();
|
|
|
$res = $order_service->afterOrderPaySuccess($order['id'], $data['trade_no']);
|
|
|
if ($res['code'] != 0) {
|
|
|
Db::rollback();
|
|
|
return json($res);
|
|
|
}
|
|
|
Db::commit();
|
|
|
|
|
|
// 告诉支付宝,我已经处理完了,别再通知我了
|
|
|
echo "success";
|
|
|
|
|
|
exit;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 付呗支付回调 商城订单
|
|
|
* @date 2022-11-03
|
|
|
*/
|
|
|
public function fubeiNotify()
|
|
|
{
|
|
|
$uid = input('param.uid');
|
|
|
defined('UID') || define('UID', $uid);
|
|
|
$user_agent = input('param.agent');
|
|
|
|
|
|
// 获得参数
|
|
|
$data = input('post.');
|
|
|
$biz_data = json_decode($data['data'], true);
|
|
|
$biz_data['attach'] = json_decode($biz_data['attach'], true);
|
|
|
|
|
|
$fubei_param = get_fubei_config();
|
|
|
|
|
|
$notify_class = new \fubei\pay\Notify($fubei_param);
|
|
|
$order_class = new \fubei\pay\Order($fubei_param);
|
|
|
$order_model = new MoneyOrder();
|
|
|
$order_service = new \app\money\service\Order();
|
|
|
|
|
|
|
|
|
// 校验参数
|
|
|
$result = $notify_class->notify($data);
|
|
|
|
|
|
// 验证签名失败
|
|
|
if (!$result) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// 获取订单编号
|
|
|
$order_number = $biz_data['attach']['order_number'];
|
|
|
|
|
|
// 在这里调用付呗的【订单查询】接口查一下该笔订单的情况,确认是已经支付
|
|
|
$result = $order_class->orderQuery($order_number);
|
|
|
// 有报错
|
|
|
if ($result['result_code'] != 200) {
|
|
|
return false;
|
|
|
}
|
|
|
// 没有支付成功
|
|
|
if ($result['data']['order_status'] !== 'SUCCESS') {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// 找到订单
|
|
|
$order = $order_model->getOneData([
|
|
|
['order_number', '=', $order_number]
|
|
|
], 'id,order_number,is_pay,pay_money');
|
|
|
|
|
|
// 没有订单,返回异常
|
|
|
if (empty($order)) {
|
|
|
exit;
|
|
|
}
|
|
|
|
|
|
// 数据库存储的支付金额与一步通知的金额不符,返回异常
|
|
|
if ($order['pay_money'] != $biz_data['total_amount']) {
|
|
|
exit;
|
|
|
}
|
|
|
|
|
|
// 订单已经支付过了,告诉付呗,我已经处理完了,别再通知我了
|
|
|
if ($order['is_pay'] == 1) {
|
|
|
exit('success');
|
|
|
}
|
|
|
|
|
|
// 订单支付成功之后的处理
|
|
|
Db::startTrans();
|
|
|
// 订单支付成功之后
|
|
|
$result = $order_service->afterOrderPaySuccess($order['id'], $biz_data['ins_order_sn']);
|
|
|
if ($result['code'] != 0) {
|
|
|
Db::rollback();
|
|
|
}
|
|
|
Db::commit();
|
|
|
|
|
|
// 统计系统 - 新增交易流水
|
|
|
$statistics_class = new \jucheng\tongji\Statistics();
|
|
|
$statistics_class->updateTurnoverMoney($order['pay_money']);
|
|
|
|
|
|
// 告诉付呗,我已经处理完了,别再通知我了
|
|
|
exit('success');
|
|
|
}
|
|
|
}
|