您好,登錄后才能下訂單哦!
這篇“怎么用ThinkPhp5.1腳本制作微信支付”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“怎么用ThinkPhp5.1腳本制作微信支付”文章吧。
【掃碼支付】
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"; //實(shí)例化配置信息 $config = new WxPayConfig(); $input = new \WxPayUnifiedOrder(); //設(shè)置商品描述 $input->SetBody($body); //設(shè)置訂單號(hào) $input->SetOut_trade_no($out_trade_no); //設(shè)置商品金額(單位:分) $input->SetTotal_fee($fee); //設(shè)置異步通知地址 $notify = $config->GetNotifyUrl(); $input->SetNotify_url($notify); //設(shè)置交易類型 $input->SetTrade_Type('NATIVE'); //設(shè)置商品ID $input->SetProduct_id($product_id); $input->SetDevice_info($pro_id); $input->SetAttach($attach); //調(diào)用統(tǒng)一下單API $result = \WxPayApi::unifiedOrder($config,$input); //dump($result); $qr_url = $result['code_url']; $img = $this->createCode($qr_url,$logo,$path); //生成數(shù)組 $array = array('img'=>$img,'out_trade_no'=>$out_trade_no); return $array; } //生成二維碼 public function createCode($code_url,$logo,$path){ //創(chuàng)建基本的QR碼 $qrCode = new QrCode($code_url); $qrCode->setSize(300); //設(shè)置高級(jí)選項(xiàng) $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); //邊距設(shè)置 $qrCode->setMargin(10); //直接輸出二維碼 header('Content-Type: '.$qrCode->getContentType()); //echo $qrCode->writeString(); //將二維碼保存到指定目錄 $qrCode->writeFile($path); //生成數(shù)據(jù)URI以內(nèi)聯(lián)圖像數(shù)據(jù)(即在<img>標(biāo)記內(nèi)) $dataUri = $qrCode->writeDataUri(); return $dataUri; }
【JSAPI接口支付】
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(); //設(shè)置商品描述 $input->SetBody($Body); //設(shè)置訂單號(hào) $input->SetOut_trade_no($out_trade_no); //設(shè)置商品金額(單位:分) $input->SetTotal_fee($fee); //設(shè)置異步通知地址 $config = new WxPayConfig(); $notify = $config->GetNotifyUrl(); $input->SetNotify_url($notify); //設(shè)置交易類型 $input->SetTrade_Type('JSAPI'); //設(shè)置商品ID $input->SetProduct_id($product_id); //用戶openID $input->SetOpenid($openId); $input->SetAttach($attach); //調(diào)用統(tǒng)一下單API $result = \WxPayApi::unifiedOrder($config,$input); $jsApiParameters = $tools->GetJsApiParameters($result); return $jsApiParameters; }
【訂單查詢】
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_code'=>$result['result_code'],'attach'=>$arr,'pay_time'=>$pay_time,'result'=>$result,'msg'=>"支付成功"]; }else{ return ['code'=>101,'result_code'=>$result['result_code'],'err_code'=>$result['err_code'],'result'=>$result,'msg'=>"訂單未支付"]; } }else{ return ['code'=>103,'result_code'=>$result['result_code'],'result'=>$result,'msg'=>"訂單不存在"]; } //dump($result); //return $result; }
attach,官方解釋:附加數(shù)據(jù),在查詢API和支付通知中原樣返回,可作為自定義參數(shù)使用,實(shí)際情況下只有支付完成狀態(tài)才會(huì)返回該字段。有時(shí)我們?cè)陂_發(fā)時(shí)需要在查詢訂單時(shí)返回一些特定的數(shù)值,比如:會(huì)員ID、支付訂單ID、申請(qǐng)ID、會(huì)員名稱等等,這時(shí)可以使用attach進(jìn)行傳遞參數(shù),但是attach是一個(gè)字符串,有時(shí)往往傳值的是數(shù)組,那么只需要利json_encode進(jìn)行轉(zhuǎn)化即可,例如:
$remarks = array( 'cid'=>1, 'member_id'=>2, 'apply_id'=>28, ); $attach = json_encode($remarks);
查詢訂單后,再用json_decode進(jìn)行轉(zhuǎn)化,例如:
$arr = json_decode($result['attach'],true);
這里得到的$arr是一個(gè)數(shù)組,直接按照數(shù)組的調(diào)用規(guī)則調(diào)用即可
當(dāng)所查詢的訂單信息不存在時(shí),返回如下:
array(9) { ["appid"] => string(18) "公眾號(hào)APPId" ["err_code"] => string(13) "ORDERNOTEXIST" ["err_code_des"] => string(15) "訂單不存在" ["mch_id"] => string(10) "商戶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" }
當(dāng)所查詢的訂單信息支付失敗,返回如下:
array(11) { ["appid"] => string(18) "公眾號(hào)APPId" ["attach"] => string(13) "你的傳參值" ["mch_id"] => string(10) "商戶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) "訂單未支付" }
當(dāng)所查詢訂單信息存在時(shí)返回如下:
array(21) { ["appid"] => string(18) "公眾號(hào)APPId" ["attach"] => string(13) "你的傳參值" ["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) "商戶ID" ["nonce_str"] => string(16) "Y8iYqXqHfs22hexX" ["openid"] => string(28) "支付者微信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) "支付成功" ["trade_type"] => string(5) "JSAPI" ["transaction_id"] => string(28) "4200001198202111115284536175" }
Native支付成功
array(22) { ["appid"] => string(18) "公眾號(hào)APPId" ["attach"] => string(13) "你的傳參值" ["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) "商戶ID" ["nonce_str"] => string(16) "Y8iYqXqHfs22hexX" ["openid"] => string(28) "支付者微信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) "支付成功" ["trade_type"] => string(6) "NATIVE" ["transaction_id"] => string(28) "4200001358202201201811257221" }
以上就是關(guān)于“怎么用ThinkPhp5.1腳本制作微信支付”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。