Files
jx-callback/business/jxstore/financial/financial.go
苏尹岚 cbe94c7916 pay
2020-10-14 15:25:26 +08:00

219 lines
7.2 KiB
Go

package financial
import (
"encoding/json"
"fmt"
"strings"
"time"
"git.rosy.net.cn/baseapi/platformapi/tonglianpayapi"
"git.rosy.net.cn/baseapi/utils"
"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/jxcontext"
"git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/business/model/dao"
"git.rosy.net.cn/jx-callback/globals"
"git.rosy.net.cn/jx-callback/globals/api"
)
var (
IPayHandler PayHandlerInterface
)
func (p *PayHandler) CreatePay(order *model.Order, payType int, vendorPayType string) (err error) {
switch payType {
case model.PayTypeTL:
param := &tonglianpayapi.CreateUnitorderOrderParam{
Trxamt: int(order.PayPrice),
NotifyUrl: globals.TLPayNotifyURL,
Reqsn: utils.Int64ToStr(order.OrderID),
PayType: vendorPayType,
}
if vendorPayType == tonglianpayapi.PayTypeWxXcx {
if authInfo, err := ctx.GetV2AuthInfo(); err == nil && authInfo.GetAuthType() == weixin.AuthTypeMini {
param.Acct = authInfo.GetAuthID()
}
}
result, err := api.TLpayAPI.CreateUnitorderOrder(param)
if err == nil {
var result2 tonglianpayapi.PayInfo
json.Unmarshal([]byte(result.PayInfo), &result2)
order.PrepayID = result2.Package[strings.LastIndex(result2.Package, "=")+1 : len(result2.Package)]
orderPay = &model.OrderPay{
PayOrderID: param.Reqsn,
PayType: payType,
VendorPayType: vendorPayType,
TransactionID: result.TrxID,
VendorOrderID: order.VendorOrderID,
VendorID: order.VendorID,
Status: 0,
PayCreatedAt: payCreatedAt,
PrepayID: prePayID,
CodeURL: utils.LimitUTF8StringLen(result.PayInfo, 3200),
TotalFee: int(order.ActualPayPrice),
}
}
default:
err = fmt.Errorf("支付方式:%d当前不支持", payType)
}
return err
}
func Pay4OrderByTL(ctx *jxcontext.Context, order *model.GoodsOrder, payType int, vendorPayType string) (orderPay *model.OrderPay, err error) {
payCreatedAt := time.Now()
param := &tonglianpayapi.CreateUnitorderOrderParam{
Trxamt: int(order.ActualPayPrice),
NotifyUrl: globals.TLPayNotifyURL,
Reqsn: order.VendorOrderID,
PayType: vendorPayType,
}
//暂时做兼容处理
if vendorPayType == "JSAPI" {
param.PayType = tonglianpayapi.PayTypeWxXcx
}
if vendorPayType == tonglianpayapi.PayTypeWxXcx {
if authInfo, err := ctx.GetV2AuthInfo(); err == nil && authInfo.GetAuthType() == weixin.AuthTypeMini {
param.Acct = authInfo.GetAuthID()
}
}
if vendorPayType == tonglianpayapi.PayTypeH5 {
param2 := &tonglianpayapi.CreateH5UnitorderOrderParam{
Trxamt: int(order.ActualPayPrice),
NotifyUrl: globals.TLPayNotifyURL,
Body: "京西菜市",
Charset: "UTF-8",
}
err = api.TLpayAPI.CreateH5UnitorderOrder(param2)
} else {
result, err := api.TLpayAPI.CreateUnitorderOrder(param)
if err == nil {
var result2 tonglianpayapi.PayInfo
json.Unmarshal([]byte(result.PayInfo), &result2)
prePayID := result2.Package[strings.LastIndex(result2.Package, "=")+1 : len(result2.Package)]
orderPay = &model.OrderPay{
PayOrderID: param.Reqsn,
PayType: payType,
VendorPayType: vendorPayType,
TransactionID: result.TrxID,
VendorOrderID: order.VendorOrderID,
VendorID: order.VendorID,
Status: 0,
PayCreatedAt: payCreatedAt,
PrepayID: prePayID,
CodeURL: utils.LimitUTF8StringLen(result.PayInfo, 3200),
TotalFee: int(order.ActualPayPrice),
}
}
}
return orderPay, err
}
func OnTLPayCallback(call *tonglianpayapi.CallBackResult) (err error) {
globals.SugarLogger.Debugf("OnTLPayCallback msg:%s", utils.Format4Output(call, true))
switch call.TrxCode {
case tonglianpayapi.MsgTypePay:
err = onTLpayFinished(call)
case tonglianpayapi.MsgTypeRefund:
err = onTLpayRefund(call)
}
return err
}
func onTLpayFinished(call *tonglianpayapi.CallBackResult) (err error) {
orderPay := &model.OrderPay{
PayOrderID: call.CusorderID,
// PayType: model.PayTypeTL,
}
orderPay.DeletedAt = utils.DefaultTimeValue
db := dao.GetDB()
if err = dao.GetEntity(db, orderPay, "PayOrderID", "DeletedAt"); err == nil {
if orderPay.Status != 0 {
globals.SugarLogger.Debugf("already pay msg:%s, err:%v", utils.Format4Output(call, true), err)
return err
}
loc, _ := time.LoadLocation("Local")
t1, _ := time.ParseInLocation("20060102150405", call.PayTime, loc)
orderPay.PayFinishedAt = utils.Time2Pointer(t1)
// orderPay.TransactionID = call.ChnlTrxID
orderPay.OriginalData = utils.Format4Output(call, true)
if call.TrxStatus == tonglianpayapi.TrxStatusSuccess {
orderPay.Status = model.PayStatusYes
} else {
orderPay.Status = model.PayStatusFailed
}
dao.UpdateEntity(db, orderPay)
if call.TrxStatus == tonglianpayapi.TrxStatusSuccess {
// err = OnPayFinished(orderPay)
}
} else {
globals.SugarLogger.Debugf("onTLpayFinished msg:%s, err:%v", utils.Format4Output(call, true), err)
}
return err
}
func onTLpayRefund(call *tonglianpayapi.CallBackResult) (err error) {
orderPayRefund := &model.OrderPayRefund{
RefundID: call.CusorderID,
}
db := dao.GetDB()
if err = dao.GetEntity(db, orderPayRefund, "RefundID"); err == nil {
if call.TrxStatus == tonglianpayapi.TrxStatusSuccess {
orderPayRefund.Status = model.RefundStatusYes
} else {
orderPayRefund.Status = model.RefundStatusFailed
}
orderPayRefund.OriginalData = utils.Format4Output(call, true)
dao.UpdateEntity(db, orderPayRefund)
} else if dao.IsNoRowsError(err) {
globals.SugarLogger.Warnf("收到异常的退款事件, call:%s", utils.Format4Output(call, 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
}
func RefundOrderByTL(ctx *jxcontext.Context, orderPay *model.OrderPay, refundID string, refundFee int, refundDesc string) (orderPayRefund *model.OrderPayRefund, err error) {
result, err := api.TLpayAPI.PayRefund(&tonglianpayapi.PayRefundParam{
Trxamt: refundFee,
Reqsn: utils.GetUUID(),
Remark: refundDesc,
OldTrxID: orderPay.TransactionID,
})
if err == nil {
orderPayRefund = &model.OrderPayRefund{
RefundID: refundID,
VendorRefundID: result.TrxID,
VendorOrderID: orderPay.VendorOrderID,
VendorID: orderPay.VendorID,
Status: model.RefundStatusNo,
TransactionID: orderPay.TransactionID,
RefundFee: refundFee,
RefundCreatedAt: time.Now(),
}
dao.WrapAddIDCULDEntity(orderPayRefund, ctx.GetUserName())
db := dao.GetDB()
if result.TrxStatus == tonglianpayapi.TrxStatusSuccess {
orderPayRefund.Status = model.RefundStatusYes
} else {
orderPayRefund.Status = model.RefundStatusFailed
}
orderPayRefund.OriginalData = utils.Format4Output(result, true)
dao.CreateEntity(db, orderPayRefund)
orderPay.Status = model.PayStatusRefund
dao.UpdateEntity(db, orderPay)
}
return orderPayRefund, err
}