272 行
4.8 KiB
PHP
272 行
4.8 KiB
PHP
<?php
|
|
|
|
|
|
|
|
namespace addons\shopro_huifu\library;
|
|
|
|
|
|
|
|
use think\Db;
|
|
|
|
use addons\shopro\model\Order;
|
|
|
|
|
|
|
|
class HuifuQueryService
|
|
|
|
{
|
|
|
|
protected $client;
|
|
|
|
protected $config;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->config = HuifuConfig::getInstance();
|
|
|
|
$this->client = new HuifuClient();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function queryByOrderSn($orderSn)
|
|
|
|
{
|
|
|
|
SdkLoader::requireRequest('v4/payment/TradePaymentScanpayQueryRequest.php');
|
|
|
|
$ext = Db::name('shopro_huifu_pay_ext')->where('order_sn', $orderSn)->find();
|
|
|
|
if (!$ext) {
|
|
|
|
$ext = Db::name('shopro_huifu_pay_ext')->where('req_seq_id', $orderSn)->find();
|
|
|
|
}
|
|
|
|
|
|
|
|
$reqDate = $ext['req_date'] ?? date('Ymd');
|
|
|
|
$reqSeqId = $ext['req_seq_id'] ?? $orderSn;
|
|
|
|
|
|
|
|
return $this->queryByReq($reqDate, $reqSeqId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function queryByReq($reqDate, $reqSeqId)
|
|
|
|
{
|
|
|
|
SdkLoader::requireRequest('v4/payment/TradePaymentScanpayQueryRequest.php');
|
|
|
|
|
|
|
|
$request = new \BsPaySdk\request\v4\payment\TradePaymentScanpayQueryRequest();
|
|
|
|
$request->setHuifuId($this->config->get('huifu_id'));
|
|
|
|
$request->setReqDate($reqDate);
|
|
|
|
$request->setReqSeqId($reqSeqId);
|
|
|
|
|
|
|
|
$result = $this->client->payment()->query($request);
|
|
|
|
return $this->client->parseResponse($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function compensateByOrderSn($orderSn)
|
|
|
|
{
|
|
|
|
$orderSn = trim((string) $orderSn);
|
|
|
|
if ($orderSn === '') {
|
|
|
|
throw new \RuntimeException('order_sn 不能为空');
|
|
|
|
}
|
|
|
|
|
|
|
|
$order = Order::where('order_sn', $orderSn)->find();
|
|
|
|
if (!$order) {
|
|
|
|
$handler = new HuifuNotifyHandler();
|
|
|
|
$order = $handler->resolveOrder($orderSn);
|
|
|
|
}
|
|
|
|
if (!$order) {
|
|
|
|
throw new \RuntimeException('未找到商城订单: ' . $orderSn);
|
|
|
|
}
|
|
|
|
if ($order['status'] > 0) {
|
|
|
|
return ['ok' => true, 'message' => '订单已是已支付状态', 'order_id' => $order['id']];
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = $this->queryByOrderSn($orderSn);
|
|
|
|
if (($data['trans_stat'] ?? '') !== 'S') {
|
|
|
|
return [
|
|
|
|
'ok' => false,
|
|
|
|
'message' => '汇付交易未成功,trans_stat=' . ($data['trans_stat'] ?? ''),
|
|
|
|
'data' => $data,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$handler = new HuifuNotifyHandler();
|
|
|
|
$handler->applyPaidOrder($order, $data, (string) ($data['req_seq_id'] ?? $orderSn));
|
|
|
|
|
|
|
|
return [
|
|
|
|
'ok' => true,
|
|
|
|
'message' => '查单补偿成功,订单已更新为已支付',
|
|
|
|
'order_id' => $order['id'],
|
|
|
|
'req_seq_id' => $data['req_seq_id'] ?? $orderSn,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function compensatePending()
|
|
|
|
{
|
|
|
|
$rows = Db::name('shopro_huifu_pay_ext')
|
|
|
|
->where('trans_stat', 'in', ['P', ''])
|
|
|
|
->where('createtime', '<', time() - 60)
|
|
|
|
->limit(50)
|
|
|
|
->select();
|
|
|
|
|
|
|
|
$results = ['ok' => 0, 'fail' => 0];
|
|
|
|
foreach ($rows as $ext) {
|
|
|
|
try {
|
|
|
|
$data = $this->queryByOrderSn($ext['order_sn']);
|
|
|
|
if (($data['trans_stat'] ?? '') === 'S') {
|
|
|
|
$order = Order::where('id', $ext['order_id'])->find();
|
|
|
|
if (!$order) {
|
|
|
|
$order = Order::where('order_sn', $ext['order_sn'])->find();
|
|
|
|
}
|
|
|
|
if ($order && $order['status'] <= 0) {
|
|
|
|
$handler = new HuifuNotifyHandler();
|
|
|
|
$handler->applyPaidOrder($order, $data, (string) ($data['req_seq_id'] ?? $ext['order_sn']));
|
|
|
|
$results['ok']++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
HuifuPayService::savePayExt($ext['order_id'], $ext['order_sn'], [
|
|
|
|
'trans_stat' => $data['trans_stat'] ?? 'P',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$results['fail']++;
|
|
|
|
\think\Log::error('[shopro_huifu] compensate fail: ' . $ext['order_sn'] . ' ' . $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function closeOrder($orderSn)
|
|
|
|
{
|
|
|
|
SdkLoader::requireRequest('v4/payment/TradePaymentScanpayCloseRequest.php');
|
|
|
|
$ext = Db::name('shopro_huifu_pay_ext')->where('order_sn', $orderSn)->find();
|
|
|
|
if (!$ext) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$request = new \BsPaySdk\request\v4\payment\TradePaymentScanpayCloseRequest();
|
|
|
|
$request->setHuifuId($this->config->get('huifu_id'));
|
|
|
|
$request->setReqDate($ext['req_date']);
|
|
|
|
$request->setReqSeqId($ext['req_seq_id']);
|
|
|
|
$result = $this->client->payment()->close($request);
|
|
|
|
return $this->client->parseResponse($result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|