Skip to content

Commit

Permalink
Merge 7316774 into 8bce032
Browse files Browse the repository at this point in the history
  • Loading branch information
hawx authored May 31, 2024
2 parents 8bce032 + 7316774 commit 90966af
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 15 deletions.
6 changes: 4 additions & 2 deletions internal/page/donor/pay.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -12,6 +12,7 @@ import (
)

func Pay(
logger Logger,
sessionStore SessionStore,
donorStore DonorStore,
payClient PayClient,
Expand Down Expand Up @@ -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 pay.IsPaymentURL(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)
}
}
29 changes: 21 additions & 8 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"
Expand All @@ -17,6 +18,7 @@ func TestPay(t *testing.T) {
testcases := map[string]struct {
nextURL string
redirect string
willLog bool
}{
"real": {
nextURL: "https://www.payments.service.gov.uk/path-from/response",
Expand All @@ -25,6 +27,7 @@ func TestPay(t *testing.T) {
"fake": {
nextURL: "/lpa/lpa-id/something-else",
redirect: page.Paths.PaymentConfirmation.Format("lpa-id"),
willLog: true,
},
}

Expand Down Expand Up @@ -57,7 +60,13 @@ func TestPay(t *testing.T) {
},
}, nil)

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})
logger := newMockLogger(t)
if tc.willLog {
logger.EXPECT().
InfoContext(r.Context(), "skipping payment", slog.String("next_url", tc.nextURL))
}

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)
Expand Down Expand Up @@ -88,7 +97,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,
Expand Down Expand Up @@ -123,7 +132,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,
Expand Down Expand Up @@ -151,7 +160,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},
Expand All @@ -177,7 +186,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,
})
Expand Down Expand Up @@ -215,7 +224,11 @@ func TestPayWhenFeeDenied(t *testing.T) {
},
}, nil)

err := Pay(sessionStore, nil, payClient, func(int) string { return "123456789012" }, "http://example.org")(testAppData, w, r, &actor.DonorProvidedDetails{
logger := newMockLogger(t)
logger.EXPECT().
InfoContext(r.Context(), mock.Anything, mock.Anything)

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]"},
Expand All @@ -239,7 +252,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)
}
Expand All @@ -265,7 +278,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)
}
2 changes: 1 addition & 1 deletion internal/page/donor/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func Register(
progressTracker ProgressTracker,
lpaStoreResolvingService LpaStoreResolvingService,
) {
payer := Pay(sessionStore, donorStore, payClient, random.String, appPublicURL)
payer := Pay(logger, sessionStore, donorStore, payClient, random.String, appPublicURL)

handleRoot := makeHandle(rootMux, sessionStore, errorHandler)

Expand Down
13 changes: 9 additions & 4 deletions internal/pay/data.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package pay

import "time"

const (
PaymentPublicServiceUrl = "https://www.payments.service.gov.uk"
import (
"regexp"
"time"
)

var paymentsURLRe = regexp.MustCompile(`https://[a-z]+\.payments\.service\.gov\.uk/.+`)

func IsPaymentURL(s string) bool {
return paymentsURLRe.MatchString(s)
}

type CreatePaymentBody struct {
Amount int `json:"amount"`
Reference string `json:"reference"`
Expand Down
15 changes: 15 additions & 0 deletions internal/pay/data_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package pay

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestIsPaymentURL(t *testing.T) {
assert.True(t, IsPaymentURL("https://www.payments.service.gov.uk/whatever?hey"))
assert.True(t, IsPaymentURL("https://card.payments.service.gov.uk/whatever?hey"))

assert.False(t, IsPaymentURL("https://card.payments.service.gov.co/whatever?hey"))
assert.False(t, IsPaymentURL("http://card.payments.service.gov.uk/whatever?hey"))
}

0 comments on commit 90966af

Please sign in to comment.