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); } }