feat: 初始化雄戈商城服务端代码与Webhook自动部署配置
这个提交包含在:
@@ -0,0 +1,145 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro_wechat_trade\library;
|
||||
|
||||
use addons\shopro\model\UserOauth;
|
||||
use think\Db;
|
||||
|
||||
class WxOrderContext
|
||||
{
|
||||
public static function resolveTransactionId($order)
|
||||
{
|
||||
$order = self::normalizeOrder($order);
|
||||
$payType = $order['pay_type'] ?? '';
|
||||
|
||||
if ($payType === 'huifu_wechat') {
|
||||
$ext = self::getHuifuPayExt($order['id']);
|
||||
if ($ext) {
|
||||
$extra = self::decodeHuifuExtra($ext['extra'] ?? '');
|
||||
if (!empty($extra['wx_transaction_id'])) {
|
||||
return $extra['wx_transaction_id'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$txn = trim((string) ($order['transaction_id'] ?? ''));
|
||||
if ($txn !== '' && self::looksLikeWxTransactionId($txn)) {
|
||||
return $txn;
|
||||
}
|
||||
|
||||
$payment = self::decodePaymentJson($order['payment_json'] ?? '');
|
||||
if (!empty($payment['transaction_id']) && self::looksLikeWxTransactionId($payment['transaction_id'])) {
|
||||
return $payment['transaction_id'];
|
||||
}
|
||||
if ($payType === 'huifu_wechat' && !empty($payment['out_trans_id'])) {
|
||||
return $payment['out_trans_id'];
|
||||
}
|
||||
if ($payType === 'huifu_wechat') {
|
||||
$wxResp = $payment['wx_response'] ?? [];
|
||||
if (is_string($wxResp)) {
|
||||
$wxResp = json_decode($wxResp, true) ?: [];
|
||||
}
|
||||
if (!empty($wxResp['transaction_id'])) {
|
||||
return $wxResp['transaction_id'];
|
||||
}
|
||||
}
|
||||
|
||||
return $txn;
|
||||
}
|
||||
|
||||
public static function resolveOpenid($order)
|
||||
{
|
||||
$order = self::normalizeOrder($order);
|
||||
$payType = $order['pay_type'] ?? '';
|
||||
|
||||
if ($payType === 'huifu_wechat') {
|
||||
$ext = self::getHuifuPayExt($order['id']);
|
||||
if ($ext) {
|
||||
$extra = self::decodeHuifuExtra($ext['extra'] ?? '');
|
||||
if (!empty($extra['wx_sub_openid'])) {
|
||||
return $extra['wx_sub_openid'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$payment = self::decodePaymentJson($order['payment_json'] ?? '');
|
||||
if (!empty($payment['sub_openid'])) {
|
||||
return $payment['sub_openid'];
|
||||
}
|
||||
if (!empty($payment['openid'])) {
|
||||
return $payment['openid'];
|
||||
}
|
||||
|
||||
$oauth = UserOauth::where('user_id', $order['user_id'])
|
||||
->where('platform', 'wxMiniProgram')
|
||||
->order('id', 'desc')
|
||||
->find();
|
||||
return $oauth['openid'] ?? '';
|
||||
}
|
||||
|
||||
public static function resolveMchid($order)
|
||||
{
|
||||
$order = self::normalizeOrder($order);
|
||||
if (($order['pay_type'] ?? '') === 'huifu_wechat') {
|
||||
$ext = self::getHuifuPayExt($order['id']);
|
||||
if ($ext) {
|
||||
$extra = self::decodeHuifuExtra($ext['extra'] ?? '');
|
||||
if (!empty($extra['wx_sub_mch_id'])) {
|
||||
return $extra['wx_sub_mch_id'];
|
||||
}
|
||||
}
|
||||
$payment = self::decodePaymentJson($order['payment_json'] ?? '');
|
||||
$wxResp = $payment['wx_response'] ?? [];
|
||||
if (is_string($wxResp)) {
|
||||
$wxResp = json_decode($wxResp, true) ?: [];
|
||||
}
|
||||
if (!empty($wxResp['sub_mch_id'])) {
|
||||
return $wxResp['sub_mch_id'];
|
||||
}
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
protected static function normalizeOrder($order)
|
||||
{
|
||||
if (is_object($order) && method_exists($order, 'toArray')) {
|
||||
return $order->toArray();
|
||||
}
|
||||
return (array) $order;
|
||||
}
|
||||
|
||||
protected static function getHuifuPayExt($orderId)
|
||||
{
|
||||
try {
|
||||
return Db::name('shopro_huifu_pay_ext')->where('order_id', $orderId)->find();
|
||||
} catch (\Exception $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
protected static function decodeHuifuExtra($extra)
|
||||
{
|
||||
if (class_exists('\\addons\\shopro_huifu\\library\\HuifuWxNotifyHelper')) {
|
||||
return \addons\shopro_huifu\library\HuifuWxNotifyHelper::decodeExtra($extra);
|
||||
}
|
||||
if (!$extra) {
|
||||
return [];
|
||||
}
|
||||
$decoded = json_decode($extra, true);
|
||||
return is_array($decoded) ? $decoded : [];
|
||||
}
|
||||
|
||||
protected static function decodePaymentJson($json)
|
||||
{
|
||||
if (!$json) {
|
||||
return [];
|
||||
}
|
||||
$decoded = json_decode($json, true);
|
||||
return is_array($decoded) ? $decoded : [];
|
||||
}
|
||||
|
||||
protected static function looksLikeWxTransactionId($txn)
|
||||
{
|
||||
return (bool) preg_match('/^\d{20,}$/', (string) $txn);
|
||||
}
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户