Compare commits

...

8 Commits

8 changed files with 301 additions and 48 deletions

View File

@ -35,6 +35,7 @@ const (
AppIpNotAllow = 1202
AppRsaDecryptKeyNotFound = 1203
AppDecryptDataDiscrepancy = 1204
AppNotifyUrlNotFound = 1205
AppRsaDecryptFail = 1210
AppRsaEncryptKeyNotFound = 1211
AppRsaEncryptFail = 1212
@ -56,11 +57,16 @@ const (
OrdersNotFound = 1401
OrdersExist = 1402
OrderTypeNotFount = 1403
OrderIsDelete = 1405
OrderStatusErr = 1406
//请求日志
RequestLogErrors = 1500
RequestLogNotFound = 1501
RequestResponseValid = 1502
//回调
NotifySendFail = 1600
)
var MsgEN = map[int]string{
@ -85,6 +91,7 @@ var MsgZH = map[int]string{
AppIpNotAllow: "ip不在白名单内",
AppDecryptDataDiscrepancy: "解密数据不一致",
SystemError: "系统错误",
AppNotifyUrlNotFound: "未设置回调地址",
AppRsaDecryptKeyNotFound: "密匙缺失无法进行Rsa解密",
AppRsaDecryptFail: "Rsa解密失败",
@ -112,6 +119,10 @@ var MsgZH = map[int]string{
OrdersNotFound: "未找到订单",
OrdersExist: "订单已存在",
OrderTypeNotFount: "未知的支付方式",
OrderIsDelete: "订单已删除",
OrderStatusErr: "订单状态错误",
NotifySendFail: "回调发送失败",
}
var MsgMap map[string]map[int]string = map[string]map[int]string{"en": MsgZH}

View File

@ -72,9 +72,6 @@ func WxCallback(c *gin.Context) {
return
}
// ====↓↓↓====异步通知应答====↓↓↓====
// 退款通知http应答码为200且返回状态码为SUCCESS才会当做商户接收成功否则会重试。
// 注意:重试过多会导致微信支付端积压过多通知而堵塞,影响其他正常通知。
c.JSON(http.StatusOK, &wechat.V3NotifyRsp{Code: gopay.SUCCESS, Message: "成功"})
return
}
@ -105,28 +102,18 @@ func AliCallback(c *gin.Context) {
}
var aliConfig paymentService.AliPay
var aliConfigModel struct {
PrivateKey string `json:"private_key"` // 应用私钥
AppPublicCert string `json:"app_public_cert"` // 应用公钥
AlipayRootCert string `json:"alipay_root_cert"` // 支付宝根证书
AlipayPublicCert string `json:"alipay_public_cert"` // 支付宝公钥
}
err := json.Unmarshal([]byte(payChannelModel.ExtJson), &aliConfigModel)
err := json.Unmarshal([]byte(payChannelModel.ExtJson), &aliConfig)
if err != nil {
logger.Error(c, "AliCallback-回调数据解析支付配置错误", fmt.Sprintf("错误原因:%s", err.Error()))
c.String(http.StatusBadRequest, "%s", "fail")
return
}
if aliConfigModel.AlipayPublicCert == "" || aliConfigModel.PrivateKey == "" || aliConfigModel.AppPublicCert == "" || aliConfigModel.AlipayRootCert == "" {
if aliConfig.AlipayPublicCert == "" || aliConfig.PrivateKey == "" || aliConfig.AppPublicCert == "" || aliConfig.AlipayRootCert == "" {
logger.Error(c, "AliCallback-回调数据解析支付配置错误,解析出来的信息为空")
c.String(http.StatusBadRequest, "%s", "fail")
return
}
aliConfig.AppId = payChannelModel.AppId
aliConfig.PrivateKey = aliConfigModel.PrivateKey
aliConfig.AppPublicCert = aliConfigModel.AppPublicCert
aliConfig.AlipayRootCert = aliConfigModel.AlipayRootCert
aliConfig.AlipayPublicCert = aliConfigModel.AlipayPublicCert
notifyReq, err := alipay.ParseNotifyToBodyMap(c.Request) // c.Request 是 gin 框架的写法
if err != nil {

View File

@ -82,6 +82,17 @@ func OrderCreate(orderIn *ordersmodel.Orders) (orderOut *ordersmodel.Orders, cod
return orderIn, code
}
func OrderUpdate(order *ordersmodel.Orders, col ...string) (code int) {
repo := data.NewOrderRepo(ordersmodel.GetInstance().GetDb())
// 拼接查询条件
conn := builder.NewCond()
conn = conn.And(builder.Eq{"Id": order.Id})
_, err := repo.OrderUpdate(order, conn, col...)
code = handErr(err)
return
}
func OrderFindOne(order *ordersmodel.Orders, conn builder.Cond, col ...string) (merchantInfo *ordersmodel.Orders, code int) {
repo := data.NewOrderRepo(ordersmodel.GetInstance().GetDb())
// 拼接查询条件

View File

@ -1,6 +1,7 @@
package do
import (
"PaymentCenter/app/constants/common"
"PaymentCenter/app/constants/errorcode"
"PaymentCenter/app/services"
@ -31,6 +32,7 @@ func (w *Pay) CreateOrder() {
Amount: w.paycheck.WebPayReqs.Amount,
ExtJson: w.paycheck.WebPayReqs.ExtJson,
Desc: w.paycheck.WebPayReqs.Desc,
Status: common.ORDER_STATUS_WAITPAY,
},
)
}
@ -58,7 +60,7 @@ func (w *Pay) Pay() {
return
}
res := paymentService.PaymentService(*w.paycheck.ctx, *thirdPay)
// payCommon.PAY_SUCCESS_CODE
w.PayCode = res.Code
return
}

View File

@ -0,0 +1,164 @@
package notify
import (
"PaymentCenter/app/constants/common"
"PaymentCenter/app/constants/errorcode"
"PaymentCenter/app/http/entities"
"PaymentCenter/app/models/appmodel"
"PaymentCenter/app/models/ordersmodel"
"PaymentCenter/app/services"
"PaymentCenter/app/utils/httpclient"
"github.com/bytedance/sonic"
"time"
"xorm.io/builder"
)
type OrderNotify struct {
order *ordersmodel.Orders
code int
app *appmodel.App
OrderId int64
Status int //订单状态
ActualAmount int
Msg string
CompleteTime time.Time
}
type OrderNotifyResp struct {
OrderId int64
Send bool
ErrCode int
SendTime time.Time
Content *OrderNotify
}
type OrderNotifySendContent struct {
OrderId int64 `json:"order_id"`
OutTreadNo string `json:"out_tread_no"`
CompleteTime time.Time `json:"complete_time"`
Status int `json:"status"`
Msg string `json:"msg"`
ErrCode int `json:"err_code"`
AppId int64 `json:"app_id"`
ChannelId int64 `json:"channel_id"`
MerchantId int64 `json:"merchant_id"`
}
func NewOrderNotifyWithHandle(orderId int64, Status int, actualAmount int, msg string) *OrderNotifyResp {
orderNotify := &OrderNotify{
OrderId: orderId,
Status: Status,
ActualAmount: actualAmount,
Msg: msg,
code: errorcode.Success,
}
return orderNotify.handle()
}
func (o *OrderNotify) NotifyRespFail(errorCode int) *OrderNotifyResp {
return &OrderNotifyResp{
OrderId: o.OrderId,
Send: false,
ErrCode: errorCode,
Content: o,
}
}
func (o *OrderNotify) handle() (res *OrderNotifyResp) {
o.checkOrder()
if o.code != errorcode.Success {
return o.NotifyRespFail(o.code)
}
o.checkApp()
if o.code != errorcode.Success {
return o.NotifyRespFail(o.code)
}
o.updateOrder()
if o.code != errorcode.Success {
return o.NotifyRespFail(o.code)
}
o.sendNotify(o.setBody())
if o.code != errorcode.Success {
return o.NotifyRespFail(o.code)
}
return &OrderNotifyResp{
OrderId: o.OrderId,
Send: true,
ErrCode: o.code,
Content: o,
}
}
func (o *OrderNotify) sendNotify(body *OrderNotifySendContent) {
bodyByte, _ := sonic.Marshal(&body)
headers := make(map[string]string, 1)
headers["Content-Type"] = "application/json"
resByte, err := httpclient.FastHttpPost(o.app.NotifyUrl, headers, bodyByte, 0)
if err != nil || string(resByte) != "success" {
o.code = errorcode.NotifySendFail
}
return
}
func (o *OrderNotify) setBody() *OrderNotifySendContent {
return &OrderNotifySendContent{
OrderId: o.OrderId,
OutTreadNo: o.order.OutTreadNo,
CompleteTime: o.CompleteTime,
Status: o.order.Status,
Msg: o.Msg,
ErrCode: o.code,
AppId: o.order.AppId,
ChannelId: o.order.PayChannelId,
MerchantId: o.order.MerchantId,
}
}
func (o *OrderNotify) updateOrder() {
if o.code != errorcode.Success {
o.order.Status = common.ORDER_STATUS_FAILED
} else {
o.order.Status = common.ORDER_STATUS_PAYED
}
o.code = services.OrderUpdate(o.order, "status")
return
}
func (o *OrderNotify) checkApp() {
o.app, o.code = services.AppFindOne(entities.IdRequest{Id: o.order.AppId})
if o.code != errorcode.Success {
return
}
if o.app.DeleteTime.IsZero() {
o.code = errorcode.AppDisabled
return
}
if o.app.NotifyUrl == "" {
o.code = errorcode.AppNotifyUrlNotFound
return
}
return
}
func (o *OrderNotify) checkOrder() {
cond := builder.NewCond()
cond = cond.And(builder.Eq{"id": o.OrderId})
o.order, o.code = services.OrderFindOne(&ordersmodel.Orders{}, cond)
if o.code != errorcode.Success {
return
}
if o.order.DeleteTime.IsZero() {
o.code = errorcode.OrderIsDelete
return
}
if o.order.Status != common.ORDER_STATUS_PAYING {
o.code = errorcode.OrderStatusErr
return
}
return
}

View File

@ -1,9 +1,13 @@
package paymentService
import (
"PaymentCenter/app/constants/common"
"PaymentCenter/app/constants/errorcode"
"PaymentCenter/app/services/thirdpay/notify"
"PaymentCenter/app/third/paymentService/payCommon"
"PaymentCenter/config"
"context"
"encoding/json"
"errors"
"fmt"
"github.com/go-pay/gopay"
@ -99,7 +103,43 @@ func ALiCallBack(notifyReq gopay.BodyMap, aliConfig AliPay) error {
return err
}
// todo 拼装数据触发下游回调,数据待定
// 拼装数据触发下游回调,触发下游回调
orderId, _ := strconv.Atoi(notifyReq.Get("out_trade_no"))
payerTotal, _ := strconv.Atoi(notifyReq.Get("buyer_pay_amount"))
// 订单状态
tradeStatus := notifyReq.Get("trade_status")
errCode := 0
msg := ""
switch tradeStatus {
case "TRADE_CLOSED":
errCode = errorcode.ParamError
msg = "未付款交易超时关闭,或支付完成后全额退款。"
case "TRADE_SUCCESS":
errCode = errorcode.Success
msg = "交易支付成功。"
case "TRADE_FINISHED":
errCode = errorcode.Success
msg = "交易结束,不可退款。"
}
if errCode == 0 {
// 等待买家付款,不走后续回调
return errors.New("订单状态异常,无法进行后续回调")
}
res := notify.NewOrderNotifyWithHandle(int64(orderId), errCode, payerTotal, msg)
merchantCallback, _ := json.Marshal(res)
// 记录日志
go func() {
payCallback, _ := json.Marshal(notifyReq)
payParam := ""
saveLog(int64(orderId), common.THIRD_ORDER_TYPE_CALL_BACK, string(payCallback), payParam, string(merchantCallback))
}()
if res.ErrCode != errorcode.Success {
logger.Error(context.Background(), "ALiCallBack 发生错误", fmt.Sprintf("回调时,下游处理订单失败,返回数据为:%s", string(merchantCallback)))
return errors.New("回调时,下游处理订单失败")
}
return nil
}
@ -127,7 +167,7 @@ func ALiOrderQuery(ctx context.Context, aliConfig AliPay, OrderNo string) (PayOr
return PayOrderQueryInfo{}, err
}
// 同步返回验签
ok, err := alipay.VerifySyncSignWithCert(aliConfig.AlipayPublicCert, aliRsp.SignData, aliRsp.Sign)
ok, err := alipay.VerifySyncSignWithCert([]byte(aliConfig.AlipayPublicCert), aliRsp.SignData, aliRsp.Sign)
if err != nil || !ok {
return PayOrderQueryInfo{}, errors.New(fmt.Sprintf("验签失败,失败原因:%s", err.Error()))
}
@ -141,6 +181,7 @@ func ALiOrderQuery(ctx context.Context, aliConfig AliPay, OrderNo string) (PayOr
tradeStateDesc = "交易支付成功"
case "REFUND":
tradeState = "REFUND"
tradeStateDesc = "转入退款"
case "WAIT_BUYER_PAY":
tradeState = "NOTPAY"
tradeStateDesc = "交易创建,等待买家付款"

File diff suppressed because one or more lines are too long

View File

@ -1,9 +1,13 @@
package paymentService
import (
"PaymentCenter/app/constants/common"
"PaymentCenter/app/constants/errorcode"
"PaymentCenter/app/services/thirdpay/notify"
"PaymentCenter/app/third/paymentService/payCommon"
"PaymentCenter/config"
"context"
"encoding/json"
"errors"
"fmt"
"github.com/go-pay/gopay"
@ -135,7 +139,40 @@ func WxPayCallBack(notifyReq *wechat.V3NotifyReq, wxConfig WxPay) error {
if err != nil {
return err
}
// todo 返回触发下游回调的格式
errCode := 0
msg := ""
// 订单状态
switch CallBackInfo.TradeState {
case "SUCCESS":
errCode = errorcode.Success
msg = "支付成功"
case "CLOSED":
errCode = errorcode.ParamError
msg = "已关闭"
case "REVOKED":
errCode = errorcode.ParamError
msg = "已撤销(付款码支付)"
case "PAYERROR":
errCode = errorcode.ParamError
msg = "支付失败(其他原因,如银行返回失败)"
}
// 触发下游回调的格式
orderId, _ := strconv.Atoi(CallBackInfo.OutTradeNo)
res := notify.NewOrderNotifyWithHandle(int64(orderId), errCode, int(CallBackInfo.Amount.PayerTotal), msg)
merchantCallback, _ := json.Marshal(res)
// 记录日志
go func() {
payCallback, _ := json.Marshal(CallBackInfo)
payParam := ""
saveLog(int64(orderId), common.THIRD_ORDER_TYPE_CALL_BACK, string(payCallback), payParam, string(merchantCallback))
}()
if res.ErrCode != errorcode.Success {
logger.Error(context.Background(), "WxPayCallBack 发生错误", fmt.Sprintf("回调时,下游处理订单失败,返回数据为:%s", string(merchantCallback)))
return errors.New("回调时,下游处理订单失败")
}
return nil
}