新增检测支付回调,支付退款回调

This commit is contained in:
wucongxing 2023-08-01 13:31:06 +08:00
parent a1b0d6556c
commit 2068019145
2 changed files with 290 additions and 2 deletions

View File

@ -9,6 +9,8 @@ use App\Exception\BusinessException;
use App\Model\BasicLogisticsCompany;
use App\Model\Hospital;
use App\Model\MessageIm;
use App\Model\OrderDetection;
use App\Model\OrderDetectionRefund;
use App\Model\OrderInquiry;
use App\Model\OrderInquiryCoupon;
use App\Model\OrderInquiryRefund;
@ -247,7 +249,7 @@ class CallBackController extends AbstractController
// 验证订单支付状态
if (in_array($order_inquiry['inquiry_pay_status'], [1, 3, 4, 5, 6, 7])) {
// 支付状态1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款)
Log::getInstance()->error("队列执行失败原因:订单未支付");
Log::getInstance()->info("订单支付状态错误:当前为" . $order_inquiry['inquiry_pay_status']);
return $server->serve();
}
@ -265,7 +267,7 @@ class CallBackController extends AbstractController
if (empty($inquiry_refund_status)) {
// 错误,无退款状态
Log::getInstance()->error("队列执行失败原因:订单未支付");
Log::getInstance()->info("订单支付状态错误:未接收到退款状态");
return $this->wxPayErrorReturn("退款状态错误");
}
@ -1102,4 +1104,206 @@ class CallBackController extends AbstractController
);
}
/**
* 患者端检测支付回调
* @return ResponseInterface
* @throws \Throwable
*/
public function wxPayDetectionSuccessCallBack(): ResponseInterface
{
try {
// 处理支付结果事件
$WechatPay = new WechatPay(1, 3);
$app = $WechatPay->createApp();
$server = $app->getServer();
$message = $server->getRequestMessage();
if (empty($message)) {
return $this->response->withStatus(500)->withBody(new SwooleStream(strval(json_encode(['code' => 'ERROR', 'message' => "问诊微信支付回调数据为空"], JSON_UNESCAPED_UNICODE))));
}
// 验证推送消息签名
$app->getValidator()->validate($app->getRequest());
Log::getInstance()->info("检测微信支付回调数据:" . json_encode($message->toArray(), JSON_UNESCAPED_UNICODE));
if (empty($message['out_trade_no'])) {
Log::getInstance()->info("检测微信支付回调数据处理失败,缺少外部订单号");
return $server->serve();
}
// 查询订单
$params = array();
$params['detection_no'] = $message['out_trade_no'];
$order_detection = OrderDetection::getOne($params);
if (empty($order_detection)) {
Log::getInstance()->info("检测微信支付回调数据处理失败,无订单数据");
return $server->serve();
}
// 验证订单状态
if ($order_detection['detection_status'] != 1) {
// 检测订单状态(1:待支付 2:待绑定 3:检测中 4:检测完成 5:已取消)
Log::getInstance()->info("检测微信支付回调数据处理失败,订单状态当前为" . $order_detection['detection_status']);
return $server->serve();
}
// 支付状态无需验证,如第一次支付失败,会修改支付状态,再次支付时,会出现验证不通过的情况
// 修改支付状态
$data = array();
if ($message['trade_state'] == "SUCCESS") {
// 支付成功
$data['detection_pay_status'] = 2;
$data['pay_time'] = date('Y-m-d H:i:s', strtotime($message['success_time']));// 支付时间
$data['detection_status'] = 2;// 2:待绑定
if (empty($message['amount'])){
Log::getInstance()->error("检测微信支付回调数据处理失败:无支付金额");
return $this->wxPayErrorReturn("检测微信支付回调数据处理失败:无支付金额");
}
// 实际付款金额
$data['payment_amount_total'] = $message['amount']['payer_total'];
} elseif ($message['trade_state'] == "CLOSED") {
// 已关闭
$data['detection_pay_status'] = 6;
} elseif ($message['trade_state'] == "REVOKED") {
// 已撤销(付款码支付)
$data['detection_pay_status'] = 7;
} elseif ($message['trade_state'] == "USERPAYING") {
// 用户支付中(付款码支付)
$data['detection_pay_status'] = 3;
} elseif ($message['trade_state'] == "PAYERROR") {
// 支付失败(其他原因,如银行返回失败)
$data['detection_pay_status'] = 4;
}
$data['escrow_trade_no'] = $message['transaction_id'];
$data['updated_at'] = date('Y-m-d H:i:s', time());
$params = array();
$params['order_detection_id'] = $order_detection['order_detection_id'];
OrderDetection::editOrderDetection($params, $data);
} catch (\Exception $e) {
// 验证失败
Log::getInstance()->error("问诊微信支付回调数据处理失败:" . $e->getMessage());
return $this->wxPayErrorReturn($e->getMessage());
}
Log::getInstance()->info("检测微信支付回调处理成功");
return $server->serve();
}
/**
* 微信检测退款回调
* @return ResponseInterface
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws \Throwable
*/
public function wxPayDetectionRefundCallBack(): ResponseInterface
{
Db::beginTransaction();
try {
// 处理支付结果事件
$WechatPay = new WechatPay(1, 1);
$app = $WechatPay->createApp();
$server = $app->getServer();
$message = $server->getRequestMessage();
if (empty($message)) {
Db::rollBack();
return $this->response->withStatus(500)->withBody(new SwooleStream(strval(json_encode(['code' => 'ERROR', 'message' => "回调数据为空"], JSON_UNESCAPED_UNICODE))));
}
// 验证推送消息签名
$app->getValidator()->validate($app->getRequest());
Log::getInstance()->info("微信退款回调数据:" . json_encode($message->toArray(), JSON_UNESCAPED_UNICODE));
if (empty($message['out_trade_no'])) {
Log::getInstance()->info("微信退款回调数据错误");
return $server->serve();
}
// 验证订单数据
$params = array();
$params['detection_no'] = $message['out_trade_no'];
$order_detection = OrderDetection::getOne($params);
if (empty($order_detection)) {
Log::getInstance()->info("非法订单");
return $server->serve();
}
// 验证订单状态
if ($order_detection['detection_status'] == 1) {
// 检测订单状态(1:待支付 2:待绑定 3:检测中 4:检测完成 5:已取消)
Log::getInstance()->info("订单状态错误:当前为" . $order_detection['detection_status']);
return $server->serve();
}
// 验证订单退款状态
if ($order_detection['detection_refund_status'] == 3) {
// 检测订单退款状态0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常)
Log::getInstance()->info("订单退款状态错误:当前为" . $order_detection['detection_refund_status']);
return $server->serve();
}
// 验证订单支付状态
if (in_array($order_detection['detection_pay_status'], [1, 3, 4, 5, 6, 7])) {
// 支付状态1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款)
Log::getInstance()->info("订单支付状态错误:当前为" . $order_detection['detection_pay_status']);
return $server->serve();
}
// 退款状态
if ($message['refund_status'] == "SUCCESS") {
// 退款成功
$detection_refund_status = 3;
} elseif ($message['refund_status'] == "CLOSED") {
// 退款关闭
$detection_refund_status = 5;
} elseif ($message['refund_status'] == "ABNORMAL") {
// 退款异常
$detection_refund_status = 6;
}
if (empty($detection_refund_status)) {
// 错误,无退款状态
Log::getInstance()->error("队列执行失败原因:订单未支付");
return $this->wxPayErrorReturn("退款状态错误");
}
// 修改订单
$data = array();
$data['detection_refund_status'] = $detection_refund_status;
$params = array();
$params['order_detection_id'] = $order_detection['order_detection_id'];
OrderDetection::editOrderDetection($params, $data);
// 修改退款订单
$data = array();
$data['detection_refund_status'] = $detection_refund_status;
$data['success_time'] = $message['success_time'];
$params = array();
$params['order_detection_id'] = $order_detection['order_detection_id'];
OrderDetectionRefund::edit($params, $data);
Db::commit();
} catch (\Exception $e) {
// 验证失败
Db::rollBack();
Log::getInstance()->error("微信支付回调数据验证失败:" . $e->getMessage());
return $this->wxPayErrorReturn($e->getMessage());
}
Log::getInstance()->info("微信退款回调处理成功");
return $server->serve();
}
}

View File

@ -0,0 +1,84 @@
<?php
declare(strict_types=1);
namespace App\Model;
use Hyperf\Database\Model\Collection;
use Hyperf\Snowflake\Concern\Snowflake;
/**
* @property int $detection_refund_id 主键id
* @property int $order_detection_id 订单-检测id
* @property int $patient_id 患者id
* @property string $detection_no 系统订单编号
* @property string $detection_refund_no 系统退款编号
* @property string $refund_id 第三方退款单号
* @property int $detection_refund_status 检测订单退款状态0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常)
* @property string $refund_total 退款金额
* @property string $refund_reason 退款原因
* @property string $success_time 退款成功时间
* @property \Carbon\Carbon $created_at 创建时间
* @property \Carbon\Carbon $updated_at 修改时间
*/
class OrderDetectionRefund extends Model
{
use Snowflake;
/**
* The table associated with the model.
*/
protected ?string $table = 'order_detection_refund';
/**
* The attributes that are mass assignable.
*/
protected array $fillable = ['detection_refund_id', 'order_detection_id', 'patient_id', 'detection_no', 'detection_refund_no', 'refund_id', 'detection_refund_status', 'refund_total', 'refund_reason', 'success_time', 'created_at', 'updated_at'];
protected string $primaryKey = "detection_refund_id";
/**
* 获取信息-单条
* @param array $params
* @param array $fields
* @return object|null
*/
public static function getOne(array $params, array $fields = ['*']): object|null
{
return self::where($params)->first($fields);
}
/**
* 获取数据-
* @param array $params
* @param array $fields
* @return Collection|array
*/
public static function getList(array $params = [], array $fields = ['*']): Collection|array
{
return self::where($params)->get($fields);
}
/**
* 新增-批量
* @param array $data 新增数据
* @return \Hyperf\Database\Model\Model|OrderDetectionRefund
*/
public static function add(array $data): \Hyperf\Database\Model\Model|OrderDetectionRefund
{
return self::create($data);
}
/**
* 修改-批量
* @param array $params
* @param array $data
* @return int
*/
public static function edit(array $params = [], array $data = []): int
{
return self::where($params)->update($data);
}
}