香雨站

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 114|回复: 0

关于php微信SDK H5支付

[复制链接]

4

主题

5

帖子

13

积分

新手上路

Rank: 1

积分
13
发表于 2023-1-5 17:10:33 | 显示全部楼层 |阅读模式
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['scene_info'] = $value;
    }
    /**
     * 判断trade_type=MWEB,此参数必传。该字段用于上报支付的场景信息,针对H5支付有三种场景,请根据对应场景上报
     * @return true 或 false
     **/
    public function IsScene_info()
    {
        return array_key_exists('scene_info', $this->values);
}

4、在example--->NativePay.php中添加代码
/**
     *
     * 生成H5支付url,支付url有效期为2小时
     * @param UnifiedOrderInput $input
     */
    public function GetH5PayUrl($input)
    {
        if($input->GetTrade_type() == "MWEB")
        {
            $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("YmdHis")); //支付发起时间
$input->setTimeExpire(date("YmdHis", time() + 600)); //支付超时
$input->setGoodsTag("test");
$input->setNotifyUrl("URL");//回调地址
$input->setTradeType("MWEB");//h5支付类型
$input->setProductId($order->deal_id);
$input->setSpbillCreateIp($this->ipGet());//获取用户ip
$input->SetScene_info('{"h5_info": {"type":"Wap","wap_url": "域名","wap_name": "家网"}}'); //H5支付的特定格式
$jsApiParameters = $notify->GetH5PayUrl($input);
if(empty($jsApiParameters['mweb_url'])){
$jsApiParameters= '';
}else{
        $jsApiParameters=$jsApiParameters["mweb_url"].'&redirect_url=URL ';
}         
$this->assign(' jsApiParameters ',$ jsApiParameters);

Html页面js代码如下:

<html>
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <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="modal">
    <div class="dialog">
        <div class="head">
            支付
        </div>
        <div class="paymoney">
            <span>xxx有限公司</span>
            <b>¥<?php echo $trade['fee']; ?></b>
            <a type="button" href="<?php echo $url; ?>">立即支付</a>
        </div>

    </div>
</div>
</body>
</html>
中间可能遇到一些问题   我就把我遇到的问题写在这里:
主要还是网络环境未能通过验证  我找了很久的问题 发现是用户ip传入不对 这里放上获取ip的方法
private function ipGet(){
         if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
          $ip = getenv('HTTP_CLIENT_IP');
          } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
          $ip = getenv('HTTP_X_FORWARDED_FOR');
          } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
          $ip = getenv('REMOTE_ADDR');
        } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
           $ip = $_SERVER['REMOTE_ADDR'];
        }
         return preg_match ( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : '';

}
在所有的配置结束之后发起支付   老是有ip报错
这样就结束了 参考网址:https://www.cnblogs.com/cuculus/p/8405942.html
https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=15_4
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|香雨站

GMT+8, 2025-3-15 10:07 , Processed in 0.541447 second(s), 22 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.. 技术支持 by 巅峰设计

快速回复 返回顶部 返回列表