调用接口失败:https://api.mch.weixin.qq.com/pay/unifiedorder,代码片段分别是:class JsApiPay
{
/**
* 统一下单
* @param array $params
* @param string $prepay_id 预支付订单号 如需发送小程序模板消息传入
* @return array
* @throws \WxPayException
*/
public function SetUnifiedOrder($params = array(), &$prepay_id = '')
{
\think\Log::write('JsApiPay::SetUnifiedOrder方法被调用,参数:' . json_encode($params, JSON_UNESCAPED_UNICODE), 'debug');
try {
$notify_url = array_key_exists('notify_url', $params) ? $params['notify_url'] : \WxPayConfig::NOTIFY_URL;
$input = new \WxPayUnifiedOrder();
$input->SetBody($params['body']); // 商品描述
$input->SetAttach("");
$input->SetOut_trade_no($params['out_trade_no']); // 订单ID
$input->SetTotal_fee($params['price']); // 订单金额
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 1200));
$input->SetTrade_type("JSAPI");
$input->SetNotify_url($notify_url);
$input->SetOpenid($params['openid']);
// 记录请求参数
\think\Log::write('统一下单请求参数详细信息:' .
"\nAppid: " . \WxPayConfig::$appid .
"\nMchid: " . \WxPayConfig::$mchid .
"\nOpenid: " . $params['openid'] .
"\nOut_trade_no: " . $params['out_trade_no'] .
"\nTotal_fee: " . $params['price'] .
"\nNotify_url: " . $notify_url .
"\nTrade_type: JSAPI", 'debug');
// 调用统一下单接口
\think\Log::write('开始调用统一下单接口...', 'debug');
$order = \WxPayApi::unifiedOrder($input);
\think\Log::write('统一下单返回结果:' . json_encode($order, JSON_UNESCAPED_UNICODE), 'debug');
// 检查返回结果
if (!isset($order['return_code']) || $order['return_code'] !== 'SUCCESS') {
$errMsg = isset($order['return_msg']) ? $order['return_msg'] : '未知错误';
\think\Log::write('统一下单返回失败,return_code不为SUCCESS: ' . $errMsg, 'error');
throw new \WxPayException('统一下单失败: ' . $errMsg);
}
if (!isset($order['result_code']) || $order['result_code'] !== 'SUCCESS') {
$errCode = isset($order['err_code']) ? $order['err_code'] : '';
$errMsg = isset($order['err_code_des']) ? $order['err_code_des'] : '未知错误';
\think\Log::write('统一下单业务结果失败,result_code不为SUCCESS: ' . $errCode . ' - ' . $errMsg, 'error');
throw new \WxPayException('统一下单业务结果失败: ' . $errMsg);
}
$prepay_id = array_key_exists('prepay_id', $order) ? $order['prepay_id'] : '';
if (empty($prepay_id)) {
\think\Log::write('统一下单返回中没有prepay_id', 'error');
throw new \WxPayException('获取prepay_id失败');
}
\think\Log::write('获取到prepay_id: ' . $prepay_id, 'debug');
// 获取JSAPI支付参数
$jsApiParameters = $this->GetJsApiParameters($order);
\think\Log::write('生成JSAPI支付参数成功: ' . json_encode($jsApiParameters, JSON_UNESCAPED_UNICODE), 'debug');
return $jsApiParameters;
} catch (\WxPayException $e) {
\think\Log::write('微信支付统一下单异常:' . $e->getMessage(), 'error');
throw $e;
} catch (\Exception $e) {
\think\Log::write('微信支付统一下单未知异常:' . $e->getMessage(), 'error');
throw new \WxPayException($e->getMessage());
}
}
/**
* 获取jsapi支付的参数
* @param $UnifiedOrderResult array 统一支付接口返回的数据
* @return array json数据,可直接填入js函数作为参数
* @throws \WxPayException
*/
public function GetJsApiParameters($UnifiedOrderResult)
{
\think\Log::write('GetJsApiParameters方法被调用,参数:' . json_encode($UnifiedOrderResult, JSON_UNESCAPED_UNICODE), 'debug');
if (!array_key_exists("appid", $UnifiedOrderResult) || empty($UnifiedOrderResult["appid"])) {
\think\Log::write('GetJsApiParameters错误:返回结果中缺少appid参数', 'error');
throw new \WxPayException("参数错误:返回结果中缺少appid");
}
if (!array_key_exists("prepay_id", $UnifiedOrderResult) || empty($UnifiedOrderResult['prepay_id'])) {
\think\Log::write('GetJsApiParameters错误:返回结果中缺少prepay_id参数', 'error');
throw new \WxPayException("参数错误:返回结果中缺少prepay_id");
}
try {
$jsapi = new \WxPayJsApiPay();
$jsapi->SetAppid($UnifiedOrderResult["appid"]);
$timeStamp = time();
$jsapi->SetTimeStamp("$timeStamp");
$jsapi->SetNonceStr(\WxPayApi::getNonceStr());
$jsapi->SetPackage("prepay_id=" . $UnifiedOrderResult['prepay_id']);
$jsapi->SetSignType("MD5");
$jsapi->SetPaySign($jsapi->MakeSign());
$parameters = $jsapi->GetValues();
\think\Log::write('成功生成JSAPI支付参数:' . json_encode($parameters, JSON_UNESCAPED_UNICODE), 'debug');
return $parameters;
} catch (\Exception $e) {
\think\Log::write('生成JSAPI支付参数异常:' . $e->getMessage(), 'error');
throw new \WxPayException("生成支付参数失败:" . $e->getMessage());
}
}
/**
* 订单查询
* @param array $params
* @return array|bool
* @throws \WxPayException
*/
public function OrderQueryResult($params = array())
{
$input = new \WxPayOrderQuery();
if (isset($params['transaction_id']) && $params['transaction_id']) {
$input->SetTransaction_id($params['transaction_id']);
} else if (isset($params['out_trade_no']) && $params['out_trade_no']) {
$input->SetOut_trade_no($params['out_trade_no']);
} else {
return false;
}
return \WxPayApi::orderQuery($input);
}
/**
* 订单退款
* @param array $params transaction_id:微信订单号 out_trade_no:商户订单号 二选一
* @return array|bool
* @throws \WxPayException
*/
public function OrderRefundResult($params = array())
{
$input = new \WxPayRefund();
if (isset($params['transaction_id']) && $params['transaction_id']) {
$refund = 'TK' . $params['transaction_id'] . rand(1, 10);
$input->SetTransaction_id($params['transaction_id']);
} else if (isset($params['out_trade_no']) && $params['out_trade_no']) {
$refund = 'TK' . $params['out_trade_no'];
$input->SetOut_trade_no($params['out_trade_no']);
} else {
return false;
}
$input->SetTotal_fee($params['total_fee'] * 100);
$input->SetRefund_fee($params['refund_fee'] * 100);
$input->SetOut_refund_no($params['out_refund_no'] ?? $refund);
$input->SetOp_user_id(\WxPayConfig::$mchid);
return \WxPayApi::refund($input);
}
/*
*
* 订单退款查询
* $params transaction_id:微信订单号 out_trade_no:商户订单号
*/
/**
* @param array $params transaction_id:微信订单号 out_trade_no:商户订单号 二选一
* @return array|bool
* @throws \WxPayException
*/
public function OrderRefundQuery($params = array())
{
$input = new \WxPayRefundQuery();
if (isset($params['transaction_id']) && $params['transaction_id']) {
$input->SetTransaction_id($params['transaction_id']);
} else if (isset($params['out_trade_no']) && $params['out_trade_no']) {
$input->SetOut_trade_no($params['out_trade_no']);
} else {
return false;
}
return \WxPayApi::refundQuery($input);
}
/**
* 拼接签名字符串
* @param array $urlObj
* @return string 返回已经拼接好的字符串
*/
protected function ToUrlParams($urlObj)
{
$buff = "";
foreach ($urlObj as $k => $v) {
if ($k != "sign") {
$buff .= $k . "=" . $v . "&";
}
}
$buff = trim($buff, "&");
return $buff;
}
}
第二个代码片段是: // 新增:生成神券包订单,返回支付参数
public function createCouponPackOrder()
{
try {
$user_id = $this->auth->id;
$pack_id = $this->request->post('pack_id');
\think\Log::info('下单请求参数: user_id=' . var_export($user_id, true) . ' pack_id=' . var_export($pack_id, true));
if (!$user_id || !$pack_id) {
\think\Log::error('用户ID或券包ID为空,user_id=' . var_export($user_id, true) . ' pack_id=' . var_export($pack_id, true));
return json(['code'=>0, 'msg'=>'用户信息或券包信息有误[user_id=' . var_export($user_id, true) . ', pack_id=' . var_export($pack_id, true) . ']']);
}
$pack = \app\common\model\CouponPack::get($pack_id);
if (!$pack) {
\think\Log::error('券包不存在: ' . $pack_id);
return json(['code'=>0, 'msg'=>'券包不存在']);
}
if ($pack->status != 1) {
\think\Log::error('券包未上架: ' . $pack_id);
return json(['code'=>0, 'msg'=>'券包未上架']);
}
if (!isset($pack->price) || $pack->price <= 0) {
\think\Log::error('券包价格异常: ' . json_encode(['pack_id'=>$pack_id, 'price'=>$pack->price], JSON_UNESCAPED_UNICODE));
return json(['code'=>0, 'msg'=>'券包价格异常']);
}
// 生成订单号
$order_sn = 'CP' . date('YmdHis') . mt_rand(1000,9999);
$total_fee = $pack->price; // 假设有 price 字段
// 写入订单表
$order = \app\common\model\CouponPackOrder::create([
'user_id' => $user_id,
'pack_id' => $pack_id,
'order_sn' => $order_sn,
'total_fee' => $total_fee,
'status' => 0,
'createtime' => time()
]);
// 获取用户openid
$openid = \app\common\model\User::where('id', $user_id)->value('openid');
// 微信支付参数
$params = [
'body' => '神券包-' . $pack->name,
'out_trade_no' => $order_sn,
'price' => intval($total_fee * 100),
'openid' => $openid,
'notify_url' => 'https://' . $_SERVER['SERVER_NAME'] . '/api/member/couponPackNotify'
];
require_once __DIR__ . '/../../../extend/wechat/wxpay/lib/WxPayConfig.php';
\WxPayConfig::setWechatPayConfig();
$wxpay = new \wechat\wxpay\JsApiPay();
$prepay_id = '';
$jsApiParameters = $wxpay->SetUnifiedOrder($params, $prepay_id);
if (empty($jsApiParameters)) {
\think\Log::error('微信下单失败: ' . json_encode($params, JSON_UNESCAPED_UNICODE));
return json(['code'=>0, 'msg'=>'微信下单失败']);
}
// 更新订单
$order->prepay_id = $prepay_id;
$order->save();
return json(['code'=>1, 'data'=>$jsApiParameters]);
} catch (\Throwable $e) {
\think\Log::error('下单异常: ' . $e->getMessage() . ' @' . $e->getFile() . ':' . $e->getLine() . ' ' . $e->getTraceAsString());
return json(['code'=>0, 'msg'=>'下单异常: ' . $e->getMessage()]);
}
}
以上代码调用的时候,服务器出现报错:[运行时间:0.189665s] [吞吐率:5.27req/s] [内存消耗:4,816.16kb] [文件加载:155]
[ error ] 下单异常: 统一下单业务结果失败: 此商家的收款功能已被限制,暂无法支付。商家可登录微信商户平台/微信支付商家助手小程序/经营账户页面看原因和解决方案。 @/www/wwwroot/bawangcan/extend/wechat/wxpay/JsApiPay.php:6 #0 /www/wwwroot/bawangcan/application/api/controller/Member.php(111): wechat\wxpay\JsApiPay->SetUnifiedOrder(Array, '')
