- my $search = FS::Cursor->new({
- table => $table,
- hashref => { 'payby' => 'CARD' },
- },$dbh);
- while (my $record = $search->fetch) {
- next if $record->tokenized;
- next if !$record->payinfo; #shouldn't happen, but at least it's not a card number
- next if $record->payinfo =~ /N\/A/; # ??? Not sure why we do this, but it's not a card number
-
- # don't use customer agent gateway here, use the gatewaynum specified by the record
- my $gatewaynum = $record->gatewaynum || '';
- my $gateway = FS::agent->payment_gateway( load_gatewaynum => $gatewaynum );
- unless ($gateway) { # already died if $gatewaynum
- # only fatal if we expected everyone to tokenize
- next unless $disallow_untokenized;
- $search->DESTROY;
- $dbh->rollback if $oldAutoCommit;
- return "No gateway found for $table ".$record->get($record->primary_key);
+ warn "Checking $table" if $debug;
+
+ # FS::Cursor does not seem to work over multiple commits (gives cursor not found errors)
+ # loading only record ids, then loading individual records one at a time
+ my $tclass = 'FS::'.$table;
+ $offset = 0;
+ @recnums = ();
+
+ while (my $recnum = _token_check_next_recnum($dbh,$table,$step,\$offset,\@recnums)) {
+ my $record = $tclass->by_key($recnum);
+ unless ($record->payby eq 'CARD') {
+ warn "Skipping non-card record for $table ".$record->get($record->primary_key) if $debug;
+ next;
+ }
+ if (FS::cust_main::Billing_Realtime->tokenized($record->payinfo)) {
+ warn "Skipping tokenized record for $table ".$record->get($record->primary_key) if $debug;
+ next;
+ }
+ if (!$record->payinfo) { #shouldn't happen, but at least it's not a card number
+ warn "Skipping blank payinfo for $table ".$record->get($record->primary_key) if $debug;
+ next;
+ }
+ if ($record->payinfo =~ /N\/A/) { # ??? Not sure why we do this, but it's not a card number
+ warn "Skipping NA payinfo for $table ".$record->get($record->primary_key) if $debug;
+ next;
+ }
+
+ if ($require_tokenized && $opt{'daily'}) {
+ $log->info("Untokenized card number detected in $table ".$record->get($record->primary_key). ';tokenizing');
+ $dbh->commit or die $dbh->errstr; # commit log message
+ }
+
+ my $cust_main = $record->cust_main;
+ if (!$cust_main) {
+ # might happen for cust_pay_pending from failed verify records,
+ # in which case we attempt tokenization without cust_main
+ # everything else should absolutely have a cust_main
+ if ($table eq 'cust_pay_pending' and !$record->custnum ) {
+ # override the usual safety check and allow the record to be
+ # updated even without a custnum.
+ $record->set('custnum_pending', 1);
+ } else {
+ my $error = "Could not load cust_main for $table ".$record->get($record->primary_key);
+ if ($opt{'queue'}) {
+ $hascritical = 1;
+ $log->critical($error);
+ $dbh->commit or die $dbh->errstr; # commit log message
+ next;
+ }
+ $dbh->rollback if $oldAutoCommit;
+ die $error;
+ }