微信小程序下单带调起支付 ——PHP(简易版本)

微信小程序下单带调起支付 ——PHP(简易版本)

老板们觉着自己项目流量较大可以自行在生成订单时进行锁单

不吹牛批了,上代码——觉着我们手艺人不错的可以点点关注哈(笑出了猪叫声)


/**
 * 公司名 小程序名
 * @Descript: 订单模型
 * @Filename: XcxOrder.php
 * @Author:   cjr
 * @Date: 2019-8-21 14:16:44
 * @Last Modified by: NULL
 * @Last Modified time: NULL
 */
class Model_XcxOrder  extends  Model
{
    public $response;
    public $parameters;
    public $url;//统一下单接口
    public $curl_timeout;//curl超时时间
    
    function __construct ()
    {
        //统一下单
        $this->url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
        //设置curl超时时间
        $this->curl_timeout = 30;
    }
    
    /**
     * 生成签名
     * @return array
     */
    public function getXCXPreResult ()
    {
        $this->postXcxXml ();
        $result = $this->xmlToArray ($this->response);
        $arr = [
            'appId' => $result['appid'] ,
            'timeStamp' => (string) time () ,
            'nonceStr' => $this->createNoncestr () ,//随机字符串
            'package' => 'prepay_id=' . $result['prepay_id'] ,
            'signType' => 'MD5'
        ];
        $arr['paySign'] = $this->getXcxSign ($arr);//签名
        return $arr;
    }
    
    /**
     * 产生随机字符串,不长于32位
     * @param $length int 随机长度
     * @return string
     */
    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;
    }
    
    /**
     * 获取prepay_id
     * @return int
     */
    public function getPrepayId ()
    {
        $this->postXml ();
        $return_info = $this->xmlToArray ($this->response);
        $prepay_id = $return_info["prepay_id"];
        return $prepay_id;
    }
    
    /**
     * array转xml
     * @param $arr array 需求数组
     * @return xml
     */
    public function arrayToXml ($arr)
    {
        $xml = "";
        foreach( $arr as $key => $val ) {
            if( is_numeric ($val) ) {
                $xml .= "<" . $key . ">" . $val . " . $key . ">";
                
            } else
                $xml .= "<" . $key . "> . $val . "]]> . $key . ">";
        }
        $xml .= "";
        return $xml;
    }
    
    /**
     * 将xml转为array
     * @param $xml xml
     * @return array
     */
    public function xmlToArray ($xml)
    {
        //将XML转为array
        $array_data = json_decode (json_encode (simplexml_load_string ($xml , 'SimpleXMLElement' , LIBXML_NOCDATA)) , true);
        return $array_data;
    }
    
    /**
     * 以post方式提交xml到对应的接口url
     * @param $xml xml
     * @param $url string
     * @param $second int
     * @return object
     */
    public function postXmlCurl ($xml , $url , $second = 30)
    {
        //初始化curl
        $ch = curl_init ();
        curl_setopt ($ch , CURLOPT_TIMEOUT , $second);
        curl_setopt ($ch , CURLOPT_URL , $url);
        curl_setopt ($ch , CURLOPT_SSL_VERIFYPEER , FALSE);
        curl_setopt ($ch , CURLOPT_SSL_VERIFYHOST , FALSE);
        curl_setopt ($ch , CURLOPT_HEADER , FALSE);
        curl_setopt ($ch , CURLOPT_RETURNTRANSFER , TRUE);
        curl_setopt ($ch , CURLOPT_POST , TRUE);
        curl_setopt ($ch , CURLOPT_POSTFIELDS , $xml);
        $data = curl_exec ($ch);
        curl_close ($ch);
        return $data;
    }
    
    public function postXcxXml ()
    {
        $xml = $this->createXcxXml ();
        $this->response = $this->postXmlCurl ($xml , $this->url , $this->curl_timeout);
        return $this->response;
    }
    
    /*** @return string  小程序获取支付信息
     */
    public function createXcxXml ()
    {
        $this->parameters["appid"] = '';//小程序ID
        $this->parameters["mch_id"] = '';//商户号
        $this->parameters["nonce_str"] = $this->createNoncestr ();//随机字符串
        $this->parameters["spbill_create_ip"] = $_SERVER['REMOTE_ADDR'];//终端IP
        $this->parameters["trade_type"] = 'JSAPI';//支付类型
        $this->parameters["sign"] = $this->getXcxSign ($this->parameters);//签名
        return $this->arrayToXml ($this->parameters);
    }
    
    public function getXcxSign ($Obj)
    {
        foreach( $Obj as $k => $v ) {
            $Parameters[$k] = $v;
        }
        ksort ($Parameters);
        $String = $this->formatBizQueryParaMap ($Parameters , false);
        $String = $String . "&key=9574d579ea12f60cd974a57e138c78b2";
        $String = md5 ($String);
        $result_ = strtoupper ($String);
        return $result_;
    }
    
    /**
     * 格式化参数,签名过程需要使用
     * @param array $paraMap
     * @param string $urlencode
     * @return string
     */
    public function formatBizQueryParaMap ($paraMap , $urlencode)
    {
        $buff = "";
        ksort ($paraMap);
        foreach( $paraMap as $k => $v ) {
            if( $urlencode ) {
                $v = urlencode ($v);
            }
            $buff .= $k . "=" . $v . "&";
        }
        $reqPar = '';
        if( strlen ($buff) > 0 ) {
            $reqPar = substr ($buff , 0 , strlen ($buff) - 1);
        }
        return $reqPar;
    }
}

这么用——
//微信支付
$payModel = (new Model_XcxOrder());
$payModel->parameters[‘notify_url’] = ‘’;//支付回调地址
$payModel->parameters[“total_fee”] = $params[‘price’];//标价金额
/生成订单/
$payModel->parameters[“out_trade_no”] = Model_XcxOrder::wxPayBill();//这里可以自行处理订单流量
$payModel->parameters[“body”] = ‘XXX小程序订单’;//订单描述
/当前用户openid/
$payModel->parameters[‘openid’] = $params[‘openid’];//小程序openid
$return_arr = $payModel->getXCXPreResult();//这一堆给了小程序就可以掉起支付了

你可能感兴趣的:(PHP)