ThinkPhp5.1制作微信支付以及支付后的几种状态说明

 4479

很多时候,在项目开发的时候需要用到微信支付接口调用,例如:商城制作、在线缴费、保险缴费等等,小编最近做了几个水费收费系统、保函出具系统、在线报名系统,均用到了微信支付,以下把微信支付的制作以及支付后的几种状态说明列出,希望对大家有所帮助。

微信支付

【扫码支付】

  1. public function wxPayImg($body,$out_trade_no,$fee,$product_id,$logo,$path,$attach,$pro_id){
  2.     require_once Env::get('app_path')."api/wxpay/lib/WxPay.Api.php";
  3.     //实例化配置信息
  4.     $config = new WxPayConfig();
  5.     $input = new \WxPayUnifiedOrder();
  6.     //设置商品描述
  7.     $input->SetBody($body);
  8.     //设置订单号
  9.     $input->SetOut_trade_no($out_trade_no);
  10.     //设置商品金额(单位:分)
  11.     $input->SetTotal_fee($fee);
  12.     //设置异步通知地址
  13.     $notify = $config->GetNotifyUrl();
  14.     $input->SetNotify_url($notify);
  15.     //设置交易类型
  16.     $input->SetTrade_Type('NATIVE');
  17.     //设置商品ID
  18.     $input->SetProduct_id($product_id);
  19.     $input->SetDevice_info($pro_id);
  20.     $input->SetAttach($attach);
  21.     //调用统一下单API
  22.     $result = \WxPayApi::unifiedOrder($config,$input);
  23.     //dump($result);
  24.     $qr_url = $result['code_url'];
  25.     $img = $this->createCode($qr_url,$logo,$path);
  26.     //生成数组
  27.     $array = array('img'=>$img,'out_trade_no'=>$out_trade_no);
  28.     return $array;
  29. }
  30. //生成二维码
  31. public function createCode($code_url,$logo,$path){
  32.     //创建基本的QR码
  33.     $qrCode = new QrCode($code_url);
  34.     $qrCode->setSize(300);
  35.     //设置高级选项
  36.     $qrCode->setWriterByName('png');
  37.     $qrCode->setEncoding('UTF-8');
  38.     $qrCode->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH());
  39.     $qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]);
  40.     $qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]);
  41.     //$qrCode->setLabel('', 16, App::getAppPath().'/../public/static/user/font/msyhl.ttc', LabelAlignment::CENTER());
  42.     $qrCode->setLogoPath($logo);
  43.     $qrCode->setLogoWidth(50);
  44.     $qrCode->setValidateResult(false);
  45.     //边距设置
  46.     $qrCode->setMargin(10);
  47.     //直接输出二维码
  48.     header('Content-Type: '.$qrCode->getContentType());
  49.     //echo $qrCode->writeString();
  50.     //将二维码保存到指定目录
  51.     $qrCode->writeFile($path);
  52.     //生成数据URI以内联图像数据(即在<img>标记内)
  53.     $dataUri = $qrCode->writeDataUri();
  54.     return $dataUri;
  55. }


【JSAPI接口支付】

  1. public function jsApiPay($openId,$Body,$out_trade_no,$fee,$product_id,$attach){
  2.     require_once Env::get('app_path')."api/wxpay/lib/WxPay.Api.php";
  3.     $tools = new JsApiPay();
  4.     $input = new \WxPayUnifiedOrder();
  5.     //设置商品描述
  6.     $input->SetBody($Body);
  7.     //设置订单号
  8.     $input->SetOut_trade_no($out_trade_no);
  9.     //设置商品金额(单位:分)
  10.     $input->SetTotal_fee($fee);
  11.     //设置异步通知地址
  12.     $config = new WxPayConfig();
  13.     $notify = $config->GetNotifyUrl();
  14.     $input->SetNotify_url($notify);
  15.     //设置交易类型
  16.     $input->SetTrade_Type('JSAPI');
  17.     //设置商品ID
  18.     $input->SetProduct_id($product_id);
  19.     //用户openID
  20.     $input->SetOpenid($openId);
  21.     $input->SetAttach($attach);
  22.     //调用统一下单API
  23.     $result = \WxPayApi::unifiedOrder($config,$input);
  24.     $jsApiParameters = $tools->GetJsApiParameters($result);
  25.     return $jsApiParameters;
  26. }


【订单查询】

  1. public function QueryOrder($number,$transaction_id=NULL){
  2.     require_once App::getAppPath().'api/wxpay/lib/WxPay.Api.php';
  3.     require_once App::getAppPath().'api/wxpay/lib/WxPay.Notify.php';
  4.     $input = new \WxPayOrderQuery();
  5.     $input->SetOut_trade_no($number);
  6.     $input->SetTransaction_id($transaction_id);
  7.     $config = new WxPayConfig();
  8.     $result = \WxPayApi::orderQuery($config, $input);
  9.     if($result['result_code']=='SUCCESS'){
  10.         if($result['trade_state']=='SUCCESS'){
  11.             $arr = json_decode($result['attach'],true);
  12.             $pay_time = $this->getPayTime($result['time_end']);
  13.             return ['code'=>100,'result_code'=>$result['result_code'],'attach'=>$arr,'pay_time'=>$pay_time,'result'=>$result,'msg'=>"支付成功"];
  14.         }else{
  15.             return ['code'=>101,'result_code'=>$result['result_code'],'err_code'=>$result['err_code'],'result'=>$result,'msg'=>"订单未支付"];
  16.         }
  17.     }else{
  18.         return ['code'=>103,'result_code'=>$result['result_code'],'result'=>$result,'msg'=>"订单不存在"];
  19.     }
  20.     //dump($result);
  21.     //return $result;
  22. }


微信支付时attach参数的设置

attach,官方解释:附加数据,在查询API和支付通知中原样返回,可作为自定义参数使用,实际情况下只有支付完成状态才会返回该字段。有时我们在开发时需要在查询订单时返回一些特定的数值,比如:会员ID、支付订单ID、申请ID、会员名称等等,这时可以使用attach进行传递参数,但是attach是一个字符串,有时往往传值的是数组,那么只需要利json_encode进行转化即可,例如:

  1. $remarks = array(
  2.     'cid'=>1,
  3.     'member_id'=>2,
  4.     'apply_id'=>28,
  5. );
  6. $attach = json_encode($remarks);

查询订单后,再用json_decode进行转化,例如:

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

这里得到的$arr是一个数组,直接按照数组的调用规则调用即可


微信支付的几种状态

当所查询的订单信息不存在时,返回如下:

  1. array(9) {
  2.   ["appid"] => string(18) "公众号APPId"
  3.   ["err_code"] => string(13) "ORDERNOTEXIST"
  4.   ["err_code_des"] => string(15) "订单不存在"
  5.   ["mch_id"] => string(10) "商户ID"
  6.   ["nonce_str"] => string(16) "H0K6KPemexExM5DV"
  7.   ["result_code"] => string(4) "FAIL"
  8.   ["return_code"] => string(7) "SUCCESS"
  9.   ["return_msg"] => string(2) "OK"
  10.   ["sign"] => string(64) "8779CA8C7F4931B4296C19FFFB176A3111F74B7244123A0C0EB7AD8DB2B1BDA49DA"
  11. }

当所查询的订单信息支付失败,返回如下:

  1. array(11) {
  2.   ["appid"] => string(18) "公众号APPId"
  3.   ["attach"] => string(13) "你的传参值"
  4.   ["mch_id"] => string(10) "商户ID"
  5.   ["nonce_str"] => string(16) "BR3zfazCdI3vZj5e"
  6.   ["out_trade_no"] => string(13) "1636555204840"
  7.   ["result_code"] => string(7) "SUCCESS"
  8.   ["return_code"] => string(7) "SUCCESS"
  9.   ["return_msg"] => string(2) "OK"
  10.   ["sign"] => string(64) "7927EC724A7FDBFF034621B1EC492DB4D130AC13A43E4C66C7B6AD7889736CD5"
  11.   ["trade_state"] => string(6) "NOTPAY"
  12.   ["trade_state_desc"] => string(15) "订单未支付"
  13. }

当所查询订单信息存在时返回如下:

  1. array(21) {
  2.   ["appid"] => string(18) "公众号APPId"
  3.   ["attach"] => string(13) "你的传参值"
  4.   ["bank_type"] => string(10) "LZB_CREDIT"
  5.   ["cash_fee"] => string(4) "2000"
  6.   ["cash_fee_type"] => string(3) "CNY"
  7.   ["fee_type"] => string(3) "CNY"
  8.   ["is_subscribe"] => string(1) "Y"
  9.   ["mch_id"] => string(10) "商户ID"
  10.   ["nonce_str"] => string(16) "Y8iYqXqHfs22hexX"
  11.   ["openid"] => string(28) "支付者微信openid"
  12.   ["out_trade_no"] => string(13) "1636600772812"
  13.   ["result_code"] => string(7) "SUCCESS"
  14.   ["return_code"] => string(7) "SUCCESS"
  15.   ["return_msg"] => string(2) "OK"
  16.   ["sign"] => string(64) "7AC36F5EA6C4EE5D33584F0F1CDB54F804F0B196B49B61A4FFB6C045D521DA3C"
  17.   ["time_end"] => string(14) "20211111111938"
  18.   ["total_fee"] => string(4) "2000"
  19.   ["trade_state"] => string(7) "SUCCESS"
  20.   ["trade_state_desc"] => string(12) "支付成功"
  21.   ["trade_type"] => string(5) "JSAPI"
  22.   ["transaction_id"] => string(28) "4200001198202111115284536175"
  23. }

Native支付成功

  1. array(22) {
  2.   ["appid"] => string(18) "公众号APPId"
  3.   ["attach"] => string(13) "你的传参值"
  4.   ["bank_type"] => string(6) "OTHERS"
  5.   ["cash_fee"] => string(1) "1"
  6.   ["cash_fee_type"] => string(3) "CNY"
  7.   ["device_info"] => string(1) "1"
  8.   ["fee_type"] => string(3) "CNY"
  9.   ["is_subscribe"] => string(1) "Y"
  10.   ["mch_id"] => string(10) "商户ID"
  11.   ["nonce_str"] => string(16) "Y8iYqXqHfs22hexX"
  12.   ["openid"] => string(28) "支付者微信openid"
  13.   ["out_trade_no"] => string(13) "1642682408295"
  14.   ["result_code"] => string(7) "SUCCESS"
  15.   ["return_code"] => string(7) "SUCCESS"
  16.   ["return_msg"] => string(2) "OK"
  17.   ["sign"] => string(64) "2F25084A568BBDA805DA8EE3FEB846448C9778DCBC2B745E8210D950E0742E38"
  18.   ["time_end"] => string(14) "20220120204024"
  19.   ["total_fee"] => string(1) "1"
  20.   ["trade_state"] => string(7) "SUCCESS"
  21.   ["trade_state_desc"] => string(12) "支付成功"
  22.   ["trade_type"] => string(6) "NATIVE"
  23.   ["transaction_id"] => string(28) "4200001358202201201811257221"
  24. }


本文网址:https://www.zztuku.com/index.php/detail-10994.html
站长图库 - ThinkPhp5.1制作微信支付以及支付后的几种状态说明
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐