9a3ef3f2238e25eeaae4354f45eed0315bdfc687
[freeside.git] / FS / t / suite / 13-tokenization.t
1 #!/usr/bin/perl
2
3 use FS::Test;
4 use Test::More tests => 9;
5 use FS::Conf;
6
7 ### can only run on test database (company name "Freeside Test")
8 ### will run upgrade, which uses lots of prints & warns beyond regular test output
9
10 my $fs = FS::Test->new( user => 'admin' );
11 my $conf = new_ok('FS::Conf');
12 my $err;
13 my $bopconf;
14
15 like( $conf->config('company_name'), qr/^Freeside Test/, 'using test database' ) or BAIL_OUT('');
16
17 # some pre-upgrade cleanup, upgrade will fail if these are still configured
18 foreach my $cust_main ( $fs->qsearch('cust_main') ) {
19   my @count = $fs->qsearch('agent_payment_gateway', { agentnum => $cust_main->agentnum } );
20   if (@count > 1) {
21     note("DELETING CARDTYPE GATEWAYS");
22     foreach my $apg (@count) {
23       $err = $apg->delete if $apg->cardtype;
24       last if $err;
25     }
26     @count = $fs->qsearch('agent_payment_gateway', { agentnum => $cust_main->agentnum } );
27     if (@count > 1) {
28       $err = "Still found ".@count." gateways for custnum ".$cust_main->custnum;
29       last;
30     }
31   }
32 }
33 ok( !$err, "remove obsolete payment gateways" ) or BAIL_OUT($err);
34
35 $bopconf = 
36 'IPPay
37 TESTTERMINAL';
38 $conf->set('business-onlinepayment' => $bopconf);
39 is( join("\n",$conf->config('business-onlinepayment')), $bopconf, "setting first default gateway" ) or BAIL_OUT('');
40
41 # generate a few void/refund records for upgrading
42 my $counter = 20;
43 foreach my $cust_pay ( $fs->qsearch('cust_pay',{ payby => 'CARD' }) ) {
44   if ($counter % 2) {
45     $err = $cust_pay->void('Testing');
46     $err = "Voiding: $err" if $err;
47   } else {
48     # from realtime_refund_bop, just the important bits    
49     while ( $cust_pay->unapplied < $cust_pay->paid ) {
50       my @cust_bill_pay = $cust_pay->cust_bill_pay;
51       last unless @cust_bill_pay;
52       my $cust_bill_pay = pop @cust_bill_pay;
53       $err = $cust_bill_pay->delete;
54       $err = "Refund unapply: $err" if $err;
55       last if $err;
56     }
57     last if $err;
58     my $cust_refund = new FS::cust_refund ( {
59       'custnum'  => $cust_pay->cust_main->custnum,
60       'paynum'   => $cust_pay->paynum,
61       'source_paynum' => $cust_pay->paynum,
62       'refund'   => $cust_pay->paid,
63       '_date'    => '',
64       'payby'    => $cust_pay->payby,
65       'payinfo'  => $cust_pay->payinfo,
66       'reason'     => 'Testing',
67       'gatewaynum'    => $cust_pay->gatewaynum,
68       'processor'     => $cust_pay->payment_gateway ? $cust_pay->payment_gateway->processor : '',
69       'auth'          => $cust_pay->auth,
70       'order_number'  => $cust_pay->order_number,
71     } );
72     $err = $cust_refund->insert( reason_type => 'Refund' );
73     $err = "Refunding: $err" if $err;
74   }
75   last if $err;
76   $counter -= 1;
77   last unless $counter > 0;
78 }
79 ok( !$err, "create some refunds and voids" ) or BAIL_OUT($err);
80
81 $err = system('freeside-upgrade','admin');
82 ok( !$err, 'initial upgrade' ) or BAIL_OUT('Error string: '.$!);
83
84 $bopconf =
85 'CardFortress
86 cardfortresstest
87 (TEST54)
88 Normal Authorization
89 gateway
90 IPPay
91 gateway_login
92 TESTTERMINAL
93 gateway_password
94
95 private_key
96 /usr/local/etc/freeside/cardfortresstest.txt';
97 $conf->set('business-onlinepayment' => $bopconf);
98 is( join("\n",$conf->config('business-onlinepayment')), $bopconf, "setting tokenizable default gateway" ) or BAIL_OUT('');
99
100 foreach my $pg ($fs->qsearch('payment_gateway')) {
101   unless ($pg->gateway_module eq 'CardFortress') {
102     note('UPGRADING NON-CF PAYMENT GATEWAY');
103     my %pgopts = (
104       gateway          => $pg->gateway_module,
105       gateway_login    => $pg->gateway_username,
106       gateway_password => $pg->gateway_password,
107       private_key      => '/usr/local/etc/freeside/cardfortresstest.txt',
108     );
109     $pg->gateway_module('CardFortress');
110     $pg->gateway_username('cardfortresstest');
111     $pg->gateway_password('(TEST54)');
112     $err = $pg->replace(\%pgopts);
113     last if $err;
114   }
115 }
116 ok( !$err, "remove non-CF payment gateways" ) or BAIL_OUT($err);
117
118 $err = system('freeside-upgrade','admin');
119 ok( !$err, 'tokenizable upgrade' ) or BAIL_OUT('Error string: '.$!);
120
121 1;
122