Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ThinkPhp5.1 script to make WeChat Pay

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge of this article "how to use ThinkPhp5.1 script to make WeChat Pay", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use ThinkPhp5.1 script to make WeChat Pay" article.

1 WeChat Pay

[payment by scanning code]

Public function wxPayImg ($body,$out_trade_no,$fee,$product_id,$logo,$path,$attach,$pro_id) {require_once Env::get ('app_path'). "api/wxpay/lib/WxPay.Api.php"; / / instantiate configuration information $config = new WxPayConfig (); $input = new\ WxPayUnifiedOrder (); / / set item description $input- > SetBody ($body) / / set order number $input- > SetOut_trade_no ($out_trade_no); / / set commodity amount (in cents) $input- > SetTotal_fee ($fee); / / set asynchronous notification address $notify = $config- > GetNotifyUrl (); $input- > SetNotify_url ($notify); / / set transaction type $input- > SetTrade_Type ('NATIVE'); / / set commodity ID $input- > SetProduct_id ($product_id) $input- > SetDevice_info ($pro_id); $input- > SetAttach ($attach); / / call Unified order API $result =\ WxPayApi::unifiedOrder ($config,$input); / / dump ($result); $qr_url = $result ['code_url']; $img = $this- > createCode ($qr_url,$logo,$path); / / generate array $array = array (' img'= > $img,'out_trade_no'= > $out_trade_no); return $array } / / generate public function createCode ($code_url,$logo,$path) {/ / create basic QR code $qrCode = new QrCode ($code_url); $qrCode- > setSize (300); / / set advanced options $qrCode- > setWriterByName ('png'); $qrCode- > setEncoding (' UTF-8'); $qrCode- > setErrorCorrectionLevel (ErrorCorrectionLevel::HIGH ()) $qrCode- > setForegroundColor (['r'= > 0,'g'= > 0,'b' = > 0,'a'= > 0]); $qrCode- > setBackgroundColor (['r'= > 255,'g'= > 255,'b' = > 255,'a'= > 0]); / / $qrCode- > setLabel ('', 16, App::getAppPath (). /. / public/static/user/font/msyhl.ttc', LabelAlignment::CENTER ()); $qrCode- > setLogoPath ($logo) $qrCode- > setLogoWidth (50); $qrCode- > setValidateResult (false); / / margin setting $qrCode- > setMargin (10); / / Direct output of the QR code header ('Content-Type:'. $qrCode- > getContentType ()); / / echo $qrCode- > writeString (); / / Save the QR code to the specified directory $qrCode- > writeFile ($path); / / generate data URI to inline image data

Within the tag) $dataUri = $qrCode- > writeDataUri (); return $dataUri;}

[payment via JSAPI interface]

Public function jsApiPay ($openId,$Body,$out_trade_no,$fee,$product_id,$attach) {require_once Env::get ('app_path'). "api/wxpay/lib/WxPay.Api.php"; $tools = new JsApiPay (); $input = new\ WxPayUnifiedOrder (); / / set item description $input- > SetBody ($Body); / / set order number $input- > SetOut_trade_no ($out_trade_no) / / set commodity amount (in minutes) $input- > SetTotal_fee ($fee); / / set asynchronous notification address $config = new WxPayConfig (); $notify = $config- > GetNotifyUrl (); $input- > SetNotify_url ($notify); / / set transaction type $input- > SetTrade_Type ('JSAPI'); / / set commodity ID $input- > SetProduct_id ($product_id); / / user openID $input- > SetOpenid ($openId) $input- > SetAttach ($attach); / / call Unified order API $result =\ WxPayApi::unifiedOrder ($config,$input); $jsApiParameters = $tools- > GetJsApiParameters ($result); return $jsApiParameters;}

[order query]

Public function QueryOrder ($number,$transaction_id=NULL) {require_once App::getAppPath (). 'api/wxpay/lib/WxPay.Api.php'; require_once App::getAppPath ().' api/wxpay/lib/WxPay.Notify.php'; $input = new\ WxPayOrderQuery (); $input- > SetOut_trade_no ($number); $input- > SetTransaction_id ($transaction_id); $config = new WxPayConfig (); $result =\ WxPayApi::orderQuery ($config, $input) If ($result ['result_code'] = =' SUCCESS') {if ($result ['trade_state'] = =' SUCCESS') {$arr = json_decode ($result ['attach'], true); $pay_time = $this- > getPayTime ($result [' time_end']) Return ['code'= > 100 result codebook = > $result [' result_code'], 'attach'= > $arr,'pay_time'= > $pay_time,'result'= > $result,'msg'= > "payment success"];} else {return [' code'= > 101 camera result codebook = > $result ['result_code'],' err_code'= > $result ['err_code'],' result'= > $result,'msg'= > "order not paid"] }} else {return ['code'= > 103 attach parameters = > $result [' result_code'], 'result'= > $result,'msg'= > "order does not exist"];} / / dump ($result); / / return $result;} 2 setting of attach parameters when WeChat Pay

Attach, official explanation: additional data, which is returned as is in query API and payment notification, can be used as a custom parameter. In practice, this field will be returned only if the payment status is completed. Sometimes we need to return some specific values when querying the order, such as member ID, payment order ID, application ID, member name and so on. We can use attach to pass parameters, but attach is a string, and sometimes the value is an array, so you only need to convert it to json_encode, for example:

$remarks = array ('cid'= > 1,' member_id'= > 2, 'apply_id'= > 28,); $attach = json_encode ($remarks)

After querying the order, convert it with json_decode, for example:

$arr = json_decode ($result ['attach'], true)

The $arr obtained here is an array, which can be called directly according to the calling rules of the array.

3 several states of WeChat Pay

When the queried order information does not exist, it is returned as follows:

Array (9) {["appid"] = > string (18) "official account APPId" ["err_code"] = > string (13) "ORDERNOTEXIST" ["err_code_des"] = > string (15) "order does not exist" ["mch_id"] = > string (10) "merchant ID" ["nonce_str"] = > string (16) "H0K6KPemexExM5DV" ["result_code"] = > string (4) "FAIL" [" Return_code "] = > string (7)" SUCCESS "[" return_msg "] = > string (2)" OK "[" sign "] = > string (64)" 8779CA8C7F4931B4296C19FFFB176A3111F74B7244123A0C0EB7AD8DB2B1BDA49DA "}

When the payment of the queried order information fails, it is returned as follows:

Array (11) {["appid"] = > string (18) "official account APPId" ["attach"] = > string (13) "your reference value" ["mch_id"] = > string (10) "merchant ID" ["nonce_str"] = > string (16) "BR3zfazCdI3vZj5e" ["out_trade_no"] = > string (13) "1636555204840" ["result_code"] = > string (7) "SUCCESS" ["return_" Code "] = > string (7)" SUCCESS "[" return_msg "] = > string (2)" OK "[" sign "] = > string (64)" 7927EC724A7FDBFF034621B1EC492DB4D130AC13A43E4C66C7B6AD7889736CD5 "[" trade_state "] = > string (6)" NOTPAY "[" trade_state_desc "] = > string (15)" order unpaid "}

When the queried order information exists, it is returned as follows:

Array (21) {["appid"] = > string (18) "official account APPId" ["attach"] = > string (13) "your reference value" ["bank_type"] = > string (10) "LZB_CREDIT" ["cash_fee"] = > string (4) "2000" ["cash_fee_type"] = > string (3) "CNY" ["fee_type"] = > string (3) "CNY" ["is_" Subscribe "] = > string (1)" Y "[" mch_id "] = > string (10)" Merchant ID "[" nonce_str "] = > string (16)" Y8iYqXqHfs22hexX "[" openid "] = > string (28)" Wechat openid "[" out_trade_no "] = > string (13)" 1636600772812 "[" result_code "] = > string (7)" SUCCESS "[" return_code "] = > string (7)" SUCCESS " "[" return_msg "] = > string (2)" OK "[" sign "] = > string (64)" 7AC36F5EA6C4EE5D33584F0F1CDB54F804F0B196B49B61A4FFB6C045D521DA3C "[" time_end "] = > string (14)" 20211111111938 "[" total_fee "] = > string (4)" 2000 "[" trade_state "] = > string (7)" SUCCESS "[" trade_state_desc "] = > string (12)" payment succeeded "[" trade_type "] = > string (5) "JSAPI" ["transaction_id"] = > string (28) "4200001198202111115284536175"}

Native payment succeeded

Array (22) {["appid"] = > string (18) "official account APPId" ["attach"] = > string (13) "your reference value" ["bank_type"] = > string (6) "OTHERS" ["cash_fee"] = > string (1) "1" ["cash_fee_type"] = > string (3) "CNY" ["device_info"] = > string (1) "1" ["fee_type" ] = > string (3) "CNY" ["is_subscribe"] = > string (1) "Y" ["mch_id"] = > string (10) "Merchant ID" ["nonce_str"] = > string (16) "Y8iYqXqHfs22hexX" ["openid"] = > string (28) "Wechat openid" ["out_trade_no"] = > string (13) "1642682408295" ["result_code"] = > string (7) "SUCCESS" ["return_code"] = > string (7) "SUCCESS" ["return_msg"] = > string (2) "OK" ["sign"] = > string (64) "2F25084A568BBDA805DA8EE3FEB846448C9778DCBC2B745E8210D950E0742E38" ["time_end"] = > string (14) "20220120204024" ["total_fee"] = > string (1) "1" ["trade_state"] = > string (7) "SUCCESS" ["trade_state_desc"] = > string (12) "payment succeeded "[" trade_type "] = > string (6)" NATIVE "[" transaction_id "] = > string (28)" 4200001358202201811257221 "} the above is about" how to make WeChat Pay with ThinkPhp5.1 script " I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report