103 行
3.7 KiB
PHP
103 行
3.7 KiB
PHP
<?php
|
|
|
|
namespace addons\shopro_huifu\library;
|
|
|
|
/**
|
|
* 从汇付支付通知/查单响应中提取微信侧交易标识(供购物订单/发货同步使用)
|
|
*/
|
|
class HuifuWxNotifyHelper
|
|
{
|
|
public static function extract(array $data)
|
|
{
|
|
$wxResp = $data['wx_response'] ?? [];
|
|
if (is_string($wxResp)) {
|
|
$decoded = json_decode($wxResp, true);
|
|
$wxResp = is_array($decoded) ? $decoded : [];
|
|
}
|
|
if (!is_array($wxResp)) {
|
|
$wxResp = [];
|
|
}
|
|
|
|
$wxTransactionId = trim((string) ($wxResp['transaction_id'] ?? ''));
|
|
if ($wxTransactionId === '') {
|
|
$wxTransactionId = trim((string) ($data['out_trans_id'] ?? ''));
|
|
}
|
|
|
|
$wxSubOpenid = trim((string) ($wxResp['sub_openid'] ?? ''));
|
|
if ($wxSubOpenid === '') {
|
|
$wxSubOpenid = trim((string) ($data['sub_openid'] ?? ''));
|
|
}
|
|
|
|
return [
|
|
'wx_transaction_id' => $wxTransactionId,
|
|
'wx_sub_mch_id' => trim((string) ($wxResp['sub_mch_id'] ?? '')),
|
|
'wx_sub_openid' => $wxSubOpenid,
|
|
'hf_seq_id' => trim((string) ($data['hf_seq_id'] ?? '')),
|
|
];
|
|
}
|
|
|
|
public static function encodeExtra(array $wxFields)
|
|
{
|
|
return json_encode([
|
|
'wx_transaction_id' => $wxFields['wx_transaction_id'] ?? '',
|
|
'wx_sub_mch_id' => $wxFields['wx_sub_mch_id'] ?? '',
|
|
'wx_sub_openid' => $wxFields['wx_sub_openid'] ?? '',
|
|
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
|
}
|
|
|
|
public static function decodeExtra($extra)
|
|
{
|
|
if (!$extra) {
|
|
return [];
|
|
}
|
|
if (is_array($extra)) {
|
|
return $extra;
|
|
}
|
|
$decoded = json_decode($extra, true);
|
|
return is_array($decoded) ? $decoded : [];
|
|
}
|
|
|
|
public static function resolveTransactionId(array $wxFields)
|
|
{
|
|
$wxTxn = trim((string) ($wxFields['wx_transaction_id'] ?? ''));
|
|
if ($wxTxn !== '') {
|
|
return $wxTxn;
|
|
}
|
|
return trim((string) ($wxFields['hf_seq_id'] ?? ''));
|
|
}
|
|
|
|
/**
|
|
* Shopro 订单 payment_json 列为 varchar(2500),汇付完整 notify 会超长
|
|
*/
|
|
public static function buildPaymentJson(array $data, array $wxFields = null)
|
|
{
|
|
$wxFields = $wxFields ?: self::extract($data);
|
|
|
|
$snapshot = [
|
|
'req_seq_id' => trim((string) ($data['req_seq_id'] ?? '')),
|
|
'hf_seq_id' => trim((string) ($data['hf_seq_id'] ?? '')),
|
|
'trans_stat' => trim((string) ($data['trans_stat'] ?? '')),
|
|
'trans_amt' => trim((string) ($data['trans_amt'] ?? '')),
|
|
'resp_code' => trim((string) ($data['resp_code'] ?? '')),
|
|
'out_trans_id' => trim((string) ($data['out_trans_id'] ?? '')),
|
|
'transaction_id' => trim((string) ($wxFields['wx_transaction_id'] ?? '')),
|
|
'sub_openid' => trim((string) ($wxFields['wx_sub_openid'] ?? '')),
|
|
'huifu_id' => trim((string) ($data['huifu_id'] ?? '')),
|
|
'pay_type' => 'huifu_wechat',
|
|
'wx_response' => array_filter([
|
|
'transaction_id' => trim((string) ($wxFields['wx_transaction_id'] ?? '')),
|
|
'sub_mch_id' => trim((string) ($wxFields['wx_sub_mch_id'] ?? '')),
|
|
'sub_openid' => trim((string) ($wxFields['wx_sub_openid'] ?? '')),
|
|
]),
|
|
];
|
|
|
|
$json = json_encode($snapshot, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
|
if (strlen($json) <= 2400) {
|
|
return $json;
|
|
}
|
|
|
|
unset($snapshot['wx_response']);
|
|
return json_encode($snapshot, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
|
}
|
|
}
|