8 use Business::CreditCard qw(generate_last_digit);
10 if ( stat('/usr/local/etc/freeside/cardfortresstest.txt') ) {
13 plan skip_all => 'CardFortress test encryption key is not installed.';
16 ### can only run on test database (company name "Freeside Test")
17 ### will run upgrade, which uses lots of prints & warns beyond regular test output
19 my $fs = FS::Test->new( user => 'admin' );
20 my $conf = FS::Conf->new;
24 like( $conf->config('company_name'), qr/^Freeside Test/, 'using test database' ) or BAIL_OUT('');
26 # upgrade just schema, or v3 test db cannot create refunds
27 $err = system('freeside-upgrade','-s','admin');
28 ok( !$err, 'schema upgrade' ) or BAIL_OUT('Error string: '.$!);
30 # some pre-upgrade cleanup, upgrade will fail if these are still configured
31 foreach my $cust_main ( $fs->qsearch('cust_main') ) {
32 my @count = $fs->qsearch('agent_payment_gateway', { agentnum => $cust_main->agentnum } );
34 note("DELETING CARDTYPE GATEWAYS");
35 foreach my $apg (@count) {
36 $err = $apg->delete if $apg->cardtype;
39 @count = $fs->qsearch('agent_payment_gateway', { agentnum => $cust_main->agentnum } );
41 $err = "Still found ".@count." gateways for custnum ".$cust_main->custnum;
46 ok( !$err, "remove obsolete payment gateways" ) or BAIL_OUT($err);
51 $conf->set('business-onlinepayment' => $bopconf);
52 is( join("\n",$conf->config('business-onlinepayment')), $bopconf, "setting first default gateway" ) or BAIL_OUT('');
54 # generate a few void/refund records for upgrading
56 foreach my $cust_pay ( $fs->qsearch('cust_pay',{ payby => 'CARD' }) ) {
58 $err = $cust_pay->void('Testing');
59 $err = "Voiding: $err" if $err;
61 if ($fs->qsearch('cust_refund',{ source_paynum => $cust_pay->paynum })) {
62 note('refund skipping cust_pay '.$cust_pay->paynum.', already refunded');
65 # from realtime_refund_bop, just the important bits
66 while ( $cust_pay->unapplied < $cust_pay->paid ) {
67 my @cust_bill_pay = $cust_pay->cust_bill_pay;
68 last unless @cust_bill_pay;
69 my $cust_bill_pay = pop @cust_bill_pay;
70 $err = $cust_bill_pay->delete;
71 $err = "Refund unapply: $err" if $err;
75 my $cust_refund = new FS::cust_refund ( {
76 'custnum' => $cust_pay->cust_main->custnum,
77 'paynum' => $cust_pay->paynum,
78 'source_paynum' => $cust_pay->paynum,
79 'refund' => $cust_pay->paid,
81 'payby' => $cust_pay->payby,
82 'payinfo' => $cust_pay->payinfo,
83 'reason' => 'Testing',
84 'gatewaynum' => $cust_pay->gatewaynum,
85 'processor' => $cust_pay->payment_gateway ? $cust_pay->payment_gateway->processor : '',
86 'auth' => $cust_pay->auth,
87 'order_number' => $cust_pay->order_number,
89 $err = $cust_refund->insert( reason_type => 'Refund' );
90 $err = "Refunding ".$cust_pay->paynum.": $err" if $err;
94 last unless $counter > 0;
96 $err ||= 'not enough records' if $counter;
97 ok( !$err, "create some refunds and voids" ) or BAIL_OUT($err);
99 # also, just to test behavior in this case, create a record for an aborted
100 # verification payment. this will have no customer number.
102 my $pending_failed = FS::cust_pay_pending->new({
103 'custnum_pending' => 1,
105 '_date' => time - 86400,
107 'status' => 'failed',
108 'statustext' => 'Tokenization upgrade test',
110 $err = $pending_failed->insert;
111 ok( !$err, "create a failed payment attempt" ) or BAIL_OUT($err);
113 # find two stored credit cards, and one stored checking account (to overwrite with CARD)
115 $fs->qsearch({ table=>'cust_main', hashref=>{payby=>'CARD'}, extra_sql=>' LIMIT 2' }),
116 $fs->qsearch({ table=>'cust_main', hashref=>{payby=>'CHEK'}, extra_sql=>' LIMIT 1' }),
120 ok( $cust[0]->payby eq 'CARD' && !$cust[0]->tokenized,
121 "first customer has a non-tokenized card"
124 $err = $cust[0]->realtime_bop({method => 'CC', amount => '2.00'});
125 ok( !$err, "create a payment through IPPay" )
127 $payment[0] = $fs->qsearchs('cust_pay', { custnum => $cust[0]->custnum,
129 or BAIL_OUT("can't find payment record");
131 $err = system('freeside-upgrade','admin');
132 ok( !$err, 'initial upgrade' ) or BAIL_OUT('Error string: '.$!);
134 # switch to CardFortress
147 /usr/local/etc/freeside/cardfortresstest.txt';
148 $conf->set('business-onlinepayment' => $bopconf);
149 is( join("\n",$conf->config('business-onlinepayment')), $bopconf, "setting tokenizable default gateway" ) or BAIL_OUT('');
151 foreach my $pg ($fs->qsearch('payment_gateway')) {
152 unless ($pg->gateway_module eq 'CardFortress') {
153 note('UPGRADING NON-CF PAYMENT GATEWAY');
155 gateway => $pg->gateway_module,
156 gateway_login => $pg->gateway_username,
157 gateway_password => $pg->gateway_password,
158 private_key => '/usr/local/etc/freeside/cardfortresstest.txt',
160 $pg->gateway_module('CardFortress');
161 $pg->gateway_username('cardfortresstest');
162 $pg->gateway_password('(TEST54)');
163 $err = $pg->replace(\%pgopts);
167 ok( !$err, "remove non-CF payment gateways" ) or BAIL_OUT($err);
169 # create a payment using a non-tokenized card. this should immediately
170 # trigger tokenization.
171 ok( $cust[1]->payby eq 'CARD' && ! $cust[1]->tokenized,
172 "second customer has a non-tokenized card"
175 $err = $cust[1]->realtime_bop({method => 'CC', amount => '3.00'});
176 ok( !$err, "tokenize a card when it's first used for payment" )
178 $payment[1] = $fs->qsearchs('cust_pay', { custnum => $cust[1]->custnum,
180 or BAIL_OUT("can't find payment record");
181 ok( $payment[1]->tokenized, "payment is tokenized" );
182 $cust[1] = $cust[1]->replace_old;
183 ok( $cust[1]->tokenized, "card is now tokenized" );
186 # invoke the part of freeside-upgrade that tokenizes
187 FS::cust_main->queueable_upgrade();
188 #$err = system('freeside-upgrade','admin');
189 #ok( !$err, 'tokenizable upgrade' ) or BAIL_OUT('Error string: '.$!);
191 $cust[0] = $cust[0]->replace_old;
192 ok( $cust[0]->tokenized, "old card was tokenized during upgrade" );
193 $payment[0] = $payment[0]->replace_old;
194 ok( $payment[0]->tokenized, "old payment was tokenized during upgrade" );
195 my $old_pending = $fs->qsearchs('cust_pay_pending',{ paynum => $payment[0]->paynum });
196 ok( $old_pending->tokenized, "old cust_pay_pending was tokenized during upgrade" );
198 $pending_failed = $pending_failed->replace_old;
199 ok( $pending_failed->tokenized, "cust_pay_pending with no customer was tokenized" );
201 # add a new payment card to one customer
202 my %newcard = random_card();
203 $cust[2]->$_($newcard{$_}) foreach keys %newcard;
204 $err = $cust[2]->replace;
205 ok( !$err, "new card was saved" ) or BAIL_OUT($err);
206 ok($cust[2]->tokenized, "new card is tokenized" );
209 my $payinfo = '4111' . join('', map { int(rand(10)) } 1 .. 11);
210 $payinfo .= generate_last_digit($payinfo);
211 my $paydate = DateTime->now
213 ->truncate(to => 'month')
215 return ( 'payby' => 'CARD',
216 'payinfo' => $payinfo,
217 'paydate' => $paydate,
218 'payname' => 'Tokenize Me',