微信小程序支付 后台处理逻辑 (转)

<?php
namespace appparentcontroller;

use thinkRequest;

class Wxpay
{
    function wechat(){ //微信配置信息和初始逻辑

        $openid            =Request::instance()->param('openid'); 

        $out_trade_no    =Request::instance()->param('out_trade_no');

        $total_fee        =Request::instance()->param('total_fee');
        
        $appid            = "*******";

        $body            = mb_convert_encoding("订单详情","utf-8","gbk"); //商品描述

        $mch_id            = "*******"; //商户号

        $nonce_str         = $this->getNonceStr();  //随机字符

        $notify_url     = *******'; //回调地址

        $openid         = $openid;/*"o-E0L0X-hr-kXdHVrLwjzxF--sE4";*/ //openid

        $out_trade_no    = $out_trade_no; //商户订单编号

        $ip             = request()->ip(); //ip

        $total_fee        = $total_fee; //支付金额 分
    
        $key             = "*******";

        $param             = $this->signature($appid,$body,$mch_id,$nonce_str,$notify_url,$openid,$out_trade_no,$ip,$total_fee,$key);

        //发起请求
        $xml             = $this->post_curl("https://api.mch.weixin.qq.com/pay/unifiedorder",$param); //发起请求
        //数据结果解析
        $info             = json_decode(json_encode(simplexml_load_string($xml,'SimpleXMLElement',LIBXML_NOCDATA)),true);        

        $prepay_id        = $info['prepay_id'];
        
        $noncestr         = $info['nonce_str'];

        $timestamp         = time();

        $SignA = strtoupper(md5("appid=$appid&noncestr=$noncestr&package=Sign=WXPay&partnerid=$mch_id&prepayid=$prepay_id&timestamp=$timestamp&key=$key"));
        
        $info['sign']    = $SignA;
        
        $info['timestamp'] = $timestamp;

        return json_decode($info);
    }    


    private function signature($appid,$body,$mch_id,$nonce_str,$notify_url,$openid,$out_trade_no,$ip,$total_fee,$key){ //清新支付请求数据组装
        $stringA = "appid=$appid&body=$body&mch_id=$mch_id&nonce_str=$nonce_str&notify_url=$notify_url&openid=$openid&out_trade_no=$out_trade_no&spbill_create_ip=$ip&total_fee=$total_fee&trade_type=JSAPI";
        $stringSignTemp = $stringA."&key=$key";
        $sign  = strtoupper(md5($stringSignTemp));
        $param = "<xml> ";
        $param .= "<appid>{$appid}</appid> ";
        $param .= "<body>{$body}</body> ";      
        $param .= "<mch_id>{$mch_id}</mch_id> ";
        $param .= "<nonce_str>{$nonce_str}</nonce_str> ";
        $param .= "<notify_url>{$notify_url}</notify_url> ";
        $param .= "<openid>{$openid}</openid> ";
        $param .= "<out_trade_no>{$out_trade_no}</out_trade_no> ";
        $param .= "<spbill_create_ip>{$ip}</spbill_create_ip> ";
        $param .= "<total_fee>{$total_fee}</total_fee> ";
        $param .= "<trade_type>JSAPI</trade_type> ";
        $param .= "<sign>{$sign}</sign> ";
        $param .= "</xml>";
        return $param;
    }

    /**
    * 产生随机字符串,不长于32位
    * @param int $length
    * @return 产生的随机字符串
    */
    private function getNonceStr($length = 32)
    {
        $chars = "abcdefghijklmnopqrstuvwxyz0123456789";  
        $str ="";
        for ( $i = 0; $i < $length; $i++ )  {  
            $str .= substr($chars, mt_rand(0, strlen($chars)-1), 1);  
        }
        return $str;
    }

    private function post_curl($url,$data,$agreement = 0){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        if($agreement == 0){//0 https   1   http
            unset($_REQUEST['agreement']);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        }
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
}

转载自:https://blog.csdn.net/qq_34629975/article/details/72420411

原文地址:https://www.cnblogs.com/myJuly/p/10077742.html