很多时候,在项目开发的时候需要用到微信支付接口调用,例如:商城制作、在线缴费、保险缴费等等,小编最近做了几个水费收费系统、保函出具系统、在线报名系统,均用到了微信支付,以下把微信支付的制作以及支付后的几种状态说明列出,希望对大家有所帮助。
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('app_path')."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('jsapi'); //设置商品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().'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;}
2 微信支付时attach参数的设置
attach,官方解释:附加数据,在查询api和支付通知中原样返回,可作为自定义参数使用,实际情况下只有支付完成状态才会返回该字段。有时我们在开发时需要在查询订单时返回一些特定的数值,比如:会员id、支付订单id、申请id、会员名称等等,这时可以使用attach进行传递参数,但是attach是一个字符串,有时往往传值的是数组,那么只需要利json_encode进行转化即可,例如:
$remarks = array( 'cid'=>1, 'member_id'=>2, 'apply_id'=>28,);$attach = json_encode($remarks);
查询订单后,再用json_decode进行转化,例如:
$arr = json_decode($result['attach'],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制作微信支付以及支付后的几种状态说明的详细内容。