Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
admpub committed Jul 2, 2023
1 parent 3b7fd4d commit 035700d
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 101 deletions.
10 changes: 5 additions & 5 deletions base.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/webx-top/payment/config"
)

var _ Hook = New()
var _ Driver = New()

func New() *Base {
return &Base{}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
74 changes: 33 additions & 41 deletions driver/alipay/alipay.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func init() {
payment.Register(Name, `支付宝`, New)
}

func New() payment.Hook {
func New() payment.Driver {
return &Alipay{}
}

Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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))
Expand All @@ -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
Expand Down Expand Up @@ -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`:
Expand All @@ -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
}
8 changes: 3 additions & 5 deletions driver/alipay/helpers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package alipay

import (
"fmt"
"net/url"

alipay "github.com/admpub/alipay/v3"
Expand All @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions driver/epusdt/epusdt.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func init() {
payment.Register(Name, `USDT`, New)
}

func New() payment.Hook {
func New() payment.Driver {
return &EPUSDT{}
}

Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions driver/mugglepay/mugglepay.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func init() {
payment.Register(Name, `麻瓜宝`, New)
}

func New() payment.Hook {
func New() payment.Driver {
return &Mugglepay{}
}

Expand All @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions driver/payjs/payjs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func init() {
payment.Register(Name, `PayJS支付`, New)
}

func New() payment.Hook {
func New() payment.Driver {
return &PayJS{}
}

Expand All @@ -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
}
Expand Down
34 changes: 17 additions & 17 deletions driver/paypal/paypal.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func init() {
payment.Register(Name, `贝宝`, New)
}

func New() payment.Hook {
func New() payment.Driver {
return &Paypal{}
}

Expand All @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand All @@ -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{
Expand Down
Loading

0 comments on commit 035700d

Please sign in to comment.