insert([ 'trade_no' => $tradeNo, 'biz' => (string) ($order['biz'] ?? ''), 'biz_id' => (int) ($order['biz_id'] ?? 0), 'channel' => $channel, 'scene' => $scene, 'subject' => mb_substr((string) ($order['subject'] ?? ''), 0, 200), 'amount' => (float) ($order['amount'] ?? 0), 'status' => 0, 'attach' => mb_substr((string) json_encode($order['attach'] ?? [], JSON_UNESCAPED_UNICODE), 0, 500), 'create_at' => time(), 'update_at' => time(), ]); } catch (\Throwable $e) { trace('[paycenter] 流水写入失败:' . $e->getMessage(), 'error'); } } /** * 回调到达:流水不存在时按回调内容补建(业务未走插件下单 / 插件重装过)。 */ public static function touchNotify(array $data, string $channel): ?array { Schema::ensure(); $tradeNo = (string) ($data['trade_no'] ?? ''); if ($tradeNo === '') { return null; } $trade = self::find($tradeNo); if (!$trade) { try { Db::name('paycenter_trade')->insert([ 'trade_no' => $tradeNo, 'channel' => $channel, 'amount' => (float) ($data['amount'] ?? 0), 'gateway_no' => (string) ($data['gateway_no'] ?? ''), 'status' => 0, 'notify_at' => time(), 'create_at' => time(), 'update_at' => time(), ]); } catch (\Throwable $e) { trace('[paycenter] 回调补建流水失败:' . $e->getMessage(), 'error'); } return self::find($tradeNo); } try { Db::name('paycenter_trade')->where('trade_no', $tradeNo)->update([ 'notify_at' => time(), 'update_at' => time(), ]); } catch (\Throwable) { // 更新失败不影响发货 } return $trade; } /** 标记已支付,返回最新流水 */ public static function markPaid(array $data): ?array { $tradeNo = (string) ($data['trade_no'] ?? ''); if ($tradeNo === '') { return null; } try { Db::name('paycenter_trade')->where('trade_no', $tradeNo)->update([ 'status' => 1, 'gateway_no' => (string) ($data['gateway_no'] ?? ''), 'pay_at' => time(), 'notify_at' => time(), 'update_at' => time(), ]); } catch (\Throwable $e) { trace('[paycenter] 流水置已付失败:' . $e->getMessage(), 'error'); } return self::find($tradeNo); } public static function markRefunded(string $tradeNo): void { try { Db::name('paycenter_trade')->where('trade_no', $tradeNo)->update([ 'status' => 2, 'update_at' => time(), ]); } catch (\Throwable) { // 忽略 } } public static function find(string $tradeNo): ?array { if ($tradeNo === '') { return null; } try { $row = Db::name('paycenter_trade')->where('trade_no', $tradeNo)->find(); } catch (\Throwable) { return null; } return $row ? (array) $row : null; } }