微信小程序支付

小程序直接调用  改一写参数 就OK了

 

<?php 
/**
 * 支付
 */
class Cspay extends Common
{
    public function pay()
    {
        $uid = input('openid');  //下单用户id
        $orderid = input('orderid');    //订单ID

        $usermodel = new User;    //实例化model
        $userinfo = $usermodel->setdetails($uid);   //查询下单用户信息
        $openid = $userinfo['openid'];   //下单用户openid
        //判断  订单价格
        $order = new Order;
        $orderfind = $order->chatfind($orderid);   //查询订单信息
        $fee = $orderfind['virtual_price'];    //需要支付的价格
        // $fee = 0.03;
        $appid = '  ';   //小程序aapid
        $body = '订单支付';              //支付介绍
        $mch_id = '   ';            //商户号
        $nonce_str = $this->nonce_str();        //生成随机字符串 
        $notify_url = 'https://www.txkuaiyou.com/index.php/wxone/cspay/syntonys';           //回调地址
        $openid = $openid;                 //下单用户的openid
        $out_trade_no = $orderfind['order'];            //订单号
        $spbill_create_ip = '122.112.230.149';          //服务器ip地址
        $total_fee = $fee*100;                     //需要支付的金额  *100  0.01元就是1
        $trade_type = 'JSAPI';                    

        $post['appid'] = $appid; 
        $post['body'] = $body;
        $post['mch_id'] = $mch_id;
        $post['nonce_str'] = $nonce_str;//随机字符串
        $post['notify_url'] = $notify_url;
        $post['openid'] = $openid;
        $post['out_trade_no'] = $out_trade_no;
        $post['spbill_create_ip'] = $spbill_create_ip;//终端的ip
        $post['total_fee'] = $total_fee;//总金额?
        $post['trade_type'] = $trade_type;
        $sign = $this->sign($post);//签名         
        $post_xml = '<xml>
            <appid>'.$appid.'</appid>
            <body>'.$body.'</body>
            <mch_id>'.$mch_id.'</mch_id>
            <nonce_str>'.$nonce_str.'</nonce_str>
            <notify_url>'.$notify_url.'</notify_url>
            <openid>'.$openid.'</openid>
            <out_trade_no>'.$out_trade_no.'</out_trade_no>
            <spbill_create_ip>'.$spbill_create_ip.'</spbill_create_ip>
            <total_fee>'.$total_fee.'</total_fee>
            <trade_type>'.$trade_type.'</trade_type>
            <sign>'.$sign.'</sign>
        ? </xml> ';
        $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
        $xml = $this->http_request($url,$post_xml);

        $array = $this->xml($xml);                        //全要大写
        if($array['return_code'] == 'SUCCESS' && $array['result_code'] == 'SUCCESS'){
            $time = time();
            $tmp = [];
            $tmp['appId'] = $appid;
            $tmp['nonceStr'] = $nonce_str;
            $tmp['package'] = 'prepay_id='.$array['prepay_id'];
            $tmp['signType'] = 'MD5';
            $tmp['timeStamp'] = "$time";

            $data['state'] = 200;
            $data['timeStamp'] = "$time";
            $data['nonceStr'] = $nonce_str;
            $data['signType'] = 'MD5';
            $data['package'] = 'prepay_id='.$array['prepay_id'];
            $data['paySign'] = $this->sign($tmp);
            $data['out_trade_no'] = $out_trade_no;
            $data['price'] = 1;
        }else{
            $data['state'] = 0;
            $data['text'] = "错误";
            $data['return_code'] = $array['return_code'];
            $data['result_code'] = $array['result_code'];
        }
        return json_encode($data); 
    }

    //随机32位字符串
    private function nonce_str()
    {
        $result = '';
        $str = 'QWERTYUIOPASDFGHJKLZXVBNMqwertyuioplkjhgfdsamnbvcxz';
        for($i=0;$i<32;$i++){
         $result .= $str[rand(0,48)]; 
        }
        return $result;
    }
    //生成订单号
    private function order_number($openid)
    {
        return md5($openid.time().rand(11,99)); //32位
    }
    //签名 $data要先排好顺序
    private function sign($data)
    {
        $stringA = '';
        foreach($data as $key=>$value){
             if(!$value) continue;
              if($stringA) $stringA.='&'.$key."=".$value;
              else $stringA = $key."=".$value;
        }
        $wx_key = '    ';                                       // 申请的key
        $stringSignTemp = $stringA.'&key='.$wx_key;
        return strtoupper(md5($stringSignTemp));
    }
    //curl请求
    public function http_request($url,$data=null,$headers=array())
    {
        $curl = curl_init();
        if(count($headers) >= 1){
          curl_setopt($curl,CURLOPT_HTTPHEADER,$headers);
        }
        curl_setopt($curl,CURLOPT_URL,$url);

        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if(!empty($data)){
          curl_setopt($curl,CURLOPT_POST,1);
          curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
        }
        curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
    //获取xml
    private function xml($xml){
        if(!$xml){
            return false;
        }
        //将XML转为array
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $data;
    }


    //微信支付回调地址
    public function syntonys()
    {

        $post = file_get_contents("php://input");
         
        if ($post == null || $post == '') {
            //阻止微信接口反复回调接口  文档地址 https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=9_7&index=7,下面这句非常重要!!!
            $str='<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';  
            echo $str;
            exit('Notify 非法回调');
        }

        $post_data = $this->xml($post);   //XML转数组

         //处理逻辑
        }
    }
}
?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值