getMessage(); self::refundFailHandle($refundRecordId, $e->getMessage()); return false; } } /** * @notes 退款前校验 * @param $refundAmount * @throws \Exception * @author 段誉 * @date 2023/2/28 16:27 */ public static function refundBeforeCheck($refundAmount) { if ($refundAmount <= 0) { throw new \Exception('订单金额异常'); } } /** * @notes 微信支付退款 * @param $order * @param $refundAmount * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException * @author 段誉 * @date 2023/2/28 17:19 */ public static function wechatPayRefund($order, $refundAmount) { // 发起退款。 若发起退款请求返回明确错误,退款日志和记录标记状态为退款失败 // 退款日志及记录的成功状态目前统一由定时任务查询退款结果为退款成功后才标记成功 // 也可通过设置退款回调,在退款回调时处理退款记录状态为成功 (new WeChatPayService($order['order_terminal']))->refund([ 'transaction_id' => $order['transaction_id'], 'refund_sn' => self::$refundLog['sn'], 'refund_amount' => $refundAmount,// 退款金额 'total_amount' => $order['order_amount'],// 订单金额 ]); } /** * @notes 退款请求失败处理 * @param $refundRecordId * @author 段誉 * @date 2023/2/28 16:00 * @remark 【微信,支付宝】退款接口请求失败时,更新退款记录及日志为失败,在退款记录重新发起 */ public static function refundFailHandle($refundRecordId, $msg) { // 更新退款日志记录 RefundLog::update([ 'id' => self::$refundLog['id'], 'refund_status' => RefundEnum::REFUND_ERROR, 'refund_msg' => $msg, ]); // 更新退款记录状态为退款失败 RefundRecord::update([ 'id' => $refundRecordId, 'refund_status' => RefundEnum::REFUND_ERROR, 'refund_msg' => $msg, ]); } /** * @notes 退款日志 * @param $order * @param $refundRecordId * @param $refundAmount * @param $handleId * @param int $refundStatus * @author 段誉 * @date 2023/2/28 15:29 */ public static function log($order, $refundRecordId, $refundAmount, $handleId, $refundStatus = RefundEnum::REFUND_ING) { $sn = generate_sn(RefundLog::class, 'sn'); self::$refundLog = RefundLog::create([ 'sn' => $sn, 'record_id' => $refundRecordId, 'user_id' => $order['user_id'], 'handle_id' => $handleId, 'order_amount' => $order['order_amount'], 'refund_amount' => $refundAmount, 'refund_status' => $refundStatus ]); } }