Subversion Repositories PEEPS

Rev

Rev 31 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
#!/usr/bin/perl
2
 
3
# Redirect error messages to a log of my choosing. (it's annoying to filter for errors in the shared env)
4
#my $error_log_path = $ENV{SERVER_NAME} eq "volunteers.rollercon.com" ? "/home3/rollerco/logs/" : "/tmp/";
5
#close STDERR;
6
#open STDERR, '>>', $error_log_path.'vorc_error.log' or warn "Failed to open redirected logfile ($0): $!";
7
#warn "Redirecting errors to ${error_log_path}vorc_error.log";
8
 
9
use strict;
10
use PEEPS;
11
use CGI qw/param header start_html url redirect/;
12
use CGI::Cookie;
13
our $h = HTML::Tiny->new( mode => 'html' );
14
my $dbh = getRCDBH ();
4 - 15
$ENV{HTTPS} = 'ON' if $ENV{SERVER_NAME} =~ /^peeps/;
2 - 16
 
17
my $cookie_string = authenticate (1) || die;
18
my ($EML, $PWD, $LVL) = split /&/, $cookie_string;
19
my $user = $ORCUSER;
24 - 20
my $RENEWAL_WINDOW = 90;
2 - 21
#my $activated = $ORCUSER->{access};
22
 
23
use DateTime;
22 - 24
my $dt = DateTime->today()->strftime ('%Y-%m-%d');
2 - 25
 
22 - 26
 
27
## Which Policy ID is being requested?
28
##
29
my $policy_id = WebDB::trim scalar param ("policy") // "ERROR";
30
my $policy = getPolicyByID ($policy_id) // {};
31
if ($policy_id ne $policy->{id} or !$policy->{active}) {
32
  ERROR ("Policy ID [$policy_id] not found",
33
         "It's unclear what type of insurance you're attempting to purchase.",
34
         "Viewed Attestation page without valid policy_id.");
2 - 35
}
36
 
22 - 37
 
38
## We have a valid Policy ID, check purchase eligibility.
39
##
40
my ($eligible, $daysremaining, $active_policy, $league_id, $league_name);
41
 
42
if ($policy->{type} eq "league") {
43
  ## If it's a League Policy Type, then get the Leauge ID too.
44
  ##
45
  $league_id = $policy->{type} eq "league" ? WebDB::trim scalar param ("league") // undef : undef;
46
  $league_name = $league_id =~ /^\d+$/ ? getLeagueName ($league_id) : undef;
47
 
48
  if (!$league_name) {
49
    ## If there's no leauge_name, it's not a valid league...
50
    ERROR ("League ID [$league_id] not found",
51
           "It's unclear which league you are attempting to cover.",
52
           "Viewed Attestation page for league coverage without valid league_id.");
53
  }
54
 
55
  ## Only Leauge Admins should be purchasing league type polcies
47 - 56
#  use tableViewer qw/ notInArray /;
57
  if (tableViewer::notInArray ($league_id, isLeagueAdmin ($user->{person_id}))) {
22 - 58
    ERROR ("Not a League Admin [$league_id]",
59
           "You must be a League Admin to purchase insurance for the league.",
60
           "Viewed Attestation page for league coverage but not a league admin.");
61
  }
62
 
31 - 63
  ## Leagues have to be approved to purchase Alcohol Liability coverage
64
  if ($policy->{id} eq "3" and !getLeague ($league_id)->{alcohol_liability_eligible}) {
65
    ERROR ("League Not Eligible [$league_id]",
66
           "You must be pre-approved to purchase Alcohol Liability coverage for the league. Please email insurance\@wftdi.com to begin approval process.",
67
           "Viewed Attestation page for Alchohol Liability coverage but not pre-approved.");
68
  }
69
 
22 - 70
  ## Check to see if this is a new purchase or within the renewal period.  If neither, eligible will be empty.
31 - 71
  $active_policy = isLeagueCovered ($league_id, undef, $policy->{name});
22 - 72
  if ($active_policy) {
73
    $daysremaining = remainingOrgPolicyDays ($league_id, $active_policy);
74
    if ($daysremaining <= $RENEWAL_WINDOW) {
75
      $eligible = "renew";
76
    }
77
  } else {
78
    $eligible = "new";
79
  }
80
} elsif ($policy->{type} eq "personal") {
81
  if ($policy->{id} eq "4") { # They're trying to buy RollerCon Coverage...
82
    $active_policy = isPersonCovered ($user->{id}, '2026-07-11');
83
    if (!$active_policy) {
84
      ($active_policy) = $dbh->selectrow_array ("select id from coverage where policy_id = ? and person_id = ?", undef, 4, $user->{id});
85
    }
86
 
87
    ERROR ("Already Covered",
88
           "You are already covered for RollerCon 2026 by Policy $active_policy. No need to purchase.",
89
           "Attempted to purchase RollerCon 2026 covereage but not needed") unless !$active_policy;
90
 
91
  } else {
92
    ## Check to see if this is a new purchase or within the renewal period.  If neither, eligible will be empty.
93
    $active_policy = isPersonCovered ($user->{id});
94
    if ($active_policy) {
95
      $daysremaining = remainingPolicyDays ($user->{id}, $active_policy);
96
      if ($daysremaining <= $RENEWAL_WINDOW) {
97
        $eligible = "renew";
98
      }
99
    } else {
100
      $eligible = "new";
101
    }
102
  }
103
} else {
104
  ## If we're here, it means the policy type wasn't recognized...
105
  ERROR ("Something weird happened...",
106
         "The type of policy you attempted to puchase doesn't seem to exist.",
107
         "Viewed Attestation page for invalid policy type.");
2 - 108
}
109
 
22 - 110
ERROR ("Not Eligible for Renewal",
111
       "You".($policy->{type} eq "league" ? "r league is" : " are")." currently covered by policy $active_policy, which has $daysremaining days remaining. You can only renew within the last $RENEWAL_WINDOW days of your current policy.",
112
       "Viewed Attestation page but wasn't eligible to renew.") unless $eligible;
113
 
114
## Check to see if there is an abandoned checkout that needs to be completed...
115
my ($abandoned_checkout) = $policy->{type} eq "league" ?
116
      $dbh->selectrow_array ("select url from square_order where organization_id = ? and policy_id = ? and status = ? order by created desc limit 1", undef, $league_id, $policy->{id}, "DRAFT") :
117
      $dbh->selectrow_array ("select url from square_order where person_id = ? and policy_id = ? and status = ? order by created desc limit 1", undef, $user->{id}, $policy->{id}, "DRAFT") ;
2 - 118
if ($abandoned_checkout) {
119
  my $cgi = CGI->new();
22 - 120
  logit ($user->{id}, "Returned to abandoned $policy->{type} policy checkout.");
2 - 121
  print $cgi->redirect ($abandoned_checkout);
122
  exit;
123
}
124
 
22 - 125
## Get the terms for attestion...
126
my @terms = map { @{$_} } @{ $dbh->selectall_arrayref ("select term from attestation where id = ? order by attestation.index", undef, $policy->{attestation_id}) };
2 - 127
my $terms;
22 - 128
foreach (1..scalar @terms) {
2 - 129
  $terms++ if param ("Term".$_);
130
}
131
my $esig = param ("esignature") // "";
132
 
22 - 133
if ($terms == scalar @terms and $esig =~ /^\w+$/) {
2 - 134
  # All of the checkboxes were accepted and an eSignature was entered.
135
  # Create the Square Checkout link and redirect the user there.
22 - 136
  logit ($user->{id}, "Submitted purchase attestation for $policy->{type} $policy->{name} with all terms accepted and esignature entered.");
137
  sendToSquare ($esig, $terms, $policy);
2 - 138
  exit;
139
}
140
 
141
print header (-cookie=>CGI::Cookie->new(-name=>'PEEPSAUTH',-value=>"$cookie_string"));
142
printRCHeader("Atttestation");
143
 
144
print $h->close ("table");
145
 
146
print $h->open ("form", { name => "Attestation", method => "post", action => url });
147
 
148
print $h->input ( { type => "hidden", name => "timezone", id => "TZFIELD", value => "" } );
22 - 149
print $h->input ( { type => "hidden", name => "policy", value => $policy_id } );
150
print $h->input ( { type => "hidden", name => "league", value => $league_id } ) unless !$league_id;
2 - 151
 
152
 
22 - 153
print $h->div ({ class=>"error" }, "&nbsp;", "ERROR: You must agree to each term to continue.") if $terms and $terms < scalar @terms;
2 - 154
 
22 - 155
print $h->h2 ("Policy Details");
156
print $h->div ({ style=>"max-width:850px;" }, $h->ul ([$h->li (map { "$_: $policy->{$_}" } keys %{$policy})]));
2 - 157
 
22 - 158
print map { $h->div ({ class => "lisp0" }, [
159
    $h->div ({ class=>"liRight", style=>"margin-right:.5em" }, ucfirst ($_).": "),
160
    $h->div ({ class=>"liLeft" }, $policy->{$_} )]) } qw/ name type fee /;
2 - 161
 
22 - 162
 
163
 
164
if ($policy->{type} eq "personal") {
165
  print $h->h2 ("Release and Waiver of Liability");
166
 
167
  print $h->div ({ style=>"max-width:850px;" }, "RELEASE AND WAIVER OF LIABILITY, ASSUMPTION OF RISK, AND INDEMNITY AGREEMENT (\"Agreement\") IN CONSIDERATION of being permitted to participate this date, in any way, at any time, in Women’s Flat Track Roller Derby (\"Activity\"), I, for myself, my personal representatives, assigns, heirs, and next of kin:");
168
 
169
  print $h->ol ({ style=>"max-width:850px;" }, [
170
    $h->li ("ACKNOWLEDGE, agree, and represent that I understand the nature of this Activity, and that I am qualified, in good health, and in proper physical condition to participate in such Activity. I further agree and warrant that if, at any time, I believe the conditions to be unsafe, I will immediately discontinue further participation in this Activity."),
171
    $h->li ("FULLY UNDERSTAND that: (a) THIS ACTIVITY INVOLVES RISKS AND DANGERS OF SERIOUS BODILY INJURY, INCLUDING PERMANENT DISABILITY, PARALYSIS, AND DEATH (\"Risks\"); (b) these Risks and dangers may be caused by my own actions or inactions, the actions or inactions of others participating in the Activity, the conditions in which the Activity takes place, or THE NEGLIGENCE OF THE \"RELEASEES\" NAMED BELOW; (c) there may be OTHER RISKS or SOCIAL AND ECONOMIC LOSSES either not known to me or not readily foreseeable at this time; and I FULLY ACCEPT AND ASSUME ALL SUCH RISKS AND ALL RESPONSIBILITY FOR LOSSES, COSTS, AND DAMAGES I incur as a result of my participation, in the Activity."),
172
    $h->li ("HEREBY RELEASE, DISCHARGE, AND COVENANT NOT TO SUE the sanctioning organization(s), their administrators, directors, agents, officers, members, volunteers, and employees, other participants, officials, rescue personnel, sponsors, advertisers, owners and lessees of Premises on which the Activity is conducted, (each of the forgoing shall be considered one of the RELEASEES herein) FROM ALL LIABILITY, CLAIMS, DEMANDS, LOSSES, OR DAMAGES ON MY ACCOUNT CAUSED, OR ALLEGED TO BE CAUSED, IN WHOLE OR IN PART BY THE NEGLIGENCE OF THE RELEASEES OR OTHERWISE, INCLUDING NEGLIGENT RESCUE OPERATIONS; AND I FURTHER AGREE that if, despite this RELEASE AND WAIVER OF LIABILITY, ASSUMPTION OF RISK, AND INDEMNITY AGREEMENT I, or anyone on my behalf, makes a claim against any of the RELEASEES, I WILL INDEMNIFY, SAVE, AND HOLD HARMLESS EACH OF THE RELEASEES from any litigation expenses, attorney fees, loss, liability, damage, or cost which may be incurred as the result of such claim."),
173
    ]);
174
 
175
  print $h->div ({ style=>"max-width:850px;" }, "I ACKNOWLEDGE THAT I AM OVER THE AGE OF 18 YEARS, HAVE READ THIS AGREEMENT AND FULLY UNDERSTAND ITS TERMS, UNDERSTAND THAT I HAVE GIVEN UP SUBSTANTIAL RIGHTS BY SIGNING IT, HAVE SIGNED IT FREELY AND WITHOUT ANY INDUCEMENT OR ASSURANCE OF ANY NATURE, AND I INTEND IT TO BE A COMPLETE AND UNCONDITIONAL RELEASE OF ALL LIABILITY TO THE GREATEST EXTENT ALLOWED BY LAW AND AGREE THAT IF ANY PORTION OF THIS AGREEMENT IS HELD TO BE INVALID, THE BALANCE, NOTWITHSTANDING, SHALL CONTINUE IN FULL FORCE AND EFFECT.");
176
  print $h->br;
177
}
178
my $t = 1;
179
my @terms = map { $h->div ({ class => "lisp0", style=>"margin-bottom:1em" }, [
180
    $h->div ({ class=>"liLeft" }, $_),
181
    $h->div ({ class=>"liRight", onClick=>"document.forms['Attestation'].elements['Term$t'].click();" }, ["I Agree. ", $h->input ({ type => "checkbox", class => "attest", name => "Term".$t++, onClick=>"event.stopPropagation();" })])]) } @terms;
182
 
2 - 183
print $h->div ({ style=>"max-width:850px;" }, [
22 - 184
  $h->h3 ($policy->{name}),
185
  @terms,
186
#  $h->div ({ class => "lisp0"}, [
187
#    $h->div ({ class=>"liLeft" }, "I have read and agree with the WFTDA Release and Waiver of Liability, Assumption of Risk, and Indemnity Agreement *Click here to download the Release and Waiver of Liability form."),
188
#    $h->div ({ class=>"liRight" }, ["I Agree. ", $h->input ({ type => "checkbox", class => "attest", name => "Term1" })])]),
189
#  $h->h3 ("Accidental Medical"),
190
#  $h->div ({ class => "lisp0", style=>"margin-bottom:1em"}, [
191
#    $h->div ({ class=>"liLeft" }, "I am 18 or older."),
192
#    $h->div ({ class=>"liRight" }, ["I Agree. ", $h->input ({ type => "checkbox", class => "attest", name => "Term2" })])]),
193
#  $h->div ({ class => "lisp0", style=>"margin-bottom:1em"}, [
194
#    $h->div ({ class=>"liLeft" }, "I am obtaining coverage for myself (I may not acquire coverage on behalf of someone else)."),
195
#    $h->div ({ class=>"liRight" }, ["I Agree. ", $h->input ({ type => "checkbox", class => "attest", name => "Term3" })])]),
196
#  $h->div ({ class => "lisp0", style=>"margin-bottom:1em"}, [
197
#    $h->div ({ class=>"liLeft" }, "My league has WFTDA Insurance. I understand I must obtain the same coverage as my league. If I am not affiliated with a league, I understand coverage is only valid when I skate with a WFTDA insured league."),
198
#    $h->div ({ class=>"liRight" }, ["I Agree. ", $h->input ({ type => "checkbox", class => "attest", name => "Term4" })])]),
199
#  $h->div ({ class => "lisp0", style=>"margin-bottom:1em"}, [
200
#    $h->div ({ class=>"liLeft" }, "I understand in order for insurance coverage to be in effect, I must adhere to the applicable Risk Management Guidelines and/or rules for the activity in which I am participating. Failure to do so will result in denial of claims and may result in revocation of coverage."),
201
#    $h->div ({ class=>"liRight" }, ["I Agree. ", $h->input ({ type => "checkbox", class => "attest", name => "Term5" })])]),
202
#  $h->div ({ class => "lisp0", style=>"margin-bottom:1em"}, [
203
#    $h->div ({ class=>"liLeft" }, "I understand if I am injured, I am responsible to submit an injury report, and must do so within 14 days of the date of my injury. I can request injury reports from insurance\@wftda.com. My league may assist me with this, but ultimately, it is my responsibility. Failure to submit an injury report within the allowable grace period will result in the denial of my claim."),
204
#    $h->div ({ class=>"liRight" }, ["I Agree. ", $h->input ({ type => "checkbox", class => "attest", name => "Term6" })])]),
205
#  $h->div ({ class => "lisp0", style=>"margin-bottom:1em"}, [
206
#    $h->div ({ class=>"liLeft" }, "I understand coverage is non-refundable and non-transferable. Coverage only transfers to other WFTDA Insured leagues."),
207
#    $h->div ({ class=>"liRight" }, ["I Agree. ", $h->input ({ type => "checkbox", class => "attest", name => "Term7" })])]),
208
#  $h->div ({ class => "lisp0", style=>"margin-bottom:1em"}, [
209
#    $h->div ({ class=>"liLeft" }, "I understand reckless or negligent behavior that is outside of the confines of game-related contact, including fighting, punching, kicking, or intentional injury, that puts you or any participant at risk is not tolerated, and is grounds for immediate termination of coverage."),
210
#    $h->div ({ class=>"liRight" }, ["I Agree. ", $h->input ({ type => "checkbox", class => "attest", name => "Term8" })])]),
2 - 211
  $h->div ({ class => "lisp0", style=>"margin-bottom:1em"}, [
22 - 212
    $h->div ({ class=>"liLeft" }, "Electronic Signature:&nbsp;&nbsp;".$h->input ({ type => "text", name => "esignature", id => "esig", value => "<enter civil name>", onFocus => "this.value = '';"})),
213
    $h->div ({ class=>"liRight" }, $h->input ({ type => "submit", id=>"submitButton", disabled=>[], onClick => "if (esignature.value == '' || esignature.value == '<enter civil name>') { alert('Please type your civil name in the Electronic Signature box'); document.getElementById('esig').focus(); return false; }" }))]),
2 - 214
 
215
]);
216
 
217
print<<javascript;
218
 
219
<script>
220
  document.addEventListener('DOMContentLoaded', function() {
221
    const checkboxes = document.querySelectorAll('.attest');
222
    const submitButton = document.getElementById('submitButton');
223
 
224
    function checkAllCheckboxes() {
225
        let allChecked = true;
226
        checkboxes.forEach(checkbox => {
227
            if (!checkbox.checked) {
228
                allChecked = false;
229
            }
230
        });
231
        submitButton.disabled = !allChecked;
232
    }
233
 
234
    checkboxes.forEach(checkbox => {
235
        checkbox.addEventListener('change', checkAllCheckboxes);
236
    });
237
 
238
    // Initial check when the page loads
239
    checkAllCheckboxes();
240
  });
241
 
242
  const hiddenField = document.getElementById('TZFIELD');
243
  hiddenField.value = Intl.DateTimeFormat().resolvedOptions().timeZone;
244
</script>
245
 
246
javascript
247
 
248
print $h->close ("form"), $h->close ("body"), $h->close ("html");
249
 
250
 
251
 
252
 
253
sub sendToSquare {
254
  my $esig = shift || return "ERROR: No Signature";
22 - 255
  my $terms = shift // return "ERROR: No Term Count";
256
  my $policy = shift // return "ERROR: No Policy ID";
2 - 257
  my $user_tz = param ("timezone") // 'America/Chicago';
258
 
259
  use REST::Client;
260
  use JSON;
261
  use Data::Dumper;
262
  use UUID::Tiny qw(create_UUID_as_string UUID_V4);
263
 
264
  my $uuid_v4_string = create_UUID_as_string(UUID_V4);
22 - 265
#  my $policy = getPolicyByID (1);
2 - 266
 
267
  my $client = REST::Client->new();
268
  $client->setHost (getSetting ("SQUARE_API_HOST"));
269
 
270
  my $headers = {
271
    "Authorization" => 'Bearer '.getSetting ("SQUARE_AUTH_TOKEN"),
272
    "Square-Version" => "2025-10-16",
273
    "Content-Type" => "application/json",
274
    };
275
 
276
  my $body = {
277
    "idempotency_key" => "$uuid_v4_string",
278
    "checkout_options" => {
22 - 279
      "redirect_url"  => url ( -base => 1 )."/confirmation",
2 - 280
      "enable_coupon" => JSON::false
281
    },
282
#    "quick_pay" => {
283
#      "name" => $policy->{name},
284
#      "price_money" => {
285
#        "amount" => $policy->{fee} * 100,
286
#        "currency" => "USD"
287
#      },
288
#      "location_id" => getSetting ("SQUARE_LOCATION_ID")
289
#    },
290
    "order" => {
291
    	"customer_id" => "$user->{id}",
292
      "location_id" => getSetting ("SQUARE_LOCATION_ID"),
293
      "discounts"   => isWFTDAMember ($user->{id}) ? [{
294
        "name"  => "WFTDA Member Discount",
295
        "scope" => "ORDER",
296
#        "type" => "FIXED_AMOUNT",
297
        "amount_money" => {
22 - 298
#          "amount"   => getSetting ("WFTDA_MEMBER_DISCOUNT") * 100,
299
          "amount"   => $policy->{member_discount} * 100,
2 - 300
          "currency" => "USD"
301
        }
302
      }] : [],
303
      "line_items" => [{
304
        "name" => $policy->{name},
305
        "quantity" => "1",
306
        "base_price_money" => {
307
          "amount" => $policy->{fee} * 100,
308
          "currency" => "USD"
309
        },
310
      }]
311
    },
312
    "payment_note" => $user_tz
313
  };
314
  my $json_body = encode_json $body;
315
 
316
  $client->POST(
317
    '/v2/online-checkout/payment-links',
318
    $json_body,
319
    $headers
320
  );
321
  my $response = from_json($client->responseContent());
322
 
22 - 323
  warn '/v2/online-checkout/payment-links/'.$response->{payment_link}->{id};
324
  $body->{payment_link}->{checkout_options}->{redirect_url} = url ( -base => 1 )."/confirmation?order_id=".$response->{payment_link}->{order_id};
325
  $body->{payment_link}->{version} = $response->{payment_link}->{version};
326
  $json_body = encode_json $body;
327
  $client->PUT(
328
    '/v2/online-checkout/payment-links/'.$response->{payment_link}->{id},
329
    $json_body,
330
    $headers
331
  );
332
  my $response2 = from_json($client->responseContent());
333
 
334
 
335
  warn Dumper($response2);
336
 
2 - 337
#  warn $response->{payment_link}->{url};
338
 
339
  $response->{payment_link}->{created_at} =~ s/T/ /;
340
  $response->{payment_link}->{created_at} =~ s/Z$//;
341
 
342
  $dbh->do ("insert into square_order
22 - 343
             (square_id, order_id, person_id, organization_id, policy_id, url, idempotency_key, created, user_ip, amount, status, attestation)
344
             values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", undef,
2 - 345
             $response->{payment_link}->{id},
346
             $response->{payment_link}->{order_id},
347
             $user->{id},
22 - 348
             $league_id,
349
             $policy->{id},
2 - 350
             $response->{payment_link}->{url},
351
             $uuid_v4_string,
352
             $response->{payment_link}->{created_at},
353
             $ENV{REMOTE_ADDR},
354
             $policy->{fee},
355
             $response->{related_resources}->{orders}->[0]->{state},
356
             $terms." + ".$esig
357
           ) unless !$response->{payment_link}->{id};
358
 
359
  my $cgi = CGI->new();
360
  logit ($user->{id}, "Redirected to Square for payment");
361
  print $cgi->redirect ($response->{payment_link}->{url});
362
 
363
  exit;
5 - 364
}
22 - 365
 
366
sub ERROR {
367
  my $header = shift // "Unknown Error";
368
  my $text   = shift // "Something unexpectedly bad happened.";
369
  my $logmsg = shift // "Unknown Error happened while viewing the Attestation page";
370
 
371
  print header (-cookie=>CGI::Cookie->new(-name=>'PEEPSAUTH',-value=>"$cookie_string"));
372
  printRCHeader("Atttestation");
373
  print $h->close ("table");
374
  print $h->h2 ($header);
375
  print $h->div ({ style=>"max-width:450px;" }, $text, "&nbsp;");
376
  print $h->button ({onclick => "window.location.href='/';"}, "Home");
377
  print $h->close ("BODY", "HTML");
378
  logit ($user->{id}, $logmsg);
379
  exit;
380
}