首页  >  文章  >  后端开发  >  基于php开发app微信支付接口

基于php开发app微信支付接口

不言
不言原创
2018-05-25 14:46:143093浏览

这篇文章主要介绍了关于基于php开发app微信支付接口,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

定义好商户支付信息 

define(APPID, $payment ['appId']); // appid

 define(APPSECRET, $payment ['appSecret']); // appSecret

 define(MCHID, $payment ['partnerId']);//商户号
  define(KEY, $payment ['partnerKey']); // 通加密串

   define(NOTIFY_URL, $return_url); // 成功回调url

     //签名所需
        $signArray = array(
            'appid' => $payment['appId'], //appid
            'mch_id' => $payment['partnerId'],
            'nonce_str' => self::createNoncestr(),
            'out_trade_no' => $order['order_sn'],           
            'body' => $order['body'],
            "total_fee" => $order ['order_amount'],
            "notify_url" => $return_url,
            "spbill_create_ip" => $_SERVER["REMOTE_ADDR"],
            "trade_type" => "APP",
        );
 $sign=self::getSign($signArray); 
        //统一下单
        $goPay = array(
            'appid' => $payment['appId'], //appid
            'mch_id' => $payment['partnerId'],
            'nonce_str' => $signArray['nonce_str'], //生成随机字符串
            'sign' => $sign,
            'out_trade_no' => $order['order_sn'],            
            "total_fee" => $order ['order_amount'],
             'body' => $order['body'],
            "notify_url" => $return_url,
             "spbill_create_ip" => $_SERVER["REMOTE_ADDR"],
            "trade_type" => "APP",
        );
        //转化xml
        $goPayXml=self::arrayToXml($goPay);
        $result=self::sendPrePayCurl($goPayXml);
        return $result;

生成随机字符串函数

    //随机生成字符串
    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;
    }

签名函数

    /**
     * 	作用:生成签名
     */
    public function getSign($Obj) {
        foreach ($Obj as $k => $v) {
            //if($k == &#39;code&#39;) continue;
            //if($k == &#39;from&#39;) continue;
            $Parameters[$k] = $v;
        }
        //签名步骤一:按字典序排序参数
        ksort($Parameters);
        $String = self::formatBizQueryParaMap($Parameters, false);
        //echo &#39;【string1】&#39;.$String.&#39;</br>&#39;;
        //签名步骤二:在string后加入KEY
        $String = $String . "&key=" . KEY;       
        //echo "【string2】".$String."</br>";
        //签名步骤三:MD5加密
        $String = md5($String);
        //echo "【string3】 ".$String."</br>";
        //签名步骤四:所有字符转为大写
        $result_ = strtoupper($String);
        //echo "【result】 ".$result_."</br>";
        return $result_;
    }
 //格式化签名所需参数
    public function formatBizQueryParaMap($paraMap, $urlencode) {
        $buff = "";
        ksort($paraMap);
        foreach ($paraMap as $k => $v) {
            if ($urlencode) {
                $v = urlencode($v);
            }
            //$buff .= strtolower($k) . "=" . $v . "&";
            $buff .= $k . "=" . $v . "&";
        }
        $reqPar;
        if (strlen($buff) > 0) {
            $reqPar = substr($buff, 0, strlen($buff) - 1);
        }
        return $reqPar;
    }

把所要请求参数组装xml

	/**
	 * 	作用:array转xml,把请求参数组装xml
	 */
	function arrayToXml($arr)
    {
        $xml = "<xml>";
        foreach ($arr as $key=>$val)
        {
        	 if (is_numeric($val))
        	 {
        	 	$xml.="<".$key.">".$val."</".$key.">"; 

        	 }
        	 else
        	 	$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";  
        }
        $xml.="</xml>";
        return $xml; 
    }
//通过curl发送数据给微信接口的函数
    public function sendPrePayCurl($xmlData) {
        $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
        $header[] = "Content-type: text/xml";
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $xmlData);
        $data = curl_exec($curl);
        if (curl_errno($curl)) {
            print curl_error($curl);
        }
        curl_close($curl);
        return self::XMLDataParse($data);
    }

//xml格式数据解析函数
    public static function XMLDataParse($data) {
        $msg = array();
        $msg = (array) simplexml_load_string($data, &#39;SimpleXMLElement&#39;, LIBXML_NOCDATA);
        return $msg;
    }

相关推荐:

php开发电影网站视频教程课程介绍(仿爱奇艺)

以上是基于php开发app微信支付接口的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn