| 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/;
|
|
|
12 |
use CGI::Cookie;
|
|
|
13 |
our $h = HTML::Tiny->new( mode => 'html' );
|
|
|
14 |
my $dbh = getRCDBH ();
|
|
|
15 |
$dbh->{PrintError} = 1;
|
|
|
16 |
|
|
|
17 |
my $cookie_string = authenticate (1) || die;
|
|
|
18 |
my ($EML, $PWD, $LVL) = split /&/, $cookie_string;
|
|
|
19 |
my $user = $ORCUSER;
|
|
|
20 |
#my $activated = $ORCUSER->{access};
|
|
|
21 |
|
|
|
22 |
print header (-cookie=>CGI::Cookie->new(-name=>'PEEPSAUTH',-value=>"$cookie_string"));
|
|
|
23 |
|
|
|
24 |
use DateTime;
|
|
|
25 |
#use DateTime::Format::Strptime;
|
|
|
26 |
my $dt = DateTime->today;
|
|
|
27 |
$dt =~ s/T00\:00\:00$//;
|
|
|
28 |
|
|
|
29 |
$user->{policy} = isPersonCovered ($user->{id});
|
|
|
30 |
if ($user->{policy}) {
|
|
|
31 |
my $daysremaining = remainingPolicyDays ($user->{id}, $user->{policy});
|
|
|
32 |
printRCHeader("Atttestation");
|
|
|
33 |
print $h->close ("table");
|
|
|
34 |
print $h->h2 ("Not Eligible for Renewal");
|
|
|
35 |
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.", " ");
|
|
|
36 |
print $h->button ({onclick => "window.location.href='/';"}, "Home");
|
|
|
37 |
print $h->close ("BODY", "HTML");
|
|
|
38 |
logit ($user->{id}, "Viewed Purchase Confirmation page but wasn't eligible to renew.");
|
|
|
39 |
exit;
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
## Check for DRAFT payments for the user, get the last one...
|
|
|
43 |
|
|
|
44 |
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});
|
|
|
45 |
|
|
|
46 |
if (!$checkout->{square_id}) {
|
|
|
47 |
printRCHeader("Atttestation");
|
|
|
48 |
print $h->div ({class=>"error"}, "ERROR: No open payments found for user!", " ", $h->button ({onclick => "window.location.href='/';"}, "Home"));
|
|
|
49 |
logit ($user->{id}, "Viewed Purchase Confirmation page but didn't have any open payments.");
|
|
|
50 |
exit;
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
use REST::Client;
|
|
|
54 |
use JSON;
|
|
|
55 |
use Data::Dumper;
|
|
|
56 |
|
|
|
57 |
my $client = REST::Client->new();
|
|
|
58 |
|
|
|
59 |
my $headers = {
|
|
|
60 |
"Authorization" => 'Bearer '.getSetting ("SQUARE_AUTH_TOKEN"),
|
|
|
61 |
"Content-Type" => "application/json",
|
|
|
62 |
};
|
|
|
63 |
|
|
|
64 |
$client->setHost (getSetting ("SQUARE_API_HOST"));
|
|
|
65 |
|
|
|
66 |
$client->GET(
|
|
|
67 |
'/v2/orders/'.$checkout->{order_id},
|
|
|
68 |
$headers
|
|
|
69 |
);
|
|
|
70 |
my $response = from_json($client->responseContent());
|
|
|
71 |
#warn Dumper($response);
|
|
|
72 |
|
|
|
73 |
|
|
|
74 |
if ($response->{order}->{state} eq "DRAFT") {
|
|
|
75 |
# Checkout hasn't complete yet...
|
|
|
76 |
printRCHeader("Atttestation");
|
|
|
77 |
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."));
|
|
|
78 |
logit ($user->{id}, "Viewed Confirmation page with an open payment.");
|
|
|
79 |
} elsif ($response->{order}->{state} eq "OPEN") {
|
|
|
80 |
# Checkout completed...
|
|
|
81 |
# create start and end dates based on the user's timezone
|
|
|
82 |
my $user_tz = $response->{order}->{tenders}->[0]->{note} || 'America/Chicago';
|
|
|
83 |
# warn $user_tz;
|
|
|
84 |
# Parse the UTC timestamp and set its timezone to 'UTC'
|
|
|
85 |
use DateTime::Format::Strptime qw( );
|
|
|
86 |
my $format = DateTime::Format::Strptime->new(
|
|
|
87 |
pattern => '%Y-%m-%dT%H:%M:%SZ',
|
|
|
88 |
strict => 1,
|
|
|
89 |
time_zone => "UTC",
|
|
|
90 |
# on_error => "croak",
|
|
|
91 |
);
|
|
|
92 |
my $dt = $format->parse_datetime( $response->{order}->{tenders}->[0]->{created_at} );
|
|
|
93 |
$dt->set_time_zone( $user_tz );
|
|
|
94 |
my $adjusted_created_at = $dt->strftime( '%Y-%m-%d %H:%M:%S' );
|
|
|
95 |
|
|
|
96 |
# "Fix" UTC format for MySQL
|
|
|
97 |
$response->{order}->{tenders}->[0]->{created_at} =~ s/T/ /;
|
|
|
98 |
$response->{order}->{tenders}->[0]->{created_at} =~ s/Z$//;
|
|
|
99 |
|
|
|
100 |
# Update the square order status and add the exact time of payment.
|
|
|
101 |
$dbh->do ("update square_order set status = ?, payment_id = ?, payment_time = ? where square_id = ?", undef,
|
|
|
102 |
"PAID",
|
|
|
103 |
$response->{order}->{tenders}->[0]->{id},
|
|
|
104 |
$response->{order}->{tenders}->[0]->{created_at},
|
|
|
105 |
$checkout->{square_id}
|
|
|
106 |
);
|
|
|
107 |
|
|
|
108 |
# Update the users coverage record
|
|
|
109 |
my $policy_id = isPersonCovered ($user->{id});
|
|
|
110 |
if ($policy_id) {
|
|
|
111 |
# extend existing coverage
|
|
|
112 |
$dbh->do ("update coverage set end = date_add(end, INTERVAL 1 YEAR) where id = ?", undef, $policy_id);
|
|
|
113 |
} else {
|
|
|
114 |
# insert new coverage
|
|
|
115 |
my ($new_policy_id) = $dbh->selectrow_array ("select max(id)+1 from coverage");
|
|
|
116 |
$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,
|
|
|
117 |
$user->{id},
|
|
|
118 |
$response->{order}->{line_items}->[0]->{name},
|
|
|
119 |
$response->{order}->{tenders}->[0]->{amount_money}->{amount} / 100,
|
|
|
120 |
$response->{order}->{tenders}->[0]->{created_at},
|
|
|
121 |
$adjusted_created_at,
|
|
|
122 |
$adjusted_created_at,
|
|
|
123 |
1
|
|
|
124 |
);
|
|
|
125 |
$policy_id = isPersonCovered ($user->{id});
|
|
|
126 |
|
|
|
127 |
printRCHeader("Purchase Confirmation");
|
|
|
128 |
print $h->close ("table");
|
|
|
129 |
if ($policy_id) {
|
|
|
130 |
my $daysremaining = remainingPolicyDays ($user->{id}, $policy_id);
|
|
|
131 |
print $h->h2 ("Congratulations!");
|
|
|
132 |
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.", " ");
|
|
|
133 |
print $h->button ({onclick => "window.location.href='/';"}, "Home");
|
|
|
134 |
print $h->close ("BODY", "HTML");
|
|
|
135 |
logit ($user->{id}, "Successfully renewed insurance.");
|
|
|
136 |
exit;
|
|
|
137 |
} else {
|
|
|
138 |
print $h->h2 ("That's weird!");
|
|
|
139 |
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.", " ");
|
|
|
140 |
print $h->button ({onclick => "window.location.href='/';"}, "Home");
|
|
|
141 |
print $h->close ("BODY", "HTML");
|
|
|
142 |
logit ($user->{id}, "ERROR: Checkout complete, but PEEPS coverage failed to update.");
|
|
|
143 |
exit;
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
} else {
|
|
|
149 |
# Something weird has happened.
|
|
|
150 |
printRCHeader("Purchase Confirmation");
|
|
|
151 |
print $h->h2 ("Whoa, ERROR!");
|
|
|
152 |
print $h->div ("Something weird has happened. You should probably email peeps\@wftdi.com and tell them to investigate.", " ");
|
|
|
153 |
print $h->button ({onclick => "window.location.href='/';"}, "Home");
|
|
|
154 |
logit ($user->{id}, "ERROR: Something really weird happend during checkout confirmation.");
|
|
|
155 |
}
|
|
|
156 |
|