|
|
@ -9,8 +9,6 @@ import ( |
|
|
|
"hudongzhuanjia/models" |
|
|
|
"hudongzhuanjia/utils" |
|
|
|
"hudongzhuanjia/utils/define" |
|
|
|
"io" |
|
|
|
"io/ioutil" |
|
|
|
"strconv" |
|
|
|
"time" |
|
|
|
) |
|
|
@ -107,7 +105,7 @@ func ReOrder(outTradeNo string) (map[string]interface{}, error) { |
|
|
|
} |
|
|
|
|
|
|
|
// Notify
|
|
|
|
type NotifyRequest struct { |
|
|
|
type NotifyOrderParam struct { |
|
|
|
ReturnCode string `xml:"return_code,omitempty" json:"return_code,omitempty"` |
|
|
|
ReturnMsg string `xml:"return_msg,omitempty" json:"return_msg,omitempty"` |
|
|
|
ResultCode string `xml:"result_code,omitempty" json:"result_code,omitempty"` |
|
|
@ -128,62 +126,54 @@ type NotifyRequest struct { |
|
|
|
FeeType string `xml:"fee_type,omitempty" json:"fee_type,omitempty"` |
|
|
|
CashFee int `xml:"cash_fee,omitempty" json:"cash_fee,omitempty"` |
|
|
|
CashFeeType string `xml:"cash_fee_type,omitempty" json:"cash_fee_type,omitempty"` |
|
|
|
CouponFee int `xml:"coupon_fee,omitempty" json:"coupon_fee,omitempty"` |
|
|
|
CouponCount int `xml:"coupon_count,omitempty" json:"coupon_count,omitempty"` |
|
|
|
CouponType0 string `xml:"coupon_type_0,omitempty" json:"coupon_type_0,omitempty"` |
|
|
|
CouponType1 string `xml:"coupon_type_1,omitempty" json:"coupon_type_1,omitempty"` |
|
|
|
CouponId0 string `xml:"coupon_id_0,omitempty" json:"coupon_id_0,omitempty"` |
|
|
|
CouponId1 string `xml:"coupon_id_1,omitempty" json:"coupon_id_1,omitempty"` |
|
|
|
CouponFee0 int `xml:"coupon_fee_0,omitempty" json:"coupon_fee_0,omitempty"` |
|
|
|
CouponFee1 int `xml:"coupon_fee_1,omitempty" json:"coupon_fee_1,omitempty"` |
|
|
|
TransactionId string `xml:"transaction_id,omitempty" json:"transaction_id,omitempty"` |
|
|
|
OutTradeNo string `xml:"out_trade_no,omitempty" json:"out_trade_no,omitempty"` |
|
|
|
Attach string `xml:"attach,omitempty" json:"attach,omitempty"` |
|
|
|
TimeEnd string `xml:"time_end,omitempty" json:"time_end,omitempty"` |
|
|
|
} |
|
|
|
|
|
|
|
func NotifyOrder(w io.Reader) (order *models.UserOrder, err error) { |
|
|
|
var exist bool |
|
|
|
res := NotifyRequest{} |
|
|
|
body, err := ioutil.ReadAll(w) |
|
|
|
if err != nil { |
|
|
|
err = fmt.Errorf("body read all error:%+v", err) |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
err = xml.Unmarshal(body, &res) |
|
|
|
func NotifyOrder(body string) (*models.UserOrder, error) { |
|
|
|
res := NotifyOrderParam{} |
|
|
|
err := xml.Unmarshal([]byte(body), &res) |
|
|
|
if err != nil { |
|
|
|
err = fmt.Errorf("xml unmarsal error:%+v", err) |
|
|
|
return |
|
|
|
logger.Error(err) |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
if res.ReturnCode != define.CODE_SUCCESS { |
|
|
|
return nil, fmt.Errorf("network error, retrun_code: %+v and return_msg: %+v", res.ReturnCode, res.ReturnMsg) |
|
|
|
err = fmt.Errorf("network error, retrun_code: %+v and return_msg: %+v", res.ReturnCode, res.ReturnMsg) |
|
|
|
logger.Error(err) |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
if res.ResultCode != define.CODE_SUCCESS { |
|
|
|
err = fmt.Errorf("trade error, result_code: %+v and err_code: %+v and err_desc: %+v", |
|
|
|
res.ResultCode, res.ErrCode, res.ErrCodeDes) |
|
|
|
logger.Error(err) |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
order := new(models.UserOrder) |
|
|
|
exist, err := order.GetByOutTradeNo(res.OutTradeNo) |
|
|
|
if err == nil || !exist { |
|
|
|
err = fmt.Errorf("user order get by out_trade_no: %+v, error: %+v, exist: %+v", res.OutTradeNo, err, exist) |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
order.TransactionId = res.TransactionId |
|
|
|
order.Status = 1 |
|
|
|
_, err = models.Update(order.Id, order, "status", "transaction_id") |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
if res.ResultCode == define.CODE_SUCCESS && res.TradeType == define.CODE_TRADE_SUCCESS { |
|
|
|
userOrder := new(models.UserOrder) |
|
|
|
userOrder.TransactionId = res.TransactionId |
|
|
|
userOrder.Status = 1 |
|
|
|
_, err = userOrder.UpdateStatusByOutTradeNo(res.OutTradeNo, 1) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
exist, err = userOrder.GetByOutTradeNo(res.OutTradeNo) |
|
|
|
if err == nil || !exist { |
|
|
|
err = fmt.Errorf("user order get by out_trade_no: %+v, error: %+v, exist: %+v", res.OutTradeNo, err, exist) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
err = HandleSuccess(userOrder) |
|
|
|
if err != nil { |
|
|
|
logger.Error(fmt.Sprintf("handle success: %v", err)) |
|
|
|
return |
|
|
|
} |
|
|
|
return userOrder, nil |
|
|
|
} else { |
|
|
|
err = fmt.Errorf("trade error, err_code: %+v and err_code_des: %+v", res.ErrCode, res.ErrCodeDes) |
|
|
|
return |
|
|
|
err = HandleSuccess(order) |
|
|
|
if err != nil { |
|
|
|
logger.Error(fmt.Sprintf("callback 错误处理: %v", err)) |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
return order, nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
type OrderQueryResult struct { |
|
|
@ -286,3 +276,68 @@ func QueryRefund(outTradeNo string) (*pay.RefundQueryResponse, error) { |
|
|
|
} |
|
|
|
return res, nil |
|
|
|
} |
|
|
|
|
|
|
|
type NotifyRefundParam struct { |
|
|
|
ReturnCode string `xml:"return_code,omitempty" json:"return_code,omitempty"` |
|
|
|
ReturnMsg string `xml:"return_msg,omitempty" json:"return_msg,omitempty"` |
|
|
|
ResultCode string `xml:"result_code,omitempty" json:"result_code,omitempty"` |
|
|
|
ErrCode string `xml:"err_code,omitempty" json:"err_code,omitempty"` |
|
|
|
ErrCodeDes string `xml:"err_code_des,omitempty" json:"err_code_des,omitempty"` |
|
|
|
Appid string `xml:"appid,omitempty" json:"appid,omitempty"` |
|
|
|
MchId string `xml:"mch_id,omitempty" json:"mch_id,omitempty"` |
|
|
|
NonceStr string `xml:"nonce_str,omitempty" json:"nonce_str,omitempty"` |
|
|
|
ReqInfo string `xml:"req_info,omitempty" json:"req_info,omitempty"` |
|
|
|
TransactionId string `xml:"transaction_id,omitempty" json:"transaction_id,omitempty"` |
|
|
|
OutTradeNo string `xml:"out_trade_no,omitempty" json:"out_trade_no,omitempty"` |
|
|
|
RefundId string `xml:"refund_id,omitempty" json:"refund_id,omitempty"` |
|
|
|
OutRefundNo string `xml:"out_refund_no,omitempty" json:"out_refund_no,omitempty"` |
|
|
|
TotalFee int `xml:"total_fee,omitempty" json:"total_fee,omitempty"` |
|
|
|
SettlementTotalFee int `xml:"settlement_total_fee,omitempty" json:"settlement_total_fee,omitempty"` |
|
|
|
RefundFee int `xml:"refund_fee,omitempty" json:"refund_fee,omitempty"` |
|
|
|
SettlementRefundFee int `xml:"settlement_refund_fee,omitempty" json:"settlement_refund_fee,omitempty"` |
|
|
|
RefundStatus string `xml:"refund_status,omitempty" json:"refund_status,omitempty"` |
|
|
|
SuccessTime string `xml:"success_time,omitempty" json:"success_time,omitempty"` |
|
|
|
RefundRecvAccout string `xml:"refund_recv_accout,omitempty" json:"refund_recv_accout,omitempty"` |
|
|
|
RefundAccount string `xml:"refund_account,omitempty" json:"refund_account,omitempty"` |
|
|
|
RefundRequestSource string `xml:"refund_request_source,omitempty" json:"refund_request_source,omitempty"` |
|
|
|
} |
|
|
|
|
|
|
|
func NotifyRefund(body string) (*models.UserOrder, error) { |
|
|
|
res := NotifyRefundParam{} |
|
|
|
err := xml.Unmarshal([]byte(body), &res) |
|
|
|
if err != nil { |
|
|
|
err = fmt.Errorf("xml unmarsal error: %+v", err) |
|
|
|
logger.Error(err) |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
if res.ReturnCode != define.CODE_SUCCESS { |
|
|
|
err = fmt.Errorf("network error, retrun_code: %+v and return_msg: %+v", res.ReturnCode, res.ReturnMsg) |
|
|
|
logger.Error(err) |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
if res.ResultCode != define.CODE_SUCCESS { |
|
|
|
err = fmt.Errorf("trade error, result_code: %+v and err_code: %+v and err_desc: %+v", |
|
|
|
res.ResultCode, res.ErrCode, res.ErrCodeDes) |
|
|
|
logger.Error(err) |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
order := new(models.UserOrder) |
|
|
|
exist, err := order.GetByOutTradeNo(res.OutTradeNo) |
|
|
|
if err == nil || !exist { |
|
|
|
err = fmt.Errorf("user order get by out_trade_no: %+v, error: %+v, exist: %+v", res.OutTradeNo, err, exist) |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
order.RefundRecvAccount = res.RefundRecvAccout |
|
|
|
order.RefundAccount = res.RefundAccount |
|
|
|
order.TransactionId = res.TransactionId |
|
|
|
order.Status = 4 |
|
|
|
_, err = models.Update(order.Id, order, "status", "transaction_id") |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
return order, nil |
|
|
|
} |