<samp id="2sk8i"><acronym id="2sk8i"></acronym></samp>
  • <sup id="2sk8i"><tbody id="2sk8i"></tbody></sup><center id="2sk8i"></center>
    <fieldset id="2sk8i"></fieldset>
  • '.$body.'<\/body>\n           '.$mch_id.'<\/mch_id>\n           '.$nonce_str.'<\/nonce_str>\n           '.$notify_url.'<\/notify_url>\n           '.$openid.'<\/openid>\n           '.$out_trade_no.'<\/out_trade_no>\n           '.$spbill_create_ip.'<\/spbill_create_ip>\n           '.$total_fee.'<\/total_fee>\n           '.$trade_type.'<\/trade_type>\n           '.$sign.'<\/sign>\n        <\/xml> ';\n        \/\/統(tǒng)一接口prepay_id\n        $url = 'https:\/\/api.mch.weixin.qq.com\/pay\/unifiedorder';\n        $xml = $this->http_request($url,$post_xml);\n        $array = $this->xml($xml);\/\/全要大寫(xiě)\n        if($array['RETURN_CODE'] == 'SUCCESS' && $array['RESULT_CODE'] == 'SUCCESS'){\n            $time = time();\n            $tmp='';\/\/臨時(shí)數(shù)組用于簽名\n            $tmp['appId'] = $appid;\n            $tmp['nonceStr'] = $nonce_str;\n            $tmp['package'] = 'prepay_id='.$array['PREPAY_ID'];\n            $tmp['signType'] = 'MD5';\n            $tmp['timeStamp'] = \"$time\";\n            $data['state'] = 1;\n            $data['timeStamp'] = \"$time\";\/\/時(shí)間戳\n            $data['nonceStr'] = $nonce_str;\/\/隨機(jī)字符串\n            $data['signType'] = 'MD5';\/\/簽名算法,暫支持 MD5\n            $data['package'] = 'prepay_id='.$array['PREPAY_ID'];\/\/統(tǒng)一下單接口返回的 prepay_id 參數(shù)值,提交格式如:prepay_id=*\n            $data['paySign'] = $this->sign($tmp);\/\/簽名,具體簽名方案參見(jiàn)微信公眾號(hào)支付幫助文檔;\n            $data['out_trade_no'] = $out_trade_no;\n        }else{\n            $data['state'] = 0;\n            $data['text'] = \"錯(cuò)誤\";\n            $data['RETURN_CODE'] = $array['RETURN_CODE'];\n            $data['RETURN_MSG'] = $array['RETURN_MSG'];\n        }\n      echo json_encode($data);\n    }\n\/\/隨機(jī)32位字符串\n    private function nonce_str(){\n        $result = '';\n        $str = 'QWERTYUIOPASDFGHJKLZXVBNMqwertyuioplkjhgfdsamnbvcxz';\n        for ($i=0;$i<32;$i++){\n            $result .= $str[rand(0,48)];\n        }\n        return $result;\n    }\n\/\/生成訂單號(hào)\n    private function order_number($openid){\n        \/\/date('Ymd',time()).time().rand(10,99);\/\/18位\n        return md5($openid.time().rand(10,99));\/\/32位\n    }\n\/\/簽名 $data要先排好順序\n    public function sign($data)\n    {\n        $stringA = '';\n        foreach ($data as $key => $value) {\n            if (!$value) continue;\n            if ($stringA) $stringA .= '&' . $key . \"=\" . $value;\n            else $stringA = $key . \"=\" . $value;\n        }\n        $wx_key = 'Zhangyusheng19810318015729366660';\/\/申請(qǐng)支付后有給予一個(gè)商戶賬號(hào)和密碼,登陸后自己設(shè)置key\n        $stringSignTemp = $stringA . '&key=' . $wx_key;\/\/申請(qǐng)支付后有給予一個(gè)商戶賬號(hào)和密碼,登陸后自己設(shè)置key \n      return strtoupper(md5($stringSignTemp));\n    }\n\/\/curl請(qǐng)求啊\n        function http_request($url, $data = null, $headers = array())\n        {\n            $curl = curl_init();\n            if (count($headers) >= 1) {\n                curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);\n            }\n            curl_setopt($curl, CURLOPT_URL, $url);\n            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);\n            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);\n            if (!empty($data)) {\n                curl_setopt($curl, CURLOPT_POST, 1);\n                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);\n            }\n            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);\n            $output = curl_exec($curl);\n            curl_close($curl);\n            return $output;\n        }\n\/\/獲取xml\n        private function xml($xml){\n            $p = xml_parser_create();\n            xml_parse_into_struct($p, $xml, $vals, $index);\n            xml_parser_free($p);\n            $data = \"\";\n            foreach ($index as $key=>$value) {\n                if($key == 'xml' || $key == 'XML') continue;\n                $tag = $vals[$value[0]]['tag'];\n                $value = $vals[$value[0]]['value'];\n                $data[$tag] = $value;\n            }\n            return $data;\n        }\n\/\/微信支付結(jié)束<\/pre>

    If you want to learn more about programming, please pay attention to the php training<\/a> column!
    <\/p><\/blockquote>"}

    亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

    Home WeChat Applet Mini Program Development How to implement mini program payment function

    How to implement mini program payment function

    Aug 31, 2020 am 10:41 AM
    Applets pay

    Method to implement the mini program payment function: first request payment on the front end, and request the WeChat server on the back end; then the back end accepts the WeChat server and returns the data; then the front end initiates payment; finally the back end accepts the WeChat server callback That’s it.

    How to implement mini program payment function

    【Related learning recommendations: 小program development tutorial

    Methods to implement the mini program payment function:

    1. Front-end request for payment

    Front-end request for payment is to simply carry the payment requirements Data, such as user ID, payment amount, payment order ID, etc. are related to your business logic or related to the data required for the next step of requesting the WeChat server payment unified order interface**, use the of the WeChat applet wx.request( ) To request the backend payment interface.

    2. The backend requests the WeChat server

    After the backend receives the payment request sent by the frontend, it can perform related verifications, such as determining whether the user has any problems. Is the payment amount correct, etc.? After verifying that there is no problem and you can apply for payment to the WeChat server, the backend needs to use the data format specified by WeChat to request WeChat's unified payment order interface.

    After processing all the data, organize the data in XML format and send it to the WeChat payment unified ordering interface using the POST method

    3. The backend accepts the data returned by the WeChat server

    After the WeChat server receives the payment data, if there is no problem with the data, it will return the corresponding data for payment. The most important one is the data field named prepay_id, which needs to be returned. Front-end, the front-end can continue to pay.

    Therefore, after the back-end receives the return data from the WeChat server, it needs to perform corresponding processing and finally return the data to the front-end. The back-end payment interface has completed receiving the front-end payment request and returned the front-end payment request. Functions that require data.

    4. Front-end initiates payment

    After receiving the returned data, the front-end uses wx.requestPayment() to request payment initiation. The values ??of the object parameters required by this API are the data returned in the previous step.

    5. The backend accepts the WeChat server callback

    After the frontend completes the payment, the WeChat server confirms that the payment has been completed. A notification will be sent to the callback address set in the first step. After receiving the notification, the back-end receiving callback interface can determine whether the payment is completed and determine subsequent actions.

    After confirming the payment, the WeChat server will determine whether the payment was successful based on the notification result_code field. After receiving the successful notification, the backend needs to return success data to the WeChat server to inform the WeChat server that a callback notification has been received to complete the payment process. Otherwise, the WeChat server will continue to send messages to the backend.

    After comparison, it can be found that in fact, payment in the mini program is much easier than payment with the official account, because there is no need to pay for the authorized directory or authorized domain name, but the payment process has one more step than the official account. That is, the unified order is prepaid, and then the prepaid result needs to be signed again before payment can be initiated.

    The complete code is as follows:

    //小程序端代碼:
    pay:function(){
    var that=this
    wx.getStorage({
    key: &#39;openid&#39;,
    success: function(res) {
    wx.request({
    //這里是后臺(tái)的處理方法,url是自定義的,直接換成你自己的后臺(tái)處理方法即可,Wx_Pay這個(gè)方法在下面寫(xiě)的有
    //后臺(tái)用的php做處理,java的可以參考方法,道理都是一樣的
    url: url + &#39;Wx_Pay&#39;,
    data: {
    //用戶的openid
    openid:res.data,
    fee: that.data.totalPrice, //支付金額
    details: that.data.goodsList[0].goods_name,//支付商品的名稱
    },
    success:function(result){
    if(result.data){
    //out_trade_no=res.data[&#39;out_trade_no&#39;];
    wx.requestPayment({
    timeStamp: result.data[&#39;timeStamp&#39;],
    nonceStr: result.data[&#39;nonceStr&#39;],
    package: result.data[&#39;package&#39;],
    signType: &#39;MD5&#39;,
    paySign: result.data[&#39;paySign&#39;],
    &#39;success&#39;:function(successret){
    console.log(&#39;支付成功&#39;);
    //獲取支付用戶的信息
    wx.getStorage({
    key: &#39;userInfo&#39;,
    success: function (getuser) {
    //加入訂單表做記錄
    wx.request({
    url: url + &#39;Wx_AddOrder&#39;,
    data: {
    uname: getuser.data.nickName,
    goods: that.data.goodsList[0].goods_name,
    price: that.data.totalPrice,
    openid:res.data,
    },
    success: function (lastreturn) {
    console.log("存取成功");
    }
    })
    },
    })
    },&#39;fail&#39;:function(res){
    }
    })
    }
    }
    })
    },
    })
    },
    //后臺(tái)
    //微信支付
        public function Wx_Pay(){
            $request=Request::instance();
            $fee=$request->param(&#39;fee&#39;);
            $details=$request->param(&#39;details&#39;);//商品的詳情,比如iPhone8,紫色
           // $fee = 0.01;//舉例充值0.01
            $appid =        &#39;appid&#39;;//appid
            $body =        $details;// &#39;金邦匯商城&#39;;//&#39;【自己填寫(xiě)】&#39;
            $mch_id =       &#39;1486742092&#39;;//&#39;你的商戶號(hào)【自己填寫(xiě)】&#39;
            $nonce_str =    $this->nonce_str();//隨機(jī)字符串
            $notify_url =   &#39;https://zys.jinbh.cn/admin/Api/Wx_Speech&#39;;//回調(diào)的url【自己填寫(xiě)】&#39;;
            $openid =       $request->param(&#39;openid&#39;);//&#39;用戶的openid【自己填寫(xiě)】&#39;;
            $out_trade_no = $this->order_number($openid);//商戶訂單號(hào)
            $spbill_create_ip = &#39;123.206.45.131&#39;;//&#39;服務(wù)器的ip【自己填寫(xiě)】&#39;;
            $total_fee =    $fee*100;//因?yàn)槌渲到痤~最小是1 而且單位為分 如果是充值1元所以這里需要*100
            $trade_type = &#39;JSAPI&#39;;//交易類型 默認(rèn)
            //這里是按照順序的 因?yàn)橄旅娴暮灻前凑枕樞?排序錯(cuò)誤 肯定出錯(cuò)
            $post[&#39;appid&#39;] = $appid;
            $post[&#39;body&#39;] = $body;
            
            $post[&#39;mch_id&#39;] = $mch_id;
          
            $post[&#39;nonce_str&#39;] = $nonce_str;//隨機(jī)字符串
          
            $post[&#39;notify_url&#39;] = $notify_url;
          
            $post[&#39;openid&#39;] = $openid;
          
            $post[&#39;out_trade_no&#39;] = $out_trade_no;
          
            $post[&#39;spbill_create_ip&#39;] = $spbill_create_ip;//終端的ip
          
            $post[&#39;total_fee&#39;] = $total_fee;//總金額 最低為一塊錢(qián) 必須是整數(shù)
         
            $post[&#39;trade_type&#39;] = $trade_type;
            $sign = $this->sign($post);//簽名
            $post_xml = &#39;<xml>
               <appid>&#39;.$appid.&#39;</appid>
               <body>&#39;.$body.&#39;</body>
               <mch_id>&#39;.$mch_id.&#39;</mch_id>
               <nonce_str>&#39;.$nonce_str.&#39;</nonce_str>
               <notify_url>&#39;.$notify_url.&#39;</notify_url>
               <openid>&#39;.$openid.&#39;</openid>
               <out_trade_no>&#39;.$out_trade_no.&#39;</out_trade_no>
               <spbill_create_ip>&#39;.$spbill_create_ip.&#39;</spbill_create_ip>
               <total_fee>&#39;.$total_fee.&#39;</total_fee>
               <trade_type>&#39;.$trade_type.&#39;</trade_type>
               <sign>&#39;.$sign.&#39;</sign>
            </xml> &#39;;
            //統(tǒng)一接口prepay_id
            $url = &#39;https://api.mch.weixin.qq.com/pay/unifiedorder&#39;;
            $xml = $this->http_request($url,$post_xml);
            $array = $this->xml($xml);//全要大寫(xiě)
            if($array[&#39;RETURN_CODE&#39;] == &#39;SUCCESS&#39; && $array[&#39;RESULT_CODE&#39;] == &#39;SUCCESS&#39;){
                $time = time();
                $tmp=&#39;&#39;;//臨時(shí)數(shù)組用于簽名
                $tmp[&#39;appId&#39;] = $appid;
                $tmp[&#39;nonceStr&#39;] = $nonce_str;
                $tmp[&#39;package&#39;] = &#39;prepay_id=&#39;.$array[&#39;PREPAY_ID&#39;];
                $tmp[&#39;signType&#39;] = &#39;MD5&#39;;
                $tmp[&#39;timeStamp&#39;] = "$time";
                $data[&#39;state&#39;] = 1;
                $data[&#39;timeStamp&#39;] = "$time";//時(shí)間戳
                $data[&#39;nonceStr&#39;] = $nonce_str;//隨機(jī)字符串
                $data[&#39;signType&#39;] = &#39;MD5&#39;;//簽名算法,暫支持 MD5
                $data[&#39;package&#39;] = &#39;prepay_id=&#39;.$array[&#39;PREPAY_ID&#39;];//統(tǒng)一下單接口返回的 prepay_id 參數(shù)值,提交格式如:prepay_id=*
                $data[&#39;paySign&#39;] = $this->sign($tmp);//簽名,具體簽名方案參見(jiàn)微信公眾號(hào)支付幫助文檔;
                $data[&#39;out_trade_no&#39;] = $out_trade_no;
            }else{
                $data[&#39;state&#39;] = 0;
                $data[&#39;text&#39;] = "錯(cuò)誤";
                $data[&#39;RETURN_CODE&#39;] = $array[&#39;RETURN_CODE&#39;];
                $data[&#39;RETURN_MSG&#39;] = $array[&#39;RETURN_MSG&#39;];
            }
          echo json_encode($data);
        }
    //隨機(jī)32位字符串
        private function nonce_str(){
            $result = &#39;&#39;;
            $str = &#39;QWERTYUIOPASDFGHJKLZXVBNMqwertyuioplkjhgfdsamnbvcxz&#39;;
            for ($i=0;$i<32;$i++){
                $result .= $str[rand(0,48)];
            }
            return $result;
        }
    //生成訂單號(hào)
        private function order_number($openid){
            //date(&#39;Ymd&#39;,time()).time().rand(10,99);//18位
            return md5($openid.time().rand(10,99));//32位
        }
    //簽名 $data要先排好順序
        public function sign($data)
        {
            $stringA = &#39;&#39;;
            foreach ($data as $key => $value) {
                if (!$value) continue;
                if ($stringA) $stringA .= &#39;&&#39; . $key . "=" . $value;
                else $stringA = $key . "=" . $value;
            }
            $wx_key = &#39;Zhangyusheng19810318015729366660&#39;;//申請(qǐng)支付后有給予一個(gè)商戶賬號(hào)和密碼,登陸后自己設(shè)置key
            $stringSignTemp = $stringA . &#39;&key=&#39; . $wx_key;//申請(qǐng)支付后有給予一個(gè)商戶賬號(hào)和密碼,登陸后自己設(shè)置key 
          return strtoupper(md5($stringSignTemp));
        }
    //curl請(qǐng)求啊
            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){
                $p = xml_parser_create();
                xml_parse_into_struct($p, $xml, $vals, $index);
                xml_parser_free($p);
                $data = "";
                foreach ($index as $key=>$value) {
                    if($key == &#39;xml&#39; || $key == &#39;XML&#39;) continue;
                    $tag = $vals[$value[0]][&#39;tag&#39;];
                    $value = $vals[$value[0]][&#39;value&#39;];
                    $data[$tag] = $value;
                }
                return $data;
            }
    //微信支付結(jié)束

    If you want to learn more about programming, please pay attention to the php training column!

    The above is the detailed content of How to implement mini program payment function. For more information, please follow other related articles on the PHP Chinese website!

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Hot Topics

    PHP Tutorial
    1488
    72
    How to pay for a taxi ride on Baidu Maps. Introduction to the payment steps for a taxi ride. How to pay for a taxi ride on Baidu Maps. Introduction to the payment steps for a taxi ride. Mar 13, 2024 am 10:04 AM

    Baidu Map APP has now become the preferred travel navigation software for many users, so some of the functions here are comprehensive and can be selected and operated for free to solve some of the problems that you may encounter in daily travel. You can all check some of your own travel routes and plan some of your own travel plans. After checking the corresponding routes, you can choose appropriate travel methods according to your own needs. So whether you choose some public transportation, Cycling, walking or taking a taxi can all satisfy your needs. There are corresponding navigation routes that can successfully lead you to a certain place. Then everyone will feel more convenient if they choose to take a taxi. There are many drivers They are all able to take orders online, and taxi-hailing has become super

    Develop WeChat applet using Python Develop WeChat applet using Python Jun 17, 2023 pm 06:34 PM

    With the popularity of mobile Internet technology and smartphones, WeChat has become an indispensable application in people's lives. WeChat mini programs allow people to directly use mini programs to solve some simple needs without downloading and installing applications. This article will introduce how to use Python to develop WeChat applet. 1. Preparation Before using Python to develop WeChat applet, you need to install the relevant Python library. It is recommended to use the two libraries wxpy and itchat here. wxpy is a WeChat machine

    Implement card flipping effects in WeChat mini programs Implement card flipping effects in WeChat mini programs Nov 21, 2023 am 10:55 AM

    Implementing card flipping effects in WeChat mini programs In WeChat mini programs, implementing card flipping effects is a common animation effect that can improve user experience and the attractiveness of interface interactions. The following will introduce in detail how to implement the special effect of card flipping in the WeChat applet and provide relevant code examples. First, you need to define two card elements in the page layout file of the mini program, one for displaying the front content and one for displaying the back content. The specific sample code is as follows: &lt;!--index.wxml--&gt;&l

    Pay using PHP and PayPal API Pay using PHP and PayPal API Jun 19, 2023 pm 04:13 PM

    With the increasing popularity of online transactions, payment methods are gradually diversifying, among which PayPal is very popular as a widely used payment method. If you want to use PayPal to process transactions on your website or application, then you can use PHP and PayPal API to complete the payment process easily. PayPalAPI is a set of programming interfaces for interacting with PayPal. Through the API, you can receive notifications from PayPal, query the latest transaction information, and initiate payments.

    Alipay launched the 'Chinese Character Picking-Rare Characters' mini program to collect and supplement the rare character library Alipay launched the 'Chinese Character Picking-Rare Characters' mini program to collect and supplement the rare character library Oct 31, 2023 pm 09:25 PM

    According to news from this site on October 31, on May 27 this year, Ant Group announced the launch of the "Chinese Character Picking Project", and recently ushered in new progress: Alipay launched the "Chinese Character Picking-Uncommon Characters" mini program to collect collections from the society Rare characters supplement the rare character library and provide different input experiences for rare characters to help improve the rare character input method in Alipay. Currently, users can enter the "Uncommon Characters" applet by searching for keywords such as "Chinese character pick-up" and "rare characters". In the mini program, users can submit pictures of rare characters that have not been recognized and entered by the system. After confirmation, Alipay engineers will make additional entries into the font library. This website noticed that users can also experience the latest word-splitting input method in the mini program. This input method is designed for rare words with unclear pronunciation. User dismantling

    How uniapp achieves rapid conversion between mini programs and H5 How uniapp achieves rapid conversion between mini programs and H5 Oct 20, 2023 pm 02:12 PM

    How uniapp can achieve rapid conversion between mini programs and H5 requires specific code examples. In recent years, with the development of the mobile Internet and the popularity of smartphones, mini programs and H5 have become indispensable application forms. As a cross-platform development framework, uniapp can quickly realize the conversion between small programs and H5 based on a set of codes, greatly improving development efficiency. This article will introduce how uniapp can achieve rapid conversion between mini programs and H5, and give specific code examples. 1. Introduction to uniapp unia

    How uniapp application implements payment and order management How uniapp application implements payment and order management Oct 19, 2023 am 10:37 AM

    uniapp is a cross-platform application development framework that can develop small programs, Apps and H5 at the same time. In uniapp applications, payment and order management are very common needs. This article will introduce how to implement payment functions and order management in the uniapp application, and give specific code examples. 1. Implementing the payment function The payment function is the key to realizing online transactions, and it usually requires integrating the SDK of a third-party payment platform. The following are the specific steps to implement the payment function in uniapp: Register and obtain a third-party payment platform

    Tutorial on writing a simple chat program in Python Tutorial on writing a simple chat program in Python May 08, 2023 pm 06:37 PM

    Implementation idea: Establishing the server side of thread, so as to process the various functions of the chat room. The establishment of the x02 client is much simpler than the server. The function of the client is only to send and receive messages, and to enter specific characters according to specific rules. To achieve the use of different functions, therefore, on the client side, you only need to use two threads, one is dedicated to receiving messages, and the other is dedicated to sending messages. As for why not use one, that is because, only

    See all articles