Subversion Repositories PEEPS

Rev

Rev 22 | Rev 31 | Go to most recent revision | 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
56
  use tableViewer qw/ notInArray /;
57
  if (notInArray ($league_id, isLeagueAdmin ($user->{person_id}))) {
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
 
63
  ## Check to see if this is a new purchase or within the renewal period.  If neither, eligible will be empty.
64
  $active_policy = isLeagueCovered ($league_id);
65
  if ($active_policy) {
66
    $daysremaining = remainingOrgPolicyDays ($league_id, $active_policy);
67
    if ($daysremaining <= $RENEWAL_WINDOW) {
68
      $eligible = "renew";
69
    }
70
  } else {
71
    $eligible = "new";
72
  }
73
} elsif ($policy->{type} eq "personal") {
74
  if ($policy->{id} eq "4") { # They're trying to buy RollerCon Coverage...
75
    $active_policy = isPersonCovered ($user->{id}, '2026-07-11');
76
    if (!$active_policy) {
77
      ($active_policy) = $dbh->selectrow_array ("select id from coverage where policy_id = ? and person_id = ?", undef, 4, $user->{id});
78
    }
79
 
80
    ERROR ("Already Covered",
81
           "You are already covered for RollerCon 2026 by Policy $active_policy. No need to purchase.",
82
           "Attempted to purchase RollerCon 2026 covereage but not needed") unless !$active_policy;
83
 
84
  } else {
85
    ## Check to see if this is a new purchase or within the renewal period.  If neither, eligible will be empty.
86
    $active_policy = isPersonCovered ($user->{id});
87
    if ($active_policy) {
88
      $daysremaining = remainingPolicyDays ($user->{id}, $active_policy);
89
      if ($daysremaining <= $RENEWAL_WINDOW) {
90
        $eligible = "renew";
91
      }
92
    } else {
93
      $eligible = "new";
94
    }
95
  }
96
} else {
97
  ## If we're here, it means the policy type wasn't recognized...
98
  ERROR ("Something weird happened...",
99
         "The type of policy you attempted to puchase doesn't seem to exist.",
100
         "Viewed Attestation page for invalid policy type.");
2 - 101
}
102
 
22 - 103
ERROR ("Not Eligible for Renewal",
104
       "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.",
105
       "Viewed Attestation page but wasn't eligible to renew.") unless $eligible;
106
 
107
## Check to see if there is an abandoned checkout that needs to be completed...
108
my ($abandoned_checkout) = $policy->{type} eq "league" ?
109
      $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") :
110
      $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 - 111
if ($abandoned_checkout) {
112
  my $cgi = CGI->new();
22 - 113
  logit ($user->{id}, "Returned to abandoned $policy->{type} policy checkout.");
2 - 114
  print $cgi->redirect ($abandoned_checkout);
115
  exit;
116
}
117
 
22 - 118
## Get the terms for attestion...
119
my @terms = map { @{$_} } @{ $dbh->selectall_arrayref ("select term from attestation where id = ? order by attestation.index", undef, $policy->{attestation_id}) };
2 - 120
my $terms;
22 - 121
foreach (1..scalar @terms) {
2 - 122
  $terms++ if param ("Term".$_);
123
}
124
my $esig = param ("esignature") // "";
125
 
22 - 126
if ($terms == scalar @terms and $esig =~ /^\w+$/) {
2 - 127
  # All of the checkboxes were accepted and an eSignature was entered.
128
  # Create the Square Checkout link and redirect the user there.
22 - 129
  logit ($user->{id}, "Submitted purchase attestation for $policy->{type} $policy->{name} with all terms accepted and esignature entered.");
130
  sendToSquare ($esig, $terms, $policy);
2 - 131
  exit;
132
}
133
 
134
print header (-cookie=>CGI::Cookie->new(-name=>'PEEPSAUTH',-value=>"$cookie_string"));
135
printRCHeader("Atttestation");
136
 
137
print $h->close ("table");
138
 
139
print $h->open ("form", { name => "Attestation", method => "post", action => url });
140
 
141
print $h->input ( { type => "hidden", name => "timezone", id => "TZFIELD", value => "" } );
22 - 142
print $h->input ( { type => "hidden", name => "policy", value => $policy_id } );
143
print $h->input ( { type => "hidden", name => "league", value => $league_id } ) unless !$league_id;
2 - 144
 
145
 
22 - 146
print $h->div ({ class=>"error" }, "&nbsp;", "ERROR: You must agree to each term to continue.") if $terms and $terms < scalar @terms;
2 - 147
 
22 - 148
print $h->h2 ("Policy Details");
149
print $h->div ({ style=>"max-width:850px;" }, $h->ul ([$h->li (map { "$_: $policy->{$_}" } keys %{$policy})]));
2 - 150
 
22 - 151
print map { $h->div ({ class => "lisp0" }, [
152
    $h->div ({ class=>"liRight", style=>"margin-right:.5em" }, ucfirst ($_).": "),
153
    $h->div ({ class=>"liLeft" }, $policy->{$_} )]) } qw/ name type fee /;
2 - 154
 
22 - 155
 
156
 
157
if ($policy->{type} eq "personal") {
158
  print $h->h2 ("Release and Waiver of Liability");
159
 
160
  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:");
161
 
162
  print $h->ol ({ style=>"max-width:850px;" }, [
163
    $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."),
164
    $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."),
165
    $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."),
166
    ]);
167
 
168
  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.");
169
  print $h->br;
170
}
171
my $t = 1;
172
my @terms = map { $h->div ({ class => "lisp0", style=>"margin-bottom:1em" }, [
173
    $h->div ({ class=>"liLeft" }, $_),
174
    $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;
175
 
2 - 176
print $h->div ({ style=>"max-width:850px;" }, [
22 - 177
  $h->h3 ($policy->{name}),
178
  @terms,
179
#  $h->div ({ class => "lisp0"}, [
180
#    $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."),
181
#    $h->div ({ class=>"liRight" }, ["I Agree. ", $h->input ({ type => "checkbox", class => "attest", name => "Term1" })])]),
182
#  $h->h3 ("Accidental Medical"),
183
#  $h->div ({ class => "lisp0", style=>"margin-bottom:1em"}, [
184
#    $h->div ({ class=>"liLeft" }, "I am 18 or older."),
185
#    $h->div ({ class=>"liRight" }, ["I Agree. ", $h->input ({ type => "checkbox", class => "attest", name => "Term2" })])]),
186
#  $h->div ({ class => "lisp0", style=>"margin-bottom:1em"}, [
187
#    $h->div ({ class=>"liLeft" }, "I am obtaining coverage for myself (I may not acquire coverage on behalf of someone else)."),
188
#    $h->div ({ class=>"liRight" }, ["I Agree. ", $h->input ({ type => "checkbox", class => "attest", name => "Term3" })])]),
189
#  $h->div ({ class => "lisp0", style=>"margin-bottom:1em"}, [
190
#    $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."),
191
#    $h->div ({ class=>"liRight" }, ["I Agree. ", $h->input ({ type => "checkbox", class => "attest", name => "Term4" })])]),
192
#  $h->div ({ class => "lisp0", style=>"margin-bottom:1em"}, [
193
#    $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."),
194
#    $h->div ({ class=>"liRight" }, ["I Agree. ", $h->input ({ type => "checkbox", class => "attest", name => "Term5" })])]),
195
#  $h->div ({ class => "lisp0", style=>"margin-bottom:1em"}, [
196
#    $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."),
197
#    $h->div ({ class=>"liRight" }, ["I Agree. ", $h->input ({ type => "checkbox", class => "attest", name => "Term6" })])]),
198
#  $h->div ({ class => "lisp0", style=>"margin-bottom:1em"}, [
199
#    $h->div ({ class=>"liLeft" }, "I understand coverage is non-refundable and non-transferable. Coverage only transfers to other WFTDA Insured leagues."),
200
#    $h->div ({ class=>"liRight" }, ["I Agree. ", $h->input ({ type => "checkbox", class => "attest", name => "Term7" })])]),
201
#  $h->div ({ class => "lisp0", style=>"margin-bottom:1em"}, [
202
#    $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."),
203
#    $h->div ({ class=>"liRight" }, ["I Agree. ", $h->input ({ type => "checkbox", class => "attest", name => "Term8" })])]),
2 - 204
  $h->div ({ class => "lisp0", style=>"margin-bottom:1em"}, [
22 - 205
    $h->div ({ class=>"liLeft" }, "Electronic Signature:&nbsp;&nbsp;".$h->input ({ type => "text", name => "esignature", id => "esig", value => "<enter civil name>", onFocus => "this.value = '';"})),
206
    $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 - 207
 
208
]);
209
 
210
print<<javascript;
211
 
212
<script>
213
  document.addEventListener('DOMContentLoaded', function() {
214
    const checkboxes = document.querySelectorAll('.attest');
215
    const submitButton = document.getElementById('submitButton');
216
 
217
    function checkAllCheckboxes() {
218
        let allChecked = true;
219
        checkboxes.forEach(checkbox => {
220
            if (!checkbox.checked) {
221
                allChecked = false;
222
            }
223
        });
224
        submitButton.disabled = !allChecked;
225
    }
226
 
227
    checkboxes.forEach(checkbox => {
228
        checkbox.addEventListener('change', checkAllCheckboxes);
229
    });
230
 
231
    // Initial check when the page loads
232
    checkAllCheckboxes();
233
  });
234
 
235
  const hiddenField = document.getElementById('TZFIELD');
236
  hiddenField.value = Intl.DateTimeFormat().resolvedOptions().timeZone;
237
</script>
238
 
239
javascript
240
 
241
print $h->close ("form"), $h->close ("body"), $h->close ("html");
242
 
243
 
244
 
245
 
246
sub sendToSquare {
247
  my $esig = shift || return "ERROR: No Signature";
22 - 248
  my $terms = shift // return "ERROR: No Term Count";
249
  my $policy = shift // return "ERROR: No Policy ID";
2 - 250
  my $user_tz = param ("timezone") // 'America/Chicago';
251
 
252
  use REST::Client;
253
  use JSON;
254
  use Data::Dumper;
255
  use UUID::Tiny qw(create_UUID_as_string UUID_V4);
256
 
257
  my $uuid_v4_string = create_UUID_as_string(UUID_V4);
22 - 258
#  my $policy = getPolicyByID (1);
2 - 259
 
260
  my $client = REST::Client->new();
261
  $client->setHost (getSetting ("SQUARE_API_HOST"));
262
 
263
  my $headers = {
264
    "Authorization" => 'Bearer '.getSetting ("SQUARE_AUTH_TOKEN"),
265
    "Square-Version" => "2025-10-16",
266
    "Content-Type" => "application/json",
267
    };
268
 
269
  my $body = {
270
    "idempotency_key" => "$uuid_v4_string",
271
    "checkout_options" => {
22 - 272
      "redirect_url"  => url ( -base => 1 )."/confirmation",
2 - 273
      "enable_coupon" => JSON::false
274
    },
275
#    "quick_pay" => {
276
#      "name" => $policy->{name},
277
#      "price_money" => {
278
#        "amount" => $policy->{fee} * 100,
279
#        "currency" => "USD"
280
#      },
281
#      "location_id" => getSetting ("SQUARE_LOCATION_ID")
282
#    },
283
    "order" => {
284
    	"customer_id" => "$user->{id}",
285
      "location_id" => getSetting ("SQUARE_LOCATION_ID"),
286
      "discounts"   => isWFTDAMember ($user->{id}) ? [{
287
        "name"  => "WFTDA Member Discount",
288
        "scope" => "ORDER",
289
#        "type" => "FIXED_AMOUNT",
290
        "amount_money" => {
22 - 291
#          "amount"   => getSetting ("WFTDA_MEMBER_DISCOUNT") * 100,
292
          "amount"   => $policy->{member_discount} * 100,
2 - 293
          "currency" => "USD"
294
        }
295
      }] : [],
296
      "line_items" => [{
297
        "name" => $policy->{name},
298
        "quantity" => "1",
299
        "base_price_money" => {
300
          "amount" => $policy->{fee} * 100,
301
          "currency" => "USD"
302
        },
303
      }]
304
    },
305
    "payment_note" => $user_tz
306
  };
307
  my $json_body = encode_json $body;
308
 
309
  $client->POST(
310
    '/v2/online-checkout/payment-links',
311
    $json_body,
312
    $headers
313
  );
314
  my $response = from_json($client->responseContent());
315
 
22 - 316
  warn '/v2/online-checkout/payment-links/'.$response->{payment_link}->{id};
317
  $body->{payment_link}->{checkout_options}->{redirect_url} = url ( -base => 1 )."/confirmation?order_id=".$response->{payment_link}->{order_id};
318
  $body->{payment_link}->{version} = $response->{payment_link}->{version};
319
  $json_body = encode_json $body;
320
  $client->PUT(
321
    '/v2/online-checkout/payment-links/'.$response->{payment_link}->{id},
322
    $json_body,
323
    $headers
324
  );
325
  my $response2 = from_json($client->responseContent());
326
 
327
 
328
  warn Dumper($response2);
329
 
2 - 330
#  warn $response->{payment_link}->{url};
331
 
332
  $response->{payment_link}->{created_at} =~ s/T/ /;
333
  $response->{payment_link}->{created_at} =~ s/Z$//;
334
 
335
  $dbh->do ("insert into square_order
22 - 336
             (square_id, order_id, person_id, organization_id, policy_id, url, idempotency_key, created, user_ip, amount, status, attestation)
337
             values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", undef,
2 - 338
             $response->{payment_link}->{id},
339
             $response->{payment_link}->{order_id},
340
             $user->{id},
22 - 341
             $league_id,
342
             $policy->{id},
2 - 343
             $response->{payment_link}->{url},
344
             $uuid_v4_string,
345
             $response->{payment_link}->{created_at},
346
             $ENV{REMOTE_ADDR},
347
             $policy->{fee},
348
             $response->{related_resources}->{orders}->[0]->{state},
349
             $terms." + ".$esig
350
           ) unless !$response->{payment_link}->{id};
351
 
352
  my $cgi = CGI->new();
353
  logit ($user->{id}, "Redirected to Square for payment");
354
  print $cgi->redirect ($response->{payment_link}->{url});
355
 
356
  exit;
5 - 357
}
22 - 358
 
359
sub ERROR {
360
  my $header = shift // "Unknown Error";
361
  my $text   = shift // "Something unexpectedly bad happened.";
362
  my $logmsg = shift // "Unknown Error happened while viewing the Attestation page";
363
 
364
  print header (-cookie=>CGI::Cookie->new(-name=>'PEEPSAUTH',-value=>"$cookie_string"));
365
  printRCHeader("Atttestation");
366
  print $h->close ("table");
367
  print $h->h2 ($header);
368
  print $h->div ({ style=>"max-width:450px;" }, $text, "&nbsp;");
369
  print $h->button ({onclick => "window.location.href='/';"}, "Home");
370
  print $h->close ("BODY", "HTML");
371
  logit ($user->{id}, $logmsg);
372
  exit;
373
}