|
1、首先需要在微信商户网站开发配置开启H5支付功能,绑定H5支付域名
2、在WxPayConfig.php文件里面配置以下四项:
const APPID = '';
const MCHID = '';
const KEY = '';
const APPSECRET = '';
APPID:绑定支付的APPID(必须配置,开户邮件中可查看),如果不知道在微信公众号平台<开发>下面基本配置里面
MCHID:商户号,在微信商户平台上面申请的微信支付之后会得到商户好
KEY:商户支付密钥,参考开户邮件设置(必须配置,登录商户平台自行设置)
APPSECRET:公众帐号secert(仅JSAPI支付的时候需要配置, 登录公众平台,进入开发者中心可设置)
微信商户平台网址:https://pay.weixin.qq.com/index.php/core/home/login?return_url=%2Findex.php%2Fcore%2Fhome
微信公众号网址:https://mp.weixin.qq.com
3、在lib---->Wxpay.data.php大概753行中,添加代码 代码如下
/**
* 设置trade_type=MWEB,此参数必传。该字段用于上报支付的场景信息,针对H5支付有三种场景,请根据对应场景上报
* @param string $value
**/
public function SetScene_info($value)
{
$this->values[&#39;scene_info&#39;] = $value;
}
/**
* 判断trade_type=MWEB,此参数必传。该字段用于上报支付的场景信息,针对H5支付有三种场景,请根据对应场景上报
* @return true 或 false
**/
public function IsScene_info()
{
return array_key_exists(&#39;scene_info&#39;, $this->values);
}
4、在example--->NativePay.php中添加代码
/**
*
* 生成H5支付url,支付url有效期为2小时
* @param UnifiedOrderInput $input
*/
public function GetH5PayUrl($input)
{
if($input->GetTrade_type() == &#34;MWEB&#34;)
{
$result = WxPayApi::unifiedOrder($input);
return $result;
}
}
调用代码如下:
$notify = new NativePay();
$input = new WxPayUnifiedOrder();
$input->setBody($goods_name); //商品名称
$input->setAttach($goods_name); //附加参数
$input->setOutTradeNo(number); //订单号
$input->setTotalFee();//金额
$input->setTimeStart(date(&#34;YmdHis&#34;)); //支付发起时间
$input->setTimeExpire(date(&#34;YmdHis&#34;, time() + 600)); //支付超时
$input->setGoodsTag(&#34;test&#34;);
$input->setNotifyUrl(&#34;URL&#34;);//回调地址
$input->setTradeType(&#34;MWEB&#34;);//h5支付类型
$input->setProductId($order->deal_id);
$input->setSpbillCreateIp($this->ipGet());//获取用户ip
$input->SetScene_info(&#39;{&#34;h5_info&#34;: {&#34;type&#34;:&#34;Wap&#34;,&#34;wap_url&#34;: &#34;域名&#34;,&#34;wap_name&#34;: &#34;家网&#34;}}&#39;); //H5支付的特定格式
$jsApiParameters = $notify->GetH5PayUrl($input);
if(empty($jsApiParameters[&#39;mweb_url&#39;])){
$jsApiParameters= &#39;&#39;;
}else{
$jsApiParameters=$jsApiParameters[&#34;mweb_url&#34;].&#39;&redirect_url=URL &#39;;
}
$this->assign(&#39; jsApiParameters &#39;,$ jsApiParameters);
Html页面js代码如下:
<html>
<head>
<meta http-equiv=&#34;content-type&#34; content=&#34;text/html;charset=utf-8&#34;/>
<meta name=&#34;viewport&#34; content=&#34;width=device-width, initial-scale=1&#34;/>
<meta http-equiv=&#34;X-UA-Compatible&#34; content=&#34;ie=edge&#34;>
<title>xxx-支付</title>
<style>
html, body {
margin: 0;
width: 100%;
height: 100%;
}
.modal {
position: relative;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, .6);
}
.dialog {
position: absolute;
top: 50%;
left: 50%;
width: 210px;
transform: translate(-50%, -50%);
background-color: #fff;
border-radius: 4px;
}
.dialog .head {
height: 36px;
line-height: 36px;
border-bottom: 1px solid #1aad19;
text-align: center;
font-size: 14px;
}
.dialog .paymoney {
padding: 14px;
}
.dialog .paymoney span {
display: block;
font-size: 14px;
text-align: center;
}
.dialog .paymoney b {
display: block;
font-size: 28px;
text-align: center;
}
.dialog .paymoney a {
background: #1aad19;
padding: 8px 0;
margin-top:5px;
display: block;
border-radius: 4px;
text-decoration: none;
text-align: center;
font-size: 18px;
color: #fff;
}
.dialog .btn-pay {
}
</style>
</head>
<body>
<div class=&#34;modal&#34;>
<div class=&#34;dialog&#34;>
<div class=&#34;head&#34;>
支付
</div>
<div class=&#34;paymoney&#34;>
<span>xxx有限公司</span>
<b>¥<?php echo $trade[&#39;fee&#39;]; ?></b>
<a type=&#34;button&#34; href=&#34;<?php echo $url; ?>&#34;>立即支付</a>
</div>
</div>
</div>
</body>
</html>
中间可能遇到一些问题 我就把我遇到的问题写在这里:
主要还是网络环境未能通过验证 我找了很久的问题 发现是用户ip传入不对 这里放上获取ip的方法
private function ipGet(){
if(getenv(&#39;HTTP_CLIENT_IP&#39;) && strcasecmp(getenv(&#39;HTTP_CLIENT_IP&#39;), &#39;unknown&#39;)) {
$ip = getenv(&#39;HTTP_CLIENT_IP&#39;);
} elseif(getenv(&#39;HTTP_X_FORWARDED_FOR&#39;) && strcasecmp(getenv(&#39;HTTP_X_FORWARDED_FOR&#39;), &#39;unknown&#39;)) {
$ip = getenv(&#39;HTTP_X_FORWARDED_FOR&#39;);
} elseif(getenv(&#39;REMOTE_ADDR&#39;) && strcasecmp(getenv(&#39;REMOTE_ADDR&#39;), &#39;unknown&#39;)) {
$ip = getenv(&#39;REMOTE_ADDR&#39;);
} elseif(isset($_SERVER[&#39;REMOTE_ADDR&#39;]) && $_SERVER[&#39;REMOTE_ADDR&#39;] && strcasecmp($_SERVER[&#39;REMOTE_ADDR&#39;], &#39;unknown&#39;)) {
$ip = $_SERVER[&#39;REMOTE_ADDR&#39;];
}
return preg_match ( &#39;/[\d\.]{7,15}/&#39;, $ip, $matches ) ? $matches [0] : &#39;&#39;;
}
在所有的配置结束之后发起支付 老是有ip报错
这样就结束了 参考网址:https://www.cnblogs.com/cuculus/p/8405942.html
https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=15_4 |
|