Skip to content

Commit

Permalink
Merge a76e71d into a8df668
Browse files Browse the repository at this point in the history
  • Loading branch information
hawx authored Jun 5, 2024
2 parents a8df668 + a76e71d commit 12a3504
Showing 25 changed files with 461 additions and 367 deletions.
2 changes: 1 addition & 1 deletion cmd/mlpa/main.go
Original file line number Diff line number Diff line change
@@ -261,7 +261,7 @@ func run(ctx context.Context, logger *slog.Logger) error {
return err
}

payClient := pay.New(logger, httpClient, eventClient, payBaseURL, payApiKey)
payClient := pay.New(logger, httpClient, payBaseURL, payApiKey)

osApiKey, err := secretsClient.Secret(ctx, secrets.OrdnanceSurvey)
if err != nil {
13 changes: 6 additions & 7 deletions cypress/e2e/donor/payment.cy.js
Original file line number Diff line number Diff line change
@@ -28,9 +28,9 @@ describe('Pay for LPA', () => {
cy.contains('.govuk-summary-list__row', 'Reference number').find('.govuk-summary-list__value')
.invoke('text')
.then((uid) => {
cy.visit(`http://localhost:9001/?detail-type=payment-created&detail=${uid}`);
cy.visit(`http://localhost:9001/?detail-type=payment-received&detail=${uid}`);

cy.contains('"amount":1200');
cy.contains('"amount":8200');
cy.contains('"paymentId":"hu20sqlact5260q2nanm0q8u93"');
});
});
@@ -371,12 +371,11 @@ describe('Pay for LPA', () => {
cy.contains('.govuk-summary-list__row', 'Reference number').find('.govuk-summary-list__value')
.invoke('text')
.then((uid) => {
cy.visit(`http://localhost:9001/?detail-type=payment-created&detail=${uid}`);
cy.visit(`http://localhost:9001/?detail-type=payment-received&detail=${uid}`);

cy.contains('"amount":1200');
cy.contains('"amount":4100');
cy.contains('"paymentId":"hu20sqlact5260q2nanm0q8u93"');
});

});

it('can pay remaining amount when denied', () => {
@@ -401,9 +400,9 @@ describe('Pay for LPA', () => {
cy.contains('.govuk-summary-list__row', 'Reference number').find('.govuk-summary-list__value')
.invoke('text')
.then((uid) => {
cy.visit(`http://localhost:9001/?detail-type=payment-created&detail=${uid}`);
cy.visit(`http://localhost:9001/?detail-type=payment-received&detail=${uid}`);

cy.contains('"amount":1200');
cy.contains('"amount":8200');
cy.contains('"paymentId":"hu20sqlact5260q2nanm0q8u93"');
});
});
2 changes: 1 addition & 1 deletion docker/mock-pay/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM badouralix/curl-jq:latest as build

RUN curl https://raw.githubusercontent.com/alphagov/pay-publicapi/master/openapi/publicapi_spec.json --output openapi-spec.json
RUN cat openapi-spec.json | jq '.paths."/v1/payments/{paymentId}".get.responses."200".content."application/json".examples = { "full-fee": { value: { amount: 8200, payment_id: "hu20sqlact5260q2nanm0q8u93", reference: "your-reference" } }, "half-fee": { value: { amount: 4100, payment_id: "hu20sqlact5260q2nanm0q8u93", reference: "your-reference" } }}' > modified-openapi-spec.json
RUN cat openapi-spec.json | jq '.paths."/v1/payments/{paymentId}".get.responses."200".content."application/json".examples = { "full-fee": { value: { state: { status: "success" }, amount: 8200, payment_id: "hu20sqlact5260q2nanm0q8u93", reference: "your-reference" } }, "half-fee": { value: { state: { status: "success" }, amount: 4100, payment_id: "hu20sqlact5260q2nanm0q8u93", reference: "your-reference" } }}' > modified-openapi-spec.json

FROM outofcoffee/imposter:latest

4 changes: 2 additions & 2 deletions internal/event/client.go
Original file line number Diff line number Diff line change
@@ -53,8 +53,8 @@ func (c *Client) SendPaperFormRequested(ctx context.Context, event PaperFormRequ
return c.send(ctx, "paper-form-requested", event)
}

func (c *Client) SendPaymentCreated(ctx context.Context, event PaymentCreated) error {
return c.send(ctx, "payment-created", event)
func (c *Client) SendPaymentReceived(ctx context.Context, event PaymentReceived) error {
return c.send(ctx, "payment-received", event)
}

func (c *Client) send(ctx context.Context, detailType string, detail any) error {
6 changes: 3 additions & 3 deletions internal/event/client_test.go
Original file line number Diff line number Diff line change
@@ -51,10 +51,10 @@ func TestClientSendEvents(t *testing.T) {

return func(client *Client) error { return client.SendPaperFormRequested(ctx, event) }, event
},
"payment-created": func() (func(*Client) error, any) {
event := PaymentCreated{UID: "a", PaymentID: "xyz", Amount: 8200}
"payment-received": func() (func(*Client) error, any) {
event := PaymentReceived{UID: "a", PaymentID: "xyz", Amount: 8200}

return func(client *Client) error { return client.SendPaymentCreated(ctx, event) }, event
return func(client *Client) error { return client.SendPaymentReceived(ctx, event) }, event
},
}

2 changes: 1 addition & 1 deletion internal/event/events.go
Original file line number Diff line number Diff line change
@@ -60,7 +60,7 @@ type PaperFormRequested struct {
AccessCode string `json:"accessCode"`
}

type PaymentCreated struct {
type PaymentReceived struct {
UID string `json:"uid"`
PaymentID string `json:"paymentId"`
Amount int `json:"amount"`
28 changes: 14 additions & 14 deletions internal/page/donor/mock_EventClient_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions internal/page/donor/mock_PayClient_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions internal/page/donor/pay.go
Original file line number Diff line number Diff line change
@@ -2,8 +2,8 @@ package donor

import (
"fmt"
"log/slog"
"net/http"
"strings"

"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page"
@@ -12,6 +12,7 @@ import (
)

func Pay(
logger Logger,
sessionStore SessionStore,
donorStore DonorStore,
payClient PayClient,
@@ -53,11 +54,12 @@ func Pay(
nextUrl := resp.Links["next_url"].Href
// If URL matches expected domain for GOV UK PAY redirect there. If not,
// redirect to the confirmation code and carry on with flow.
if strings.HasPrefix(nextUrl, pay.PaymentPublicServiceUrl) {
if payClient.CanRedirect(nextUrl) {
http.Redirect(w, r, nextUrl, http.StatusFound)
return nil
}

logger.InfoContext(r.Context(), "skipping payment", slog.String("next_url", nextUrl))
return page.Paths.PaymentConfirmation.Redirect(w, r, appData, donor)
}
}
43 changes: 31 additions & 12 deletions internal/page/donor/pay_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package donor

import (
"log/slog"
"net/http"
"net/http/httptest"
"testing"
@@ -15,12 +16,14 @@ import (

func TestPay(t *testing.T) {
testcases := map[string]struct {
nextURL string
redirect string
nextURL string
redirect string
canRedirect bool
}{
"real": {
nextURL: "https://www.payments.service.gov.uk/path-from/response",
redirect: "https://www.payments.service.gov.uk/path-from/response",
nextURL: "https://www.payments.service.gov.uk/path-from/response",
redirect: "https://www.payments.service.gov.uk/path-from/response",
canRedirect: true,
},
"fake": {
nextURL: "/lpa/lpa-id/something-else",
@@ -56,8 +59,17 @@ func TestPay(t *testing.T) {
},
},
}, nil)
payClient.EXPECT().
CanRedirect(tc.nextURL).
Return(tc.canRedirect)

logger := newMockLogger(t)
if !tc.canRedirect {
logger.EXPECT().
InfoContext(r.Context(), "skipping payment", slog.String("next_url", tc.nextURL))
}

err := Pay(sessionStore, nil, payClient, func(int) string { return "123456789012" }, "http://example.org")(testAppData, w, r, &actor.DonorProvidedDetails{LpaID: "lpa-id", LpaUID: "lpa-uid", Donor: actor.Donor{Email: "[email protected]"}, FeeType: pay.FullFee})
err := Pay(logger, sessionStore, nil, payClient, func(int) string { return "123456789012" }, "http://example.org")(testAppData, w, r, &actor.DonorProvidedDetails{LpaID: "lpa-id", LpaUID: "lpa-uid", Donor: actor.Donor{Email: "[email protected]"}, FeeType: pay.FullFee})
resp := w.Result()

assert.Nil(t, err)
@@ -88,7 +100,7 @@ func TestPayWhenPaymentNotRequired(t *testing.T) {
}).
Return(nil)

err := Pay(nil, donorStore, nil, nil, "")(testAppData, w, r, &actor.DonorProvidedDetails{
err := Pay(nil, nil, donorStore, nil, nil, "")(testAppData, w, r, &actor.DonorProvidedDetails{
LpaID: "lpa-id",
FeeType: feeType,
EvidenceDelivery: pay.Upload,
@@ -123,7 +135,7 @@ func TestPayWhenPostingEvidence(t *testing.T) {
}).
Return(nil)

err := Pay(nil, donorStore, nil, nil, "")(testAppData, w, r, &actor.DonorProvidedDetails{
err := Pay(nil, nil, donorStore, nil, nil, "")(testAppData, w, r, &actor.DonorProvidedDetails{
LpaID: "lpa-id",
FeeType: feeType,
EvidenceDelivery: pay.Post,
@@ -151,7 +163,7 @@ func TestPayWhenMoreEvidenceProvided(t *testing.T) {
}).
Return(nil)

err := Pay(nil, donorStore, nil, nil, "")(testAppData, w, r, &actor.DonorProvidedDetails{
err := Pay(nil, nil, donorStore, nil, nil, "")(testAppData, w, r, &actor.DonorProvidedDetails{
LpaID: "lpa-id",
FeeType: pay.HalfFee,
Tasks: actor.DonorTasks{PayForLpa: actor.PaymentTaskMoreEvidenceRequired},
@@ -177,7 +189,7 @@ func TestPayWhenPaymentNotRequiredWhenDonorStorePutError(t *testing.T) {
}).
Return(expectedError)

err := Pay(nil, donorStore, nil, nil, "")(testAppData, w, r, &actor.DonorProvidedDetails{
err := Pay(nil, nil, donorStore, nil, nil, "")(testAppData, w, r, &actor.DonorProvidedDetails{
LpaID: "lpa-id",
FeeType: pay.NoFee,
})
@@ -214,8 +226,15 @@ func TestPayWhenFeeDenied(t *testing.T) {
},
},
}, nil)
payClient.EXPECT().
CanRedirect(page.Paths.PaymentConfirmation.Format("lpa-id")).
Return(false)

logger := newMockLogger(t)
logger.EXPECT().
InfoContext(r.Context(), mock.Anything, mock.Anything)

err := Pay(sessionStore, nil, payClient, func(int) string { return "123456789012" }, "http://example.org")(testAppData, w, r, &actor.DonorProvidedDetails{
err := Pay(logger, sessionStore, nil, payClient, func(int) string { return "123456789012" }, "http://example.org")(testAppData, w, r, &actor.DonorProvidedDetails{
LpaID: "lpa-id",
LpaUID: "lpa-uid",
Donor: actor.Donor{Email: "[email protected]"},
@@ -239,7 +258,7 @@ func TestPayWhenCreatePaymentErrors(t *testing.T) {
CreatePayment(mock.Anything, mock.Anything, mock.Anything).
Return(nil, expectedError)

err := Pay(nil, nil, payClient, func(int) string { return "123456789012" }, "")(testAppData, w, r, &actor.DonorProvidedDetails{})
err := Pay(nil, nil, nil, payClient, func(int) string { return "123456789012" }, "")(testAppData, w, r, &actor.DonorProvidedDetails{})

assert.ErrorIs(t, err, expectedError)
}
@@ -265,7 +284,7 @@ func TestPayWhenSessionErrors(t *testing.T) {
},
}, nil)

err := Pay(sessionStore, nil, payClient, func(int) string { return "123456789012" }, "")(testAppData, w, r, &actor.DonorProvidedDetails{})
err := Pay(nil, sessionStore, nil, payClient, func(int) string { return "123456789012" }, "")(testAppData, w, r, &actor.DonorProvidedDetails{})

assert.Equal(t, expectedError, err)
}
Loading

0 comments on commit 12a3504

Please sign in to comment.