Rev 5 | Blame | Compare with Previous | Last modification | View Log | RSS feed
#!/usr/bin/perl# Redirect error messages to a log of my choosing. (it's annoying to filter for errors in the shared env)#my $error_log_path = $ENV{SERVER_NAME} eq "volunteers.rollercon.com" ? "/home3/rollerco/logs/" : "/tmp/";#close STDERR;#open STDERR, '>>', $error_log_path.'vorc_error.log' or warn "Failed to open redirected logfile ($0): $!";#warn "Redirecting errors to ${error_log_path}vorc_error.log";use strict;use PEEPS;use CGI qw/param header start_html url/;use CGI::Cookie;our $h = HTML::Tiny->new( mode => 'html' );my $dbh = getRCDBH ();$ENV{HTTPS} = 'ON' if $ENV{SERVER_NAME} =~ /^peeps/;$dbh->{PrintError} = 1;my $cookie_string = authenticate (1) || die;my ($EML, $PWD, $LVL) = split /&/, $cookie_string;my $user = $ORCUSER;#my $activated = $ORCUSER->{access};print header (-cookie=>CGI::Cookie->new(-name=>'PEEPSAUTH',-value=>"$cookie_string"));use DateTime;#use DateTime::Format::Strptime;my $dt = DateTime->today;$dt =~ s/T00\:00\:00$//;$user->{policy} = isPersonCovered ($user->{id});if ($user->{policy}) {my $daysremaining = remainingPolicyDays ($user->{id}, $user->{policy});printRCHeader("Atttestation");print $h->close ("table");print $h->h2 ("Not Eligible for Renewal");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.", " ");print $h->button ({onclick => "window.location.href='/';"}, "Home");print $h->close ("BODY", "HTML");logit ($user->{id}, "Viewed Purchase Confirmation page but wasn't eligible to renew.");exit;}## Check for DRAFT payments for the user, get the last one...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});if (!$checkout->{square_id}) {printRCHeader("Atttestation");print $h->div ({class=>"error"}, "ERROR: No open payments found for user!", " ", $h->button ({onclick => "window.location.href='/';"}, "Home"));logit ($user->{id}, "Viewed Purchase Confirmation page but didn't have any open payments.");exit;}use REST::Client;use JSON;use Data::Dumper;my $client = REST::Client->new();my $headers = {"Authorization" => 'Bearer '.getSetting ("SQUARE_AUTH_TOKEN"),"Content-Type" => "application/json",};$client->setHost (getSetting ("SQUARE_API_HOST"));$client->GET('/v2/orders/'.$checkout->{order_id},$headers);my $response = from_json($client->responseContent());#warn Dumper($response);if ($response->{order}->{state} eq "DRAFT") {# Checkout hasn't complete yet...printRCHeader("Atttestation");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."));logit ($user->{id}, "Viewed Confirmation page with an open payment.");} elsif ($response->{order}->{state} eq "OPEN") {# Checkout completed...# create start and end dates based on the user's timezonemy $user_tz = $response->{order}->{tenders}->[0]->{note} || 'America/Chicago';# warn $user_tz;# Parse the UTC timestamp and set its timezone to 'UTC'use DateTime::Format::Strptime qw( );my $format = DateTime::Format::Strptime->new(pattern => '%Y-%m-%dT%H:%M:%SZ',strict => 1,time_zone => "UTC",# on_error => "croak",);my $dt = $format->parse_datetime( $response->{order}->{tenders}->[0]->{created_at} );$dt->set_time_zone( $user_tz );my $adjusted_created_at = $dt->strftime( '%Y-%m-%d %H:%M:%S' );# "Fix" UTC format for MySQL$response->{order}->{tenders}->[0]->{created_at} =~ s/T/ /;$response->{order}->{tenders}->[0]->{created_at} =~ s/Z$//;# Update the square order status and add the exact time of payment.$dbh->do ("update square_order set status = ?, payment_id = ?, payment_time = ? where square_id = ?", undef,"PAID",$response->{order}->{tenders}->[0]->{id},$response->{order}->{tenders}->[0]->{created_at},$checkout->{square_id});# Update the users coverage recordmy $policy_id = isPersonCovered ($user->{id});if ($policy_id) {# extend existing coverage$dbh->do ("update coverage set end = date_add(end, INTERVAL 1 YEAR) where id = ?", undef, $policy_id);} else {# insert new coveragemy ($new_policy_id) = $dbh->selectrow_array ("select max(id)+1 from coverage");$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,$user->{id},$response->{order}->{line_items}->[0]->{name},$response->{order}->{tenders}->[0]->{amount_money}->{amount} / 100,$response->{order}->{tenders}->[0]->{created_at},$adjusted_created_at,$adjusted_created_at,1);$policy_id = isPersonCovered ($user->{id});printRCHeader("Purchase Confirmation");print $h->close ("table");if ($policy_id) {my $daysremaining = remainingPolicyDays ($user->{id}, $policy_id);print $h->h2 ("Congratulations!");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.", " ");print $h->button ({onclick => "window.location.href='/';"}, "Home");print $h->close ("BODY", "HTML");logit ($user->{id}, "Successfully renewed insurance.");exit;} else {print $h->h2 ("That's weird!");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.", " ");print $h->button ({onclick => "window.location.href='/';"}, "Home");print $h->close ("BODY", "HTML");logit ($user->{id}, "ERROR: Checkout complete, but PEEPS coverage failed to update.");exit;}}} else {# Something weird has happened.printRCHeader("Purchase Confirmation");print $h->h2 ("Whoa, ERROR!");print $h->div ("Something weird has happened. You should probably email peeps\@wftdi.com and tell them to investigate.", " ");print $h->button ({onclick => "window.location.href='/';"}, "Home");logit ($user->{id}, "ERROR: Something really weird happend during checkout confirmation.");}