+# generate a few void/refund records for upgrading
+my $counter = 20;
+foreach my $cust_pay ( $fs->qsearch('cust_pay',{ payby => 'CARD' }) ) {
+ if ($counter % 2) {
+ $err = $cust_pay->void('Testing');
+ $err = "Voiding: $err" if $err;
+ } else {
+ # from realtime_refund_bop, just the important bits
+ while ( $cust_pay->unapplied < $cust_pay->paid ) {
+ my @cust_bill_pay = $cust_pay->cust_bill_pay;
+ last unless @cust_bill_pay;
+ my $cust_bill_pay = pop @cust_bill_pay;
+ $err = $cust_bill_pay->delete;
+ $err = "Refund unapply: $err" if $err;
+ last if $err;
+ }
+ last if $err;
+ my $cust_refund = new FS::cust_refund ( {
+ 'custnum' => $cust_pay->cust_main->custnum,
+ 'paynum' => $cust_pay->paynum,
+ 'source_paynum' => $cust_pay->paynum,
+ 'refund' => $cust_pay->paid,
+ '_date' => '',
+ 'payby' => $cust_pay->payby,
+ 'payinfo' => $cust_pay->payinfo,
+ 'reason' => 'Testing',
+ 'gatewaynum' => $cust_pay->gatewaynum,
+ 'processor' => $cust_pay->payment_gateway ? $cust_pay->payment_gateway->processor : '',
+ 'auth' => $cust_pay->auth,
+ 'order_number' => $cust_pay->order_number,
+ } );
+ $err = $cust_refund->insert( reason_type => 'Refund' );
+ $err = "Refunding: $err" if $err;
+ }
+ last if $err;
+ $counter -= 1;
+ last unless $counter > 0;
+}
+ok( !$err, "create some refunds and voids" ) or BAIL_OUT($err);
+
+# also, just to test behavior in this case, create a record for an aborted
+# verification payment. this will have no customer number.
+
+my $pending_failed = FS::cust_pay_pending->new({
+ 'custnum_pending' => 1,
+ 'paid' => '1.00',
+ '_date' => time - 86400,
+ random_card(),
+ 'status' => 'failed',
+ 'statustext' => 'Tokenization upgrade test',
+});
+$err = $pending_failed->insert;
+ok( !$err, "create a failed payment attempt" ) or BAIL_OUT($err);
+
+# create two customers with an AmEx card & paycvv,
+# then run a payment with one, just to generate some test AmEx data
+
+my $amex_cust;
+foreach my $i (0,1) {
+ my $cust_main = $fs->new_customer("AmEx $i");
+ isa_ok ( $cust_main, 'FS::cust_main', "AmEx $i customer" ) or BAIL_OUT('');
+ $err = $cust_main->insert;
+ ok( !$err, "insert AmEx $i customer" ) or BAIL_OUT($err);
+ # add card
+ my $cust_payby;
+ my %card = random_card();
+ $card{'payinfo'} = '347594362484937';
+ $card{'paycvv'} = '1234';
+ $err = $cust_main->save_cust_payby(
+ %card,
+ payment_payby => $card{'payby'},
+ auto => 1,
+ saved_cust_payby => \$cust_payby
+ );
+ ok( !$err, "save AmEx $i card" ) or BAIL_OUT($err);
+ $amex_cust = $cust_main;
+}
+$err = $amex_cust->realtime_cust_payby( amount => '1.00' );
+ok( !$err, "run AmEx payment" ) or BAIL_OUT($err);
+
+# find two stored credit cards.
+my @cust = map { FS::cust_main->by_key($_) } (10, 12);
+my @payby = map { ($_->cust_payby)[0] } @cust;
+my @payment;
+
+ok( $payby[0]->payby eq 'CARD' && !$payby[0]->tokenized,
+ "first customer has a non-tokenized card"
+ ) or BAIL_OUT();
+
+$err = $cust[0]->realtime_cust_payby(amount => '2.00');
+ok( !$err, "create a payment through IPPay" )
+ or BAIL_OUT($err);
+$payment[0] = $fs->qsearchs('cust_pay', { custnum => $cust[0]->custnum,
+ paid => '2.00' })
+ or BAIL_OUT("can't find payment record");
+