+=item export_to_gateway GATEWAY OPTIONS
+
+Given L<FS::payment_gateway> GATEWAY, export the items in this batch to
+that gateway via Business::BatchPayment. OPTIONS may include:
+
+- file: override the default transport and write to this file (name or handle)
+
+If batch contains no cust_pay_batch entries (or has them all removed by
+L</prepare_for_export>) then nothing will be transported (or written to
+the override file) and the batch will be resolved.
+
+=cut
+
+sub export_to_gateway {
+
+ my ($self, $gateway, %opt) = @_;
+
+ my $oldAutoCommit = $FS::UID::AutoCommit;
+ local $FS::UID::AutoCommit = 0;
+ my $dbh = dbh;
+
+ my $error = $self->prepare_for_export;
+ die $error if $error;
+
+ my %proc_opt = (
+ 'output' => $opt{'file'}, # will do nothing if it's empty
+ # any other constructor options go here
+ );
+ my $processor = $gateway->batch_processor(%proc_opt);
+
+ my @items = map { $_->request_item } $self->cust_pay_batch;
+ unless (@items) {
+ # if it's empty, just resolve the batch
+ $self->set_status('R');
+ $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+ return '';
+ }
+
+ try {
+ my $batch = Business::BatchPayment->create(Batch =>
+ batch_id => $self->batchnum,
+ items => \@items
+ );
+ $processor->submit($batch);
+
+ if ($batch->processor_id) {
+ $self->set('processor_id',$batch->processor_id);
+ $self->replace;
+ }
+ } catch {
+ $dbh->rollback if $oldAutoCommit;
+ die $_;
+ };
+
+ $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+ '';
+}
+
+sub manual_approve {
+ my $self = shift;
+ my $date = time;
+ my %opt = @_;
+ my $usernum = $opt{'usernum'} || die "manual approval requires a usernum";
+ my $conf = FS::Conf->new;
+ return 'manual batch approval disabled'
+ if ( ! $conf->exists('batch-manual_approval') );
+ return 'batch already resolved' if $self->status eq 'R';
+ return 'batch not yet submitted' if $self->status eq 'O';
+
+ local $SIG{HUP} = 'IGNORE';
+ local $SIG{INT} = 'IGNORE';
+ local $SIG{QUIT} = 'IGNORE';
+ local $SIG{TERM} = 'IGNORE';
+ local $SIG{TSTP} = 'IGNORE';
+ local $SIG{PIPE} = 'IGNORE';
+
+ my $oldAutoCommit = $FS::UID::AutoCommit;
+ local $FS::UID::AutoCommit = 0;
+ my $dbh = dbh;
+
+ my $payments = 0;
+ foreach my $cust_pay_batch (
+ qsearch('cust_pay_batch', { batchnum => $self->batchnum,
+ status => '' })
+ ) {
+ my $new_cust_pay_batch = new FS::cust_pay_batch {
+ $cust_pay_batch->hash,
+ 'paid' => $cust_pay_batch->amount,
+ '_date' => $date,
+ 'usernum' => $usernum,
+ };
+ my $error = $new_cust_pay_batch->approve();
+ # there are no approval options here (authorization, order_number, etc.)
+ # because the transaction wasn't really approved
+ if ( $error ) {
+ $dbh->rollback;
+ return 'paybatchnum '.$cust_pay_batch->paybatchnum.": $error";
+ }
+ $payments++;
+ }
+ $self->set_status('R');
+ $dbh->commit;
+ return;
+}
+
+=item batch_download_formats
+
+returns a hash of batch download formats.
+
+my %download_formats = FS::pay_batch::batch_download_formats;
+
+=cut
+
+sub batch_download_formats {
+
+ my @formats = (
+ '' =>
+ 'Default batch mode',
+ 'NACHA' =>
+ '94 byte NACHA',
+ 'csv-td_canada_trust-merchant_pc_batch' =>
+ 'CSV file for TD Canada Trust Merchant PC Batch',
+ 'csv-chase_canada-E-xactBatch' =>
+ 'CSV file for Chase Canada E-xactBatch',
+ 'PAP' =>
+ '80 byte file for TD Canada Trust PAP Batch',
+ 'BoM' =>
+ 'Bank of Montreal ECA batch',
+ 'ach-spiritone' =>
+ 'Spiritone ACH batch',
+ 'paymentech' =>
+ 'XML file for Chase Paymentech',
+ 'RBC' =>
+ 'Royal Bank of Canada PDS batch',
+ 'td_eft1464' =>
+ '1464 byte file for TD Commercial Banking EFT',
+ 'eft_canada' =>
+ 'EFT Canada CSV batch',
+ 'CIBC' =>
+ '80 byte file for Canadian Imperial Bank of Commerce',
+ # insert new batch formats here
+ );
+
+}
+
+=item batch_download_formats
+
+returns a hash of batch download formats.
+
+my %download_formats = FS::pay_batch::batch_download_formats;
+
+=cut
+
+sub can_handle_electronic_refunds {
+
+ my $self = shift;
+ my $format = shift;
+ my $conf = new FS::Conf;
+
+ tie my %download_formats, 'Tie::IxHash', batch_download_formats;
+
+ my %paybatch_mods = (
+ 'NACHA' => 'nacha',
+ 'csv-td_canada_trust-merchant_pc_batch' => 'td_canada_trust',
+ 'csv-chase_canada-E-xactBatch' => 'chase-canada',
+ 'PAP' => 'PAP',
+ 'BoM' => 'BoM',
+ 'ach-spiritone' => 'ach_spiritone',
+ 'paymentech' => 'paymentech',
+ 'RBC' => 'RBC',
+ 'td_eft1464' => 'td_eft1464',
+ 'eft_canada' => 'eft_canada',
+ 'CIBC' => 'CIBC',
+ );
+
+ %download_formats = ( $format => $download_formats{$format}, ) if $format;
+
+ foreach my $key (keys %download_formats) {
+ my $mod = "FS::pay_batch::".$paybatch_mods{$key};
+ if ($mod->can('can_handle_credits')) {
+ return '1' if $conf->exists('batchconfig-'.$key);
+ }
+ }
+
+ return;
+
+}
+
+use FS::upgrade_journal;
+sub _upgrade_data {
+
+ # check if there are any pending batch refunds and no download format configured
+ # that allows electronic refunds.
+ unless ( FS::upgrade_journal->is_done('removed_refunds_nodownload_format') ) {
+
+ ## get a list of all refunds in batches.
+ my $extrasql = " LEFT JOIN pay_batch USING ( batchnum ) WHERE cust_pay_batch.paycode = 'C' AND pay_batch.download IS NULL AND pay_batch.type = 'DEBIT' ";
+
+ my @batch_refunds = qsearch({
+ 'table' => 'cust_pay_batch',
+ 'select' => 'cust_pay_batch.*',
+ 'extra_sql' => $extrasql,
+ });
+
+ my $replace_error;
+
+ if (@batch_refunds) {
+ warn "found ".scalar @batch_refunds." batch refunds.\n";
+ warn "Searching for their cust refunds...\n" if (scalar @batch_refunds > 0);
+
+ my $oldAutoCommit = $FS::UID::AutoCommit;
+ local $FS::UID::AutoCommit = 0;
+ my $dbh = dbh;
+
+ ## move refund to credit batch.
+ foreach my $cust_pay_batch (@batch_refunds) {
+ my $payby = $cust_pay_batch->payby eq "CARD" ? "CARD" : "CHEK";
+
+ my %pay_batch = (
+ 'status' => 'O',
+ 'payby' => $payby,
+ 'type' => 'CREDIT',
+ );
+
+ my $pay_batch = qsearchs( 'pay_batch', \%pay_batch );
+
+ unless ( $pay_batch ) {
+ $pay_batch = new FS::pay_batch \%pay_batch;
+ my $error = $pay_batch->insert;
+ if ( $error ) {
+ $dbh->rollback if $oldAutoCommit;
+ warn "error creating a $payby credit batch: $error\n";
+ }
+ }
+
+ $cust_pay_batch->batchnum($pay_batch->batchnum);
+ $replace_error = $cust_pay_batch->replace();
+ if ( $replace_error ) {
+ $dbh->rollback if $oldAutoCommit;
+ warn "Unable to move credit to a credit batch: $replace_error";
+ }
+ else {
+ warn "Moved cust pay credit ".$cust_pay_batch->paybatchnum." to ".$cust_pay_batch->payby." credit batch ".$cust_pay_batch->batchnum."\n";
+ }
+ }
+ } #end @batch_refunds
+ else { warn "No batch refunds found\n"; }
+
+ FS::upgrade_journal->set_done('removed_refunds_nodownload_format') unless $replace_error;
+ }
+
+ # Set up configuration for gateways that have a Business::BatchPayment
+ # module.
+
+ eval "use Class::MOP;";
+ if ( $@ ) {
+ warn "Moose/Class::MOP not available.\n$@\nSkipping pay_batch upgrade.\n";
+ return;
+ }
+ my $conf = FS::Conf->new;
+ for my $format (keys %export_info) {
+ my $mod = "FS::pay_batch::$format";
+ if ( $mod->can('_upgrade_gateway')
+ and $conf->exists("batchconfig-$format") ) {
+
+ local $@;
+ my ($module, %gw_options) = $mod->_upgrade_gateway;
+ my $gateway = FS::payment_gateway->new({
+ gateway_namespace => 'Business::BatchPayment',
+ gateway_module => $module,
+ });
+ my $error = $gateway->insert(%gw_options);
+ if ( $error ) {
+ warn "Failed to migrate '$format' to a Business::BatchPayment::$module gateway:\n$error\n";
+ next;
+ }
+
+ # test whether it loads
+ my $processor = eval { $gateway->batch_processor };
+ if ( !$processor ) {
+ warn "Couldn't load Business::BatchPayment module for '$format'.\n";
+ # if not, remove it so it doesn't hang around and break things
+ $gateway->delete;
+ }
+ else {
+ # remove the batchconfig-*
+ warn "Created Business::BatchPayment gateway '".$gateway->label.
+ "' for '$format' batch processing.\n";
+ $conf->delete("batchconfig-$format");
+
+ # and if appropriate, make it the system default
+ for my $payby (qw(CARD CHEK)) {
+ if ( ($conf->config("batch-fixed_format-$payby") || '') eq $format ) {
+ warn "Setting as default for $payby.\n";
+ $conf->set("batch-gateway-$payby", $gateway->gatewaynum);
+ $conf->delete("batch-fixed_format-$payby");
+ }
+ }
+ } # if $processor
+ } #if can('_upgrade_gateway') and batchconfig-$format
+ } #for $format
+
+ '';
+}
+