@@ -7,134 +7,55 @@ use CH::Util::Pager;
77use CH::Util::DateHelper;
88use POSIX qw( strftime) ;
99use Mojo::IOLoop::Delay;
10- use Digest::SHA qw( sha1) ;
1110use MIME::Base64 qw( encode_base64url) ;
12- use Data::Dumper;
1311
1412# -------------------------------------------------------------------------------
13+
1514sub resume {
1615 my ($self ) = @_ ;
17-
18- my $encoded_id = $self -> param(' id' );
19-
16+
17+ $self -> render_later;
18+
19+ my $encoded_resume_link = $self -> param(' link' );
20+
2021 $self -> ch_api-> transactions($self -> stash(' transaction_number' ))-> get-> on(
2122 success => sub {
2223 my ($api , $tx ) = @_ ;
23- my $transaction = $tx -> success-> json;
24-
25- my $resource_and_id_match = 0;
26-
27- for my $resource ( keys %{$transaction -> {resources }}) {
28- if ( encode_base64url(sha1($resource )) eq $encoded_id ){
29- $resource_and_id_match = 1;
30- $self -> _build_resume_link($transaction , $transaction -> {resources }-> {$resource } );
31- last ;
32- }
33- }
34- if ( $resource_and_id_match eq 0) {
35- error " None of the resource keys could be matched with provided encoded id" ;
36- $self -> render_not_found;
37- }
24+
25+ my $transaction = $tx -> success-> json;
26+
27+ my $resume_link = $transaction -> {resume_journey_uri };
28+
29+ if (encode_base64url($resume_link ) ne $encoded_resume_link ) {
30+ my $message = " The transaction resume link does not match the encoded link url" ;
31+ error " %s " , $message ;
32+ $self -> render_exception($message );
33+ }
34+
35+ # TODO: When support is added for third party (i.e. external) resume links, a check will need
36+ # to be performed here to verify that the resume link matches a trusted domain for a given
37+ # software vendor. A mechanism will be needed for adding the vendor to the transaction resource
38+ # at creation time, and for registering one or more trusted domains that should be checked here.
39+ # All Companies House resume links should be relative (i.e. not include the protocol or domain).
40+
41+ $self -> redirect_to($resume_link );
3842 },
3943 failure => sub {
4044 my ($api , $tx ) = @_ ;
45+
4146 my ($error_message , $error_code ) = ($tx -> error-> {message }, $tx -> error-> {code });
42- my $message = ' Failed to fetch transaction ' . $self -> stash(' transaction_number' ). ' : ' . $error_code . ' ' . $error_message ;
47+ my $message = ' Failed to fetch transaction ' . $self -> stash(' transaction_number' ) . ' : ' . $error_code . ' ' . $error_message ;
4348 error " %s " , $message [API];
4449 $self -> render_exception($message );
4550 },
4651 error => sub {
4752 my ($api , $error ) = @_ ;
48- my $message = ' Failed to fetch transaction ' .$self -> stash(' transaction_number' ).' : ' .$error ;
49- error " %s " , $message [ROUTING];
50- $self -> render_exception($message );
51- }
52- )-> execute;
53- $self -> render_later;
54- };
55-
56- # -------------------------------------------------------------------------------
57-
58- sub _build_resume_link {
59- my ($self , $transaction , $resource )= @_ ;
60-
61- my $company_number = $transaction -> {company_number };
62- my $transaction_id = $transaction -> {id };
63- my $kind = $resource -> {kind };
64- my $abridged_accounts_id ;
65- my $resume_link ;
66-
67- my $resource_delay = Mojo::IOLoop::Delay-> new;
68- my $resource_delay_end ;
69-
70- if ( $kind eq " accounts" ) {
71- $resource_delay_end = $resource_delay -> begin(0);
72- $self -> _get_accounts_document($resource -> {links }-> {resource }, $resource_delay_end );
73- }
74-
75- $resource_delay -> on(
76- finish => sub {
77- my ($delay , $resource_link , $accounts_id ) = @_ ;
7853
79- if ( $resource_link && $kind eq " accounts" ){
80- if ( $resource_link =~/ abridged\/ (.*)$ / ) {
81- $abridged_accounts_id = $1 ;
82- }
83- $resume_link = " /company/" . $company_number . " /transaction/" . $transaction_id . " /submit-abridged-accounts/" . $accounts_id ." /" . $abridged_accounts_id . " /accounting-reference-date" ;
84- }
85-
86- $self -> redirect_to($resume_link );
87- },
88- error => sub {
89- my ($delay , $err ) = @_ ;
90-
91- my $message = " Error getting accounts links : %s " . $err ;
92- error " Error getting accounts links : %s " . $err ;
54+ my $message = ' Error when fetching transaction ' . $self -> stash(' transaction_number' ) . ' : ' . $error ;
55+ error " %s " , $message [ROUTING];
9356 $self -> render_exception($message );
9457 }
95- );
96-
97- }
98-
99-
100- # ------------------------------------------------------------------------------
101-
102- sub _get_accounts_document {
103- my ($self , $resource_link , $callback ) = @_ ;
104-
105- $self -> ch_api-> uri($resource_link )-> get-> on(
106- success => sub {
107- my ($api , $tx ) = @_ ;
108-
109- my $accounts = $tx -> success-> json;
110-
111- if ( defined $accounts -> {links }-> {abridged_accounts } ){
112- $callback -> ($accounts -> {links }-> {abridged_accounts }, $accounts -> {id });
113- }
114- return $callback -> ();
115- },
116- failure => sub {
117- my ($api , $tx ) = @_ ;
118-
119- my $error_code = $tx -> error-> {code } // 0;
120- my $error_message = $tx -> error-> {message };
121-
122- if (defined $error_code and $error_code == 404) {
123- error " Resource [%s ] not found" , $resource_link ;
124- $self -> render_not_found;
125- }
126- my $message = " Error getting accounts links : %s " . $resource_link ;
127- error " Error getting accounts links : %s " , $resource_link [ RESUME LINK ];
128- $self -> render_exception($message );
129- },
130- error => sub {
131- my ($api , $error ) = @_ ;
132-
133- my $message = " Error getting accounts links : %s " . $resource_link ;
134- error " Error getting accounts links : %s " , $resource_link [ RESUME LINK ];
135- $self -> render_exception($message );
136- }
137- )-> execute;
58+ )-> execute;
13859}
13960
14061# ------------------------------------------------------------------------------
0 commit comments