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 #local $FS::cust_main::Billing_Realtime::DEBUG = 2;
18 my $fs = FS::Test->new( user => 'admin' );
19 my $conf = FS::Conf->new;
23 ### can only run on test database (company name "Freeside Test")
24 like( $conf->config('company_name'), qr/^Freeside Test/, 'using test database' ) or BAIL_OUT('');
26 ### database might need to be upgraded before this,
27 ### but doesn't matter if existing records are tokenized or not,
28 ### this is all about testing new record creation
30 # these will just get in the way for now
31 foreach my $apg ($fs->qsearch('agent_payment_gateway')) {
35 ok( !$err, 'removing agent gateway overrides' ) or BAIL_OUT($err);
38 my $reason = FS::reason->new_or_existing(
39 reason => 'Token Test',
43 isa_ok ( $reason, 'FS::reason', "refund reason" ) or BAIL_OUT('');
45 # non-tokenizing gateway
63 /usr/local/etc/freeside/cardfortresstest.txt';
65 # for attempting refund post-tokenization
69 foreach my $tokenizing (0,1) {
71 my $adj = $tokenizing ? 'tokenizable' : 'non-tokenizable';
74 $conf->set('business-onlinepayment' => $bopconf[$tokenizing]);
75 is( join("\n",$conf->config('business-onlinepayment')), $bopconf[$tokenizing], "set $adj default gateway" ) or BAIL_OUT('');
79 my $n_paynum = $n_cust_pay->paynum;
81 # refund the previous non-tokenized payment through CF
82 $err = $n_cust_main->realtime_refund_bop({
83 reasonnum => $reason->reasonnum,
87 ok( !$err, "run post-switch refund" ) or BAIL_OUT($err);
89 # check for void record
90 my $n_cust_pay_void = $fs->qsearchs('cust_pay_void',{ paynum => $n_paynum });
91 isa_ok( $n_cust_pay_void, 'FS::cust_pay_void', 'post-switch void') or BAIL_OUT("paynum $n_paynum");
93 # check that void tokenized
94 ok ( $n_cust_pay_void->tokenized, "post-switch void tokenized" ) or BAIL_OUT("paynum $n_paynum");
96 # check for no refund record
97 ok( !$fs->qsearch('cust_refund',{ source_paynum => $n_paynum }), "post-switch refund did not generate cust_refund" ) or BAIL_OUT("paynum $n_paynum");
102 my $cust_main = $fs->new_customer($adj);
103 isa_ok ( $cust_main, 'FS::cust_main', "$adj customer" ) or BAIL_OUT('');
106 $err = $cust_main->insert;
107 ok( !$err, "insert $adj customer" ) or BAIL_OUT($err);
111 my %card = random_card();
112 $err = $cust_main->save_cust_payby(
114 payment_payby => $card{'payby'},
116 saved_cust_payby => \$cust_payby
118 ok( !$err, "save $adj card" ) or BAIL_OUT($err);
121 isa_ok ( $cust_payby, 'FS::cust_payby', "$adj card" ) or BAIL_OUT('');
123 # check that card tokenized or not
125 ok( $cust_payby->tokenized, 'new cust card tokenized' ) or BAIL_OUT('');
127 ok( !$cust_payby->tokenized, 'new cust card not tokenized' ) or BAIL_OUT('');
131 $err = $cust_main->realtime_cust_payby( amount => '1.00' );
132 ok( !$err, "run $adj payment" ) or BAIL_OUT($err);
135 my $cust_pay = $fs->qsearchs('cust_pay',{ custnum => $cust_main->custnum });
136 isa_ok ( $cust_pay, 'FS::cust_pay', "$adj payment" ) or BAIL_OUT('');
139 $err = $cust_main->realtime_refund_bop({
140 reasonnum => $reason->reasonnum,
141 paynum => $cust_pay->paynum,
144 ok( !$err, "run $adj refund" ) or BAIL_OUT($err);
146 unless ($tokenizing) {
148 # run a second payment, to refund after switch
149 $err = $cust_main->realtime_cust_payby( amount => '2.00' );
150 ok( !$err, "run $adj second payment" ) or BAIL_OUT($err);
152 # get the second payment
153 $n_cust_pay = $fs->qsearchs('cust_pay',{ custnum => $cust_main->custnum, paid => '2.00' });
154 isa_ok ( $n_cust_pay, 'FS::cust_pay', "$adj second payment" ) or BAIL_OUT('');
156 $n_cust_main = $cust_main;
160 #check that all transactions tokenized or not
161 foreach my $table (qw(cust_pay_pending cust_pay cust_pay_void)) {
162 foreach my $record ($fs->qsearch($table,{ custnum => $cust_main->custnum })) {
164 $err = "record not tokenized: $table ".$record->get($record->primary_key)
165 unless $record->tokenized;
167 $err = "record tokenized: $table ".$record->get($record->primary_key)
168 if $record->tokenized;
173 ok( !$err, "$adj transaction token check" ) or BAIL_OUT($err);
176 ok( $fs->qsearch('cust_pay_void',{ custnum => $cust_main->custnum}), "$adj refund voided" ) or BAIL_OUT('');
178 #make sure we didn't generate refund records
179 ok( !$fs->qsearch('cust_refund',{ custnum => $cust_main->custnum}), "$adj refund did not generate cust_refund" ) or BAIL_OUT('');
186 my $payinfo = '4111' . join('', map { int(rand(10)) } 1 .. 11);
187 $payinfo .= generate_last_digit($payinfo);
188 my $paydate = DateTime->now
190 ->truncate(to => 'month')
192 return ( 'payby' => 'CARD',
193 'payinfo' => $payinfo,
194 'paydate' => $paydate,
195 'payname' => 'Tokenize Me',