/
payment_confirmation.go
64 lines (52 loc) · 1.82 KB
/
payment_confirmation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package donor
import (
"fmt"
"net/http"
"github.com/gorilla/sessions"
"github.com/ministryofjustice/opg-go-common/template"
"github.com/ministryofjustice/opg-modernising-lpa/app/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/app/internal/page"
"github.com/ministryofjustice/opg-modernising-lpa/app/internal/sesh"
"github.com/ministryofjustice/opg-modernising-lpa/app/internal/validation"
)
type paymentConfirmationData struct {
App page.AppData
Errors validation.List
PaymentReference string
}
func PaymentConfirmation(logger Logger, tmpl template.Template, payClient PayClient, donorStore DonorStore, sessionStore sessions.Store) Handler {
return func(appData page.AppData, w http.ResponseWriter, r *http.Request, lpa *page.Lpa) error {
paymentSession, err := sesh.Payment(sessionStore, r)
if err != nil {
return err
}
paymentId := paymentSession.PaymentID
payment, err := payClient.GetPayment(paymentId)
if err != nil {
logger.Print(fmt.Sprintf("unable to retrieve payment info: %s", err.Error()))
return err
}
lpa.PaymentDetails = page.PaymentDetails{
PaymentReference: payment.Reference,
PaymentId: payment.PaymentId,
Amount: payment.Amount,
}
data := &paymentConfirmationData{
App: appData,
PaymentReference: payment.Reference,
}
if err := sesh.ClearPayment(sessionStore, r, w); err != nil {
logger.Print(fmt.Sprintf("unable to expire cookie in session: %s", err.Error()))
}
if lpa.FeeType.IsFullFee() {
lpa.Tasks.PayForLpa = actor.PaymentTaskCompleted
} else {
lpa.Tasks.PayForLpa = actor.PaymentTaskPending
}
if err := donorStore.Put(r.Context(), lpa); err != nil {
logger.Print(fmt.Sprintf("unable to update lpa in donorStore: %s", err.Error()))
return err
}
return tmpl(w, data)
}
}