评论

tp5开发公众号微信支付

  1. 接收前端传过来的openid和支付金额moneys和用户昵称nickName,提交给sendMoney方法


  2. public function index(Request $request)
      
        {  
      
                 $openid = $request->param('openid');//获取openId
      
                 $money = $request->param('money');//支付金额
      
                 return $this->sendMoney($moneys,$openid,'支付简介',$nickName);
      
               }
      
    }
   

2.详解sendMoney方法

①将商户账户appid、商户号、随机字符串(见createNocestr方法)、商户订单号、用户的oepnid、支付金额、充值描述信息、ip地址、trade_type(这里用的是JSAPI)、回调地址拼成数组array

②过滤函数、拼接字符串

③将商户秘钥key和字符串拼接起来,用md5得到sign并且用strtoupper转换成大写

④拼接成xml格式(见arraytoxml)方法

⑤调用统一下单接口https://api.mch.weixin.qq.com/pay/unifiedorder

⑥判断如果回调函数return_code和result_code的值都为SUCCESS的话则进行二次签名(非常重要)

⑦将appId(必须大写)、nonceStr、package(拼接成prepay_id=)、signType(MD5)、timeStamp拼成数组,过滤函数,拼接字符串,商户key,md5加密并转换成大写,得到二次签名

public function sendMoney($amount,$re_openid,$desc='你的简介',$check_name=''){
        header("Content-Type: text/html; charset=utf-8");
        $total_amount = (100) * $amount;//充值的金额
        $data=array(
        'appid'=> '',//商户账号appid
        'mch_id'=> '',//商户号
        'nonce_str'=> $this->createNoncestr(),//随机字符串
        'out_trade_no'=> date('YmdHis').rand(1000, 9999),//商户订单号
        'openid'=> $re_openid,//用户openid
        'total_fee'=>$total_amount,//金额
        'body'=> $desc,//充值描述信息
        'spbill_create_ip'=> '',//Ip地址
        'trade_type'=>'JSAPI',
        'notify_url'=>''//你的回调地址
       );
        $secrect_key = '';//API密码
        $data = array_filter($data);//过滤函数
        ksort($data);
        $str ='';
        foreach($data as $k=>$v) {
           $str.=$k.'='.$v.'&';
        }
        $str.='key='.$secrect_key;//把秘钥和字符串拼接起来
        $data['sign'] = strtoupper(md5($str));//用md5得到sign  转换成大写
        $xml = $this->arraytoxml($data);//拼接成xml的格式
        $url='https://api.mch.weixin.qq.com/pay/unifiedorder'; //调用接口
        $res = $this->wx_curl($xml,$url);
        $return = $this->xmltoarray($res);
        $responseObj = simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA);
        $jsonStr = json_encode($responseObj);//转换为字符串
        $jsonArray = json_decode($jsonStr,true);//转换为数组
        if($jsonArray['return_code']==='SUCCESS' && $jsonArray['result_code']==='SUCCESS'){
             $time = time();//时间戳
             $info = array(
                 'appId'=>'',
                 'nonceStr'=>$jsonArray['nonce_str'],
                 'package'=>'prepay_id='.$jsonArray['prepay_id'],
                 'signType'=>'MD5',
                 'timeStamp'=>"".$time."",
             );
             $info_s = array_filter($info);//过滤函数
                ksort($info_s);
                $str_s ='';
                foreach($info as $k=>$v) {
                   $str_s.=$k.'='.$v.'&';
                }
            $str_s.='key=你的商户秘钥';//把秘钥和字符串拼接起来
            $info['sign']=strtoupper(md5($str_s));
             $infos = array(
                 'status'=>1,
                 'content'=>'success',
                 'result'=>$info
             );
             return $infos;
        }else{
          return json([
               "status"=>0,
               "content"=>$jsonArray['return_msg'],
               "result"=>$jsonArray['err_code_des']
            ]);
        }
     }

3.createNocestr随机字符串方法

public function createNoncestr($length =32){
$chars = "abcdefghijklmnopqrstuvwxyz0123456789"
$str ="";
for ( $i = 0; $i < $length; $i++ )  { 
    $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1); 
return $str;
}

4.arraytoxml拼接成xml

    public function arraytoxml($data){
        $str='<xml>';
        foreach($data as $k=>$v) {
            $str.='<'.$k.'>'.$v.'</'.$k.'>';
        }
        $str.='</xml>';
        return $str;
    }
/*拼接xml*/
public function xmltoarray($xml) {
libxml_disable_entity_loader(true);//禁止引用外部xml实体
$xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$val = json_decode(json_encode($xml),true);
return $val;
 }

5.MD5加密方法

public function sign($info)
{
    $key='';
    ksort($info);
    $buff = "";
    foreach ($info as $k => $v) {
        if($k!="sign" && $v!="" && !is_array($v)){
            $buff.="".$v."&";
        }
    }
    $buff = trim($buff,"&");
    $string=$buff."&key=".$key;
    $string = md5($string);
    $sign = strtoupper($string);
    return $sign;
}

到此tp5实现微信支付的方法就已经结束啦,直接返回给前端就可以啦,大家觉得不错的话给个赞哦!

最后一次编辑于  08-27  
点赞 0
收藏
评论