Wechat applet development practice 11_3 payment order query

12.4 Query Payment Order

Merchants can actively query the order status through the query order interface, and execute the next business logic according to the payment result. Inquiry order status can be queried through WeChat payment order number or merchant order number, and the results returned by the two query methods are the same. The situations that need to call the query interface are:

  • When the merchant's background, network, server, etc. are abnormal, the merchant's system does not receive the payment notification in the end.
  • After calling the payment interface, a system error or unknown transaction status is returned.
  • Call the payment code payment API and return the status of USERPAYING.
  • Before calling the customs clearance interface API, the payment status needs to be confirmed.
    Similar to the payment order interface, WeChat Pay provides different query interfaces for directly connected merchants and service providers (the addresses and parameters of the interfaces are different). Next, we will introduce how to use the payment order query interface in the two modes.

12.4.1 Service provider model

The payment order query interface is invoked through the GET method of HTTP, and provides two ways to query payment orders through WeChat order number and merchant order number.
1) Query the payment order through the service provider’s WeChat order number.
The format of the interface is:
https://api.mch.weixin.qq.com/v3/pay/partner/transactions/id/{transaction_id}?sp_mchid={sp_mchid}&sub_mchid
The request parameters in the ={sub_mchid} interface are described as follows:

  • transaction_id: WeChat order number
  • sp_mchid: service provider merchant number
  • sub_mchid: sub-merchant merchant ID

2) Query the payment order through the merchant order number of the service provider.
The format of the interface is:
https://api.mch.weixin.qq.com/v3/pay/partner/transactions/out-trade-no/{out-trade-no }?sp_mchid={sp_mchid}&sub_mchid={sub_mchid}
The request parameters in the interface are described as follows:

  • out-trade-no: Merchant order number
  • sp_mchid: service provider merchant number
  • sub_mchid: sub-merchant merchant ID

The results returned by the two query methods are the same, and the data structure of the returned results is as follows:

parameter name variable describe
Service provider application ID sp_appid Official account or mobile application appid applied by the service provider.
Service merchant number sp_mchid Service Merchant ID, generated and issued by WeChat Pay
Sub-merchant application ID sub_appid The official account or mobile application appid applied by the sub-merchant.
Sub-merchant number sub_mchid The merchant ID of the sub-merchant is generated and issued by WeChat Pay.
Merchant order number out_trade_no The internal order number of the merchant system can only be numbers, uppercase and lowercase letters_-* and is unique under the same merchant number.
WeChat payment order number transaction_id The order number generated by the WeChat payment system.
Transaction Type trade_type Transaction type, enumeration value:
trading status trade_state Transaction status, enumeration value:
Transaction status description trade_state_desc Transaction status description
payment bank bank_type Bank type, using the bank ID of string type.
additional data attach Additional data, returned as-is in the query API and payment notification, can be used as custom parameters
payment completion time success_time Payment completion time.
+ payer payer payer information
+ order amount amount Order amount information, this field is returned when the payment is successful.
+Scene information scene_info Payment Scenario Description
+Preferential function promotion_detail Offer function, this field is returned when enjoying a offer.

The relevant codes for querying payment orders through the merchant order number and WeChat order number are as follows:

//支付订单查询
//transaction_id:微信支付订单号
//mchid:子商户号
func queryByTransactionIdX(ent *MchParam, transaction_id string, mchid string) (WxPayInfo, error) {
    
    
   var par_info WxPayInfo
   url := fmt.Sprintf("https://api.mch.weixin.qq.com/v3/pay/partner/transactions/id/%s?sp_mchid=%s&sub_mchid=%s",
      transaction_id, ent.Mchid, mchid)
   result, err := WxPayGetV3(ent, url)
   if err != nil {
    
    
      fmt.Println(err)
      return par_info, err
   }
   err = json.Unmarshal([]byte(result), &par_info)
   if err != nil {
    
    
      fmt.Println(err)
      return par_info, err
   }

   return par_info, nil
}

//支付订单查询
//out_trade_no:业务订单号
//mchid:子商户号
func queryByOutTradeNoX(ent *MchParam, out_trade_no string, mchid string) (WxPayInfo, error) {
    
    
   var par_info WxPayInfo
   url := fmt.Sprintf("https://api.mch.weixin.qq.com/v3/pay/partner/transactions/out-trade-no/%s?sp_mchid=%s&sub_mchid=%s",
      out_trade_no, ent.Mchid, mchid)
   result, err := WxPayGetV3(ent, url)
   if err != nil {
    
    
      fmt.Println(err)
      return par_info, err
   }
   err = json.Unmarshal([]byte(result), &par_info)
   if err != nil {
    
    
      fmt.Println(err)
      return par_info, err
   }

   return par_info, nil
}

12.4.2 Directly connected merchant model

Directly connected merchant order query is basically the same as service provider order query. You can also query orders through WeChat payment order number or merchant order number: 1) Query payment order through direct connection merchant
WeChat order number
The format of the interface is:
https:/
The request parameters in the /api.mch.weixin.qq.com/v3/pay/transactions/id/{transaction_id}?mchid={mchid} interface are described as follows:

  • transaction_id: WeChat order number
  • mchid: direct connection merchant number

2) Query the payment order through the WeChat order number of the directly connected merchant.
The format of the interface is:
https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/{out-trade-no}
The request parameters in the ?mchid={mchid} interface are described as follows:

  • out-trade-no: Merchant order number
  • mchid: direct connection merchant number

The relevant codes for querying payment orders through the merchant order number and WeChat order number are as follows:

//支付订单查询
//transaction_id:微信支付订单号
//mchid:商户号
func queryByTransactionId(ent *MchParam, transaction_id string, mchid string) (WxPayInfo, error) {
    
    
   var par_info WxPayInfo
   url := fmt.Sprintf("https://api.mch.weixin.qq.com/v3/pay/transactions/id/%s?mchid=%s", transaction_id, mchid)
   result, err := WxPayGetV3(ent, url)
   if err != nil {
    
    
      fmt.Println(err)
      return par_info, err
   }
   err = json.Unmarshal([]byte(result), &par_info)
   if err != nil {
    
    
      fmt.Println(err)
      return par_info, err
   }

   return par_info, nil
}

//支付订单查询
//out_trade_no:业务订单号
//mchid:商户号
func queryByOutTradeNo(ent *MchParam, out_trade_no string, mchid string) (WxPayInfo, error) {
    
    
   var par_info WxPayInfo
   url := fmt.Sprintf("https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/%s?mchid=%s", out_trade_no, mchid)
   result, err := WxPayGetV3(ent, url)
   if err != nil {
    
    
      return par_info, err
   }
   err = json.Unmarshal([]byte(result), &par_info)
   if err != nil {
    
    
      return par_info, err
   }

   return par_info, nil
}

Guess you like

Origin blog.csdn.net/gz_hm/article/details/128517907