微信app支付

AlgoSolarStrider
• 阅读 6333

文档
注:开放平台的微信支付和公众号的微信支付是不一样的,公众平台和开放平台的支付申请下来之后会有各自对应的商户平台账号

function wechat($appid,$mchid,$appkey,$cert_path,$key_path,$order_id,$openid,$amount,$desc){
    $arr = [
            'mch_appid'=>$appid,//注意区分公众号和app商户号不同
            'mchid'=>$mchid,
            'nonce_str'=>str_random(32),//随机数
            'partner_trade_no'=>$order_id,//自己定义一个不重复订单号
            'openid'=>$openid,//微信openid 通过微信授权登录获取
            'check_name'=>'NO_CHECK',
            'amount'=>$amount*100,//注意这里传给微信的单位是分
            'desc'=>$desc,
            'spbill_create_ip'=>\Request::getClientIp(),
            'sign'=>'',
        ];
        ksort($arr);
        $sign="";
        foreach ($arr as $key => $value) {
            if($value && $key!="sign" && $key!="key"){
                $sign.=$key."=".$value."&";
            }
        }
        $sign.="key=".$appkey;//商户后台自定义的
        $arr['sign'] = strtoupper(md5($sign));
        $xml = "<xml>";
        foreach ($arr as $key=>$val)
        {
                if (is_numeric($val))
             {
                $xml.="<".$key.">".$val."</".$key.">"; 

             }
             else
                $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";  
        }
        $xml.="</xml>";
       
        $ch = curl_init();
        //超时时间
        curl_setopt($ch,CURLOPT_TIMEOUT,60);
        curl_setopt($ch,CURLOPT_URL,'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers');
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
        //默认格式为PEM
        curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
        curl_setopt($ch,CURLOPT_SSLCERT,$cert_path);//注意区分公众号和app商户号的证书不同,需要到pay.weixin.qq.com后台下载
        curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
        curl_setopt($ch,CURLOPT_SSLKEY,$key_path);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
        curl_setopt($ch,CURLOPT_POST, 1);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);
        $data = curl_exec($ch);
        $data = json_decode(json_encode(simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        curl_close($ch);
        return $data;//$data['return_code'] == 'SUCCESS' && $data['result_code'] == 'SUCCESS' 支付成功
    }
    }

微信上传文件

    function https_request($url,$type="get",$res="json",$data = ''){
        //1.初始化curl
        $curl = curl_init();
        //2.设置curl的参数
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,2);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        if ($type == "post"){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        //3.采集
        $output = curl_exec($curl);
        //4.关闭
        curl_close($curl);
        if ($res == 'json') {
            return json_decode($output,true);
        }
    }    
    function getWxAccessToken(){
        if ( $_SESSION['access_token'] && $_SESSION['expire_time'] > time() ) {
            //未过期
            return $_SESSION['access_token'];
        }else {
            $appid        = "我的id";
            $appsecret = "我的appsecret ";
            $url          = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
            $res =$this->https_request($url,'get','json');
            $access_token = $res["access_token"];
            //将重新获取到的access_token存到session里
            $_SESSION['access_token']=$access_token;
            $_SESSION['expire_time']=time()+7200;
            return $access_token; 
        }
    }
    //上传图片素材(该方法只能获取到图片的URL)      
    function addMaterialUrl() {
        $access_token = $this->getWxAccessToken();    
        $url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=".$access_token;
        $filename = '我是图片';
        $data=array("media"=>'@'. $filename);
        $res=$this->https_request( $url ,'post', 'json', $data);
        //dump($res); exit();
        return $res['url'];
    }
    
    php5.6加上curl_setopt ($curl, CURLOPT_SAFE_UPLOAD, false);或者直接"media" => new CURLFile(realpath($real_path)), https://segmentfault.com/a/1190000000725185 

附:
https://youqingkui.me/note/e5...
python版微信支付
http://bblove.me/2015/10/25/w...
微信APP支付服务端php sdk开发教程
https://github.com/fanhefan/w...
微信红包API接口
http://jeffchen.sinaapp.com/
http://tao.logdown.com/posts/...
微信支付 一步一个坑的APP支付

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
vue h5 对接支付宝,微信支付,微信js支付
vueh5实现支付(支付宝,微信)h5端实现支付难度不大,只是有些小的点需要注意下,其他的看文档撸就行了。支付宝很简单,后端返回一个html,前端插入调用就行了,微信支付分两种:1、微信内支付(jsapi,微信内浏览器)2、微信外支付(h5支付)。一、支付宝支付//前端啥都不用管,交给后端去干,返回html调用点击就好了/
翼
4年前
微信支付 (JSSDK支付)
官方文档微信支付https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter7_7&index6微信授权获取codehttps://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JSSDK.html58准备工作微信公
ThinkPHP V5.0 接入微信支付+回调
ThinkPHPV5.0接入微信支付微信支付接口组装访问数组$data'body''商城购买商品';//订单标题$data'outtradeno'generaterandstr(8,0);//平台订单号(非小程序订单,自己平台生成的)$data'notifyurl'$thisrequestdomain().'';//微信支付
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
美凌格栋栋酱 美凌格栋栋酱
6个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
Easter79 Easter79
3年前
thinkcmf+jsapi 实现微信支付
首先从小程序端接收订单号、金额等参数,然后后台进行统一下单,把微信支付的订单号返回,在把订单号发送给前台,前台拉起支付,返回参数后更改支付状态。。。回调publicfunctionnotify(){$wechatDb::name('wechat')where('status',1)find();
梦
4年前
微信小程序new Date()转换时间异常问题
微信小程序苹果手机页面上显示时间异常,安卓机正常问题image(https://imghelloworld.osscnbeijing.aliyuncs.com/imgs/b691e1230e2f15efbd81fe11ef734d4f.png)错误代码vardate'2021030617:00:00'vardateT
Wesley13 Wesley13
3年前
Oracle ADG究竟是否收费?
!(https://oscimg.oschina.net/oscnet/faaf5c218b3045fe9a38ffa00c48a996.png)前两天微信群中,有位朋友问了,OracleDatabaseStandardEdition11gR2标准版支持activedataguard么?可能平时
Stella981 Stella981
3年前
KaliTools说明书+BurpSuit实战指南+SQL注入知识库+国外渗透报告
!(https://oscimg.oschina.net/oscnet/d1c876a571bb41a7942dd9752f68632e.gif"15254461546.gif")0X00KaliLinux Tools中文说明书!(https://oscimg.oschina.net/oscnet/
Python进阶者 Python进阶者
1年前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这