Table of Contents
1 WeChat Pay
2 Settings of attach parameters during WeChat payment
3 Several statuses of WeChat payment
Home PHP Framework ThinkPHP ThinkPhp5.1 makes WeChat payment and several status descriptions after payment

ThinkPhp5.1 makes WeChat payment and several status descriptions after payment

Feb 10, 2022 pm 03:06 PM
thinkphp5.1

Many times, WeChat payment interface calls need to be used during project development, such as mall production, online payment, insurance payment, etc. The editor has recently built several water fee charging systems, letter of guarantee issuance systems, and online registration. The system all uses WeChat payment. The production of WeChat payment and the description of several statuses after payment are listed below. I hope it will be helpful to everyone.

1 WeChat Pay

【Scan QR code to pay】

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;
}
Copy after login

【JSAPI interface payment】

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;
}
Copy after login

【Order inquiry】

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;
}
Copy after login

2 Settings of attach parameters during WeChat payment

attach, official explanation: additional data is returned as is in the query API and payment notification, and can be used as custom parameters. Actual situation This field will only be returned if the payment is completed. Sometimes we need to return some specific values ​​when querying orders during development, such as: member ID, payment order ID, application ID, member name, etc. At this time, we can use attach to pass parameters, but attach is a string. Sometimes Often the value passed is an array, so you only need to use json_encode for conversion, for example:

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

After querying the order, use json_decode for conversion, for example:

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

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

3 Several statuses of WeChat payment

When the queried order information does not exist, The return is as follows:

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"
}
Copy after login

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

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) "订单未支付"
}
Copy after login

When the queried order information exists, the return is as follows:

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"
}
Copy after login

Native payment is successful

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"
}
Copy after login

Related recommendations: "thinkphp video tutorial"

The above is the editor’s summary of some relevant knowledge about WeChat payment , I hope it will be helpful to all developers!

The above is the detailed content of ThinkPhp5.1 makes WeChat payment and several status descriptions after payment. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What Are the Key Considerations for Using ThinkPHP in a Serverless Architecture? What Are the Key Considerations for Using ThinkPHP in a Serverless Architecture? Mar 18, 2025 pm 04:54 PM

The article discusses key considerations for using ThinkPHP in serverless architectures, focusing on performance optimization, stateless design, and security. It highlights benefits like cost efficiency and scalability, but also addresses challenges

What Are the Advanced Features of ThinkPHP's Dependency Injection Container? What Are the Advanced Features of ThinkPHP's Dependency Injection Container? Mar 18, 2025 pm 04:50 PM

ThinkPHP's IoC container offers advanced features like lazy loading, contextual binding, and method injection for efficient dependency management in PHP apps.Character count: 159

What Are the Key Features of ThinkPHP's Built-in Testing Framework? What Are the Key Features of ThinkPHP's Built-in Testing Framework? Mar 18, 2025 pm 05:01 PM

The article discusses ThinkPHP's built-in testing framework, highlighting its key features like unit and integration testing, and how it enhances application reliability through early bug detection and improved code quality.

How to Implement Service Discovery and Load Balancing in ThinkPHP Microservices? How to Implement Service Discovery and Load Balancing in ThinkPHP Microservices? Mar 18, 2025 pm 04:51 PM

The article discusses implementing service discovery and load balancing in ThinkPHP microservices, focusing on setup, best practices, integration methods, and recommended tools.[159 characters]

What Are the Best Ways to Handle File Uploads and Cloud Storage in ThinkPHP? What Are the Best Ways to Handle File Uploads and Cloud Storage in ThinkPHP? Mar 17, 2025 pm 02:28 PM

The article discusses best practices for handling file uploads and integrating cloud storage in ThinkPHP, focusing on security, efficiency, and scalability.

How to Build a Distributed Task Queue System with ThinkPHP and RabbitMQ? How to Build a Distributed Task Queue System with ThinkPHP and RabbitMQ? Mar 18, 2025 pm 04:45 PM

The article outlines building a distributed task queue system using ThinkPHP and RabbitMQ, focusing on installation, configuration, task management, and scalability. Key issues include ensuring high availability, avoiding common pitfalls like imprope

How to Use ThinkPHP for Building Real-Time Collaboration Tools? How to Use ThinkPHP for Building Real-Time Collaboration Tools? Mar 18, 2025 pm 04:49 PM

The article discusses using ThinkPHP to build real-time collaboration tools, focusing on setup, WebSocket integration, and security best practices.

How to Use ThinkPHP for Building Real-Time Stock Market Data Feeds? How to Use ThinkPHP for Building Real-Time Stock Market Data Feeds? Mar 18, 2025 pm 04:57 PM

Article discusses using ThinkPHP for real-time stock market data feeds, focusing on setup, data accuracy, optimization, and security measures.

See all articles