|
|
<?php
|
|
|
|
|
|
namespace app\integral\controller\api;
|
|
|
|
|
|
use app\integral\model\IntegralOrder;
|
|
|
use think\facade\Db;
|
|
|
|
|
|
class Callback extends Base
|
|
|
{
|
|
|
|
|
|
/**
|
|
|
* 微信支付回调
|
|
|
* @date 2022-11-20
|
|
|
*/
|
|
|
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 IntegralOrder();
|
|
|
$order_service = new \app\integral\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();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 支付宝支付回调
|
|
|
*/
|
|
|
public function alipayNotify()
|
|
|
{
|
|
|
$uid = input('param.uid');
|
|
|
defined('UID') || define('UID', $uid);
|
|
|
$user_agent = input('param.agent');
|
|
|
|
|
|
$data = input('post.');
|
|
|
|
|
|
$order_model = new IntegralOrder();
|
|
|
$order_service = new \app\integral\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;
|
|
|
// H5项目
|
|
|
case 'h5':
|
|
|
$ali_pay_param = get_h5_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'];
|
|
|
|
|
|
// 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单,商户需要验证该通知数据中的 out_trade_no 是否为商户系统中创建的订单号
|
|
|
$order = $order_model->getOneData([
|
|
|
['order_number', '=', $order_number]
|
|
|
], 'id,order_number,is_pay,pay_money');
|
|
|
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();
|
|
|
|
|
|
// 统计系统 - 新增交易流水
|
|
|
$statistics_class = new \jucheng\tongji\Statistics();
|
|
|
$result = $statistics_class->updateTurnoverMoney($order['pay_money']);
|
|
|
|
|
|
// 告诉支付宝,我已经处理完了,别再通知我了
|
|
|
echo "success";
|
|
|
|
|
|
exit;
|
|
|
}
|
|
|
}
|