文件
xionggeshop/addons/shopro_wechat_trade/library/SyncLogger.php

74 行
3.0 KiB
PHP

<?php
namespace addons\shopro_wechat_trade\library;
use think\Db;
class SyncLogger
{
public static function create($order, $syncType, array $payload = [], $orderItemId = 0, $deliveryMode = 1)
{
$order = is_object($order) && method_exists($order, 'toArray') ? $order->toArray() : (array) $order;
$now = time();
$id = Db::name('shopro_wechat_trade_sync')->insertGetId([
'order_id' => intval($order['id'] ?? 0),
'order_sn' => $order['order_sn'] ?? '',
'order_item_id' => intval($orderItemId),
'sync_type' => $syncType,
'status' => 'pending',
'delivery_mode' => intval($deliveryMode),
'request_payload' => json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
'retry_count' => 0,
'createtime' => $now,
'updatetime' => $now,
]);
return $id;
}
public static function markSuccess($logId, array $response, array $request = null)
{
$update = [
'status' => 'success',
'response_payload' => json_encode($response, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
'errcode' => 0,
'errmsg' => 'ok',
'updatetime' => time(),
];
if ($request !== null) {
$update['request_payload'] = json_encode($request, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}
Db::name('shopro_wechat_trade_sync')->where('id', $logId)->update($update);
}
public static function markFailed($logId, \Exception $e, array $response = [], array $request = null)
{
$row = Db::name('shopro_wechat_trade_sync')->where('id', $logId)->find();
$retryCount = intval($row['retry_count'] ?? 0) + 1;
$interval = WxTradeConfig::getInstance()->getRetryInterval();
$maxRetry = WxTradeConfig::getInstance()->getMaxRetry();
$update = [
'status' => $retryCount >= $maxRetry ? 'failed' : 'pending',
'response_payload' => json_encode($response, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
'errcode' => intval($response['errcode'] ?? -1),
'errmsg' => mb_substr($e->getMessage(), 0, 250),
'retry_count' => $retryCount,
'next_retry_time' => time() + $interval,
'updatetime' => time(),
];
if ($request !== null) {
$update['request_payload'] = json_encode($request, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}
Db::name('shopro_wechat_trade_sync')->where('id', $logId)->update($update);
}
public static function hasSuccessfulConfirm($orderId)
{
return (bool) Db::name('shopro_wechat_trade_sync')
->where('order_id', $orderId)
->where('sync_type', 'confirm')
->where('status', 'success')
->find();
}
}