wxpay
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.rosy.net.cn/baseapi/platformapi/tonglianpayapi"
|
"git.rosy.net.cn/baseapi/platformapi/tonglianpayapi"
|
||||||
|
"git.rosy.net.cn/baseapi/platformapi/wxpayapi"
|
||||||
"git.rosy.net.cn/baseapi/utils"
|
"git.rosy.net.cn/baseapi/utils"
|
||||||
"git.rosy.net.cn/jx-callback/business/auth2/authprovider/weixin"
|
"git.rosy.net.cn/jx-callback/business/auth2/authprovider/weixin"
|
||||||
"git.rosy.net.cn/jx-callback/business/jxutils"
|
"git.rosy.net.cn/jx-callback/business/jxutils"
|
||||||
@@ -27,7 +28,7 @@ func (p *PayHandler) CreatePay() (err error) {
|
|||||||
PayType: p.VendorPayType,
|
PayType: p.VendorPayType,
|
||||||
}
|
}
|
||||||
if p.VendorPayType == tonglianpayapi.PayTypeWxXcx {
|
if p.VendorPayType == tonglianpayapi.PayTypeWxXcx {
|
||||||
if authInfo, err := p.Ctx.GetV2AuthInfo(); err == nil && authInfo.GetAuthType() == weixin.AuthTypeMini {
|
if authInfo, err := p.Ctx.GetV2AuthInfo(); err == nil && authInfo.GetAuthType() == weixin.AuthTypeWxApp {
|
||||||
param.Acct = authInfo.GetAuthID()
|
param.Acct = authInfo.GetAuthID()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -37,42 +38,28 @@ func (p *PayHandler) CreatePay() (err error) {
|
|||||||
json.Unmarshal([]byte(result.PayInfo), &result2)
|
json.Unmarshal([]byte(result.PayInfo), &result2)
|
||||||
p.Order.PrepayID = result2.Package[strings.LastIndex(result2.Package, "=")+1 : len(result2.Package)]
|
p.Order.PrepayID = result2.Package[strings.LastIndex(result2.Package, "=")+1 : len(result2.Package)]
|
||||||
p.Order.TransactionID = result.TrxID
|
p.Order.TransactionID = result.TrxID
|
||||||
// p.Order.OriginalData = utils.LimitUTF8StringLen(result.PayInfo, 3200)
|
_, err = dao.UpdateEntity(dao.GetDB(), p.Order, "PrepayID", "TransactionID")
|
||||||
_, err = dao.UpdateEntity(dao.GetDB(), p.Order, "PrepayID", "TransactionID", "OriginalData")
|
|
||||||
}
|
}
|
||||||
case model.PayTypeWX:
|
case model.PayTypeWX:
|
||||||
// payCreatedAt := time.Now()
|
param := &wxpayapi.CreateOrderParam{
|
||||||
// param := &wxpayapi.CreateOrderParam{
|
OutTradeNo: utils.Int64ToStr(p.Order.OrderID),
|
||||||
// OutTradeNo: utils.Int64ToStr(p.Order.OrderID),
|
Body: fmt.Sprintf("付款给冲天猴儿app:%f 元", jxutils.IntPrice2Standard(int64(p.Order.PayPrice))),
|
||||||
// Body: fmt.Sprintf("付款给冲天猴儿app:%f 元", jxutils.IntPrice2Standard(int64(p.Order.PayPrice))),
|
NotifyURL: globals.WxpayNotifyURL,
|
||||||
// NotifyURL: globals.WxpayNotifyURL,
|
SpbillCreateIP: p.Ctx.GetRealRemoteIP(),
|
||||||
// SpbillCreateIP: p.Ctx.GetRealRemoteIP(),
|
TradeType: p.VendorPayType,
|
||||||
// TradeType: p.VendorPayType,
|
TotalFee: p.Order.PayPrice,
|
||||||
// TotalFee: int(order.ActualPayPrice),
|
TimeStart: wxpayapi.Time2PayTime(time.Now()),
|
||||||
|
ProfitSharing: wxpayapi.OptYes,
|
||||||
// TimeStart: wxpayapi.Time2PayTime(payCreatedAt),
|
}
|
||||||
// // TimeExpire: wxpayapi.Time2PayTime(payCreatedAt.Add(PayWaitingTime)),
|
if authInfo, err := p.Ctx.GetV2AuthInfo(); err == nil && authInfo.GetAuthType() == weixin.AuthTypeWxApp {
|
||||||
// ProfitSharing: wxpayapi.OptYes,
|
param.OpenID = authInfo.GetAuthID()
|
||||||
// }
|
}
|
||||||
// if authInfo, err := ctx.GetV2AuthInfo(); err == nil && authInfo.GetAuthType() == weixin.AuthTypeMini {
|
result, err := api.WxpayAPI.CreateUnifiedOrder(param)
|
||||||
// param.OpenID = authInfo.GetAuthID()
|
if err == nil {
|
||||||
// }
|
p.Order.PrepayID = result.PrepayID
|
||||||
// result, err := api.WxpayAPI.CreateUnifiedOrder(param)
|
p.Order.Comment = result.CodeURL
|
||||||
// if err == nil {
|
_, err = dao.UpdateEntity(dao.GetDB(), p.Order, "PrepayID", "Comment")
|
||||||
// orderPay = &model.OrderPay{
|
}
|
||||||
// PayOrderID: param.OutTradeNo,
|
|
||||||
// PayType: model.PayTypeWX,
|
|
||||||
// VendorPayType: vendorPayType,
|
|
||||||
|
|
||||||
// VendorOrderID: order.VendorOrderID,
|
|
||||||
// VendorID: order.VendorID,
|
|
||||||
// Status: 0,
|
|
||||||
// PayCreatedAt: payCreatedAt,
|
|
||||||
// PrepayID: result.PrepayID,
|
|
||||||
// CodeURL: result.CodeURL,
|
|
||||||
// TotalFee: int(order.ActualPayPrice),
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
default:
|
default:
|
||||||
err = fmt.Errorf("支付方式:%d当前不支持", p.PayType)
|
err = fmt.Errorf("支付方式:%d当前不支持", p.PayType)
|
||||||
}
|
}
|
||||||
@@ -192,3 +179,69 @@ func RefundOrderByTL(ctx *jxcontext.Context, orderPay *model.OrderPay, refundID
|
|||||||
}
|
}
|
||||||
return orderPayRefund, err
|
return orderPayRefund, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func OnWxPayCallback(msg *wxpayapi.CallbackMsg) (err error) {
|
||||||
|
globals.SugarLogger.Debugf("OnWxPayCallback msg:%s", utils.Format4Output(msg, true))
|
||||||
|
switch msg.MsgType {
|
||||||
|
case wxpayapi.MsgTypePay:
|
||||||
|
err = onWxpayFinished(msg.Data.(*wxpayapi.PayResultMsg))
|
||||||
|
case wxpayapi.MsgTypeRefund:
|
||||||
|
err = onWxpayRefund(msg.Data.(*wxpayapi.RefundResultMsg))
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func onWxpayFinished(msg *wxpayapi.PayResultMsg) (err error) {
|
||||||
|
order := &model.Order{
|
||||||
|
OrderID: utils.Str2Int64(msg.OutTradeNo),
|
||||||
|
}
|
||||||
|
db := dao.GetDB()
|
||||||
|
if err = dao.GetEntity(db, order, "OrderID"); err == nil {
|
||||||
|
order.PayFinishedAt = utils.Time2Pointer(wxpayapi.PayTime2Time(msg.TimeEnd))
|
||||||
|
order.TransactionID = msg.TransactionID
|
||||||
|
order.OriginalData = utils.Format4Output(msg, true)
|
||||||
|
if msg.ResultCode == wxpayapi.ResponseCodeSuccess {
|
||||||
|
order.Status = model.OrderStatusFinished
|
||||||
|
} else {
|
||||||
|
order.Status = model.OrderStatusCanceled
|
||||||
|
}
|
||||||
|
dao.UpdateEntity(db, order)
|
||||||
|
if msg.ResultCode == wxpayapi.ResponseCodeSuccess {
|
||||||
|
err = OnPayFinished(order)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
globals.SugarLogger.Debugf("onWxpayFinished msg:%s, err:%v", utils.Format4Output(msg, true), err)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func onWxpayRefund(msg *wxpayapi.RefundResultMsg) (err error) {
|
||||||
|
orderPayRefund := &model.OrderPayRefund{
|
||||||
|
RefundID: msg.ReqInfoObj.OutRefundNo,
|
||||||
|
}
|
||||||
|
db := dao.GetDB()
|
||||||
|
if err = dao.GetEntity(db, orderPayRefund, "RefundID"); err == nil {
|
||||||
|
if msg.ResultCode == wxpayapi.ResponseCodeSuccess {
|
||||||
|
orderPayRefund.Status = model.RefundStatusYes
|
||||||
|
} else {
|
||||||
|
orderPayRefund.Status = model.RefundStatusFailed
|
||||||
|
}
|
||||||
|
orderPayRefund.OriginalData = utils.Format4Output(msg, true)
|
||||||
|
dao.UpdateEntity(db, orderPayRefund)
|
||||||
|
} else if dao.IsNoRowsError(err) {
|
||||||
|
globals.SugarLogger.Warnf("收到异常的退款事件, msg:%s", utils.Format4Output(msg, true))
|
||||||
|
}
|
||||||
|
|
||||||
|
orderPay := &model.OrderPay{
|
||||||
|
VendorOrderID: orderPayRefund.VendorOrderID,
|
||||||
|
VendorID: jxutils.GetPossibleVendorIDFromVendorOrderID(orderPayRefund.VendorOrderID),
|
||||||
|
PayType: model.PayTypeWX,
|
||||||
|
Status: model.PayStatusYes,
|
||||||
|
}
|
||||||
|
orderPay.DeletedAt = utils.DefaultTimeValue
|
||||||
|
if err = dao.GetEntity(db, orderPay, "VendorOrderID", "VendorID", "PayType", "Status", "DeletedAt"); err == nil {
|
||||||
|
orderPay.Status = model.PayStatusRefund
|
||||||
|
dao.UpdateEntity(db, orderPay)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user