fix: harden payment resume and wxpay webhook routing
This commit is contained in:
@@ -3,11 +3,14 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/payment"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -131,3 +134,70 @@ func TestExtractOutTradeNo(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyNotificationWithProvidersReturnsMatchedProvider(t *testing.T) {
|
||||
firstErr := errors.New("wrong provider")
|
||||
providers := []payment.Provider{
|
||||
webhookHandlerProviderStub{
|
||||
key: payment.TypeWxpay,
|
||||
verifyErr: firstErr,
|
||||
},
|
||||
webhookHandlerProviderStub{
|
||||
key: payment.TypeWxpay,
|
||||
notification: &payment.PaymentNotification{
|
||||
OrderID: "sub2_42",
|
||||
TradeNo: "trade-42",
|
||||
Status: payment.NotificationStatusSuccess,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
providerKey, notification, err := verifyNotificationWithProviders(context.Background(), providers, "{}", map[string]string{"wechatpay-signature": "sig"})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, payment.TypeWxpay, providerKey)
|
||||
require.NotNil(t, notification)
|
||||
require.Equal(t, "sub2_42", notification.OrderID)
|
||||
}
|
||||
|
||||
func TestVerifyNotificationWithProvidersFailsWhenAllProvidersReject(t *testing.T) {
|
||||
providers := []payment.Provider{
|
||||
webhookHandlerProviderStub{
|
||||
key: payment.TypeWxpay,
|
||||
verifyErr: errors.New("verify failed a"),
|
||||
},
|
||||
webhookHandlerProviderStub{
|
||||
key: payment.TypeWxpay,
|
||||
verifyErr: errors.New("verify failed b"),
|
||||
},
|
||||
}
|
||||
|
||||
_, _, err := verifyNotificationWithProviders(context.Background(), providers, "{}", nil)
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
type webhookHandlerProviderStub struct {
|
||||
key string
|
||||
notification *payment.PaymentNotification
|
||||
verifyErr error
|
||||
}
|
||||
|
||||
func (p webhookHandlerProviderStub) Name() string { return p.key }
|
||||
func (p webhookHandlerProviderStub) ProviderKey() string { return p.key }
|
||||
func (p webhookHandlerProviderStub) SupportedTypes() []payment.PaymentType {
|
||||
return []payment.PaymentType{payment.PaymentType(p.key)}
|
||||
}
|
||||
func (p webhookHandlerProviderStub) CreatePayment(context.Context, payment.CreatePaymentRequest) (*payment.CreatePaymentResponse, error) {
|
||||
panic("unexpected call")
|
||||
}
|
||||
func (p webhookHandlerProviderStub) QueryOrder(context.Context, string) (*payment.QueryOrderResponse, error) {
|
||||
panic("unexpected call")
|
||||
}
|
||||
func (p webhookHandlerProviderStub) VerifyNotification(context.Context, string, map[string]string) (*payment.PaymentNotification, error) {
|
||||
if p.verifyErr != nil {
|
||||
return nil, p.verifyErr
|
||||
}
|
||||
return p.notification, nil
|
||||
}
|
||||
func (p webhookHandlerProviderStub) Refund(context.Context, payment.RefundRequest) (*payment.RefundResponse, error) {
|
||||
panic("unexpected call")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user