Repository navigation
fix(payment): harden EasyPay callbacks and refund parsing #1989
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
18848298290-del
wants to merge
3
commits into
TokenFlux:main
from
18848298290-del:fix/easypay-v1-callback-refund
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| package httpapi | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/TokenFlux/TokenRouter/internal/payment" | ||
| ) | ||
|
|
||
| var errWebhookOrderNotFound = errors.New("webhook order not found") | ||
|
|
||
| // webhookOrderStore supplies only the provider fallback and notification-order | ||
| // reads reached by these handler tests. Other promoted store operations would | ||
| // panic if callback handling unexpectedly continued into fulfillment. | ||
| type webhookOrderStore struct { | ||
| payment.BindingStore | ||
| payment.FulfillmentStore | ||
| order *payment.Order | ||
| err error | ||
| } | ||
|
|
||
| // webhookConfirmer verifies one fixed success notification and records whether | ||
| // handler ordering reached upstream confirmation. | ||
| type webhookConfirmer struct { | ||
| payment.Provider | ||
| notification *payment.PaymentNotification | ||
| confirmErr error | ||
| confirmCalls int | ||
| } | ||
|
|
||
| func TestEasyPayNotifyChecksLocalOrderBeforeConfirmation(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| order *payment.Order | ||
| orderErr error | ||
| confirmErr error | ||
| wantCode int | ||
| wantBody string | ||
| wantConfirms int | ||
| }{ | ||
| { | ||
| name: "missing order is acknowledged", | ||
| orderErr: errWebhookOrderNotFound, | ||
| confirmErr: errors.New("confirmation must not run"), | ||
| wantCode: http.StatusOK, | ||
| wantBody: "success", | ||
| wantConfirms: 0, | ||
| }, | ||
| { | ||
| name: "database failure remains retryable", | ||
| orderErr: errors.New("database unavailable"), | ||
| confirmErr: errors.New("confirmation must not run"), | ||
| wantCode: http.StatusInternalServerError, | ||
| wantBody: "order lookup failed", | ||
| wantConfirms: 0, | ||
| }, | ||
| { | ||
| name: "existing order still requires confirmation", | ||
| order: &payment.Order{ID: 42, OutTradeNo: "order-42"}, | ||
| confirmErr: errors.New("upstream unavailable"), | ||
| wantCode: http.StatusBadGateway, | ||
| wantBody: "confirm failed", | ||
| wantConfirms: 1, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| store := &webhookOrderStore{order: tt.order, err: tt.orderErr} | ||
| provider := &webhookConfirmer{ | ||
| notification: &payment.PaymentNotification{ | ||
| OrderID: "order-42", | ||
| Status: payment.NotificationStatusSuccess, | ||
| }, | ||
| confirmErr: tt.confirmErr, | ||
| } | ||
| registry := payment.NewRegistry() | ||
| registry.Register(provider) | ||
| bindings := payment.NewProviderBindings(store, registry, nil, payment.BindingRuntime{}, true) | ||
| fulfillment := payment.NewFulfillment(store, bindings, registry, nil, nil, payment.FulfillmentRuntime{}) | ||
| runtime := &payment.Runtime{ | ||
| OrderLifecycle: payment.NewOrderLifecycle(fulfillment, nil, nil), | ||
| ProviderBindings: bindings, | ||
| } | ||
|
|
||
| recorder := httptest.NewRecorder() | ||
| ctx, _ := gin.CreateTestContext(recorder) | ||
| ctx.Request = httptest.NewRequest(http.MethodGet, "/payment/webhook/easypay", nil) | ||
|
|
||
| NewPaymentWebhookHandler(runtime).EasyPayNotify(ctx) | ||
|
|
||
| require.Equal(t, tt.wantCode, recorder.Code) | ||
| require.Equal(t, tt.wantBody, recorder.Body.String()) | ||
| require.Equal(t, tt.wantConfirms, provider.confirmCalls) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func (s *webhookOrderStore) OrderByTradeNumber(context.Context, string) (*payment.Order, error) { | ||
| return s.order, s.err | ||
| } | ||
|
|
||
| func (*webhookOrderStore) CountEnabledInstances(context.Context, string) (int, error) { | ||
| return 0, nil | ||
| } | ||
|
|
||
| func (*webhookOrderStore) IsNotFound(err error) bool { | ||
| return errors.Is(err, errWebhookOrderNotFound) | ||
| } | ||
|
|
||
| func (*webhookConfirmer) Name() string { return "webhook confirmer" } | ||
|
|
||
| func (*webhookConfirmer) ProviderKey() string { return payment.TypeEasyPay } | ||
|
|
||
| func (*webhookConfirmer) SupportedTypes() []payment.PaymentType { | ||
| return []payment.PaymentType{payment.TypeEasyPay} | ||
| } | ||
|
|
||
| func (p *webhookConfirmer) VerifyNotification(context.Context, string, map[string]string) (*payment.PaymentNotification, error) { | ||
| if p.notification == nil { | ||
| return nil, fmt.Errorf("test notification missing") | ||
| } | ||
| copy := *p.notification | ||
| return ©, nil | ||
| } | ||
|
|
||
| func (p *webhookConfirmer) ConfirmNotification(context.Context, *payment.PaymentNotification) error { | ||
| p.confirmCalls++ | ||
| return p.confirmErr | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P2] Handle a missing local order before requiring upstream confirmation
This confirmation runs before
HandlePaymentNotificationperforms the local order lookup. If an accepted callback names a missing local order and the upstream lookup fails, the handler now returns502 confirm failedbefore reaching the existingErrOrderNotFoundacknowledgement below. This can keep obsolete callbacks retrying even though there is no local order to fulfill.A handler regression test with one enabled merchant, an accepted success notification, a classified missing local order, and a failing confirmer returns 502 on this head; the base returns
200 success.After signature verification, resolve the local order and preserve the missing-order acknowledgement before querying upstream. Genuine database failures should remain retryable, and existing orders that need payment confirmation should retain the upstream check.