-
Notifications
You must be signed in to change notification settings - Fork 2
/
register.go
540 lines (461 loc) · 24.7 KB
/
register.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
package donor
import (
"context"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/gorilla/sessions"
"github.com/ministryofjustice/opg-go-common/template"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor/actoruid"
"github.com/ministryofjustice/opg-modernising-lpa/internal/date"
"github.com/ministryofjustice/opg-modernising-lpa/internal/event"
"github.com/ministryofjustice/opg-modernising-lpa/internal/identity"
"github.com/ministryofjustice/opg-modernising-lpa/internal/notify"
"github.com/ministryofjustice/opg-modernising-lpa/internal/onelogin"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page"
"github.com/ministryofjustice/opg-modernising-lpa/internal/pay"
"github.com/ministryofjustice/opg-modernising-lpa/internal/place"
"github.com/ministryofjustice/opg-modernising-lpa/internal/random"
"github.com/ministryofjustice/opg-modernising-lpa/internal/sesh"
"github.com/ministryofjustice/opg-modernising-lpa/internal/uid"
)
type Handler func(data page.AppData, w http.ResponseWriter, r *http.Request, donor *actor.DonorProvidedDetails) error
type Template func(io.Writer, interface{}) error
type Logger interface {
Print(v ...interface{})
}
type DonorStore interface {
Get(context.Context) (*actor.DonorProvidedDetails, error)
Latest(context.Context) (*actor.DonorProvidedDetails, error)
Put(context.Context, *actor.DonorProvidedDetails) error
Delete(context.Context) error
}
type GetDonorStore interface {
Get(context.Context) (*actor.DonorProvidedDetails, error)
}
type CertificateProviderStore interface {
GetAny(ctx context.Context) (*actor.CertificateProviderProvidedDetails, error)
}
type AttorneyStore interface {
GetAny(ctx context.Context) ([]*actor.AttorneyProvidedDetails, error)
}
type EvidenceReceivedStore interface {
Get(context.Context) (bool, error)
}
type S3Client interface {
PutObject(context.Context, string, []byte) error
DeleteObject(context.Context, string) error
}
type PayClient interface {
CreatePayment(context.Context, pay.CreatePaymentBody) (pay.CreatePaymentResponse, error)
GetPayment(context.Context, string) (pay.GetPaymentResponse, error)
}
type AddressClient interface {
LookupPostcode(ctx context.Context, postcode string) ([]place.Address, error)
}
type ShareCodeSender interface {
SendCertificateProviderInvite(context.Context, page.AppData, *actor.DonorProvidedDetails) error
SendCertificateProviderPrompt(context.Context, page.AppData, *actor.DonorProvidedDetails) error
}
type OneLoginClient interface {
AuthCodeURL(state, nonce, locale string, identity bool) (string, error)
Exchange(ctx context.Context, code, nonce string) (idToken, accessToken string, err error)
UserInfo(ctx context.Context, accessToken string) (onelogin.UserInfo, error)
ParseIdentityClaim(ctx context.Context, userInfo onelogin.UserInfo) (identity.UserData, error)
}
type NotifyClient interface {
SendActorSMS(ctx context.Context, to, lpaUID string, sms notify.SMS) error
}
type SessionStore interface {
Get(r *http.Request, name string) (*sessions.Session, error)
New(r *http.Request, name string) (*sessions.Session, error)
Save(r *http.Request, w http.ResponseWriter, s *sessions.Session) error
}
type WitnessCodeSender interface {
SendToCertificateProvider(context.Context, *actor.DonorProvidedDetails, page.Localizer) error
SendToIndependentWitness(context.Context, *actor.DonorProvidedDetails, page.Localizer) error
}
type UidClient interface {
CreateCase(context.Context, *uid.CreateCaseRequestBody) (uid.CreateCaseResponse, error)
}
type RequestSigner interface {
Sign(context.Context, *http.Request, string) error
}
type Payer interface {
Pay(page.AppData, http.ResponseWriter, *http.Request, *actor.DonorProvidedDetails) error
}
type Localizer interface {
Format(string, map[string]any) string
T(string) string
Count(messageID string, count int) string
FormatCount(messageID string, count int, data map[string]interface{}) string
ShowTranslationKeys() bool
SetShowTranslationKeys(s bool)
Possessive(s string) string
Concat([]string, string) string
FormatDate(date.TimeOrDate) string
FormatDateTime(time.Time) string
}
type DocumentStore interface {
GetAll(context.Context) (page.Documents, error)
Put(context.Context, page.Document) error
Delete(context.Context, page.Document) error
DeleteInfectedDocuments(context.Context, page.Documents) error
Create(context.Context, *actor.DonorProvidedDetails, string, []byte) (page.Document, error)
Submit(context.Context, *actor.DonorProvidedDetails, page.Documents) error
}
type EventClient interface {
SendReducedFeeRequested(context.Context, event.ReducedFeeRequested) error
}
type DashboardStore interface {
GetAll(ctx context.Context) (donor, attorney, certificateProvider []page.LpaAndActorTasks, err error)
SubExistsForActorType(ctx context.Context, sub string, actorType actor.Type) (bool, error)
}
type LpaStoreClient interface {
SendLpa(context.Context, *actor.DonorProvidedDetails) error
}
type ErrorHandler func(http.ResponseWriter, *http.Request, error)
func Register(
rootMux *http.ServeMux,
logger Logger,
commonTmpls, tmpls template.Templates,
sessionStore SessionStore,
donorStore DonorStore,
oneLoginClient OneLoginClient,
addressClient AddressClient,
appPublicURL string,
payClient PayClient,
shareCodeSender ShareCodeSender,
witnessCodeSender WitnessCodeSender,
errorHandler page.ErrorHandler,
notFoundHandler page.Handler,
certificateProviderStore CertificateProviderStore,
attorneyStore AttorneyStore,
notifyClient NotifyClient,
evidenceReceivedStore EvidenceReceivedStore,
documentStore DocumentStore,
eventClient EventClient,
dashboardStore DashboardStore,
lpaStoreClient LpaStoreClient,
) {
payer := &payHelper{
logger: logger,
sessionStore: sessionStore,
donorStore: donorStore,
payClient: payClient,
randomString: random.String,
appPublicURL: appPublicURL,
}
handleRoot := makeHandle(rootMux, sessionStore, page.None, errorHandler)
handleRoot(page.Paths.Login, page.None,
page.Login(oneLoginClient, sessionStore, random.String, page.Paths.LoginCallback))
handleRoot(page.Paths.LoginCallback, page.None,
page.LoginCallback(oneLoginClient, sessionStore, page.Paths.Dashboard, dashboardStore, actor.TypeDonor))
lpaMux := http.NewServeMux()
rootMux.Handle("/lpa/", page.RouteToPrefix("/lpa/", lpaMux, notFoundHandler))
handleDonor := makeHandle(lpaMux, sessionStore, page.RequireSession, errorHandler)
handleWithDonor := makeLpaHandle(lpaMux, sessionStore, page.RequireSession, errorHandler, donorStore)
handleDonor(page.Paths.Root, page.None, notFoundHandler)
handleWithDonor(page.Paths.DeleteThisLpa, page.None,
DeleteLpa(tmpls.Get("delete_this_lpa.gohtml"), donorStore))
handleWithDonor(page.Paths.WithdrawThisLpa, page.None,
WithdrawLpa(tmpls.Get("withdraw_this_lpa.gohtml"), donorStore, time.Now))
handleWithDonor(page.Paths.MakeANewLPA, page.None,
Guidance(tmpls.Get("make_a_new_lpa.gohtml")))
handleWithDonor(page.Paths.YourDetails, page.None,
YourDetails(tmpls.Get("your_details.gohtml"), donorStore, sessionStore))
handleWithDonor(page.Paths.YourName, page.None,
YourName(tmpls.Get("your_name.gohtml"), donorStore))
handleWithDonor(page.Paths.YourDateOfBirth, page.None,
YourDateOfBirth(tmpls.Get("your_date_of_birth.gohtml"), donorStore))
handleWithDonor(page.Paths.YourAddress, page.None,
YourAddress(logger, tmpls.Get("your_address.gohtml"), addressClient, donorStore))
handleWithDonor(page.Paths.WeHaveUpdatedYourDetails, page.None,
Guidance(tmpls.Get("we_have_updated_your_details.gohtml")))
handleWithDonor(page.Paths.YourPreferredLanguage, page.None,
YourPreferredLanguage(commonTmpls.Get("your_preferred_language.gohtml"), donorStore))
handleWithDonor(page.Paths.LpaType, page.None,
LpaType(tmpls.Get("lpa_type.gohtml"), donorStore))
handleWithDonor(page.Paths.CheckYouCanSign, page.None,
CheckYouCanSign(tmpls.Get("check_you_can_sign.gohtml"), donorStore))
handleWithDonor(page.Paths.NeedHelpSigningConfirmation, page.None,
Guidance(tmpls.Get("need_help_signing_confirmation.gohtml")))
handleWithDonor(page.Paths.TaskList, page.None,
TaskList(tmpls.Get("task_list.gohtml"), evidenceReceivedStore))
handleWithDonor(page.Paths.ChooseAttorneysGuidance, page.None,
Guidance(tmpls.Get("choose_attorneys_guidance.gohtml")))
handleWithDonor(page.Paths.ChooseAttorneys, page.CanGoBack,
ChooseAttorneys(tmpls.Get("choose_attorneys.gohtml"), donorStore, actoruid.New))
handleWithDonor(page.Paths.ChooseAttorneysAddress, page.CanGoBack,
ChooseAttorneysAddress(logger, tmpls.Get("choose_address.gohtml"), addressClient, donorStore))
handleWithDonor(page.Paths.EnterTrustCorporation, page.CanGoBack,
EnterTrustCorporation(tmpls.Get("enter_trust_corporation.gohtml"), donorStore))
handleWithDonor(page.Paths.EnterTrustCorporationAddress, page.CanGoBack,
EnterTrustCorporationAddress(logger, tmpls.Get("choose_address.gohtml"), addressClient, donorStore))
handleWithDonor(page.Paths.ChooseAttorneysSummary, page.CanGoBack,
ChooseAttorneysSummary(tmpls.Get("choose_attorneys_summary.gohtml")))
handleWithDonor(page.Paths.RemoveAttorney, page.CanGoBack,
RemoveAttorney(logger, tmpls.Get("remove_attorney.gohtml"), donorStore))
handleWithDonor(page.Paths.RemoveTrustCorporation, page.CanGoBack,
RemoveTrustCorporation(tmpls.Get("remove_attorney.gohtml"), donorStore, false))
handleWithDonor(page.Paths.HowShouldAttorneysMakeDecisions, page.CanGoBack,
HowShouldAttorneysMakeDecisions(tmpls.Get("how_should_attorneys_make_decisions.gohtml"), donorStore))
handleWithDonor(page.Paths.DoYouWantReplacementAttorneys, page.None,
WantReplacementAttorneys(tmpls.Get("do_you_want_replacement_attorneys.gohtml"), donorStore))
handleWithDonor(page.Paths.ChooseReplacementAttorneys, page.CanGoBack,
ChooseReplacementAttorneys(tmpls.Get("choose_replacement_attorneys.gohtml"), donorStore, actoruid.New))
handleWithDonor(page.Paths.ChooseReplacementAttorneysAddress, page.CanGoBack,
ChooseReplacementAttorneysAddress(logger, tmpls.Get("choose_address.gohtml"), addressClient, donorStore))
handleWithDonor(page.Paths.EnterReplacementTrustCorporation, page.CanGoBack,
EnterReplacementTrustCorporation(tmpls.Get("enter_replacement_trust_corporation.gohtml"), donorStore))
handleWithDonor(page.Paths.EnterReplacementTrustCorporationAddress, page.CanGoBack,
EnterReplacementTrustCorporationAddress(logger, tmpls.Get("choose_address.gohtml"), addressClient, donorStore))
handleWithDonor(page.Paths.ChooseReplacementAttorneysSummary, page.CanGoBack,
ChooseReplacementAttorneysSummary(tmpls.Get("choose_replacement_attorneys_summary.gohtml")))
handleWithDonor(page.Paths.RemoveReplacementAttorney, page.CanGoBack,
RemoveReplacementAttorney(logger, tmpls.Get("remove_attorney.gohtml"), donorStore))
handleWithDonor(page.Paths.RemoveReplacementTrustCorporation, page.CanGoBack,
RemoveTrustCorporation(tmpls.Get("remove_attorney.gohtml"), donorStore, true))
handleWithDonor(page.Paths.HowShouldReplacementAttorneysStepIn, page.CanGoBack,
HowShouldReplacementAttorneysStepIn(tmpls.Get("how_should_replacement_attorneys_step_in.gohtml"), donorStore))
handleWithDonor(page.Paths.HowShouldReplacementAttorneysMakeDecisions, page.CanGoBack,
HowShouldReplacementAttorneysMakeDecisions(tmpls.Get("how_should_replacement_attorneys_make_decisions.gohtml"), donorStore))
handleWithDonor(page.Paths.WhenCanTheLpaBeUsed, page.None,
WhenCanTheLpaBeUsed(tmpls.Get("when_can_the_lpa_be_used.gohtml"), donorStore))
handleWithDonor(page.Paths.LifeSustainingTreatment, page.None,
LifeSustainingTreatment(tmpls.Get("life_sustaining_treatment.gohtml"), donorStore))
handleWithDonor(page.Paths.Restrictions, page.None,
Restrictions(tmpls.Get("restrictions.gohtml"), donorStore))
handleWithDonor(page.Paths.WhatACertificateProviderDoes, page.None,
Guidance(tmpls.Get("what_a_certificate_provider_does.gohtml")))
handleWithDonor(page.Paths.ChooseYourCertificateProvider, page.None,
Guidance(tmpls.Get("choose_your_certificate_provider.gohtml")))
handleWithDonor(page.Paths.ChooseNewCertificateProvider, page.None,
ChooseNewCertificateProvider(tmpls.Get("choose_new_certificate_provider.gohtml"), donorStore))
handleWithDonor(page.Paths.CertificateProviderDetails, page.CanGoBack,
CertificateProviderDetails(tmpls.Get("certificate_provider_details.gohtml"), donorStore, actoruid.New))
handleWithDonor(page.Paths.HowWouldCertificateProviderPreferToCarryOutTheirRole, page.CanGoBack,
HowWouldCertificateProviderPreferToCarryOutTheirRole(tmpls.Get("how_would_certificate_provider_prefer_to_carry_out_their_role.gohtml"), donorStore))
handleWithDonor(page.Paths.CertificateProviderAddress, page.CanGoBack,
CertificateProviderAddress(logger, tmpls.Get("choose_address.gohtml"), addressClient, donorStore))
handleWithDonor(page.Paths.HowDoYouKnowYourCertificateProvider, page.CanGoBack,
HowDoYouKnowYourCertificateProvider(tmpls.Get("how_do_you_know_your_certificate_provider.gohtml"), donorStore))
handleWithDonor(page.Paths.HowLongHaveYouKnownCertificateProvider, page.CanGoBack,
HowLongHaveYouKnownCertificateProvider(tmpls.Get("how_long_have_you_known_certificate_provider.gohtml"), donorStore))
handleWithDonor(page.Paths.DoYouWantToNotifyPeople, page.CanGoBack,
DoYouWantToNotifyPeople(tmpls.Get("do_you_want_to_notify_people.gohtml"), donorStore))
handleWithDonor(page.Paths.ChoosePeopleToNotify, page.CanGoBack,
ChoosePeopleToNotify(tmpls.Get("choose_people_to_notify.gohtml"), donorStore, actoruid.New))
handleWithDonor(page.Paths.ChoosePeopleToNotifyAddress, page.CanGoBack,
ChoosePeopleToNotifyAddress(logger, tmpls.Get("choose_address.gohtml"), addressClient, donorStore))
handleWithDonor(page.Paths.ChoosePeopleToNotifySummary, page.CanGoBack,
ChoosePeopleToNotifySummary(tmpls.Get("choose_people_to_notify_summary.gohtml")))
handleWithDonor(page.Paths.RemovePersonToNotify, page.CanGoBack,
RemovePersonToNotify(logger, tmpls.Get("remove_person_to_notify.gohtml"), donorStore))
handleWithDonor(page.Paths.GettingHelpSigning, page.CanGoBack,
Guidance(tmpls.Get("getting_help_signing.gohtml")))
handleWithDonor(page.Paths.YourAuthorisedSignatory, page.CanGoBack,
YourAuthorisedSignatory(tmpls.Get("your_authorised_signatory.gohtml"), donorStore))
handleWithDonor(page.Paths.YourIndependentWitness, page.CanGoBack,
YourIndependentWitness(tmpls.Get("your_independent_witness.gohtml"), donorStore))
handleWithDonor(page.Paths.YourIndependentWitnessMobile, page.CanGoBack,
YourIndependentWitnessMobile(tmpls.Get("your_independent_witness_mobile.gohtml"), donorStore))
handleWithDonor(page.Paths.YourIndependentWitnessAddress, page.CanGoBack,
YourIndependentWitnessAddress(logger, tmpls.Get("choose_address.gohtml"), addressClient, donorStore))
handleWithDonor(page.Paths.YouCannotSignYourLpaYet, page.CanGoBack,
YouCannotSignYourLpaYet(tmpls.Get("you_cannot_sign_your_lpa_yet.gohtml")))
handleWithDonor(page.Paths.ConfirmYourCertificateProviderIsNotRelated, page.CanGoBack,
ConfirmYourCertificateProviderIsNotRelated(tmpls.Get("confirm_your_certificate_provider_is_not_related.gohtml"), donorStore, time.Now))
handleWithDonor(page.Paths.CheckYourLpa, page.CanGoBack,
CheckYourLpa(tmpls.Get("check_your_lpa.gohtml"), donorStore, shareCodeSender, notifyClient, certificateProviderStore, time.Now, appPublicURL))
handleWithDonor(page.Paths.LpaDetailsSaved, page.CanGoBack,
LpaDetailsSaved(tmpls.Get("lpa_details_saved.gohtml")))
handleWithDonor(page.Paths.AboutPayment, page.None,
Guidance(tmpls.Get("about_payment.gohtml")))
handleWithDonor(page.Paths.AreYouApplyingForFeeDiscountOrExemption, page.CanGoBack,
AreYouApplyingForFeeDiscountOrExemption(tmpls.Get("are_you_applying_for_a_different_fee_type.gohtml"), payer, donorStore))
handleWithDonor(page.Paths.WhichFeeTypeAreYouApplyingFor, page.CanGoBack,
WhichFeeTypeAreYouApplyingFor(tmpls.Get("which_fee_type_are_you_applying_for.gohtml"), donorStore))
handleWithDonor(page.Paths.PreviousApplicationNumber, page.None,
PreviousApplicationNumber(tmpls.Get("previous_application_number.gohtml"), donorStore))
handleWithDonor(page.Paths.PreviousFee, page.CanGoBack,
PreviousFee(tmpls.Get("previous_fee.gohtml"), payer, donorStore))
handleWithDonor(page.Paths.EvidenceRequired, page.CanGoBack,
Guidance(tmpls.Get("evidence_required.gohtml")))
handleWithDonor(page.Paths.HowWouldYouLikeToSendEvidence, page.CanGoBack,
HowWouldYouLikeToSendEvidence(tmpls.Get("how_would_you_like_to_send_evidence.gohtml"), donorStore))
handleWithDonor(page.Paths.UploadEvidence, page.CanGoBack,
UploadEvidence(tmpls.Get("upload_evidence.gohtml"), logger, payer, documentStore))
handleWithDonor(page.Paths.SendUsYourEvidenceByPost, page.CanGoBack,
SendUsYourEvidenceByPost(tmpls.Get("send_us_your_evidence_by_post.gohtml"), payer, eventClient))
handleWithDonor(page.Paths.FeeDenied, page.None,
FeeDenied(tmpls.Get("fee_denied.gohtml"), payer))
handleWithDonor(page.Paths.PaymentConfirmation, page.None,
PaymentConfirmation(logger, tmpls.Get("payment_confirmation.gohtml"), payClient, donorStore, sessionStore))
handleWithDonor(page.Paths.EvidenceSuccessfullyUploaded, page.None,
Guidance(tmpls.Get("evidence_successfully_uploaded.gohtml")))
handleWithDonor(page.Paths.WhatHappensNextPostEvidence, page.None,
Guidance(tmpls.Get("what_happens_next_post_evidence.gohtml")))
handleWithDonor(page.Paths.HowToConfirmYourIdentityAndSign, page.None,
Guidance(tmpls.Get("how_to_confirm_your_identity_and_sign.gohtml")))
handleWithDonor(page.Paths.ProveYourIdentity, page.CanGoBack,
Guidance(tmpls.Get("prove_your_identity.gohtml")))
handleWithDonor(page.Paths.IdentityWithOneLogin, page.CanGoBack,
IdentityWithOneLogin(oneLoginClient, sessionStore, random.String))
handleWithDonor(page.Paths.IdentityWithOneLoginCallback, page.CanGoBack,
IdentityWithOneLoginCallback(commonTmpls.Get("identity_with_one_login_callback.gohtml"), oneLoginClient, sessionStore, donorStore))
handleWithDonor(page.Paths.ReadYourLpa, page.None,
Guidance(tmpls.Get("read_your_lpa.gohtml")))
handleWithDonor(page.Paths.LpaYourLegalRightsAndResponsibilities, page.CanGoBack,
Guidance(tmpls.Get("your_legal_rights_and_responsibilities.gohtml")))
handleWithDonor(page.Paths.SignYourLpa, page.CanGoBack,
SignYourLpa(tmpls.Get("sign_your_lpa.gohtml"), donorStore))
handleWithDonor(page.Paths.SignTheLpaOnBehalf, page.CanGoBack,
SignYourLpa(tmpls.Get("sign_the_lpa_on_behalf.gohtml"), donorStore))
handleWithDonor(page.Paths.WitnessingYourSignature, page.None,
WitnessingYourSignature(tmpls.Get("witnessing_your_signature.gohtml"), witnessCodeSender, donorStore))
handleWithDonor(page.Paths.WitnessingAsIndependentWitness, page.None,
WitnessingAsIndependentWitness(tmpls.Get("witnessing_as_independent_witness.gohtml"), donorStore, time.Now))
handleWithDonor(page.Paths.ResendIndependentWitnessCode, page.CanGoBack,
ResendWitnessCode(tmpls.Get("resend_witness_code.gohtml"), witnessCodeSender, actor.TypeIndependentWitness))
handleWithDonor(page.Paths.ChangeIndependentWitnessMobileNumber, page.CanGoBack,
ChangeMobileNumber(tmpls.Get("change_mobile_number.gohtml"), witnessCodeSender, actor.TypeIndependentWitness))
handleWithDonor(page.Paths.WitnessingAsCertificateProvider, page.None,
WitnessingAsCertificateProvider(tmpls.Get("witnessing_as_certificate_provider.gohtml"), donorStore, shareCodeSender, lpaStoreClient, time.Now))
handleWithDonor(page.Paths.ResendCertificateProviderCode, page.CanGoBack,
ResendWitnessCode(tmpls.Get("resend_witness_code.gohtml"), witnessCodeSender, actor.TypeCertificateProvider))
handleWithDonor(page.Paths.ChangeCertificateProviderMobileNumber, page.CanGoBack,
ChangeMobileNumber(tmpls.Get("change_mobile_number.gohtml"), witnessCodeSender, actor.TypeCertificateProvider))
handleWithDonor(page.Paths.YouHaveSubmittedYourLpa, page.None,
Guidance(tmpls.Get("you_have_submitted_your_lpa.gohtml")))
handleWithDonor(page.Paths.Progress, page.CanGoBack,
LpaProgress(tmpls.Get("lpa_progress.gohtml"), certificateProviderStore, attorneyStore))
handleWithDonor(page.Paths.UploadEvidenceSSE, page.None,
UploadEvidenceSSE(documentStore, 3*time.Minute, 2*time.Second, time.Now))
}
func makeHandle(mux *http.ServeMux, store sesh.Store, defaultOptions page.HandleOpt, errorHandler page.ErrorHandler) func(page.Path, page.HandleOpt, page.Handler) {
return func(path page.Path, opt page.HandleOpt, h page.Handler) {
opt = opt | defaultOptions
mux.HandleFunc(path.String(), func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
appData := page.AppDataFromContext(ctx)
appData.Page = path.Format()
appData.CanGoBack = opt&page.CanGoBack != 0
appData.ActorType = actor.TypeDonor
if opt&page.RequireSession != 0 {
donorSession, err := sesh.Login(store, r)
if err != nil {
http.Redirect(w, r, page.Paths.Start.Format(), http.StatusFound)
return
}
appData.SessionID = donorSession.SessionID()
sessionData, err := page.SessionDataFromContext(ctx)
if err == nil {
sessionData.SessionID = appData.SessionID
ctx = page.ContextWithSessionData(ctx, sessionData)
appData.LpaID = sessionData.LpaID
} else {
ctx = page.ContextWithSessionData(ctx, &page.SessionData{SessionID: appData.SessionID, LpaID: appData.LpaID})
}
}
if err := h(appData, w, r.WithContext(page.ContextWithAppData(ctx, appData))); err != nil {
errorHandler(w, r, err)
}
})
}
}
func makeLpaHandle(mux *http.ServeMux, store sesh.Store, defaultOptions page.HandleOpt, errorHandler page.ErrorHandler, donorStore DonorStore) func(page.LpaPath, page.HandleOpt, Handler) {
return func(path page.LpaPath, opt page.HandleOpt, h Handler) {
opt = opt | defaultOptions
mux.HandleFunc(path.String(), func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
appData := page.AppDataFromContext(ctx)
appData.CanGoBack = opt&page.CanGoBack != 0
appData.ActorType = actor.TypeDonor
loginSession, err := sesh.Login(store, r)
if err != nil {
http.Redirect(w, r, page.Paths.Start.Format(), http.StatusFound)
return
}
appData.SessionID = loginSession.SessionID()
sessionData, err := page.SessionDataFromContext(ctx)
if err == nil {
sessionData.SessionID = appData.SessionID
ctx = page.ContextWithSessionData(ctx, sessionData)
appData.LpaID = sessionData.LpaID
} else {
sessionData = &page.SessionData{SessionID: appData.SessionID, LpaID: appData.LpaID}
ctx = page.ContextWithSessionData(ctx, sessionData)
}
if loginSession.OrganisationID != "" {
appData.IsSupporter = true
appData.OrganisationName = loginSession.OrganisationName
sessionData.OrganisationID = loginSession.OrganisationID
sessionData.Email = loginSession.Email
}
appData.Page = path.Format(appData.LpaID)
lpa, err := donorStore.Get(ctx)
if err != nil {
errorHandler(w, r, err)
return
}
if err := h(appData, w, r.WithContext(page.ContextWithAppData(ctx, appData)), lpa); err != nil {
errorHandler(w, r, err)
}
})
}
}
type payHelper struct {
logger Logger
sessionStore sessions.Store
donorStore DonorStore
payClient PayClient
randomString func(int) string
appPublicURL string
}
func (p *payHelper) Pay(appData page.AppData, w http.ResponseWriter, r *http.Request, donor *actor.DonorProvidedDetails) error {
if donor.FeeType.IsNoFee() || donor.FeeType.IsHardshipFee() || donor.Tasks.PayForLpa.IsMoreEvidenceRequired() {
donor.Tasks.PayForLpa = actor.PaymentTaskPending
if err := p.donorStore.Put(r.Context(), donor); err != nil {
return err
}
if donor.EvidenceDelivery.IsPost() {
return page.Paths.WhatHappensNextPostEvidence.Redirect(w, r, appData, donor)
}
return page.Paths.EvidenceSuccessfullyUploaded.Redirect(w, r, appData, donor)
}
createPaymentBody := pay.CreatePaymentBody{
Amount: donor.FeeAmount(),
Reference: p.randomString(12),
Description: "Property and Finance LPA",
ReturnUrl: p.appPublicURL + appData.Lang.URL(page.Paths.PaymentConfirmation.Format(donor.LpaID)),
Email: donor.Donor.Email,
Language: appData.Lang.String(),
}
resp, err := p.payClient.CreatePayment(r.Context(), createPaymentBody)
if err != nil {
p.logger.Print(fmt.Sprintf("Error creating payment: %s", err.Error()))
return err
}
if err = sesh.SetPayment(p.sessionStore, r, w, &sesh.PaymentSession{PaymentID: resp.PaymentId}); err != nil {
return err
}
if donor.Tasks.PayForLpa.IsDenied() {
donor.FeeType = pay.FullFee
donor.Tasks.PayForLpa = actor.PaymentTaskInProgress
if err := p.donorStore.Put(r.Context(), donor); err != nil {
return err
}
}
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) {
http.Redirect(w, r, nextUrl, http.StatusFound)
return nil
}
return page.Paths.PaymentConfirmation.Redirect(w, r, appData, donor)
}