Пример #1
0
func Refund2(clt *core.Client, req *RefundRequest) (resp *RefundResponse, err error) {
	m1 := make(map[string]string, 16)
	m1["appid"] = clt.AppId()
	m1["mch_id"] = clt.MchId()
	if req.DeviceInfo != "" {
		m1["device_info"] = req.DeviceInfo
	}
	if req.NonceStr != "" {
		m1["nonce_str"] = req.NonceStr
	} else {
		m1["nonce_str"] = string(rand.NewHex())
	}
	if req.TransactionId != "" {
		m1["transaction_id"] = req.TransactionId
	}
	if req.OutTradeNo != "" {
		m1["out_trade_no"] = req.OutTradeNo
	}
	m1["out_refund_no"] = req.OutRefundNo
	m1["total_fee"] = strconv.FormatInt(req.TotalFee, 10)
	m1["refund_fee"] = strconv.FormatInt(req.RefundFee, 10)
	if req.RefundFeeType != "" {
		m1["refund_fee_type"] = req.RefundFeeType
	}
	if req.OperUserId != "" {
		m1["op_user_id"] = req.OperUserId
	} else {
		m1["op_user_id"] = clt.MchId()
	}
	m1["sign"] = core.Sign(m1, clt.ApiKey(), md5.New)

	m2, err := Refund(clt, m1)
	if err != nil {
		return
	}

	// 判断业务状态
	resultCode, ok := m2["result_code"]
	if !ok {
		err = core.ErrNotFoundResultCode
		return
	}
	if resultCode != core.ResultCodeSuccess {
		err = &core.BizError{
			ResultCode:  resultCode,
			ErrCode:     m2["err_code"],
			ErrCodeDesc: m2["err_code_des"],
		}
		return
	}

	resp = &RefundResponse{
		AppId: m2["appid"],
		MchId: m2["mch_id"],

		TransactionId: m2["transaction_id"],
		OutTradeNo:    m2["out_trade_no"],
		OutRefundNo:   m2["out_refund_no"],
		RefundId:      m2["refund_id"],

		DeviceInfo:    m2["device_info"],
		RefundChannel: m2["refund_channel"],
		FeeType:       m2["fee_type"],
	}

	var (
		n   int64
		str string
	)
	if n, err = strconv.ParseInt(m2["refund_fee"], 10, 64); err != nil {
		err = fmt.Errorf("parse refund_fee:%q to int64 failed: %s", m2["refund_fee"], err.Error())
		return
	} else {
		resp.RefundFee = n
	}
	if n, err = strconv.ParseInt(m2["total_fee"], 10, 64); err != nil {
		err = fmt.Errorf("parse total_fee:%q to int64 failed: %s", m2["total_fee"], err.Error())
		return
	} else {
		resp.TotalFee = n
	}
	if n, err = strconv.ParseInt(m2["cash_fee"], 10, 64); err != nil {
		err = fmt.Errorf("parse cash_fee:%q to int64 failed: %s", m2["cash_fee"], err.Error())
		return
	} else {
		resp.CashFee = n
	}
	if str = m2["settlement_refund_fee"]; str != "" {
		if n, err = strconv.ParseInt(str, 10, 64); err != nil {
			err = fmt.Errorf("parse settlement_refund_fee:%q to int64 failed: %s", str, err.Error())
			return
		} else {
			resp.SettlementRefundFee = util.Int64(n)
		}
	}
	if str = m2["settlement_total_fee"]; str != "" {
		if n, err = strconv.ParseInt(str, 10, 64); err != nil {
			err = fmt.Errorf("parse settlement_total_fee:%q to int64 failed: %s", str, err.Error())
			return
		} else {
			resp.SettlementTotalFee = util.Int64(n)
		}
	}
	if str = m2["cash_refund_fee"]; str != "" {
		if n, err = strconv.ParseInt(str, 10, 64); err != nil {
			err = fmt.Errorf("parse cash_refund_fee:%q to int64 failed: %s", str, err.Error())
			return
		} else {
			resp.CashRefundFee = util.Int64(n)
		}
	}
	return
}
Пример #2
0
func OrderQuery2(clt *core.Client, req *OrderQueryRequest) (resp *OrderQueryResponse, err error) {
	m1 := make(map[string]string, 8)
	m1["appid"] = clt.AppId()
	m1["mch_id"] = clt.MchId()
	if req.TransactionId != "" {
		m1["transaction_id"] = req.TransactionId
	}
	if req.OutTradeNo != "" {
		m1["out_trade_no"] = req.OutTradeNo
	}
	if req.NonceStr != "" {
		m1["nonce_str"] = req.NonceStr
	} else {
		m1["nonce_str"] = string(rand.NewHex())
	}
	m1["sign"] = core.Sign(m1, clt.ApiKey(), md5.New)

	m2, err := OrderQuery(clt, m1)
	if err != nil {
		return
	}

	// 判断业务状态
	resultCode, ok := m2["result_code"]
	if !ok {
		err = core.ErrNotFoundResultCode
		return
	}
	if resultCode != core.ResultCodeSuccess {
		err = &core.BizError{
			ResultCode:  resultCode,
			ErrCode:     m2["err_code"],
			ErrCodeDesc: m2["err_code_des"],
		}
		return
	}

	resp = &OrderQueryResponse{
		AppId: m2["appid"],
		MchId: m2["mch_id"],

		OpenId:         m2["openid"],
		TradeType:      m2["trade_type"],
		TradeState:     m2["trade_state"],
		BankType:       m2["bank_type"],
		TransactionId:  m2["transaction_id"],
		OutTradeNo:     m2["out_trade_no"],
		TimeEnd:        m2["time_end"],
		TradeStateDesc: m2["trade_state_desc"],
	}

	var (
		n   int64
		id  int
		str string
	)
	if n, err = strconv.ParseInt(m2["total_fee"], 10, 64); err != nil {
		err = fmt.Errorf("parse total_fee:%q to int64 failed: %s", m2["total_fee"], err.Error())
		return
	} else {
		resp.TotalFee = n
	}
	if n, err = strconv.ParseInt(m2["cash_fee"], 10, 64); err != nil {
		err = fmt.Errorf("parse cash_fee:%q to int64 failed: %s", m2["cash_fee"], err.Error())
		return
	} else {
		resp.CashFee = n
	}

	resp.DeviceInfo = m2["device_info"]
	if str = m2["is_subscribe"]; str != "" {
		if str == "Y" || str == "y" {
			resp.IsSubscribe = util.Bool(true)
		} else {
			resp.IsSubscribe = util.Bool(false)
		}
	}
	if str = m2["settlement_total_fee"]; str != "" {
		if n, err = strconv.ParseInt(str, 10, 64); err != nil {
			err = fmt.Errorf("parse settlement_total_fee:%q to int64 failed: %s", str, err.Error())
			return
		} else {
			resp.SettlementTotalFee = util.Int64(n)
		}
	}
	resp.FeeType = m2["fee_type"]
	resp.CashFeeType = m2["cash_fee_type"]
	if str = m2["coupon_fee"]; str != "" {
		if n, err = strconv.ParseInt(str, 10, 64); err != nil {
			err = fmt.Errorf("parse coupon_fee:%q to int64 failed: %s", str, err.Error())
			return
		} else {
			resp.CouponFee = util.Int64(n)
		}
	}
	if str = m2["coupon_count"]; str != "" {
		if n, err = strconv.ParseInt(str, 10, 32); err != nil {
			err = fmt.Errorf("parse coupon_count:%q to int failed: %s", str, err.Error())
			return
		} else {
			resp.CouponCount = util.Int(int(n))
		}
	}
	resp.Attach = m2["attach"]

	if resp.CouponCount == nil {
		return // 没有代金券, 直接返回
	}
	CouponCount := *resp.CouponCount
	if CouponCount < 0 {
		err = fmt.Errorf("invalid coupon_count: %s", m2["coupon_count"])
		return
	}
	Coupons := make([]OrderCoupon, CouponCount)

	for k, v := range m2 {
		switch {
		case strings.HasPrefix(k, "coupon_batch_id_"):
			id, err = parseCouponId(k, k[len("coupon_batch_id_"):], CouponCount)
			if err != nil {
				return
			}
			Coupons[id].CouponBatchId = v
		case strings.HasPrefix(k, "coupon_type_"):
			id, err = parseCouponId(k, k[len("coupon_type_"):], CouponCount)
			if err != nil {
				return
			}
			Coupons[id].CouponType = v
		case strings.HasPrefix(k, "coupon_id_"):
			id, err = parseCouponId(k, k[len("coupon_id_"):], CouponCount)
			if err != nil {
				return
			}
			Coupons[id].CouponId = v
		case strings.HasPrefix(k, "coupon_fee_"):
			id, err = parseCouponId(k, k[len("coupon_fee_"):], CouponCount)
			if err != nil {
				return
			}
			n, err = strconv.ParseInt(v, 10, 64)
			if err != nil {
				return
			}
			Coupons[id].CouponFee = n
		}
	}
	return
}