diff --git a/base.go b/base.go index 4f9499e..0325952 100644 --- a/base.go +++ b/base.go @@ -5,7 +5,7 @@ import ( "github.com/webx-top/payment/config" ) -var _ Hook = New() +var _ Driver = New() func New() *Base { return &Base{} @@ -20,12 +20,12 @@ func (a *Base) IsSupported(s config.Support) bool { return false } -func (a *Base) SetNotifyCallback(callback func(echo.Context) error) Hook { +func (a *Base) SetNotifyCallback(callback func(echo.Context) error) Driver { a.NotifyCallback = callback return a } -func (a *Base) SetAccount(account *config.Account) Hook { +func (a *Base) SetAccount(account *config.Account) Driver { a.Account = account return a } @@ -35,7 +35,7 @@ func (a *Base) Pay(ctx echo.Context, cfg *config.Pay) (*config.PayResponse, erro } // PayNotify 付款回调处理 -//! *务必在内部验证签名* +// ! *务必在内部验证签名* func (a *Base) PayNotify(ctx echo.Context) error { return config.ErrUnsupported } @@ -49,7 +49,7 @@ func (a *Base) Refund(ctx echo.Context, cfg *config.Refund) (*config.Result, err } // RefundNotify 退款回调处理 -//! *务必在内部验证签名* +// ! *务必在内部验证签名* func (a *Base) RefundNotify(ctx echo.Context) error { return config.ErrUnsupported } diff --git a/driver/alipay/alipay.go b/driver/alipay/alipay.go index cb12ba5..5b9c462 100644 --- a/driver/alipay/alipay.go +++ b/driver/alipay/alipay.go @@ -29,7 +29,7 @@ func init() { payment.Register(Name, `支付宝`, New) } -func New() payment.Hook { +func New() payment.Driver { return &Alipay{} } @@ -43,12 +43,12 @@ func (a *Alipay) IsSupported(s config.Support) bool { return supports.IsSupported(s) } -func (a *Alipay) SetNotifyCallback(callback func(echo.Context) error) payment.Hook { +func (a *Alipay) SetNotifyCallback(callback func(echo.Context) error) payment.Driver { a.notifyCallback = callback return a } -func (a *Alipay) SetAccount(account *config.Account) payment.Hook { +func (a *Alipay) SetAccount(account *config.Account) payment.Driver { a.account = account return a } @@ -108,12 +108,12 @@ func (a *Alipay) Pay(ctx echo.Context, cfg *config.Pay) (*config.PayResponse, er return result, err } if !results.IsSuccess() { - if len(results.Content.SubMsg) > 0 { - results.Content.Msg += `: ` + results.Content.SubMsg + if len(results.SubMsg) > 0 { + results.Msg += `: ` + results.SubMsg } - return nil, errors.New(results.Content.Msg) + return nil, errors.New(results.Msg) } - result.QRCodeContent = results.Content.QRCode + result.QRCodeContent = results.QRCode result.Raw = results return result, err } @@ -204,20 +204,20 @@ func (a *Alipay) PayQuery(ctx echo.Context, cfg *config.Query) (*config.Result, return nil, err } if !resp.IsSuccess() { - if len(resp.Content.SubMsg) > 0 { - resp.Content.Msg += `: ` + resp.Content.SubMsg + if len(resp.SubMsg) > 0 { + resp.Msg += `: ` + resp.SubMsg } - return nil, errors.New(resp.Content.Msg) + return nil, errors.New(resp.Msg) } return &config.Result{ Operation: config.OperationPayment, - Status: string(resp.Content.TradeStatus), - TradeNo: resp.Content.TradeNo, - OutTradeNo: resp.Content.OutTradeNo, - Currency: resp.Content.PayCurrency, - TotalAmount: param.AsFloat64(resp.Content.TotalAmount), - Reason: resp.Content.SubMsg, + Status: string(resp.TradeStatus), + TradeNo: resp.TradeNo, + OutTradeNo: resp.OutTradeNo, + Currency: resp.PayCurrency, + TotalAmount: param.AsFloat64(resp.TotalAmount), + Reason: resp.SubMsg, Raw: resp, }, err } @@ -229,14 +229,6 @@ func (a *Alipay) Refund(ctx echo.Context, cfg *config.Refund) (*config.Result, e RefundAmount: MoneyFeeToString(cfg.RefundAmount), RefundReason: cfg.RefundReason, OutRequestNo: cfg.OutRefundNo, - OperatorId: ``, // 可选 商户的操作员编号 - StoreId: ``, // 可选 商户的门店编号 - TerminalId: ``, // 可选 商户的终端编号 - } - if cfg.Options != nil { - refundConfig.OperatorId = cfg.Options.String(`operatorId`) - refundConfig.StoreId = cfg.Options.String(`storeId`) - refundConfig.TerminalId = cfg.Options.String(`terminalId`) } if len(refundConfig.OutRequestNo) == 0 { refundConfig.OutRequestNo = fmt.Sprintf("%d%d", time.Now().Local().Unix(), rand.Intn(9999)) @@ -246,20 +238,20 @@ func (a *Alipay) Refund(ctx echo.Context, cfg *config.Refund) (*config.Result, e return nil, err } if !resp.IsSuccess() { - if len(resp.Content.SubMsg) > 0 { - resp.Content.Msg += `: ` + resp.Content.SubMsg + if len(resp.SubMsg) > 0 { + resp.Msg += `: ` + resp.SubMsg } - return nil, errors.New(resp.Content.Msg) + return nil, errors.New(resp.Msg) } return &config.Result{ Operation: config.OperationRefund, Status: config.TradeStatusSuccess, - TradeNo: resp.Content.TradeNo, - OutTradeNo: resp.Content.OutTradeNo, + TradeNo: resp.TradeNo, + OutTradeNo: resp.OutTradeNo, Currency: ``, TotalAmount: 0, - Reason: resp.Content.SubMsg, - RefundFee: param.AsFloat64(resp.Content.RefundFee), + Reason: resp.SubMsg, + RefundFee: param.AsFloat64(resp.RefundFee), OutRefundNo: cfg.OutRefundNo, Raw: resp, }, err @@ -314,13 +306,13 @@ func (a *Alipay) RefundQuery(ctx echo.Context, cfg *config.Query) (*config.Resul return nil, err } if !resp.IsSuccess() { - if len(resp.Content.SubMsg) > 0 { - resp.Content.Msg += `: ` + resp.Content.SubMsg + if len(resp.SubMsg) > 0 { + resp.Msg += `: ` + resp.SubMsg } - return nil, errors.New(resp.Content.Msg) + return nil, errors.New(resp.Msg) } var status string - switch resp.Content.RefundStatus { + switch resp.RefundStatus { case `REFUND_SUCCESS`: status = config.TradeStatusSuccess case `REFUND_FAIL`: @@ -333,13 +325,13 @@ func (a *Alipay) RefundQuery(ctx echo.Context, cfg *config.Query) (*config.Resul return &config.Result{ Operation: config.OperationRefund, Status: status, - TradeNo: resp.Content.TradeNo, - OutTradeNo: resp.Content.OutTradeNo, + TradeNo: resp.TradeNo, + OutTradeNo: resp.OutTradeNo, Currency: ``, - TotalAmount: param.AsFloat64(resp.Content.TotalAmount), - Reason: resp.Content.SubMsg, - RefundFee: param.AsFloat64(resp.Content.RefundAmount), - OutRefundNo: resp.Content.OutRequestNo, + TotalAmount: param.AsFloat64(resp.TotalAmount), + Reason: resp.SubMsg, + RefundFee: param.AsFloat64(resp.RefundAmount), + OutRefundNo: resp.OutRequestNo, Raw: resp, }, err } diff --git a/driver/alipay/helpers.go b/driver/alipay/helpers.go index f5a419e..beedea8 100644 --- a/driver/alipay/helpers.go +++ b/driver/alipay/helpers.go @@ -1,6 +1,7 @@ package alipay import ( + "fmt" "net/url" alipay "github.com/admpub/alipay/v3" @@ -15,12 +16,9 @@ func (a *Alipay) VerifySign(ctx echo.Context) error { } func (a *Alipay) verifySign(req url.Values) error { - ok, err := a.Client().VerifySign(req) + err := a.Client().VerifySign(req) if err != nil { - return err - } - if !ok { - return config.ErrSignature + return fmt.Errorf(`%w: %v`, config.ErrSignature, err) } return nil } diff --git a/driver/epusdt/epusdt.go b/driver/epusdt/epusdt.go index a5b53ed..86e0960 100644 --- a/driver/epusdt/epusdt.go +++ b/driver/epusdt/epusdt.go @@ -31,7 +31,7 @@ func init() { payment.Register(Name, `USDT`, New) } -func New() payment.Hook { +func New() payment.Driver { return &EPUSDT{} } @@ -45,12 +45,12 @@ func (a *EPUSDT) IsSupported(s config.Support) bool { return supports.IsSupported(s) } -func (a *EPUSDT) SetNotifyCallback(callback func(echo.Context) error) payment.Hook { +func (a *EPUSDT) SetNotifyCallback(callback func(echo.Context) error) payment.Driver { a.notifyCallback = callback return a } -func (a *EPUSDT) SetAccount(account *config.Account) payment.Hook { +func (a *EPUSDT) SetAccount(account *config.Account) payment.Driver { a.account = account a.apiURL = strings.TrimSuffix(account.Options.Extra.String(`apiURL`), `/`) return a diff --git a/driver/mugglepay/mugglepay.go b/driver/mugglepay/mugglepay.go index 55289d0..46d5fc4 100644 --- a/driver/mugglepay/mugglepay.go +++ b/driver/mugglepay/mugglepay.go @@ -25,7 +25,7 @@ func init() { payment.Register(Name, `麻瓜宝`, New) } -func New() payment.Hook { +func New() payment.Driver { return &Mugglepay{} } @@ -39,12 +39,12 @@ func (a *Mugglepay) IsSupported(s config.Support) bool { return supports.IsSupported(s) } -func (a *Mugglepay) SetNotifyCallback(callback func(echo.Context) error) payment.Hook { +func (a *Mugglepay) SetNotifyCallback(callback func(echo.Context) error) payment.Driver { a.notifyCallback = callback return a } -func (a *Mugglepay) SetAccount(account *config.Account) payment.Hook { +func (a *Mugglepay) SetAccount(account *config.Account) payment.Driver { a.account = account return a } diff --git a/driver/payjs/payjs.go b/driver/payjs/payjs.go index a6f0526..2a2b803 100644 --- a/driver/payjs/payjs.go +++ b/driver/payjs/payjs.go @@ -23,7 +23,7 @@ func init() { payment.Register(Name, `PayJS支付`, New) } -func New() payment.Hook { +func New() payment.Driver { return &PayJS{} } @@ -37,12 +37,12 @@ func (a *PayJS) IsSupported(s config.Support) bool { return supports.IsSupported(s) } -func (a *PayJS) SetNotifyCallback(callback func(echo.Context) error) payment.Hook { +func (a *PayJS) SetNotifyCallback(callback func(echo.Context) error) payment.Driver { a.notifyCallback = callback return a } -func (a *PayJS) SetAccount(account *config.Account) payment.Hook { +func (a *PayJS) SetAccount(account *config.Account) payment.Driver { a.account = account return a } diff --git a/driver/paypal/paypal.go b/driver/paypal/paypal.go index 3b12329..f6d4d8d 100644 --- a/driver/paypal/paypal.go +++ b/driver/paypal/paypal.go @@ -22,7 +22,7 @@ func init() { payment.Register(Name, `贝宝`, New) } -func New() payment.Hook { +func New() payment.Driver { return &Paypal{} } @@ -36,12 +36,12 @@ func (a *Paypal) IsSupported(s config.Support) bool { return supports.IsSupported(s) } -func (a *Paypal) SetNotifyCallback(callback func(echo.Context) error) payment.Hook { +func (a *Paypal) SetNotifyCallback(callback func(echo.Context) error) payment.Driver { a.notifyCallback = callback return a } -func (a *Paypal) SetAccount(account *config.Account) payment.Hook { +func (a *Paypal) SetAccount(account *config.Account) payment.Driver { a.account = account return a } @@ -60,9 +60,9 @@ func (a *Paypal) Client() *paypal.Client { func (a *Paypal) Pay(ctx echo.Context, cfg *config.Pay) (*config.PayResponse, error) { var p = &paypal.Payment{} - p.Intent = paypal.K_PAYMENT_INTENT_SALE + p.Intent = paypal.PaymentIntentSale p.Payer = &paypal.Payer{} - p.Payer.PaymentMethod = paypal.K_PAYMENT_METHOD_PAYPAL + p.Payer.PaymentMethod = paypal.PaymentMethodPayPal p.RedirectURLs = &paypal.RedirectURLs{} p.RedirectURLs.CancelURL = cfg.CancelURL p.RedirectURLs.ReturnURL = cfg.ReturnURL @@ -110,7 +110,7 @@ func (a *Paypal) PayQuery(ctx echo.Context, cfg *config.Query) (*config.Result, transactionFeeValue string // 交易手续费金额 transactionFeeCurrency string // 交易手续费币种 ) - if payment.State == paypal.K_PAYMENT_STATE_APPROVED { + if payment.State == paypal.PaymentStateApproved { paid = true } for _, transaction := range payment.Transactions { @@ -122,7 +122,7 @@ func (a *Paypal) PayQuery(ctx echo.Context, cfg *config.Query) (*config.Result, currency = resource.Sale.Amount.Currency transactionFeeValue = resource.Sale.TransactionFee.Value transactionFeeCurrency = resource.Sale.TransactionFee.Currency - if resource.Sale.State != paypal.K_SALE_STATE_COMPLETED { + if resource.Sale.State != paypal.SaleStateCompleted { paid = false break } @@ -158,7 +158,7 @@ func (a *Paypal) PayNotify(ctx echo.Context) error { return nil } switch event.EventType { - case paypal.K_EVENT_TYPE_PAYMENT_SALE_COMPLETED: + case paypal.EventTypePaymentSaleCompleted: sale := event.Sale() if a.notifyCallback != nil { var reason, sep string @@ -186,7 +186,7 @@ func (a *Paypal) PayNotify(ctx echo.Context) error { return err } } - case paypal.K_EVENT_TYPE_PAYMENT_SALE_REFUNDED: + case paypal.EventTypePaymentSaleRefunded: refund := event.Refund() if len(refund.Description) > 0 { refund.Reason += "; " + refund.Description @@ -235,13 +235,13 @@ func (a *Paypal) Refund(ctx echo.Context, cfg *config.Refund) (*config.Result, e } var status string switch refund.State { - case paypal.K_REFUND_STATE_COMPLETED: + case paypal.RefundStateCompleted: status = config.TradeStatusSuccess - case paypal.K_REFUND_STATE_CANCELLED: + case paypal.RefundStateCancelled: status = config.TradeStatusClosed - case paypal.K_REFUND_STATE_FAILED: + case paypal.RefundStateFailed: status = config.TradeStatusException - case paypal.K_REFUND_STATE_PENDING: + case paypal.RefundStatePending: status = config.TradeStatusProcessing } return &config.Result{ @@ -267,13 +267,13 @@ func (a *Paypal) RefundQuery(ctx echo.Context, cfg *config.Query) (*config.Resul } var status string switch refund.State { - case paypal.K_REFUND_STATE_COMPLETED: + case paypal.RefundStateCompleted: status = config.TradeStatusSuccess - case paypal.K_REFUND_STATE_CANCELLED: + case paypal.RefundStateCancelled: status = config.TradeStatusClosed - case paypal.K_REFUND_STATE_FAILED: + case paypal.RefundStateFailed: status = config.TradeStatusException - case paypal.K_REFUND_STATE_PENDING: + case paypal.RefundStatePending: status = config.TradeStatusProcessing } return &config.Result{ diff --git a/driver/wechat/wechat.go b/driver/wechat/wechat.go index 65b7b8b..30d6814 100644 --- a/driver/wechat/wechat.go +++ b/driver/wechat/wechat.go @@ -30,7 +30,7 @@ func init() { payment.Register(Name, `微信支付`, New) } -func New() payment.Hook { +func New() payment.Driver { return &Wechat{} } @@ -44,12 +44,12 @@ func (a *Wechat) IsSupported(s config.Support) bool { return supports.IsSupported(s) } -func (a *Wechat) SetNotifyCallback(callback func(echo.Context) error) payment.Hook { +func (a *Wechat) SetNotifyCallback(callback func(echo.Context) error) payment.Driver { a.notifyCallback = callback return a } -func (a *Wechat) SetAccount(account *config.Account) payment.Hook { +func (a *Wechat) SetAccount(account *config.Account) payment.Driver { a.account = account return a } diff --git a/driver/xunhupay/xunhupay.go b/driver/xunhupay/xunhupay.go index 0269bfd..d8aa450 100644 --- a/driver/xunhupay/xunhupay.go +++ b/driver/xunhupay/xunhupay.go @@ -53,7 +53,7 @@ func SetDefaults(a *config.Account) { } } -func New() payment.Hook { +func New() payment.Driver { return &XunHuPay{} } @@ -66,12 +66,12 @@ func (a *XunHuPay) IsSupported(s config.Support) bool { return supports.IsSupported(s) } -func (a *XunHuPay) SetNotifyCallback(callback func(echo.Context) error) payment.Hook { +func (a *XunHuPay) SetNotifyCallback(callback func(echo.Context) error) payment.Driver { a.notifyCallback = callback return a } -func (a *XunHuPay) SetAccount(account *config.Account) payment.Hook { +func (a *XunHuPay) SetAccount(account *config.Account) payment.Driver { a.account = account return a } diff --git a/go.mod b/go.mod index 5f5249d..a959083 100644 --- a/go.mod +++ b/go.mod @@ -3,31 +3,33 @@ module github.com/webx-top/payment go 1.16 require ( - github.com/admpub/alipay/v3 v3.0.3 + github.com/admpub/alipay/v3 v3.1.0 github.com/admpub/decimal v1.3.1 github.com/admpub/events v1.3.5 // indirect github.com/admpub/fasthttp v0.0.5 // indirect - github.com/admpub/go-isatty v0.0.10 // indirect + github.com/admpub/go-isatty v0.0.11 // indirect github.com/admpub/go-reuseport v0.0.4 // indirect github.com/admpub/humanize v0.0.0-20190501023926-5f826e92c8ca // indirect - github.com/admpub/log v1.3.3 + github.com/admpub/log v1.3.4 github.com/admpub/mugglepay v0.0.8 github.com/admpub/realip v0.0.0-20210421084339-374cf5df122d // indirect github.com/admpub/resty/v2 v2.7.1 github.com/admpub/timeago v1.2.1 // indirect github.com/goccy/go-json v0.10.2 // indirect - github.com/mattn/go-isatty v0.0.18 // indirect + github.com/hashicorp/go-version v1.6.0 // indirect + github.com/mattn/go-isatty v0.0.19 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/objcoding/wxpay v1.0.6 github.com/onsi/gomega v1.19.0 // indirect github.com/qingwg/payjs v0.0.0-20190928033402-c53dbe16b371 - github.com/smartwalle/paypal v1.0.1 + github.com/smartwalle/paypal v1.0.2 github.com/webx-top/captcha v0.1.0 // indirect - github.com/webx-top/codec v0.2.1 - github.com/webx-top/com v0.9.1 - github.com/webx-top/echo v2.33.3+incompatible + github.com/webx-top/codec v0.3.0 + github.com/webx-top/com v0.9.5 + github.com/webx-top/echo v2.36.2+incompatible github.com/webx-top/poolx v0.0.0-20210912044716-5cfa2d58e380 // indirect github.com/webx-top/restyclient v0.0.4 github.com/webx-top/validation v0.0.3 // indirect - golang.org/x/crypto v0.7.0 // indirect + golang.org/x/net v0.11.0 // indirect + golang.org/x/sync v0.3.0 // indirect ) diff --git a/payment.go b/payment.go index 410d1dc..0072ca5 100644 --- a/payment.go +++ b/payment.go @@ -5,11 +5,11 @@ import ( "github.com/webx-top/payment/config" ) -// Hook 付款驱动接口 -type Hook interface { +// Driver 付款驱动接口 +type Driver interface { IsSupported(config.Support) bool - SetNotifyCallback(callback func(echo.Context) error) Hook - SetAccount(*config.Account) Hook + SetNotifyCallback(callback func(echo.Context) error) Driver + SetAccount(*config.Account) Driver Pay(echo.Context, *config.Pay) (*config.PayResponse, error) PayQuery(echo.Context, *config.Query) (*config.Result, error) PayNotify(echo.Context) error //! *务必在内部验证签名* @@ -21,7 +21,7 @@ type Hook interface { var ( platforms = map[string]string{} //platform=>name: alipay=>支付宝 - payments = map[string]func() Hook{} + payments = map[string]func() Driver{} ) func Platforms() map[string]string { @@ -33,7 +33,7 @@ func Name(platform string) string { return name } -func Register(platform string, name string, hook func() Hook, setDefaults ...func(*config.Account)) { +func Register(platform string, name string, hook func() Driver, setDefaults ...func(*config.Account)) { payments[platform] = hook platforms[platform] = name if len(setDefaults) > 0 { @@ -47,7 +47,7 @@ func Unregister(platform string) { config.UnregisterAccountSetDefaults(platform) } -func Get(platform string) (hook func() Hook) { - hook, _ = payments[platform] - return hook +func Get(platform string) (driver func() Driver) { + driver, _ = payments[platform] + return driver }