-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.pl
executable file
·913 lines (769 loc) · 30.4 KB
/
app.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
#!/usr/bin/env perl
use FindBin;
use lib "$FindBin::Bin/local/lib/perl5";
use Mojolicious::Lite;
use Mojo::UserAgent;
use Data::Dumper;
use DateTime;
use Mojo::URL;
use Try::Tiny;
use Support::Schema;
use XML::Mini::Document;
use Mojo::Util qw(b64_encode url_escape url_unescape hmac_sha1_sum);
use Crypt::CBC;
use MIME::Base64::URLSafe;
use Email::Valid;
use Data::Dumper::HTML qw(dumper_html);
use JSON::XS;
use XML::Hash;
plugin('DefaultHelpers');
plugin 'mail';
my $config = plugin 'JSONConfig';
plugin 'Util::RandomString' => {
entropy => 256,
printable => {
alphabet => '2345679bdfhmnprtFGHJLMNPRT',
length => 20
}
};
my $ua = Mojo::UserAgent->new;
my $url = Mojo::URL->new;
my $subdomain = $config->{'subdomain'};
my $domain = $subdomain . '.recurly.com/v2/';
my $apikey = $config->{'privatekey'};
my $API = 'https://' . $apikey . ':@' . $domain;
helper schema => sub {
my $schema = Support::Schema->connect( $config->{'pg_dsn'},
$config->{'pg_user'}, $config->{'pg_pass'}, );
return $schema;
};
helper find_or_new => sub {
my $self = shift;
my $doc = shift;
my $dom = shift;
my $trans_type = shift;
my $params = shift;
my $original_params = shift;
my $dbh = $self->schema();
my $result;
try {
$result = $dbh->txn_do(
sub {
my $rs = $dbh->resultset( 'Transaction' )
->find_or_new( {%$doc} );
unless ( $rs->in_storage ) {
$rs->insert;
}
return $rs;
}
);
}
catch {
$self->app->log->warn( $_ );
};
my $hashref = {};
$doc->{'id'} = $result->id;
if ($doc) {
$hashref-> {"local"} = $doc;
}
if ($dom && $trans_type) {
$hashref->{$trans_type} = $dom;
}
if ($params) {
$hashref->{'params'} = $params;
}
if ($original_params) {
$hashref->{'original_params'} = $original_params;
}
$self->app->log->info("dump of hashref");
$self->app->log->info( Dumper($hashref));
$self->app->log->info("end dump of hashref");
my $drupal_endpoint = $config->{'drupal_endpoint'};
my $res = $ua->post( $config->{'drupal_endpoint'} => { Accept => '*/*' } => json => $hashref);
$self->app->log->info("return from drupal dump:");
$self->app->log->info( Dumper($res));
$self->app->log->info("end drupal dump:");
$self->app->log->info( "result dump");
$self->app->log->info( Dumper($result));
$self->app->log->info( "end result dump");
return $result;
};
helper search_records => sub {
my $self = shift;
my $resultset = shift;
my $search = shift;
my $schema = $self->schema;
my $rs = $schema->resultset( $resultset )->search( $search );
return $rs;
};
helper recurly_get_plans => sub {
my $self = shift;
my $filter = shift;
my $res = $ua->get( $API . '/plans?per_page=200' => { Accept => 'application/xml' } )->res;
my $xml = $res->body;
my $dom = Mojo::DOM->new( $xml );
my $plans = $dom->find( 'plan' );
# app->log->debug("Plans " . Dumper $xml);
my $filtered = [];
foreach my $plan ( $plans->each ) { # iterate the Collection object
next unless $plan->plan_code->text =~ /$filter/;
push @$filtered, $plan;
}
@$filtered = sort {
$b->unit_amount_in_cents->CAD->text <=>
$a->unit_amount_in_cents->CAD->text
} @$filtered;
return $filtered;
};
helper recurly_get_account_code => sub {
my $self = shift;
my $account = shift;
my $account_href = $account->{'href'};
my $account_code = $account_href;
$account_code =~ s!https://$subdomain\.recurly\.com/v2/accounts/!!;
return $account_code;
};
helper recurly_get_account_details => sub {
my $self = shift;
my $account_code = shift;
my $res
= $ua->get( $API
. '/accounts/'
. $account_code => { Accept => 'application/xml' } )->res;
my $xml = $res->body;
my $dom = Mojo::DOM->new( $xml );
return $dom;
};
helper recurly_get_billing_details => sub {
my $self = shift;
my $account_code = shift;
my $res
= $ua->get( $API
. '/accounts/'
. $account_code
. '/billing_info' => { Accept => 'application/xml' } )->res;
my $xml = $res->body;
my $dom = Mojo::DOM->new( $xml );
return $dom;
};
helper recurly_get_active_subs => sub {
my $self = shift;
my $account_code = shift;
my $res
= $ua->get( $API
. '/accounts/'
. $account_code
. '/subscriptions?state=active' => { Accept => 'application/xml' } )->res;
my $xml = $res->body;
my $dom = Mojo::DOM->new( $xml );
my $ub = Mojo::UserAgent->new;
my $collection = $dom->find('subscription');
my @elements = $collection->each;
if ((scalar @elements) >= 2 ) {
$ub->post($config->{'notify_url'} => json => {text => "Note: Count of subs for someone who just subscribed, account code $account_code are greater than 1, they are " . (scalar @elements) });
}
return $dom;
};
# Set the salt and initialize the cipher
my $salt = $config->{'app_secret'};
my $cipher = Crypt::CBC->new( $salt, 'Blowfish' );
helper raiser_encode => sub {
my $self = shift;
my $email = shift;
my $encrypted_data = $cipher->encrypt( $email );
my $safe_data = urlsafe_b64encode( $encrypted_data );
return $safe_data;
};
helper raiser_decode => sub {
my $self = shift;
my $safe_data = shift;
my $encrypted_data = urlsafe_b64decode( $safe_data );
my $decrypted_data = $cipher->decrypt( $encrypted_data );
return $decrypted_data;
};
group {
under [qw(GET POST)] => '/' => sub {
my $self = shift;
# Store the referrer once in the flash
my $referrer = $self->req->headers->referrer;
if ( !$self->flash( 'original_referrer' ) ) {
$self->flash( original_referrer => $referrer );
}
# Store the campaign-tracking value
my $campaign
= $self->param( 'campaign' ) || $self->flash( 'campaign' );
$self->flash( campaign => $campaign );
my $raiser = $self->param( 'raiser' ) || $self->flash( 'raiser' );
$self->flash( raiser => $raiser );
# TODO remove these two
my $onetime = $self->param( 'onetime' );
my $amount = $self->param( 'amount' );
# TODO remove this statement
if ( $self->req->method eq 'POST' && $amount =~ /\D/ ) {
$self->flash(
{ error => 'Amount needs to be a whole number',
onetime => 'onetime',
campaign => $campaign,
}
);
$self->param( { amount => '0' } );
$amount = '';
$self->redirect_to( '' );
}
# TODO remove
my $amount_in_cents;
if ( $amount ) {
$amount_in_cents = $amount * 100;
}
# TODO remove this hash
my $options = { # RecurlyJS signature options
'transaction[currency]' => 'CAD',
'transaction[amount_in_cents]' => $amount_in_cents,
'transaction[description]' =>
'Support for fact-based independent journalism at The Tyee',
};
my $plans = $self->recurly_get_plans(
$config->{'recurly_get_plans_filter'} );
my $annual_plans = $self->recurly_get_plans(
$config->{'recurly_annual_plans_filter'} );
$self->stash(
{ plans => $plans,
annual_plans => $annual_plans,
plans_onetime => $config->{'plans_onetime'},
amount => $amount,
onetime => $onetime || $self->flash( 'onetime' ),
error => $self->flash( 'error' ),
params => $self->req->query_params
}
);
};
my $ab;
any [qw(GET POST)] => '/' => sub {
my $ab;
my $self = shift;
# my $dt = DateTime->now;
# my $seconds = $dt->sec;
my $display;
my $urlstring;
my $count;
# my $params = $self->req->query_params;
# app->log->debug( "url string = $params");
# my $path = "/b" . '?' . $params;
# if ($seconds >= 29) {
# $self->redirect_to( $path);
# } else {
# $ab = 'evergreen'; # $display = "none";
# }
$ab = 'evegreen-squeeze'; # $display = "none";
$self->stash( body_id => $ab, );
$self->flash( appeal_code => $ab );
$self->stash( display => $display );
$self->flash ( original_params => $self->req->query_params);
} => 'evergreen-squeeze';
# making both of these test conditions Dec2021 so can easily ad a test if we want during campaign. Probably a waste of resources if not using later
any [qw(GET POST)] => '/dec2021' => sub {
my $ab;
my $self = shift;
my $dt = DateTime->now;
my $seconds = $dt->sec;
my $display;
if ($seconds >= 29) {
$self->redirect_to( 'b' );
} else {
$ab = 'Dec2023'; # $display = "none";
# if ($self->param( 'squeeze' ) ) {$ab = $self->param( 'evergreen-squeeze' ) ; $display = "none"; };
# if ($self->param( 'evergreen' ) ) {$ab = $self->param( 'evergreen' ) ; $display = "block"; };
$self->stash( body_id => $ab, );
$self->flash( appeal_code => $ab );
$self->stash( display => $display );
}
} => 'Dec2023';
any [qw(GET POST)] => '/b' => sub {
my $ab;
my $self = shift;
my $dt = DateTime->now;
my $seconds = $dt->sec;
my $display;
$ab = 'evergreen-squeeze';
if ($self->param( 'squeeze' ) ) {$ab = $self->param( 'evergreen-squeeze' ) ; $display = "none"; };
if ($self->param( 'evergreen' ) ) {$ab = $self->param( 'evergreen' ) ; $display = "block"; };
# $display = "block"; #undoing all the above
$self->stash( body_id => $ab, );
$self->flash( appeal_code => $ab );
$self->stash( display => $display );
} => 'evergreen-squeeze';
any [qw(GET POST)] => '/powermap' => sub {
my $self = shift;
$self->stash(
body_id => 'powermap',
appeal_code => 'powermap'
);
} => 'powermap';
any [qw(GET POST)] => '/iframe' => sub {
my $self = shift;
$self->stash(
body_id => 'iframe',
appeal_code => 'iframe'
);
} => 'iframe';
any [qw(GET POST)] => '/Spring2023' => sub {
my $self = shift;
$self->stash(
body_id => 'Spring2023',
appeal_code => 'Spring2023'
);
} => 'Spring2023';
any [qw(GET POST)] => 'rafemair' => sub {
my $self = shift;
$self->stash(
body_id => 'RafeMair2017',
appeal_code => 'RafeMair2017'
);
$self->flash( appeal_code => 'RafeMair2017');
} => 'RafeMair';
any [qw(GET POST)] => '/builders' => sub {
my $self = shift;
$self->stash(
body_id => 'builders',
appeal_code => 'builders'
);
} => 'builders';
any [qw(GET POST)] => '/builders' => sub {
my $self = shift;
$self->stash(
body_id => 'builders',
appeal_code => 'builders'
);
} => 'builders';
any [qw(GET POST)] => '/dec2018' => sub {
my $self = shift;
$self->stash(
body_id => 'dec2018',
appeal_code => 'dec2018'
);
} => 'DecBuilderCamp2019';
any [qw(GET POST)] => '/national' => sub {
my $self = shift;
$self->stash(
body_id => 'national',
appeal_code => 'national'
);
} => 'national';
any [qw(GET POST)] => '/evergreen' => sub {
my $self = shift;
$self->stash(
body_id => 'evergreen',
appeal_code => 'evergreen',
display => 'block'
);
} => 'evergreen';
any [qw(GET POST)] => '/evergreen-squeeze' => sub {
my $self = shift;
$self->stash(
body_id => 'evergreen-squeeze',
appeal_code => 'evergreen-squeeze',
display => 'block'
);
} => 'evergreen-squeeze';
any [qw(GET POST)] => '/election2017-2' => sub {
my $self = shift;
$self->stash(
body_id => 'election2017-2',
appeal_code => 'election2017-2'
);
$self->flash( appeal_code => 'election2017-2' );
} => 'election2017-2';
any [qw(GET POST)] => '/election2015' => sub {
my $self = shift;
$self->stash(
body_id => 'election2015',
appeal_code => 'election2015'
);
$self->flash( appeal_code => 'election2015' );
} => 'election2015';
any [qw(GET POST)] => '/voices' => sub {
my $self = shift;
$self->stash(
body_id => 'voices',
appeal_code => 'voices'
);
$self->flash( appeal_code => 'voices' );
} => 'voices';
};
# New route for processing EFT/ACH subscriptions
any [qw(GET POST)] => '/process_bank' => sub {
my $self = shift;
my $params = $self->flash( 'params' );
my $campaign = $self->flash( 'campaign' );
my $appeal_code = $self->flash( 'appeal_code' );
my $referrer = $self->flash( 'original_referrer' );
my $raiser = $self->flash( 'raiser' );
my $dt = DateTime->now;
$dt->set_time_zone( 'Europe/London' );
my $trans_date = $dt->ymd . ' ' . $dt->hms;
my $states = $params->{'state'};
my $original_params = $self->flash( 'original_params' );
# There are two possible state values, but only one should be used
# my $state = @$states[0] ? @$states[0] : @$states[1];
my $state;
if ( ref($states) && @$states) {
if (@$states[0] ) {$state = @$states[0]
} elsif (@$states[1]) {$state = @$states[1]
} elsif ($params->{'state'}) { $state = $params->{'state'} }
} else {
$state = $params->{'state'} ;
}
my $transaction_details = {
email => $params->{'email'},
phone => $params->{'phone'},
first_name => $params->{'first-name'},
last_name => $params->{'last-name'},
hosted_login_token => 'Bank transaction. Not applicable',
trans_date => $trans_date, # Add a date
address1 => $params->{'address1'},
city => $params->{'city'},
state => $state,
country => $params->{'country'},
zip => $params->{'postal-code'},
amount_in_cents => $params->{'amount-in-cents'},
plan_name => $params->{'plan-name'},
plan_code => $params->{'plan-code'},
payment_type => $params->{'payment-type'},
transit_number => $params->{'transit-number'},
bank_number => $params->{'bank-number'},
account_number => $params->{'account-number'},
campaign => $campaign,
( defined $raiser
? ( 'raiser' => $self->raiser_decode( $raiser ) )
: ()
),
appeal_code => $appeal_code,
referrer => $referrer,
user_agent => $self->req->headers->user_agent,
};
my $res = $ua->post( $config->{'iats_process_proxy'} => { Accept => '*/*' } => form => $transaction_details);
app->log->debug( "res->body from iats proxy dump:");
app->log->debug( Dumper($res->res->content->asset->{"content"}));
app->log->debug( "end res->body from iats proxy dump:");
my $json_return = decode_json($res->res->content->asset->{"content"});
my $result = $self->find_or_new( $transaction_details, $json_return, "IATS", $params, $original_params);
$transaction_details->{'id'} = $result->id;
$self->flash( { transaction_details => $transaction_details, } );
# app->log->debug( Dumper $res);
$self->redirect_to( 'perks' );
};
post '/get_update_link' => sub {
my $self = shift;
my $email;
my $res;
my $text;
$email = {
'account' => {
'account_code' => $self->param( 'email' )
}
};
# Post the XML to the /transacdtions endpoint
$res = $ua->get( $API. 'accounts/' .$self->param( 'email' ) => { 'Content-Type' => 'application/xml', Accept => '*/*' });
# app->log->debug( "email is " . $self->param( 'email' ) . Dumper $res);
my $xml = $res->res->content->asset->{content};
# $text .= dumper_html($res);
my $dom = Mojo::DOM->new( $xml );
if ( $dom->at( 'error' ) ) { # We got an error message back
my $error = $dom->at( 'error' )->text;
$self->flash( { error => $error } );
$text .= "Your email address was not found in our system. Please send an email to builders AT thetyee DOT ca or give us a call to update your account." ;
}
else { # Otherwise, store the transaction and send to /preferences
if ($dom->at( 'hosted_login_token' )->text && $dom->at( 'hosted_login_token' )->text ne '') {
#$text .= "hosted_login_token" . 'https://thetyee.recurly.com/account/' . $dom->at( 'hosted_login_token' )->text;
$text .= "You have been sent an email with a link to update your account with us.";
$self->mail(
from => '[email protected]',
type => 'text/html',
to => $self->param( 'email' ),
bcc => '[email protected]',
subject => 'The Tyee - Modify Your Builder Account',
data => 'You appear to have requested your sign-in for your builder account with The Tyee at https://support.thetyee.ca. You can sign in to your account by visiting the following link: https://' . $subdomain .'.recurly.com/account/' . $dom->at( 'hosted_login_token' )->text . "\n \n <br><br>" . 'Note that for security reasons, if you are updating a card you have to re-input the entire card, expiry and security code (even if only the expiry has changed). If this was sent by mistake you can delete or ignore it - we only send this information to the email registered on the account.',
);
} else {
}
}
$self->render(template => 'default', content => $text, title => "The Tyee | Get account management link", body_id => '');
# $self->render(text => $text);
};
# New route for processing the form post with the upgraded Recurly.js
# Take the token, plus the plan, and send a post to the Recurly Subscrptions API
# post '/process_transaction
post '/process_transaction' => sub {
my $self = shift;
# Capture values from flash
my $campaign = $self->flash( 'campaign' );
my $appeal_code = $self->flash( 'appeal_code' );
my $referrer = $self->flash( 'original_referrer' );
my $raiser = $self->flash( 'raiser' );
my $payment_type = $self->param( 'payment-type' );
my $token = $self->param( 'recurly-token' );
my $plan_name = $self->param( 'plan-name' );
my $plan_code = $self->param( 'plan-code' );
# TODO remove $amount
my $amount = $self->param( 'amount' );
my $amount_in_cents = $self->param( 'amount-in-cents' );
my $unit_amount_in_cents = $self->param( 'unit-amount-in-cents' );
my $first_name = $self->param( 'first-name' );
my $last_name = $self->param( 'last-name' );
my $address = $self->param( 'address1' );
my $city = $self->param( 'city' );
my $state = $self->param( 'state' );
my $country = $self->param( 'country' );
my $postal = $self->param( 'postal-code' );
my $email = $self->param( 'email' );
my $phone = $self->param( 'phone' );
my $params = $self->req->body_params->to_hash;
my $original_params = $self->flash('original_params');
if ( $payment_type eq 'bank' )
{ # If it's a EFT/ACH, redirect to /process_bank
$self->flash(
{ params => $params,
campaign => $campaign,
raiser => $raiser,
appeal_code => $appeal_code,
original_referrer => $referrer,
original_params => $original_params
}
);
$self->redirect_to( 'process_bank' );
return;
}
# Create a new XML document to work with Recurly's APIs
my $xmldoc = XML::Mini::Document->new();
# This can be done using a hash:
my $transaction;
my $res;
if ( !$plan_code && $amount_in_cents )
{ # It's a transaction, not a subscription
$transaction = {
'transaction' => {
'amount_in_cents' => $amount_in_cents,
'currency' => 'CAD',
'account' => {
'account_code' => lc $email,
'first_name' => $first_name,
'last_name' => $last_name,
'email' => lc $email,
'billing_info' => { 'token_id' => $token }
}
}
};
$xmldoc->fromHash( $transaction );
my $transxml = $xmldoc->toString;
# Post the XML to the /transacdtions endpoint
$res
= $ua->post( $API
. 'transactions' =>
{ 'Content-Type' => 'application/xml', Accept => '*/*' } =>
$transxml )->res;
}
else {
$transaction = { # It's a subscription
'subscription' => {
'plan_code' => $plan_code,
'currency' => 'CAD',
'unit_amount_in_cents' => $unit_amount_in_cents,
'account' => {
'account_code' => lc $email,
'first_name' => $first_name,
'last_name' => $last_name,
'email' => lc $email,
'billing_info' => { 'token_id' => $token }
}
}
};
$xmldoc->fromHash( $transaction );
my $transxml = $xmldoc->toString;
# Post the XML to the /subscriptions endpoint
$res
= $ua->post( $API
. 'subscriptions' =>
{ 'Content-Type' => 'application/xml', Accept => '*/*' } =>
$transxml )->res;
}
my $xml = $res->body;
$self->app->log->info("dump of transaction or subscription:");
$self->app->log->info( Dumper $xml );
$self->app->log->info("end of dump:");
my $dom = Mojo::DOM->new( $xml );
if ( $dom->at( 'error' ) ) { # We got an error message back
my $error = $dom->at( 'error' )->text;
$self->flash( { error => $error } );
$self->redirect_to( '/' );
}
else { # Otherwise, store the transaction and send to /preferences
my $account_code = $self->recurly_get_account_code( $dom->at( 'account' ) );
my $account = $self->recurly_get_account_details( $account_code );
my $activesubs = $self->recurly_get_active_subs($account_code);
my $billing_info = $self->recurly_get_billing_details( $account_code );
my $transaction_details = {
email => $email,
first_name => $first_name,
last_name => $last_name,
# Recurly data
hosted_login_token => $account->at( 'hosted_login_token' )
? $account->at( 'hosted_login_token' )->text
: '',
# Recurly data
trans_date => $dom->at( 'created_at' )
? $dom->at( 'created_at' )->text
: $dom->at( 'activated_at' ) ? $dom->at( 'activated_at' )->text
: '',
address1 => $address,
city => $city,
state => $state,
country => $country,
zip => $postal,
amount_in_cents => $amount_in_cents,
plan_name => $plan_name,
plan_code => $plan_code,
campaign => $campaign,
( defined $raiser
? ( 'raiser' => $self->raiser_decode( $raiser ) )
: ()
),
appeal_code => $appeal_code,
referrer => $referrer,
payment_type => $payment_type,
phone => $phone,
user_agent => $self->req->headers->user_agent,
};
my %domhash= %$dom;
my $domref = \%domhash;
$self->app->log->info( "domhash dump");
$self->app->log->info( Dumper(%domhash));
$self->app->log->info( "end domhash dump");
my $xml_converter = XML::Hash->new();
my $xml_hash = $xml_converter->fromXMLStringtoHash($xml);
my $raiser = $self->flash( 'raiser' );
my $result = $self->find_or_new( $transaction_details, $xml_hash, "RECURLY", $params, $original_params);
$transaction_details->{'id'} = $result->id;
$self->flash( { transaction_details => $transaction_details } );
$self->redirect_to( 'perks' );
}
};
any [qw(GET POST)] => '/perks' => sub {
my $self = shift;
my $record = $self->flash( 'transaction_details' );
$self->stash( { record => $record, } );
$self->flash( { transaction_details => $record } );
if ( $self->req->method eq 'POST' && $record ) { # Submitted form
my $noxml = '<?xml version="1.0" ?>' . "\n" . '<metadata>' . "\n" . '</metadata>';#creating blank xml so it will conform to expectations when received by the helper
my $xml_converter = XML::Hash->new();
my $xml_hash = $xml_converter->fromXMLStringtoHash($noxml);
my $params = $self->req->body_params->to_hash;
my $original_params = $self->flash('original_params');
my $update = $self->find_or_new( $record , $xml_hash, "PERKS_PHASE", $params, $original_params);
$update->update( $self->req->params->to_hash );
$record->{'pref_lapel'} = $update->pref_lapel;
$self->flash( { transaction_details => $record } );
$self->redirect_to( 'preferences' );
}
} => 'perks';
any [qw(GET POST)] => '/preferences' => sub {
my $self = shift;
my $record = $self->flash( 'transaction_details' );
$self->stash( { record => $record, } );
$self->flash( { transaction_details => $record } );
if ( $self->req->method eq 'POST' && $record ) { # Submitted form
# TODO *actually* validate parameters with custom check
my $validation = $self->validation;
$validation->required( 'pref_frequency' );
$validation->required( 'pref_anonymous' );
# Render form again if validation failed
# TODO actually render form again vs. just writing to log
$self->app->log->info( Dumper $validation ) if $validation->has_error;
$self->app->log->info( Dumper $self->req->params->to_hash )
if $validation->has_error;
#return $self->render( 'preferences' ) if $validation->has_error;
my $noxml = '<?xml version="1.0" ?>' . "\n" . '<metadata>' . "\n" . '</metadata>'; #creating blank xml so it will conform to expectations when received by the helper
my $xml_converter = XML::Hash->new();
my $xml_hash = $xml_converter->fromXMLStringtoHash($noxml);
my $params = $self->req->body_params->to_hash;
my $original_params = $self->flash('original_params');
my $update = $self->find_or_new( $record , $xml_hash, "PREFERENCES_PHASE", $params, $original_params);
$update->update( $self->req->params->to_hash );
$record->{'on_behalf_of'} = $update->on_behalf_of;
$self->flash( { transaction_details => $record } );
$self->redirect_to( 'share' );
}
} => 'preferences';
get '/share' => sub {
my $self = shift;
my $transaction_details = $self->flash( 'transaction_details' );
my $email = $transaction_details->{'email'};
$self->app->log->info( Dumper $transaction_details );
$self->stash(
{ transaction_details => $transaction_details,
raiser_id => $self->raiser_encode( $email ),
}
);
$self->render( 'share' );
} => 'share';
any [qw(GET POST)] => '/help-us-grow' => sub {
my $self = shift;
my $email = $self->param( 'email' );
$self->stash(
{ ( defined $email
? ( 'raiser_id' => $self->raiser_encode( $email ) )
: ( 'raiser_id' => '' )
),
}
);
$self->render( 'raiser' );
} => 'raiser';
get '/leaderboard' => sub {
my $self = shift;
my $rs = $self->search_records( 'Transaction',
{ raiser => { '!=', undef }, } );
#my $count = $rs->count;
#say Dumper( $count );
my $raisers = {};
while ( my $raised = $rs->next ) {
$raisers->{ $raised->raiser }->{'count'}++;
}
# say Dumper( $raisers );
for my $raiser ( keys %$raisers ) {
my $raisers_rs
= $self->search_records( 'Transaction', { email => $raiser } );
my $r = $rs->first;
if ( $r ) {
$raisers->{$raiser}->{'first_name'} = $r->first_name;
$raisers->{$raiser}->{'last_name'} = $r->last_name;
$raisers->{$raiser}->{'anonymous'} = $r->pref_anonymous;
}
}
#say Dumper( $raisers );
my @keys = sort { $raisers->{$a} cmp $raisers->{$b} } keys( %$raisers );
my @vals = @{$raisers}{@keys};
my @ordered = sort { $b->{'count'} <=> $a->{'count'} } @vals;
$self->stash( leaders => \@ordered );
$self->render( 'leaderboard' );
} => 'leaderboard';
get '/plans' => sub { # List plans; Not used
my $self = shift;
$self->render_not_found; # Doesn't exist for now
my $res
= $ua->get( $API . 'plans' => { Accept => 'application/xml' } )->res;
my $xml = $res->body;
my $dom = Mojo::DOM->new( $xml );
my @plans = $dom->find( 'plan' );
$self->stash( { plans => @plans } );
$self->render( 'index' );
};
app->secret( $config->{'app_secret'} );
app->start;
__DATA__
@@ default.html.ep
% layout 'default';
<div style="width: 100%; height: 500px; text-align:center;">
<p>
<%=$content%>
</p>
</div>