Fetch the repository succeeded.
/*
Copyright 2020 XiaochengTech
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package wxpay
import (
"encoding/xml"
"errors"
"github.com/beevik/etree"
)
// 查询订单
func (c *Client) QueryOrder(body QueryOrderBody) (wxRsp QueryOrderResponse, err error) {
// 业务逻辑
bytes, err := c.doWeChat("pay/orderquery", body)
if err != nil {
return
}
// 结果校验
if err = c.doVerifySign(bytes, true); err != nil {
return
}
// 解析返回值
if err = c.queryOrderParseResponse(bytes, &wxRsp); err != nil {
return
}
return
}
// 查询订单的参数
type QueryOrderBody struct {
SignType string `json:"sign_type,omitempty"` // 签名类型,目前支持HMAC-SHA256和MD5,默认为MD5
TransactionId string `json:"transaction_id,omitempty"` // (非必填,二选一) 微信的订单号,优先使用
OutTradeNo string `json:"out_trade_no,omitempty"` // (非必填,二选一) 商户系统内部订单号,要求32个字符内,只能是数字、大小写字母_-|*且在同一个商户号下唯一。详见商户订单号
}
// 查询订单的返回值
type QueryOrderResponse struct {
ResponseModel
// 当return_code为SUCCESS时
ServiceResponseModel
// 当return_code、result_code、trade_state都为SUCCESS时有返回,如trade_state不为SUCCESS,则只返回out_trade_no(必传)和attach(选传)。
DeviceInfo string `xml:"device_info"` // 微信支付分配的终端设备号
OpenId string `xml:"openid"` // 用户在商户appid下的唯一标识
IsSubscribe string `xml:"is_subscribe"` // 用户是否关注公众账号,Y-关注,N-未关注(机构商户不返回)
SubOpenId string `xml:"sub_openid"` // (服务商模式) 子商户appid下用户唯一标识,如需返回则请求时需要传sub_appid
SubIsSubscribe string `xml:"sub_is_subscribe"` // (服务商模式) 用户是否关注子公众账号,仅在公众账号类型支付有效,取值范围:Y或N;Y-关注;N-未关注
TradeType string `xml:"trade_type"` // 调用接口提交的交易类型,取值如下:JSAPI,NATIVE,APP,MICROPAY,详细说明见参数规定
TradeState string `xml:"trade_state"` // SUCCESS—支付成功 REFUND—转入退款 NOTPAY—未支付 CLOSED—已关闭 REVOKED—已撤销(刷卡支付) USERPAYING--用户支付中 PAYERROR--支付失败(其他原因,如银行返回失败)
BankType string `xml:"bank_type"` // 银行类型,采用字符串类型的银行标识
Detail string `xml:"detail"` // 商品详细列表,使用Json格式,传输签名前请务必使用CDATA标签将JSON文本串保护起来。如果使用了单品优惠,会有单品优惠信息返回
TotalFee int `xml:"total_fee"` // 订单总金额,单位为分
FeeType string `xml:"fee_type"` // 货币类型,符合ISO 4217标准的三位字母代码,默认人民币:CNY,其他值列表详见货币类型
SettlementTotalFee int `xml:"settlement_total_fee"` // 当订单使用了免充值型优惠券后返回该参数,应结订单金额=订单金额-免充值优惠券金额。
CashFee int `xml:"cash_fee"` // 现金支付金额订单现金支付金额,详见支付金额
CashFeeType string `xml:"cash_fee_type"` // 货币类型,符合ISO 4217标准的三位字母代码,默认人民币:CNY,其他值列表详见货币类型
CouponFee int `xml:"coupon_fee"` // "代金券或立减优惠"金额<=订单总金额,订单总金额-"代金券或立减优惠"金额=现金支付金额,详见支付金额
CouponCount int `xml:"coupon_count"` // 代金券或立减优惠使用数量
TransactionId string `xml:"transaction_id"` // 微信支付订单号
OutTradeNo string `xml:"out_trade_no"` // 商户系统内部订单号,要求32个字符内,只能是数字、大小写字母_-|*@ ,且在同一个商户号下唯一。
Attach string `xml:"attach"` // 商家数据包,原样返回
TimeEnd string `xml:"time_end"` // 订单支付时间,格式为yyyyMMddHHmmss,如2009年12月25日9点10分10秒表示为20091225091010。其他详见时间规则
TradeStateDesc string `xml:"trade_state_desc"` // 对当前查询订单状态的描述和下一步操作的指引
// 使用coupon_count的序号生成的优惠券项
Coupons []CouponResponseModel `xml:"-"`
}
// 查询订单-解析XML返回值
func (c *Client) queryOrderParseResponse(xmlStr []byte, rsp *QueryOrderResponse) (err error) {
// 常规解析
if err = xml.Unmarshal(xmlStr, rsp); err != nil {
return
}
// 解析CouponCount的对应项
if rsp.CouponCount > 0 {
doc := etree.NewDocument()
if err = doc.ReadFromBytes(xmlStr); err != nil {
return
}
root := doc.SelectElement("xml")
if root == nil {
err = errors.New("xml格式错误")
return
}
for i := 0; i < rsp.CouponCount; i++ {
m := NewCouponResponseModel(root, "coupon_id_%d", "coupon_type_%d", "coupon_fee_%d", i)
rsp.Coupons = append(rsp.Coupons, m)
}
}
return
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。