Subversion Repositories PEEPS

Rev

Rev 19 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 19 Rev 22
Line 8... Line 8...
8
 
8
 
9
use strict;
9
use strict;
10
use PEEPS;
10
use PEEPS;
11
use CGI qw/param header start_html url/;
11
use CGI qw/param header start_html url/;
-
 
12
use CGI::Cookie;
12
use CGI::Cookie;
13
use tableViewer qw/ notInArray /;
13
our $h = HTML::Tiny->new( mode => 'html' );
14
our $h = HTML::Tiny->new( mode => 'html' );
14
my $dbh = getRCDBH ();
15
my $dbh = getRCDBH ();
15
$ENV{HTTPS} = 'ON' if $ENV{SERVER_NAME} =~ /^peeps/;
16
$ENV{HTTPS} = 'ON' if $ENV{SERVER_NAME} =~ /^peeps/;
Line 16... Line 17...
16
$dbh->{PrintError} = 1;
17
$dbh->{PrintError} = 1;
17
 
18
 
18
my $cookie_string = authenticate (1) || die;
19
my $cookie_string = authenticate (1) || die;
19
my ($EML, $PWD, $LVL) = split /&/, $cookie_string;
20
my ($EML, $PWD, $LVL) = split /&/, $cookie_string;
Line 20... Line -...
20
my $user = $ORCUSER;
-
 
Line 21... Line 21...
21
#my $activated = $ORCUSER->{access};
21
my $user = $ORCUSER;
22
 
22
#my $activated = $ORCUSER->{access};
23
print header (-cookie=>CGI::Cookie->new(-name=>'PEEPSAUTH',-value=>"$cookie_string"));
23
 
24
 
24
 
Line 25... Line 25...
25
use DateTime;
25
use DateTime;
26
#use DateTime::Format::Strptime;
26
#use DateTime::Format::Strptime;
27
my $dt = DateTime->today;
27
my $dt = DateTime->today;
28
$dt =~ s/T00\:00\:00$//;
-
 
29
 
-
 
-
 
28
$dt =~ s/T00\:00\:00$//;
30
$user->{policy} = isPersonCovered ($user->{id});
29
 
31
if ($user->{policy}) {
30
#$user->{policy} = isPersonCovered ($user->{id});
32
  my $daysremaining = remainingPolicyDays ($user->{id}, $user->{policy});
-
 
33
  printRCHeader("Atttestation");
-
 
34
  print $h->close ("table");
31
#if ($user->{policy}) {
35
  print $h->h2 ("Not Eligible for Renewal");
-
 
36
  print $h->div ({ style=>"max-width:450px;" }, "You're currently covered by policy $user->{policy}, which has $daysremaining days remaining. You can only renew within the last 90 days of your current policy.", " ");
32
#  my $daysremaining = remainingPolicyDays ($user->{id}, $user->{policy});
Line 37... Line 33...
37
  print $h->button ({onclick => "window.location.href='/';"}, "Home");
33
#  
Line -... Line 34...
-
 
34
#  ERROR ("Not Eligible for Renewal",
-
 
35
#         "You're currently covered by policy $user->{policy}, which has $daysremaining days remaining. You can only renew within the last 90 days of your current policy.",
38
  print $h->close ("BODY", "HTML");
36
#         "Viewed Purchase Confirmation page but wasn't eligible to renew.");
-
 
37
#}
Line 39... Line 38...
39
  logit ($user->{id}, "Viewed Purchase Confirmation page but wasn't eligible to renew.");
38
 
40
  exit;
39
## Check for DRAFT payments for the user, get the last one...
41
}
40
 
42
 
41
my $order_id = WebDB::trim scalar param ("order_id") // ERROR ("No OrderID Provided", "No OrderID provided to confirmation page.", "No OrderID provided to confirmation page.");
43
## Check for DRAFT payments for the user, get the last one...
42
 
-
 
43
#my $checkout = $dbh->selectrow_hashref ("SELECT * FROM wftdi_peeps.square_order where status = ? and person_id = ? order by created desc limit 1", undef, "DRAFT", $user->{id});
-
 
44
my $checkout = $dbh->selectrow_hashref ("SELECT * FROM wftdi_peeps.square_order where status = ? and order_id = ? order by created desc limit 1", undef, "DRAFT", $order_id);
-
 
45
 
-
 
46
if (!$checkout->{square_id}) {
-
 
47
  ERROR ("Open Order Not Found",
-
 
48
         "There are no open orders or pending payments found with OrderID: $order_id",
-
 
49
         "Viewed Purchase Confirmation page with OrderID [$order_id] but didn't have any open payments.");
-
 
50
}
44
 
51
 
Line 45... Line 52...
45
my $checkout = $dbh->selectrow_hashref ("SELECT * FROM wftdi_peeps.square_order where status = ? and person_id = ? order by created desc limit 1", undef, "DRAFT", $user->{id});
52
# Once we're here, we've found an order_id in DRAFT status. We should make sure it belongs to the user (or their league)
46
 
53
 
47
if (!$checkout->{square_id}) {
54
if ($checkout->{person_id} ne $user->{person_id} and notInArray ($checkout->{organization_id}, isLeagueAdmin ($user->{person_id}))) {
Line 71... Line 78...
71
my $response = from_json($client->responseContent());
78
my $response = from_json($client->responseContent());
72
#warn Dumper($response);
79
#warn Dumper($response);
Line 73... Line 80...
73
 
80
 
74
 
81
 
75
if ($response->{order}->{state} eq "DRAFT") {
82
if ($response->{order}->{state} eq "DRAFT") {
76
  # Checkout hasn't complete yet...
83
  # Checkout isn't complete yet...
77
  printRCHeader("Atttestation");
84
  ERROR ("Open Payment",
78
  print $h->div ("It looks like you haven't completed check out at Square yet. ".$h->a ({href=>$checkout->{url}}, "Here's a link to complete your payment."));
85
         "It looks like you haven't completed check out at Square yet. ".$h->a ({href=>$checkout->{url}}, "[Click Here]")." to complete your payment.",
79
  logit ($user->{id}, "Viewed Confirmation page with an open payment.");
86
         "Viewed Confirmation page with an open payment.");
80
} elsif ($response->{order}->{state} eq "OPEN") {
87
} elsif ($response->{order}->{state} eq "OPEN") {
81
  # Checkout completed...
88
  # Checkout completed...
82
  # create start and end dates based on the user's timezone
89
  # create start and end dates based on the user's timezone
Line 104... Line 111...
104
    $response->{order}->{tenders}->[0]->{id},
111
    $response->{order}->{tenders}->[0]->{id},
105
    $response->{order}->{tenders}->[0]->{created_at},
112
    $response->{order}->{tenders}->[0]->{created_at},
106
    $checkout->{square_id}
113
    $checkout->{square_id}
107
  );
114
  );
Line -... Line 115...
-
 
115
  
-
 
116
  # Here's where it gets complicated.  We have to figure out what kind of policy it is to know how to update the coverage records...
-
 
117
  
-
 
118
  if ($checkout->{policy_id} eq "1") { # Personal Accident Medical
-
 
119
    my $policy_id = isPersonCovered ($user->{id});
108
  
120
    if ($policy_id) {
-
 
121
      # extend existing coverage
-
 
122
      $dbh->do ("update coverage set end = date_add(end, INTERVAL 1 YEAR) where id = ?", undef, $policy_id);
-
 
123
    } else {
-
 
124
      # insert new coverage
-
 
125
      my ($new_policy_id) = $dbh->selectrow_array ("select max(id)+1 from coverage");
-
 
126
      $dbh->do ("insert into coverage (id, person_id, policy_id, policy_name, fee, created, start, end, active) values (?, ?, ?, ?, ?, ?, date(?), date_add(date(?), INTERVAL 1 YEAR), ?)", undef,
-
 
127
        $new_policy_id,
-
 
128
        $user->{id},
-
 
129
        $checkout->{policy_id},
-
 
130
        $response->{order}->{line_items}->[0]->{name},
-
 
131
        $response->{order}->{tenders}->[0]->{amount_money}->{amount} / 100,
-
 
132
        $response->{order}->{tenders}->[0]->{created_at},
-
 
133
        $adjusted_created_at,
-
 
134
        $adjusted_created_at,
-
 
135
        1
-
 
136
      );
109
  # Update the users coverage record
137
      $dbh->do ("replace into full_person select * from v_person where id = ?", undef, $user->{id});
-
 
138
      $policy_id = isPersonCovered ($user->{id});
-
 
139
    }
-
 
140
    
-
 
141
    if ($policy_id) {      
-
 
142
      my $daysremaining = remainingPolicyDays ($user->{id}, $policy_id);
-
 
143
      communicateConfirmation ({ to => $user->{email},
-
 
144
                                 policy => $response->{order}->{line_items}->[0]->{name},
-
 
145
                                 message => "You're currently covered by policy $policy_id, which has $daysremaining days remaining." });
-
 
146
    } else {
-
 
147
      ERROR ("Unknown Error Confirming Purchase",
-
 
148
             "It seemed like you successfully purchased insurance, but then there was an issue updating our records. You should probably email peeps\@wftdi.com and tell them to investigate.",
-
 
149
             "ERROR: Checkout complete, but PEEPS coverage failed to update.");
-
 
150
    }
-
 
151
  } elsif ($checkout->{policy_id} eq "2") { # League General Liability
110
  my $policy_id = isPersonCovered ($user->{id});
152
    my $policy_id = isLeagueCovered ($checkout->{organization_id});
111
  if ($policy_id) {
153
    if ($policy_id) {
-
 
154
      # extend existing coverage
-
 
155
      $dbh->do ("update org_coverage set end = date_add(end, INTERVAL 1 YEAR) where id = ?", undef, $policy_id);
-
 
156
    } else {
-
 
157
      # insert new coverage
-
 
158
      my ($new_policy_id) = $dbh->selectrow_array ("select max(id)+1 from org_coverage");
-
 
159
      $dbh->do ("insert into org_coverage (id, organization_id, policy_id, policy_name, fee, created, start, end, active) values (?, ?, ?, ?, ?, ?, date(?), date_add(date(?), INTERVAL 1 YEAR), ?)", undef,
-
 
160
        $new_policy_id,
-
 
161
        $checkout->{organization_id},
-
 
162
        $checkout->{policy_id},        
-
 
163
        $response->{order}->{line_items}->[0]->{name},
-
 
164
        $response->{order}->{tenders}->[0]->{amount_money}->{amount} / 100,
-
 
165
        $response->{order}->{tenders}->[0]->{created_at},
-
 
166
        $adjusted_created_at,
-
 
167
        $adjusted_created_at,
-
 
168
        1
-
 
169
      );
-
 
170
      $policy_id = isLeagueCovered ($user->{id});
-
 
171
    }
-
 
172
    
-
 
173
    if ($policy_id) {
-
 
174
      my $daysremaining = remainingOrgPolicyDays ($checkout->{organization_id}, $policy_id);
-
 
175
      communicateConfirmation ({ to => $user->{email},
-
 
176
                                 policy => $response->{order}->{line_items}->[0]->{name},
-
 
177
                                 message => "Your league is currently covered by policy $policy_id, which has $daysremaining days remaining.",
-
 
178
                                 orgid => $checkout->{organization_id} });
-
 
179
    } else {
-
 
180
      ERROR ("Unknown Error Confirming Purchase",
-
 
181
             "It seemed like you successfully purchased insurance, but then there was an issue updating our records. You should probably email peeps\@wftdi.com and tell them to investigate.",
-
 
182
             "ERROR: Checkout complete, but PEEPS coverage failed to update.");
-
 
183
    }
-
 
184
  } elsif ($checkout->{policy_id} eq "3") { # League Alcohol Liability
-
 
185
    my $policy_id = isLeagueCovered ($checkout->{organization_id}, undef, "WFTDA League Alcohol Liability");
-
 
186
    if ($policy_id) {
112
    # extend existing coverage
187
      # extend existing coverage
-
 
188
      $dbh->do ("update org_coverage set end = date_add(end, INTERVAL 1 YEAR) where id = ?", undef, $policy_id);
-
 
189
    } else {
-
 
190
      # insert new coverage
-
 
191
      my ($new_policy_id) = $dbh->selectrow_array ("select max(id)+1 from org_coverage");
-
 
192
      $dbh->do ("insert into org_coverage (id, organization_id, policy_id, policy_name, fee, created, start, end, active) values (?, ?, ?, ?, ?, ?, date(?), MAKEDATE(YEAR(CURDATE()), 365), ?)", undef,
-
 
193
        $new_policy_id,
-
 
194
        $checkout->{organization_id},
-
 
195
        $checkout->{policy_id},        
-
 
196
        $response->{order}->{line_items}->[0]->{name},
-
 
197
        $response->{order}->{tenders}->[0]->{amount_money}->{amount} / 100,
-
 
198
        $response->{order}->{tenders}->[0]->{created_at},
-
 
199
        $adjusted_created_at,
-
 
200
        1
-
 
201
      );
-
 
202
      $policy_id = isLeagueCovered ($user->{id});
-
 
203
    }
-
 
204
    
-
 
205
    if ($policy_id) {
-
 
206
      my $daysremaining = remainingOrgPolicyDays ($checkout->{organization_id}, $policy_id);
-
 
207
      communicateConfirmation ({ to => $user->{email},
-
 
208
                                 policy => $response->{order}->{line_items}->[0]->{name},
-
 
209
                                 message => "Your league is currently covered by policy $policy_id, which has $daysremaining days remaining.",
113
    $dbh->do ("update coverage set end = date_add(end, INTERVAL 1 YEAR) where id = ?", undef, $policy_id);
210
                                 orgid => $checkout->{organization_id} });
-
 
211
    } else {
-
 
212
      ERROR ("Unknown Error Confirming Purchase",
-
 
213
             "It seemed like you successfully purchased insurance, but then there was an issue updating our records. You should probably email peeps\@wftdi.com and tell them to investigate.",
-
 
214
             "ERROR: Checkout complete, but PEEPS coverage failed to update.");
-
 
215
    }
114
  } else {
216
  } elsif ($checkout->{policy_id} eq "4") { # RollerCon
115
    # insert new coverage
217
    # insert new coverage
116
    my ($new_policy_id) = $dbh->selectrow_array ("select max(id)+1 from coverage");
218
    my ($new_policy_id) = $dbh->selectrow_array ("select max(id)+1 from coverage");
-
 
219
    $dbh->do ("insert into coverage (id, person_id, policy_id, policy_name, fee, created, start, end, active) values (?, ?, ?, ?, ?, ?, ?, ?, ?)", undef,
117
    $dbh->do ("insert into coverage (id, person_id, policy_name, fee, created, start, end, active) values ($new_policy_id, ?, ?, ?, ?, date(?), date_add(date(?), INTERVAL 1 YEAR), ?)", undef,
220
      $new_policy_id,
-
 
221
      $user->{id},
118
      $user->{id},
222
      $checkout->{policy_id},
119
      $response->{order}->{line_items}->[0]->{name},
223
      $response->{order}->{line_items}->[0]->{name},
120
      $response->{order}->{tenders}->[0]->{amount_money}->{amount} / 100,
224
      $response->{order}->{tenders}->[0]->{amount_money}->{amount} / 100,
121
      $response->{order}->{tenders}->[0]->{created_at},
225
      $response->{order}->{tenders}->[0]->{created_at},
122
      $adjusted_created_at,
226
      "2026-07-09",
123
      $adjusted_created_at,
227
      "2026-07-11",
124
      1
228
      1
125
    );
229
    );
126
    $policy_id = isPersonCovered ($user->{id});
230
    my $policy_id = $new_policy_id;
127
 
-
 
128
    printRCHeader("Purchase Confirmation");
-
 
129
    print $h->close ("table");
231
    
130
    if ($policy_id) {
232
    if ($policy_id) {
131
      my $daysremaining = remainingPolicyDays ($user->{id}, $policy_id);
233
      communicateConfirmation ({ to => $user->{email},
132
      print $h->h2 ("Congratulations!");
234
                                 policy => $response->{order}->{line_items}->[0]->{name},
133
      print $h->div ({ style=>"max-width:450px;" }, "You've successfully purchased insurance.", "You're currently covered by policy $policy_id, which has $daysremaining days remaining.", " ");
-
 
134
      print $h->button ({onclick => "window.location.href='/';"}, "Home");
-
 
135
      print $h->close ("BODY", "HTML");
-
 
136
      logit ($user->{id}, "Successfully renewed insurance.");
-
 
137
      emailConfirmation ($user->{email});
-
 
138
      exit;
235
                                 message => "You are covered for RollerCon on-site event skating activities by policy $policy_id, July 9-11, 2026." });
139
    } else {
236
    } else {
140
      print $h->h2 ("That's weird!");
237
      ERROR ("Unknown Error Confirming Purchase",
141
      print $h->div ({ style=>"max-width:450px;" }, "It seemed like you successfully purchased insurance, but then there was an issue updating your records. You should probably email peeps\@wftdi.com and tell them to investigate.", " ");
-
 
142
      print $h->button ({onclick => "window.location.href='/';"}, "Home");
-
 
143
      print $h->close ("BODY", "HTML");
238
             "It seemed like you successfully purchased insurance, but then there was an issue updating our records. You should probably email peeps\@wftdi.com and tell them to investigate.",
144
      logit ($user->{id}, "ERROR: Checkout complete, but PEEPS coverage failed to update.");
-
 
145
      exit;      
239
             "ERROR: Checkout complete, but PEEPS coverage failed to update.");
146
    }
-
 
147
 
240
    }
148
  }
-
 
149
  
241
  }
150
} else {
242
} else {
151
  # Something weird has happened.
-
 
152
  printRCHeader("Purchase Confirmation");
243
  # Something weird happened.
153
  print $h->h2 ("Whoa, ERROR!");
-
 
154
  print $h->div ("Something weird has happened. You should probably email peeps\@wftdi.com and tell them to investigate.", " ");
-
 
155
  print $h->button ({onclick => "window.location.href='/';"}, "Home");
-
 
156
  logit ($user->{id}, "ERROR: Something really weird happend during checkout confirmation.");
244
  ERROR ();
Line 157... Line 245...
157
}
245
}
158
 
246
 
Line 170... Line 258...
170
                         "WFTDA Insurance",
258
                         "WFTDA Insurance",
171
                         $h->br ()
259
                         $h->br ()
172
                       );
260
                       );
Line 173... Line 261...
173
  
261
  
174
  use PEEPSMailer;
262
  use PEEPSMailer;
-
 
263
  EmailUser ($target, $subject, $message); 
-
 
264
}
Line -... Line 265...
-
 
265
 
-
 
266
 
-
 
267
sub communicateConfirmation {
-
 
268
  my $parameter = shift // return;
-
 
269
  my $target = $parameter->{to};
-
 
270
  my $policy = $parameter->{policy};
-
 
271
  my $message = $parameter->{message};
-
 
272
  my $orgid = $parameter->{orgid} // "";
-
 
273
  
-
 
274
  my @league_admins = $orgid ? map { $_->[0] } @{ $dbh->selectall_arrayref ("select distinct email from person left join role on person.id = role.person_id where role = ? and member_org_id = ? and person.id <> ?", undef, "League Admin", $orgid, $user->{person_id}) } : ();
-
 
275
    
-
 
276
  my $subject = $policy." Coverage Confirmation";
-
 
277
  
-
 
278
  print header (-cookie=>CGI::Cookie->new(-name=>'PEEPSAUTH',-value=>"$cookie_string"));
-
 
279
  printRCHeader("Insurance Purchase Confirmation");
-
 
280
  print $h->close ("table");
-
 
281
  
-
 
282
  print $h->h2 ($subject);
-
 
283
  print $h->div ({ style=>"max-width:450px;" }, "You have successfully purchased $policy coverage.", $message, "&nbsp;");
Line -... Line 284...
-
 
284
  print $h->button ({onclick => "window.location.href='/';"}, "Home");
-
 
285
  print $h->close ("BODY", "HTML");
-
 
286
  
-
 
287
  my $message = $h->p ($h->br ()).
-
 
288
                $h->p ({ style => "text-align: center;" }, $h->img ({ alt => "", src => "https://peeps.gadell.org/images/wftda-insurance-logo.svg", style => "width: 300px; height: 97px;"})).
-
 
289
                $h->p (
-
 
290
                         $h->br (),
-
 
291
                         ["Greetings", $h->br (), $h->br ()],
-
 
292
                         ["You have successfully purchased $policy coverage.", $message, $h->br (), $h->br ()],
-
 
293
                         "Thank you for enrolling in WFTDA Insurance! This email confirms your coverage is in effect, please retain it for your records. For additional resources, forms and information, please visit ".$h->a ({ href => "https://wftdi.com" }, "wftdi.com").'. &nbsp;',
-
 
294
                         $h->br (),
-
 
295
                         "WFTDA Insurance",
-
 
296
                         $h->br ()
-
 
297
                       );
-
 
298
  
-
 
299
  if ($orgid) {
-
 
300
    orglogit ($user->{id}, $orgid, "Successfully purchased $policy coverage");
-
 
301
    logit ($user->{id}, "Successfully purchased $policy coverage for ".getLeagueName ($orgid).".");
-
 
302
  } else {
-
 
303
    logit ($user->{id}, "Successfully purchased $policy coverage.")    
-
 
304
  }
-
 
305
  
-
 
306
  use PEEPSMailer;
-
 
307
  if (scalar @league_admins) {
-
 
308
    EmailMultipleUsers ({ to => [$target], cc => \@league_admins, subject => $subject, body => $message });
-
 
309
  } else { 
-
 
310
    EmailUser ($target, $subject, $message);
-
 
311
  }
-
 
312
  
-
 
313
  exit;
-
 
314
}
-
 
315
 
-
 
316
 
-
 
317
sub ERROR {
-
 
318
  my $header = shift // "Unknown Error";
-
 
319
  my $text   = shift // "Something unexpectedly bad happened.";
-
 
320
  my $logmsg = shift // "Unknown Error happened while viewing the Confirmation page";
-
 
321
  
-
 
322
  print header (-cookie=>CGI::Cookie->new(-name=>'PEEPSAUTH',-value=>"$cookie_string"));
-
 
323
  printRCHeader("Confirmation");
-
 
324
  print $h->close ("table");
-
 
325
  print $h->h2 ($header);
-
 
326
  print $h->div ({ style=>"max-width:450px;" }, $text, "&nbsp;");
-
 
327
  print $h->button ({onclick => "window.location.href='/';"}, "Home");
-
 
328
  print $h->close ("BODY", "HTML");
175
  EmailUser ($target, $subject, $message);
329
  logit ($user->{id}, $logmsg);