Asp.net core wechat payment (3) [query order apiv3]

Time:2021-6-12

Official references

  Order inquiry

Theory and Practice

The government provides two ways to query orders, one is based on the order number generated by the merchants themselves, and the other is based on the order number generated by wechat. And I use the first way.

Call wechat paymentOrder inquiry, need to generate signature, according to wechat paymentInterface rulesConstruct HTTP header.

For generating signatures, please refer to my article:Asp.net core wechat payment (1) [unified order apiv3]Asp.net core wechat payment (2) [error occurred when publishing to the server for signature encryption: the system could not find the specified file apiv3]

code

Data entity mapping class

public class WxPayStatusRespModel
    {
        /// 
        ///The internal order number of the merchant system can only be numbers and uppercase and lowercase letters_-* And it is unique under the same merchant number
        /// 
        public string out_trade_no { get; set; }

        /// 
        ///The order number generated by wechat payment system.
        /// 
        public string transaction_id { get; set; }

        /// 
        ///Transaction type, enumeration value:
        // / JSAPI: official account payment
        ///National: payment by scanning code
        ///App: app payment
        ///Micropay: payment code
        ///Mweb: H5 payment
        ///Facebook: face payment
        /// 
        public string trade_type { get; set; }

        /// 
        ///Transaction status, enumeration value:
        ///Success: successful payment
        ///Refund: transfer in refund
        ///Notpay: unpaid
        ///Closed: closed
        ///Revoked: cancelled (payment code payment)
        ///User paying: user paying (payment code payment)
        ///Payerror: payment failure (other reasons, such as bank return failure)
        ///Accept: received, waiting for deduction
        /// 
        public string trade_state { get; set; }

        /// 
        ///Transaction status description
        /// 
        public string trade_state_desc { get; set; }

        /// 
        ///Payer information
        /// 
        public WxPayerModel payer { set; get; }
    }

    /// 
    ///Payment user information entity
    /// 
    public class WxPayerModel
    {
        /// 
        ///The unique identification of the user under the appid of the direct connected merchant.
        /// 
        public string openid { get; set; }
    }

Query order interface request code

var url = $"https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/{orderNumber}?mchid={WxPayConst.mchid}";
            var client = new HttpClient(new HttpHandler());
            var resp = await client.GetAsync(url);
            Nloghelper. Debug ("query order status");
            var respStr = await resp.Content.ReadAsStringAsync();// Here is the JSON data returned by wechat payment query order, and you can parse it yourself
            var payModel = respStr.ToObject();

Type conversion tools and methods

/// 
        ///The JSON string is converted to the corresponding type
        /// 
        ///Types after transformation
        ///JSON string
        ///Types after transformation
        public static T ToObject(this string json)
        {
            return json == null ? default(T) : JsonConvert.DeserializeObject(json);
        }