首頁  >  文章  >  php框架  >  ThinkPhp5.1製作微信支付以及支付後的幾種狀態說明

ThinkPhp5.1製作微信支付以及支付後的幾種狀態說明

XuPing.Yang
XuPing.Yang原創
2022-02-10 15:06:462381瀏覽

很多時候,在專案開發的時候需要用到微信支付介面調用,例如:商城製作、線上繳費、保險繳費等等,小編最近做了幾個水費收費系統、保函出具系統、線上報名系統,均用到了微信支付,以下把微信支付的製作以及支付後的幾種狀態說明列出,希望對大家有所幫助。

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";
    //实例化配置信息
    $config = new WxPayConfig();
    $input = new \WxPayUnifiedOrder();
    //设置商品描述
    $input->SetBody($body);
    //设置订单号
    $input->SetOut_trade_no($out_trade_no);
    //设置商品金额(单位:分)
    $input->SetTotal_fee($fee);
    //设置异步通知地址
    $notify = $config->GetNotifyUrl();
    $input->SetNotify_url($notify);
    //设置交易类型
    $input->SetTrade_Type('NATIVE');
    //设置商品ID
    $input->SetProduct_id($product_id);
    $input->SetDevice_info($pro_id);
    $input->SetAttach($attach);
    //调用统一下单API
    $result = \WxPayApi::unifiedOrder($config,$input);
    //dump($result);
    $qr_url = $result['code_url'];
    $img = $this->createCode($qr_url,$logo,$path);
    //生成数组
    $array = array('img'=>$img,'out_trade_no'=>$out_trade_no);
    return $array;
}
//生成二维码
public function createCode($code_url,$logo,$path){
    //创建基本的QR码
    $qrCode = new QrCode($code_url);
    $qrCode->setSize(300);
    //设置高级选项
    $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);
    //边距设置
    $qrCode->setMargin(10);
    //直接输出二维码
    header('Content-Type: '.$qrCode->getContentType());
    //echo $qrCode->writeString();
    //将二维码保存到指定目录
    $qrCode->writeFile($path);
    //生成数据URI以内联图像数据(即在<img>标记内)
    $dataUri = $qrCode->writeDataUri();
    return $dataUri;
}

【JSAPI介面支付】

public function jsApiPay($openId,$Body,$out_trade_no,$fee,$product_id,$attach){
    require_once Env::get(&#39;app_path&#39;)."api/wxpay/lib/WxPay.Api.php";
    $tools = new JsApiPay();
    $input = new \WxPayUnifiedOrder();
    //设置商品描述
    $input->SetBody($Body);
    //设置订单号
    $input->SetOut_trade_no($out_trade_no);
    //设置商品金额(单位:分)
    $input->SetTotal_fee($fee);
    //设置异步通知地址
    $config = new WxPayConfig();
    $notify = $config->GetNotifyUrl();
    $input->SetNotify_url($notify);
    //设置交易类型
    $input->SetTrade_Type(&#39;JSAPI&#39;);
    //设置商品ID
    $input->SetProduct_id($product_id);
    //用户openID
    $input->SetOpenid($openId);
    $input->SetAttach($attach);
    //调用统一下单API
    $result = \WxPayApi::unifiedOrder($config,$input);
    $jsApiParameters = $tools->GetJsApiParameters($result);
    return $jsApiParameters;
}

【訂單查詢】

#
public function QueryOrder($number,$transaction_id=NULL){
    require_once App::getAppPath().&#39;api/wxpay/lib/WxPay.Api.php&#39;;
    require_once App::getAppPath().&#39;api/wxpay/lib/WxPay.Notify.php&#39;;
    $input = new \WxPayOrderQuery();
    $input->SetOut_trade_no($number);
    $input->SetTransaction_id($transaction_id);
    $config = new WxPayConfig();
    $result = \WxPayApi::orderQuery($config, $input);
    if($result[&#39;result_code&#39;]==&#39;SUCCESS&#39;){
        if($result[&#39;trade_state&#39;]==&#39;SUCCESS&#39;){
            $arr = json_decode($result[&#39;attach&#39;],true);
            $pay_time = $this->getPayTime($result[&#39;time_end&#39;]);
            return [&#39;code&#39;=>100,&#39;result_code&#39;=>$result[&#39;result_code&#39;],&#39;attach&#39;=>$arr,&#39;pay_time&#39;=>$pay_time,&#39;result&#39;=>$result,&#39;msg&#39;=>"支付成功"];
        }else{
            return [&#39;code&#39;=>101,&#39;result_code&#39;=>$result[&#39;result_code&#39;],&#39;err_code&#39;=>$result[&#39;err_code&#39;],&#39;result&#39;=>$result,&#39;msg&#39;=>"订单未支付"];
        }
    }else{
        return [&#39;code&#39;=>103,&#39;result_code&#39;=>$result[&#39;result_code&#39;],&#39;result&#39;=>$result,&#39;msg&#39;=>"订单不存在"];
    }
    //dump($result);
    //return $result;
}

2 微信支付時attach參數的設定

attach,官方解釋:附加數據,在查詢API和支付通知中原樣返回,可作為自訂參數使用,實際情況下只有支付完成狀態才會回傳該欄位。有時我們在開發時需要在查詢訂單時傳回一些特定的數值,例如:會員ID、支付訂單ID、申請ID、會員名稱等等,這時可以使用attach進行傳遞參數,但是attach是一個字串,有時常傳值的是數組,那麼只需要利json_encode轉換即可,例如:

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

查詢訂單後,再用json_decode進行轉化,例如:

$arr = json_decode($result[&#39;attach&#39;],true);

這裡得到的$arr是一個數組,直接按照數組的調用規則調用即可

3 微信支付的幾種狀態

當所查詢的訂單資訊不存在時,傳回如下:

array(9) {
  ["appid"] => string(18) "公众号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"
}

當所查詢的訂單資訊支付失敗,傳回如下:

array(11) {
  ["appid"] => string(18) "公众号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) "订单未支付"
}

當所查詢訂單資訊存在時傳回如下:

array(21) {
  ["appid"] => string(18) "公众号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) "公众号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"
}

相關推薦:《thinkphp影片教學#》

以上便是小編總結的幾點微信支付的相關知識,希望對各位開發者有幫助!

以上是ThinkPhp5.1製作微信支付以及支付後的幾種狀態說明的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn