新手必看

新手必看

常见问题必读帮助文件
鸿宇官网

鸿宇官网

鸿宇科技官方网站
鸿宇教程库

鸿宇教程库

鸿宇科技官方教程
功能反馈与建议

功能反馈与建议

BUG反馈问题建议
鸿宇淘宝店铺

鸿宇淘宝店铺

鸿宇官方唯一淘宝店铺
旺迈特官网

旺迈特官网

B2B2C新零售智慧生态电商系统
旺迈特演示站点

旺迈特演示站点

B2B2C多用户商城、新零售智慧生态电商系统
QQ客服(技术支持)

QQ客服(技术支持)

QQ:1527200768/获取论坛账号/软件开发/功能定制

大商创 - 微信支付在微信App内不显示按钮问题解决方案

鸿宇 发表了文章 • 0 个评论 • 1993 次浏览 • 2020-06-17 18:13 • 来自相关话题

 
一、问题原因:
 
1. 无法获取微信openid:文件 mobile/app/Plugins/payment/wxpay.php 中 get_code() 函数无法正确获取 openid 造成按钮不显示问题。
2.生成订单号逻辑错误, 会造成微信接口返回订单号重复错误。

二、解决方案:
 
1. 打开文件 mobile/app/Plugins/payment/wxpay.php 约 55 行,修改get_code()函数代码:public function get_code($order, $payment)
{
include_once BASE_PATH . 'Helpers/payment_helper.php';
$this->payment = $payment;
$options = array('appid' => $this->payment['wxpay_appid'], 'mch_id' => $this->payment['wxpay_mchid'], 'key' => $this->payment['wxpay_key']);
if (isset($this->payment['wxpay_sub_mch_id']) && !empty($this->payment['wxpay_sub_mch_id'])) {
$options['sub_mch_id'] = $this->payment['wxpay_sub_mch_id'];
}

$weObj = new \App\Extensions\Wechat($options);
$order_amount = $order['order_amount'] * 100;

if (!is_wechat_browser()) {
$scene_info = json_encode(array(
'h5_info' => array('type' => 'Wap', 'wap_url' => __URL__, 'wap_name' => C('shop.shop_name'))
));
$this->setParameter('body', $order['order_sn']);
$this->setParameter('out_trade_no', $order['order_sn'] . time() . 'A' . $order['log_id']);
$this->setParameter('total_fee', $order_amount);
$this->setParameter('spbill_create_ip', $this->get_client_ip());
$this->setParameter('notify_url', notify_url(basename(__FILE__, '.php')));
$this->setParameter('trade_type', 'MWEB');
$this->setParameter('scene_info', $scene_info);
$respond = $weObj->PayUnifiedOrder($this->parameters);

if (isset($respond['mweb_url'])) {
if ($respond['result_code'] == 'SUCCESS') {
$redirect_url = __URL__ . '/respond.php?code=wxpay&type=wxh5&log_id=' . $order['log_id'];
}

$button = '<a class="box-flex btn-submit" type="button" onclick="window.open(\'' . $respond['mweb_url'] . '&redirect_url=' . urlencode($redirect_url) . '\')">微信支付</a>';
} else {
$button = '';
return false;
}
} else {
$openid = '';
if (isset($_SESSION['openid']) && !empty($_SESSION['openid'])) {
$openid = $_SESSION['openid'];
} else {
if (isset($_SESSION['openid_base']) && !empty($_SESSION['openid_base'])) {
$openid = $_SESSION['openid_base'];
} else {
//return false;
$openid = $this->getOpenid();
}
}

$this->setParameter('openid', $openid);
$this->setParameter('body', $order['order_sn']);
$this->setParameter('out_trade_no', $order['order_sn'] . time() . 'A' . $order['log_id']);
$this->setParameter('total_fee', $order_amount);
$this->setParameter('spbill_create_ip', $this->get_client_ip());
$this->setParameter('notify_url', notify_url(basename(__FILE__, '.php')));
$this->setParameter('trade_type', 'JSAPI');
$respond = $weObj->PayUnifiedOrder($this->parameters, true);
$jsApiParameters = json_encode($respond);
$js = "<script language=\"javascript\">\r\n function jsApiCall(){WeixinJSBridge.invoke(\"getBrandWCPayRequest\"," . $jsApiParameters . ',function(res){if(res.err_msg == "get_brand_wcpay_request:ok"){location.href="' . return_url(basename(__FILE__, '.php')) . '&status=1&log_id=' . $order['log_id'] . '"}else{location.href="' . return_url(basename(__FILE__, '.php')) . '&status=0&log_id=' . $order['log_id'] . "\"}})};function callpay(){if (typeof WeixinJSBridge == \"undefined\"){if( document.addEventListener ){document.addEventListener(\"WeixinJSBridgeReady\", jsApiCall, false);}else if (document.attachEvent){document.attachEvent(\"WeixinJSBridgeReady\", jsApiCall);document.attachEvent(\"onWeixinJSBridgeReady\", jsApiCall);}}else{jsApiCall();}}\r\n </script>";
$button = '<a class="box-flex btn-submit" type="button" onclick="callpay();">微信支付</a>' . $js;
}

return $button;
}2. 打开文件 mobile/app/Plugins/payment/wxpay.php 约 365 行,修改getOpenid()函数代码:private function getOpenid()
{
if (!isset($_GET['code'])) {
$redirectUrl = urlencode('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . (!empty($_SERVER['QUERY_STRING']) ? '?' : '') . $_SERVER['QUERY_STRING']);
$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $this->payment['wxpay_appid'] . '&redirect_uri=' . $redirectUrl . '&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect';
header('Location: ' . $url);
exit();
} else {
$code = $_GET['code'];
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $this->payment['wxpay_appid'] . '&secret=' . $this->payment['wxpay_appsecret'] . '&code=' . $code . '&grant_type=authorization_code';

/*
$result = \App\Extensions\Http::doGet($url);

if ($result) {
$json = json_decode($result);
if (isset($json['errCode']) && $json['errCode']) {
return false;
}

$_SESSION['openid_base'] = $json['openid'];
return $json['openid'];
}

return false;
*/

$get_token_url = $url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $get_token_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$res = curl_exec($ch);
curl_close($ch);
$json_obj = json_decode($res, true);
$openid = $json_obj['openid'];
$_SESSION['openid'] = $openid;
return $openid;
}
}
 3. 代码修改重点











 
  查看全部
 
一、问题原因:
 
1. 无法获取微信openid:文件 mobile/app/Plugins/payment/wxpay.php 中 get_code() 函数无法正确获取 openid 造成按钮不显示问题。
2.生成订单号逻辑错误, 会造成微信接口返回订单号重复错误。

二、解决方案:
 
1. 打开文件 mobile/app/Plugins/payment/wxpay.php 约 55 行,修改get_code()函数代码:
public function get_code($order, $payment)
{
include_once BASE_PATH . 'Helpers/payment_helper.php';
$this->payment = $payment;
$options = array('appid' => $this->payment['wxpay_appid'], 'mch_id' => $this->payment['wxpay_mchid'], 'key' => $this->payment['wxpay_key']);
if (isset($this->payment['wxpay_sub_mch_id']) && !empty($this->payment['wxpay_sub_mch_id'])) {
$options['sub_mch_id'] = $this->payment['wxpay_sub_mch_id'];
}

$weObj = new \App\Extensions\Wechat($options);
$order_amount = $order['order_amount'] * 100;

if (!is_wechat_browser()) {
$scene_info = json_encode(array(
'h5_info' => array('type' => 'Wap', 'wap_url' => __URL__, 'wap_name' => C('shop.shop_name'))
));
$this->setParameter('body', $order['order_sn']);
$this->setParameter('out_trade_no', $order['order_sn'] . time() . 'A' . $order['log_id']);
$this->setParameter('total_fee', $order_amount);
$this->setParameter('spbill_create_ip', $this->get_client_ip());
$this->setParameter('notify_url', notify_url(basename(__FILE__, '.php')));
$this->setParameter('trade_type', 'MWEB');
$this->setParameter('scene_info', $scene_info);
$respond = $weObj->PayUnifiedOrder($this->parameters);

if (isset($respond['mweb_url'])) {
if ($respond['result_code'] == 'SUCCESS') {
$redirect_url = __URL__ . '/respond.php?code=wxpay&type=wxh5&log_id=' . $order['log_id'];
}

$button = '<a class="box-flex btn-submit" type="button" onclick="window.open(\'' . $respond['mweb_url'] . '&redirect_url=' . urlencode($redirect_url) . '\')">微信支付</a>';
} else {
$button = '';
return false;
}
} else {
$openid = '';
if (isset($_SESSION['openid']) && !empty($_SESSION['openid'])) {
$openid = $_SESSION['openid'];
} else {
if (isset($_SESSION['openid_base']) && !empty($_SESSION['openid_base'])) {
$openid = $_SESSION['openid_base'];
} else {
//return false;
$openid = $this->getOpenid();
}
}

$this->setParameter('openid', $openid);
$this->setParameter('body', $order['order_sn']);
$this->setParameter('out_trade_no', $order['order_sn'] . time() . 'A' . $order['log_id']);
$this->setParameter('total_fee', $order_amount);
$this->setParameter('spbill_create_ip', $this->get_client_ip());
$this->setParameter('notify_url', notify_url(basename(__FILE__, '.php')));
$this->setParameter('trade_type', 'JSAPI');
$respond = $weObj->PayUnifiedOrder($this->parameters, true);
$jsApiParameters = json_encode($respond);
$js = "<script language=\"javascript\">\r\n function jsApiCall(){WeixinJSBridge.invoke(\"getBrandWCPayRequest\"," . $jsApiParameters . ',function(res){if(res.err_msg == "get_brand_wcpay_request:ok"){location.href="' . return_url(basename(__FILE__, '.php')) . '&status=1&log_id=' . $order['log_id'] . '"}else{location.href="' . return_url(basename(__FILE__, '.php')) . '&status=0&log_id=' . $order['log_id'] . "\"}})};function callpay(){if (typeof WeixinJSBridge == \"undefined\"){if( document.addEventListener ){document.addEventListener(\"WeixinJSBridgeReady\", jsApiCall, false);}else if (document.attachEvent){document.attachEvent(\"WeixinJSBridgeReady\", jsApiCall);document.attachEvent(\"onWeixinJSBridgeReady\", jsApiCall);}}else{jsApiCall();}}\r\n </script>";
$button = '<a class="box-flex btn-submit" type="button" onclick="callpay();">微信支付</a>' . $js;
}

return $button;
}
2. 打开文件 mobile/app/Plugins/payment/wxpay.php 约 365 行,修改getOpenid()函数代码:
private function getOpenid()
{
if (!isset($_GET['code'])) {
$redirectUrl = urlencode('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . (!empty($_SERVER['QUERY_STRING']) ? '?' : '') . $_SERVER['QUERY_STRING']);
$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $this->payment['wxpay_appid'] . '&redirect_uri=' . $redirectUrl . '&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect';
header('Location: ' . $url);
exit();
} else {
$code = $_GET['code'];
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $this->payment['wxpay_appid'] . '&secret=' . $this->payment['wxpay_appsecret'] . '&code=' . $code . '&grant_type=authorization_code';

/*
$result = \App\Extensions\Http::doGet($url);

if ($result) {
$json = json_decode($result);
if (isset($json['errCode']) && $json['errCode']) {
return false;
}

$_SESSION['openid_base'] = $json['openid'];
return $json['openid'];
}

return false;
*/

$get_token_url = $url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $get_token_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$res = curl_exec($ch);
curl_close($ch);
$json_obj = json_decode($res, true);
$openid = $json_obj['openid'];
$_SESSION['openid'] = $openid;
return $openid;
}
}

 3. 代码修改重点

Snipaste_2020-06-19_11-06-59.png


Snipaste_2020-06-19_11-07-42.png