package localjx import ( "crypto/rand" "crypto/rsa" "crypto/x509" "encoding/json" "encoding/pem" "os" "time" "git.rosy.net.cn/jx-callback/globals" "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/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/api" ) func pay4OrderByTL(ctx *jxcontext.Context, order *model.GoodsOrder, vendorPayType string) (orderPay *model.OrderPay, err error) { payCreatedAt := time.Now() param := &tonglianpayapi.CreateUnitorderOrderParam{ Trxamt: int(order.ActualPayPrice), NotifyUrl: globals.TLPayNotifyURL, Reqsn: order.VendorOrderID, } if authInfo, err := ctx.GetV2AuthInfo(); err == nil && authInfo.GetAuthType() == weixin.AuthTypeMini { param.Acct = authInfo.GetAuthID() } result, err := api.TLpayAPI.CreateUnitorderOrder(param) if err == nil { result2 := &tonglianpayapi.PayInfo{} json.Unmarshal([]byte(result.PayInfo), &result2) orderPay = &model.OrderPay{ PayOrderID: param.Reqsn, PayType: model.PayTypeTL, VendorPayType: vendorPayType, VendorOrderID: order.VendorOrderID, VendorID: order.VendorID, Status: 0, PayCreatedAt: payCreatedAt, PrepayID: result2.PrepayID, CodeURL: utils.LimitUTF8StringLen(result.PayInfo, 3200), TotalFee: int(order.ActualPayPrice), } plainText, err2 := RSADecrypt([]byte(result2.PaySign)) result2.PaySign = string(plainText) str, err2 := json.Marshal(result2) result.PayInfo = string(str) err = err2 } return orderPay, err } func RSADecrypt(pub []byte) (plainText []byte, err error) { //打开文件 file, err := os.Open("conf/rsa_key.pem") if err != nil { panic(err) } defer file.Close() //获取文件内容 info, _ := file.Stat() buf := make([]byte, info.Size()) file.Read(buf) //pem解码 block, _ := pem.Decode(buf) //X509解码 privateKey, err := x509.ParsePKCS8PrivateKey(block.Bytes) if err != nil { panic(err) } //对密文进行解密 plainText, err = rsa.DecryptPKCS1v15(rand.Reader, privateKey.(*rsa.PrivateKey), pub) return plainText, 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", "PayType", "DeletedAt"); err == nil { orderPay.PayFinishedAt = utils.Time2Pointer(utils.Str2Time(call.PayTime)) 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 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 }