Subversion Repositories PEEPS

Rev

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