last of the refatoring giant cust_main.pm for now, RT#9967
[freeside.git] / FS / FS / cust_main / Billing_Realtime.pm
1 package FS::cust_main::Billing_Realtime;
2
3 use strict;
4 use vars qw( $conf $DEBUG $me );
5 use vars qw( $realtime_bop_decline_quiet ); #ugh
6 use Digest::MD5 qw(md5_base64);
7 use FS::UID qw( dbh );
8 use FS::Record qw( qsearch qsearchs );
9 use FS::Misc qw( send_email );
10 use FS::payby;
11 use FS::cust_pay;
12 use FS::cust_pay_pending;
13 use FS::cust_refund;
14
15 $realtime_bop_decline_quiet = 0;
16
17 # 1 is mostly method/subroutine entry and options
18 # 2 traces progress of some operations
19 # 3 is even more information including possibly sensitive data
20 $DEBUG = 0;
21 $me = '[FS::cust_main::Billing_Realtime]';
22
23 install_callback FS::UID sub { 
24   $conf = new FS::Conf;
25   #yes, need it for stuff below (prolly should be cached)
26 };
27
28 =head1 NAME
29
30 FS::cust_main::Billing_Realtime - Realtime billing mixin for cust_main
31
32 =head1 SYNOPSIS
33
34 =head1 DESCRIPTION
35
36 These methods are available on FS::cust_main objects.
37
38 =head1 METHODS
39
40 =over 4
41
42 =item realtime_collect [ OPTION => VALUE ... ]
43
44 Runs a realtime credit card, ACH (electronic check) or phone bill transaction
45 via a Business::OnlinePayment or Business::OnlineThirdPartyPayment realtime
46 gateway.  See L<http://420.am/business-onlinepayment> and 
47 L<http://420.am/business-onlinethirdpartypayment> for supported gateways.
48
49 On failure returns an error message.
50
51 Returns false or a hashref upon success.  The hashref contains keys popup_url reference, and collectitems.  The first is a URL to which a browser should be redirected for completion of collection.  The second is a reference id for the transaction suitable for the end user.  The collectitems is a reference to a list of name value pairs suitable for assigning to a html form and posted to popup_url.
52
53 Available options are: I<method>, I<amount>, I<description>, I<invnum>, I<quiet>, I<paynum_ref>, I<payunique>, I<session_id>, I<pkgnum>
54
55 I<method> is one of: I<CC>, I<ECHECK> and I<LEC>.  If none is specified
56 then it is deduced from the customer record.
57
58 If no I<amount> is specified, then the customer balance is used.
59
60 The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
61 I<zip>, I<payinfo> and I<paydate> are also available.  Any of these options,
62 if set, will override the value from the customer record.
63
64 I<description> is a free-text field passed to the gateway.  It defaults to
65 the value defined by the business-onlinepayment-description configuration
66 option, or "Internet services" if that is unset.
67
68 If an I<invnum> is specified, this payment (if successful) is applied to the
69 specified invoice.  If you don't specify an I<invnum> you might want to
70 call the B<apply_payments> method or set the I<apply> option.
71
72 I<apply> can be set to true to apply a resulting payment.
73
74 I<quiet> can be set true to surpress email decline notices.
75
76 I<paynum_ref> can be set to a scalar reference.  It will be filled in with the
77 resulting paynum, if any.
78
79 I<payunique> is a unique identifier for this payment.
80
81 I<session_id> is a session identifier associated with this payment.
82
83 I<depend_jobnum> allows payment capture to unlock export jobs
84
85 =cut
86
87 sub realtime_collect {
88   my( $self, %options ) = @_;
89
90   if ( $DEBUG ) {
91     warn "$me realtime_collect:\n";
92     warn "  $_ => $options{$_}\n" foreach keys %options;
93   }
94
95   $options{amount} = $self->balance unless exists( $options{amount} );
96   $options{method} = FS::payby->payby2bop($self->payby)
97     unless exists( $options{method} );
98
99   return $self->realtime_bop({%options});
100
101 }
102
103 =item realtime_bop { [ ARG => VALUE ... ] }
104
105 Runs a realtime credit card, ACH (electronic check) or phone bill transaction
106 via a Business::OnlinePayment realtime gateway.  See
107 L<http://420.am/business-onlinepayment> for supported gateways.
108
109 Required arguments in the hashref are I<method>, and I<amount>
110
111 Available methods are: I<CC>, I<ECHECK> and I<LEC>
112
113 Available optional arguments are: I<description>, I<invnum>, I<apply>, I<quiet>, I<paynum_ref>, I<payunique>, I<session_id>
114
115 The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
116 I<zip>, I<payinfo> and I<paydate> are also available.  Any of these options,
117 if set, will override the value from the customer record.
118
119 I<description> is a free-text field passed to the gateway.  It defaults to
120 the value defined by the business-onlinepayment-description configuration
121 option, or "Internet services" if that is unset.
122
123 If an I<invnum> is specified, this payment (if successful) is applied to the
124 specified invoice.  If you don't specify an I<invnum> you might want to
125 call the B<apply_payments> method or set the I<apply> option.
126
127 I<apply> can be set to true to apply a resulting payment.
128
129 I<quiet> can be set true to surpress email decline notices.
130
131 I<paynum_ref> can be set to a scalar reference.  It will be filled in with the
132 resulting paynum, if any.
133
134 I<payunique> is a unique identifier for this payment.
135
136 I<session_id> is a session identifier associated with this payment.
137
138 I<depend_jobnum> allows payment capture to unlock export jobs
139
140 (moved from cust_bill) (probably should get realtime_{card,ach,lec} here too)
141
142 =cut
143
144 # some helper routines
145 sub _bop_recurring_billing {
146   my( $self, %opt ) = @_;
147
148   my $method = scalar($conf->config('credit_card-recurring_billing_flag'));
149
150   if ( defined($method) && $method eq 'transaction_is_recur' ) {
151
152     return 1 if $opt{'trans_is_recur'};
153
154   } else {
155
156     my %hash = ( 'custnum' => $self->custnum,
157                  'payby'   => 'CARD',
158                );
159
160     return 1 
161       if qsearch('cust_pay', { %hash, 'payinfo' => $opt{'payinfo'} } )
162       || qsearch('cust_pay', { %hash, 'paymask' => $self->mask_payinfo('CARD',
163                                                                $opt{'payinfo'} )
164                              } );
165
166   }
167
168   return 0;
169
170 }
171
172 sub _payment_gateway {
173   my ($self, $options) = @_;
174
175   $options->{payment_gateway} = $self->agent->payment_gateway( %$options )
176     unless exists($options->{payment_gateway});
177
178   $options->{payment_gateway};
179 }
180
181 sub _bop_auth {
182   my ($self, $options) = @_;
183
184   (
185     'login'    => $options->{payment_gateway}->gateway_username,
186     'password' => $options->{payment_gateway}->gateway_password,
187   );
188 }
189
190 sub _bop_options {
191   my ($self, $options) = @_;
192
193   $options->{payment_gateway}->gatewaynum
194     ? $options->{payment_gateway}->options
195     : @{ $options->{payment_gateway}->get('options') };
196
197 }
198
199 sub _bop_defaults {
200   my ($self, $options) = @_;
201
202   unless ( $options->{'description'} ) {
203     if ( $conf->exists('business-onlinepayment-description') ) {
204       my $dtempl = $conf->config('business-onlinepayment-description');
205
206       my $agent = $self->agent->agent;
207       #$pkgs... not here
208       $options->{'description'} = eval qq("$dtempl");
209     } else {
210       $options->{'description'} = 'Internet services';
211     }
212   }
213
214   $options->{payinfo} = $self->payinfo unless exists( $options->{payinfo} );
215   $options->{invnum} ||= '';
216   $options->{payname} = $self->payname unless exists( $options->{payname} );
217 }
218
219 sub _bop_content {
220   my ($self, $options) = @_;
221   my %content = ();
222
223   my $payip = exists($options->{'payip'}) ? $options->{'payip'} : $self->payip;
224   $content{customer_ip} = $payip if length($payip);
225
226   $content{invoice_number} = $options->{'invnum'}
227     if exists($options->{'invnum'}) && length($options->{'invnum'});
228
229   $content{email_customer} = 
230     (    $conf->exists('business-onlinepayment-email_customer')
231       || $conf->exists('business-onlinepayment-email-override') );
232       
233   my ($payname, $payfirst, $paylast);
234   if ( $options->{payname} && $options->{method} ne 'ECHECK' ) {
235     ($payname = $options->{payname}) =~
236       /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
237       or return "Illegal payname $payname";
238     ($payfirst, $paylast) = ($1, $2);
239   } else {
240     $payfirst = $self->getfield('first');
241     $paylast = $self->getfield('last');
242     $payname = "$payfirst $paylast";
243   }
244
245   $content{last_name} = $paylast;
246   $content{first_name} = $payfirst;
247
248   $content{name} = $payname;
249
250   $content{address} = exists($options->{'address1'})
251                         ? $options->{'address1'}
252                         : $self->address1;
253   my $address2 = exists($options->{'address2'})
254                    ? $options->{'address2'}
255                    : $self->address2;
256   $content{address} .= ", ". $address2 if length($address2);
257
258   $content{city} = exists($options->{city})
259                      ? $options->{city}
260                      : $self->city;
261   $content{state} = exists($options->{state})
262                       ? $options->{state}
263                       : $self->state;
264   $content{zip} = exists($options->{zip})
265                     ? $options->{'zip'}
266                     : $self->zip;
267   $content{country} = exists($options->{country})
268                         ? $options->{country}
269                         : $self->country;
270
271   $content{referer} = 'http://cleanwhisker.420.am/'; #XXX fix referer :/
272   $content{phone} = $self->daytime || $self->night;
273
274   \%content;
275 }
276
277 my %bop_method2payby = (
278   'CC'     => 'CARD',
279   'ECHECK' => 'CHEK',
280   'LEC'    => 'LECB',
281 );
282
283 sub realtime_bop {
284   my $self = shift;
285
286   my %options = ();
287   if (ref($_[0]) eq 'HASH') {
288     %options = %{$_[0]};
289   } else {
290     my ( $method, $amount ) = ( shift, shift );
291     %options = @_;
292     $options{method} = $method;
293     $options{amount} = $amount;
294   }
295   
296   if ( $DEBUG ) {
297     warn "$me realtime_bop (new): $options{method} $options{amount}\n";
298     warn "  $_ => $options{$_}\n" foreach keys %options;
299   }
300
301   return $self->fake_bop(%options) if $options{'fake'};
302
303   $self->_bop_defaults(\%options);
304
305   ###
306   # set trans_is_recur based on invnum if there is one
307   ###
308
309   my $trans_is_recur = 0;
310   if ( $options{'invnum'} ) {
311
312     my $cust_bill = qsearchs('cust_bill', { 'invnum' => $options{'invnum'} } );
313     die "invnum ". $options{'invnum'}. " not found" unless $cust_bill;
314
315     my @part_pkg =
316       map  { $_->part_pkg }
317       grep { $_ }
318       map  { $_->cust_pkg }
319       $cust_bill->cust_bill_pkg;
320
321     $trans_is_recur = 1
322       if grep { $_->freq ne '0' } @part_pkg;
323
324   }
325
326   ###
327   # select a gateway
328   ###
329
330   my $payment_gateway =  $self->_payment_gateway( \%options );
331   my $namespace = $payment_gateway->gateway_namespace;
332
333   eval "use $namespace";  
334   die $@ if $@;
335
336   ###
337   # check for banned credit card/ACH
338   ###
339
340   my $ban = qsearchs('banned_pay', {
341     'payby'   => $bop_method2payby{$options{method}},
342     'payinfo' => md5_base64($options{payinfo}),
343   } );
344   return "Banned credit card" if $ban;
345
346   ###
347   # massage data
348   ###
349
350   my $bop_content = $self->_bop_content(\%options);
351   return $bop_content unless ref($bop_content);
352
353   my @invoicing_list = $self->invoicing_list_emailonly;
354   if ( $conf->exists('emailinvoiceautoalways')
355        || $conf->exists('emailinvoiceauto') && ! @invoicing_list
356        || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
357     push @invoicing_list, $self->all_emails;
358   }
359
360   my $email = ($conf->exists('business-onlinepayment-email-override'))
361               ? $conf->config('business-onlinepayment-email-override')
362               : $invoicing_list[0];
363
364   my $paydate = '';
365   my %content = ();
366   if ( $namespace eq 'Business::OnlinePayment' && $options{method} eq 'CC' ) {
367
368     $content{card_number} = $options{payinfo};
369     $paydate = exists($options{'paydate'})
370                     ? $options{'paydate'}
371                     : $self->paydate;
372     $paydate =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
373     $content{expiration} = "$2/$1";
374
375     my $paycvv = exists($options{'paycvv'})
376                    ? $options{'paycvv'}
377                    : $self->paycvv;
378     $content{cvv2} = $paycvv
379       if length($paycvv);
380
381     my $paystart_month = exists($options{'paystart_month'})
382                            ? $options{'paystart_month'}
383                            : $self->paystart_month;
384
385     my $paystart_year  = exists($options{'paystart_year'})
386                            ? $options{'paystart_year'}
387                            : $self->paystart_year;
388
389     $content{card_start} = "$paystart_month/$paystart_year"
390       if $paystart_month && $paystart_year;
391
392     my $payissue       = exists($options{'payissue'})
393                            ? $options{'payissue'}
394                            : $self->payissue;
395     $content{issue_number} = $payissue if $payissue;
396
397     if ( $self->_bop_recurring_billing( 'payinfo'        => $options{'payinfo'},
398                                         'trans_is_recur' => $trans_is_recur,
399                                       )
400        )
401     {
402       $content{recurring_billing} = 'YES';
403       $content{acct_code} = 'rebill'
404         if $conf->exists('credit_card-recurring_billing_acct_code');
405     }
406
407   } elsif ( $namespace eq 'Business::OnlinePayment' && $options{method} eq 'ECHECK' ){
408     ( $content{account_number}, $content{routing_code} ) =
409       split('@', $options{payinfo});
410     $content{bank_name} = $options{payname};
411     $content{bank_state} = exists($options{'paystate'})
412                              ? $options{'paystate'}
413                              : $self->getfield('paystate');
414     $content{account_type} = exists($options{'paytype'})
415                                ? uc($options{'paytype'}) || 'CHECKING'
416                                : uc($self->getfield('paytype')) || 'CHECKING';
417     $content{account_name} = $self->getfield('first'). ' '.
418                              $self->getfield('last');
419
420     $content{customer_org} = $self->company ? 'B' : 'I';
421     $content{state_id}       = exists($options{'stateid'})
422                                  ? $options{'stateid'}
423                                  : $self->getfield('stateid');
424     $content{state_id_state} = exists($options{'stateid_state'})
425                                  ? $options{'stateid_state'}
426                                  : $self->getfield('stateid_state');
427     $content{customer_ssn} = exists($options{'ss'})
428                                ? $options{'ss'}
429                                : $self->ss;
430   } elsif ( $namespace eq 'Business::OnlinePayment' && $options{method} eq 'LEC' ) {
431     $content{phone} = $options{payinfo};
432   } elsif ( $namespace eq 'Business::OnlineThirdPartyPayment' ) {
433     #move along
434   } else {
435     #die an evil death
436   }
437
438   ###
439   # run transaction(s)
440   ###
441
442   my $balance = exists( $options{'balance'} )
443                   ? $options{'balance'}
444                   : $self->balance;
445
446   $self->select_for_update; #mutex ... just until we get our pending record in
447
448   #the checks here are intended to catch concurrent payments
449   #double-form-submission prevention is taken care of in cust_pay_pending::check
450
451   #check the balance
452   return "The customer's balance has changed; $options{method} transaction aborted."
453     if $self->balance < $balance;
454     #&& $self->balance < $options{amount}; #might as well anyway?
455
456   #also check and make sure there aren't *other* pending payments for this cust
457
458   my @pending = qsearch('cust_pay_pending', {
459     'custnum' => $self->custnum,
460     'status'  => { op=>'!=', value=>'done' } 
461   });
462   return "A payment is already being processed for this customer (".
463          join(', ', map 'paypendingnum '. $_->paypendingnum, @pending ).
464          "); $options{method} transaction aborted."
465     if scalar(@pending);
466
467   #okay, good to go, if we're a duplicate, cust_pay_pending will kick us out
468
469   my $cust_pay_pending = new FS::cust_pay_pending {
470     'custnum'           => $self->custnum,
471     #'invnum'            => $options{'invnum'},
472     'paid'              => $options{amount},
473     '_date'             => '',
474     'payby'             => $bop_method2payby{$options{method}},
475     'payinfo'           => $options{payinfo},
476     'paydate'           => $paydate,
477     'recurring_billing' => $content{recurring_billing},
478     'pkgnum'            => $options{'pkgnum'},
479     'status'            => 'new',
480     'gatewaynum'        => $payment_gateway->gatewaynum || '',
481     'session_id'        => $options{session_id} || '',
482     'jobnum'            => $options{depend_jobnum} || '',
483   };
484   $cust_pay_pending->payunique( $options{payunique} )
485     if defined($options{payunique}) && length($options{payunique});
486   my $cpp_new_err = $cust_pay_pending->insert; #mutex lost when this is inserted
487   return $cpp_new_err if $cpp_new_err;
488
489   my( $action1, $action2 ) =
490     split( /\s*\,\s*/, $payment_gateway->gateway_action );
491
492   my $transaction = new $namespace( $payment_gateway->gateway_module,
493                                     $self->_bop_options(\%options),
494                                   );
495
496   $transaction->content(
497     'type'           => $options{method},
498     $self->_bop_auth(\%options),          
499     'action'         => $action1,
500     'description'    => $options{'description'},
501     'amount'         => $options{amount},
502     #'invoice_number' => $options{'invnum'},
503     'customer_id'    => $self->custnum,
504     %$bop_content,
505     'reference'      => $cust_pay_pending->paypendingnum, #for now
506     'email'          => $email,
507     %content, #after
508   );
509
510   $cust_pay_pending->status('pending');
511   my $cpp_pending_err = $cust_pay_pending->replace;
512   return $cpp_pending_err if $cpp_pending_err;
513
514   #config?
515   my $BOP_TESTING = 0;
516   my $BOP_TESTING_SUCCESS = 1;
517
518   unless ( $BOP_TESTING ) {
519     $transaction->test_transaction(1)
520       if $conf->exists('business-onlinepayment-test_transaction');
521     $transaction->submit();
522   } else {
523     if ( $BOP_TESTING_SUCCESS ) {
524       $transaction->is_success(1);
525       $transaction->authorization('fake auth');
526     } else {
527       $transaction->is_success(0);
528       $transaction->error_message('fake failure');
529     }
530   }
531
532   if ( $transaction->is_success() && $namespace eq 'Business::OnlineThirdPartyPayment' ) {
533
534     return { reference => $cust_pay_pending->paypendingnum,
535              map { $_ => $transaction->$_ } qw ( popup_url collectitems ) };
536
537   } elsif ( $transaction->is_success() && $action2 ) {
538
539     $cust_pay_pending->status('authorized');
540     my $cpp_authorized_err = $cust_pay_pending->replace;
541     return $cpp_authorized_err if $cpp_authorized_err;
542
543     my $auth = $transaction->authorization;
544     my $ordernum = $transaction->can('order_number')
545                    ? $transaction->order_number
546                    : '';
547
548     my $capture =
549       new Business::OnlinePayment( $payment_gateway->gateway_module,
550                                    $self->_bop_options(\%options),
551                                  );
552
553     my %capture = (
554       %content,
555       type           => $options{method},
556       action         => $action2,
557       $self->_bop_auth(\%options),          
558       order_number   => $ordernum,
559       amount         => $options{amount},
560       authorization  => $auth,
561       description    => $options{'description'},
562     );
563
564     foreach my $field (qw( authorization_source_code returned_ACI
565                            transaction_identifier validation_code           
566                            transaction_sequence_num local_transaction_date    
567                            local_transaction_time AVS_result_code          )) {
568       $capture{$field} = $transaction->$field() if $transaction->can($field);
569     }
570
571     $capture->content( %capture );
572
573     $capture->test_transaction(1)
574       if $conf->exists('business-onlinepayment-test_transaction');
575     $capture->submit();
576
577     unless ( $capture->is_success ) {
578       my $e = "Authorization successful but capture failed, custnum #".
579               $self->custnum. ': '.  $capture->result_code.
580               ": ". $capture->error_message;
581       warn $e;
582       return $e;
583     }
584
585   }
586
587   ###
588   # remove paycvv after initial transaction
589   ###
590
591   #false laziness w/misc/process/payment.cgi - check both to make sure working
592   # correctly
593   if ( length($self->paycvv)
594        && ! grep { $_ eq cardtype($options{payinfo}) } $conf->config('cvv-save')
595   ) {
596     my $error = $self->remove_cvv;
597     if ( $error ) {
598       warn "WARNING: error removing cvv: $error\n";
599     }
600   }
601
602   ###
603   # Tokenize
604   ###
605
606
607   if ( $transaction->can('card_token') && $transaction->card_token ) {
608
609     $self->card_token($transaction->card_token);
610
611     if ( $options{'payinfo'} eq $self->payinfo ) {
612       $self->payinfo($transaction->card_token);
613       my $error = $self->replace;
614       if ( $error ) {
615         warn "WARNING: error storing token: $error, but proceeding anyway\n";
616       }
617     }
618
619   }
620
621   ###
622   # result handling
623   ###
624
625   $self->_realtime_bop_result( $cust_pay_pending, $transaction, %options );
626
627 }
628
629 =item fake_bop
630
631 =cut
632
633 sub fake_bop {
634   my $self = shift;
635
636   my %options = ();
637   if (ref($_[0]) eq 'HASH') {
638     %options = %{$_[0]};
639   } else {
640     my ( $method, $amount ) = ( shift, shift );
641     %options = @_;
642     $options{method} = $method;
643     $options{amount} = $amount;
644   }
645   
646   if ( $options{'fake_failure'} ) {
647      return "Error: No error; test failure requested with fake_failure";
648   }
649
650   #my $paybatch = '';
651   #if ( $payment_gateway->gatewaynum ) { # agent override
652   #  $paybatch = $payment_gateway->gatewaynum. '-';
653   #}
654   #
655   #$paybatch .= "$processor:". $transaction->authorization;
656   #
657   #$paybatch .= ':'. $transaction->order_number
658   #  if $transaction->can('order_number')
659   #  && length($transaction->order_number);
660
661   my $paybatch = 'FakeProcessor:54:32';
662
663   my $cust_pay = new FS::cust_pay ( {
664      'custnum'  => $self->custnum,
665      'invnum'   => $options{'invnum'},
666      'paid'     => $options{amount},
667      '_date'    => '',
668      'payby'    => $bop_method2payby{$options{method}},
669      #'payinfo'  => $payinfo,
670      'payinfo'  => '4111111111111111',
671      'paybatch' => $paybatch,
672      #'paydate'  => $paydate,
673      'paydate'  => '2012-05-01',
674   } );
675   $cust_pay->payunique( $options{payunique} ) if length($options{payunique});
676
677   my $error = $cust_pay->insert($options{'manual'} ? ( 'manual' => 1 ) : () );
678
679   if ( $error ) {
680     $cust_pay->invnum(''); #try again with no specific invnum
681     my $error2 = $cust_pay->insert( $options{'manual'} ?
682                                     ( 'manual' => 1 ) : ()
683                                   );
684     if ( $error2 ) {
685       # gah, even with transactions.
686       my $e = 'WARNING: Card/ACH debited but database not updated - '.
687               "error inserting (fake!) payment: $error2".
688               " (previously tried insert with invnum #$options{'invnum'}" .
689               ": $error )";
690       warn $e;
691       return $e;
692     }
693   }
694
695   if ( $options{'paynum_ref'} ) {
696     ${ $options{'paynum_ref'} } = $cust_pay->paynum;
697   }
698
699   return ''; #no error
700
701 }
702
703
704 # item _realtime_bop_result CUST_PAY_PENDING, BOP_OBJECT [ OPTION => VALUE ... ]
705
706 # Wraps up processing of a realtime credit card, ACH (electronic check) or
707 # phone bill transaction.
708
709 sub _realtime_bop_result {
710   my( $self, $cust_pay_pending, $transaction, %options ) = @_;
711   if ( $DEBUG ) {
712     warn "$me _realtime_bop_result: pending transaction ".
713       $cust_pay_pending->paypendingnum. "\n";
714     warn "  $_ => $options{$_}\n" foreach keys %options;
715   }
716
717   my $payment_gateway = $options{payment_gateway}
718     or return "no payment gateway in arguments to _realtime_bop_result";
719
720   $cust_pay_pending->status($transaction->is_success() ? 'captured' : 'declined');
721   my $cpp_captured_err = $cust_pay_pending->replace;
722   return $cpp_captured_err if $cpp_captured_err;
723
724   if ( $transaction->is_success() ) {
725
726     my $paybatch = '';
727     if ( $payment_gateway->gatewaynum ) { # agent override
728       $paybatch = $payment_gateway->gatewaynum. '-';
729     }
730
731     $paybatch .= $payment_gateway->gateway_module. ":".
732       $transaction->authorization;
733
734     $paybatch .= ':'. $transaction->order_number
735       if $transaction->can('order_number')
736       && length($transaction->order_number);
737
738     my $cust_pay = new FS::cust_pay ( {
739        'custnum'  => $self->custnum,
740        'invnum'   => $options{'invnum'},
741        'paid'     => $cust_pay_pending->paid,
742        '_date'    => '',
743        'payby'    => $cust_pay_pending->payby,
744        'payinfo'  => $options{'payinfo'},
745        'paybatch' => $paybatch,
746        'paydate'  => $cust_pay_pending->paydate,
747        'pkgnum'   => $cust_pay_pending->pkgnum,
748     } );
749     #doesn't hurt to know, even though the dup check is in cust_pay_pending now
750     $cust_pay->payunique( $options{payunique} )
751       if defined($options{payunique}) && length($options{payunique});
752
753     my $oldAutoCommit = $FS::UID::AutoCommit;
754     local $FS::UID::AutoCommit = 0;
755     my $dbh = dbh;
756
757     #start a transaction, insert the cust_pay and set cust_pay_pending.status to done in a single transction
758
759     my $error = $cust_pay->insert($options{'manual'} ? ( 'manual' => 1 ) : () );
760
761     if ( $error ) {
762       $cust_pay->invnum(''); #try again with no specific invnum
763       my $error2 = $cust_pay->insert( $options{'manual'} ?
764                                       ( 'manual' => 1 ) : ()
765                                     );
766       if ( $error2 ) {
767         # gah.  but at least we have a record of the state we had to abort in
768         # from cust_pay_pending now.
769         my $e = "WARNING: $options{method} captured but payment not recorded -".
770                 " error inserting payment (". $payment_gateway->gateway_module.
771                 "): $error2".
772                 " (previously tried insert with invnum #$options{'invnum'}" .
773                 ": $error ) - pending payment saved as paypendingnum ".
774                 $cust_pay_pending->paypendingnum. "\n";
775         warn $e;
776         return $e;
777       }
778     }
779
780     my $jobnum = $cust_pay_pending->jobnum;
781     if ( $jobnum ) {
782        my $placeholder = qsearchs( 'queue', { 'jobnum' => $jobnum } );
783       
784        unless ( $placeholder ) {
785          $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
786          my $e = "WARNING: $options{method} captured but job $jobnum not ".
787              "found for paypendingnum ". $cust_pay_pending->paypendingnum. "\n";
788          warn $e;
789          return $e;
790        }
791
792        $error = $placeholder->delete;
793
794        if ( $error ) {
795          $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
796          my $e = "WARNING: $options{method} captured but could not delete ".
797               "job $jobnum for paypendingnum ".
798               $cust_pay_pending->paypendingnum. ": $error\n";
799          warn $e;
800          return $e;
801        }
802
803     }
804     
805     if ( $options{'paynum_ref'} ) {
806       ${ $options{'paynum_ref'} } = $cust_pay->paynum;
807     }
808
809     $cust_pay_pending->status('done');
810     $cust_pay_pending->statustext('captured');
811     $cust_pay_pending->paynum($cust_pay->paynum);
812     my $cpp_done_err = $cust_pay_pending->replace;
813
814     if ( $cpp_done_err ) {
815
816       $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
817       my $e = "WARNING: $options{method} captured but payment not recorded - ".
818               "error updating status for paypendingnum ".
819               $cust_pay_pending->paypendingnum. ": $cpp_done_err \n";
820       warn $e;
821       return $e;
822
823     } else {
824
825       $dbh->commit or die $dbh->errstr if $oldAutoCommit;
826
827       if ( $options{'apply'} ) {
828         my $apply_error = $self->apply_payments_and_credits;
829         if ( $apply_error ) {
830           warn "WARNING: error applying payment: $apply_error\n";
831           #but we still should return no error cause the payment otherwise went
832           #through...
833         }
834       }
835
836       return ''; #no error
837
838     }
839
840   } else {
841
842     my $perror = $payment_gateway->gateway_module. " error: ".
843       $transaction->error_message;
844
845     my $jobnum = $cust_pay_pending->jobnum;
846     if ( $jobnum ) {
847        my $placeholder = qsearchs( 'queue', { 'jobnum' => $jobnum } );
848       
849        if ( $placeholder ) {
850          my $error = $placeholder->depended_delete;
851          $error ||= $placeholder->delete;
852          warn "error removing provisioning jobs after declined paypendingnum ".
853            $cust_pay_pending->paypendingnum. "\n";
854        } else {
855          my $e = "error finding job $jobnum for declined paypendingnum ".
856               $cust_pay_pending->paypendingnum. "\n";
857          warn $e;
858        }
859
860     }
861     
862     unless ( $transaction->error_message ) {
863
864       my $t_response;
865       if ( $transaction->can('response_page') ) {
866         $t_response = {
867                         'page'    => ( $transaction->can('response_page')
868                                          ? $transaction->response_page
869                                          : ''
870                                      ),
871                         'code'    => ( $transaction->can('response_code')
872                                          ? $transaction->response_code
873                                          : ''
874                                      ),
875                         'headers' => ( $transaction->can('response_headers')
876                                          ? $transaction->response_headers
877                                          : ''
878                                      ),
879                       };
880       } else {
881         $t_response .=
882           "No additional debugging information available for ".
883             $payment_gateway->gateway_module;
884       }
885
886       $perror .= "No error_message returned from ".
887                    $payment_gateway->gateway_module. " -- ".
888                  ( ref($t_response) ? Dumper($t_response) : $t_response );
889
890     }
891
892     if ( !$options{'quiet'} && !$realtime_bop_decline_quiet
893          && $conf->exists('emaildecline')
894          && grep { $_ ne 'POST' } $self->invoicing_list
895          && ! grep { $transaction->error_message =~ /$_/ }
896                    $conf->config('emaildecline-exclude')
897     ) {
898
899       # Send a decline alert to the customer.
900       my $msgnum = $conf->config('decline_msgnum', $self->agentnum);
901       my $error = '';
902       if ( $msgnum ) {
903         # include the raw error message in the transaction state
904         $cust_pay_pending->setfield('error', $transaction->error_message);
905         my $msg_template = qsearchs('msg_template', { msgnum => $msgnum });
906         $error = $msg_template->send( 'cust_main' => $self,
907                                       'object'    => $cust_pay_pending );
908       }
909       else { #!$msgnum
910
911         my @templ = $conf->config('declinetemplate');
912         my $template = new Text::Template (
913           TYPE   => 'ARRAY',
914           SOURCE => [ map "$_\n", @templ ],
915         ) or return "($perror) can't create template: $Text::Template::ERROR";
916         $template->compile()
917           or return "($perror) can't compile template: $Text::Template::ERROR";
918
919         my $templ_hash = {
920           'company_name'    =>
921             scalar( $conf->config('company_name', $self->agentnum ) ),
922           'company_address' =>
923             join("\n", $conf->config('company_address', $self->agentnum ) ),
924           'error'           => $transaction->error_message,
925         };
926
927         my $error = send_email(
928           'from'    => $conf->config('invoice_from', $self->agentnum ),
929           'to'      => [ grep { $_ ne 'POST' } $self->invoicing_list ],
930           'subject' => 'Your payment could not be processed',
931           'body'    => [ $template->fill_in(HASH => $templ_hash) ],
932         );
933       }
934
935       $perror .= " (also received error sending decline notification: $error)"
936         if $error;
937
938     }
939
940     $cust_pay_pending->status('done');
941     $cust_pay_pending->statustext("declined: $perror");
942     my $cpp_done_err = $cust_pay_pending->replace;
943     if ( $cpp_done_err ) {
944       my $e = "WARNING: $options{method} declined but pending payment not ".
945               "resolved - error updating status for paypendingnum ".
946               $cust_pay_pending->paypendingnum. ": $cpp_done_err \n";
947       warn $e;
948       $perror = "$e ($perror)";
949     }
950
951     return $perror;
952   }
953
954 }
955
956 =item realtime_botpp_capture CUST_PAY_PENDING [ OPTION => VALUE ... ]
957
958 Verifies successful third party processing of a realtime credit card,
959 ACH (electronic check) or phone bill transaction via a
960 Business::OnlineThirdPartyPayment realtime gateway.  See
961 L<http://420.am/business-onlinethirdpartypayment> for supported gateways.
962
963 Available options are: I<description>, I<invnum>, I<quiet>, I<paynum_ref>, I<payunique>
964
965 The additional options I<payname>, I<city>, I<state>,
966 I<zip>, I<payinfo> and I<paydate> are also available.  Any of these options,
967 if set, will override the value from the customer record.
968
969 I<description> is a free-text field passed to the gateway.  It defaults to
970 "Internet services".
971
972 If an I<invnum> is specified, this payment (if successful) is applied to the
973 specified invoice.  If you don't specify an I<invnum> you might want to
974 call the B<apply_payments> method.
975
976 I<quiet> can be set true to surpress email decline notices.
977
978 I<paynum_ref> can be set to a scalar reference.  It will be filled in with the
979 resulting paynum, if any.
980
981 I<payunique> is a unique identifier for this payment.
982
983 Returns a hashref containing elements bill_error (which will be undefined
984 upon success) and session_id of any associated session.
985
986 =cut
987
988 sub realtime_botpp_capture {
989   my( $self, $cust_pay_pending, %options ) = @_;
990   if ( $DEBUG ) {
991     warn "$me realtime_botpp_capture: pending transaction $cust_pay_pending\n";
992     warn "  $_ => $options{$_}\n" foreach keys %options;
993   }
994
995   eval "use Business::OnlineThirdPartyPayment";  
996   die $@ if $@;
997
998   ###
999   # select the gateway
1000   ###
1001
1002   my $method = FS::payby->payby2bop($cust_pay_pending->payby);
1003
1004   my $payment_gateway = $cust_pay_pending->gatewaynum
1005     ? qsearchs( 'payment_gateway',
1006                 { gatewaynum => $cust_pay_pending->gatewaynum }
1007               )
1008     : $self->agent->payment_gateway( 'method' => $method,
1009                                      # 'invnum'  => $cust_pay_pending->invnum,
1010                                      # 'payinfo' => $cust_pay_pending->payinfo,
1011                                    );
1012
1013   $options{payment_gateway} = $payment_gateway; # for the helper subs
1014
1015   ###
1016   # massage data
1017   ###
1018
1019   my @invoicing_list = $self->invoicing_list_emailonly;
1020   if ( $conf->exists('emailinvoiceautoalways')
1021        || $conf->exists('emailinvoiceauto') && ! @invoicing_list
1022        || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
1023     push @invoicing_list, $self->all_emails;
1024   }
1025
1026   my $email = ($conf->exists('business-onlinepayment-email-override'))
1027               ? $conf->config('business-onlinepayment-email-override')
1028               : $invoicing_list[0];
1029
1030   my %content = ();
1031
1032   $content{email_customer} = 
1033     (    $conf->exists('business-onlinepayment-email_customer')
1034       || $conf->exists('business-onlinepayment-email-override') );
1035       
1036   ###
1037   # run transaction(s)
1038   ###
1039
1040   my $transaction =
1041     new Business::OnlineThirdPartyPayment( $payment_gateway->gateway_module,
1042                                            $self->_bop_options(\%options),
1043                                          );
1044
1045   $transaction->reference({ %options }); 
1046
1047   $transaction->content(
1048     'type'           => $method,
1049     $self->_bop_auth(\%options),
1050     'action'         => 'Post Authorization',
1051     'description'    => $options{'description'},
1052     'amount'         => $cust_pay_pending->paid,
1053     #'invoice_number' => $options{'invnum'},
1054     'customer_id'    => $self->custnum,
1055     'referer'        => 'http://cleanwhisker.420.am/',
1056     'reference'      => $cust_pay_pending->paypendingnum,
1057     'email'          => $email,
1058     'phone'          => $self->daytime || $self->night,
1059     %content, #after
1060     # plus whatever is required for bogus capture avoidance
1061   );
1062
1063   $transaction->submit();
1064
1065   my $error =
1066     $self->_realtime_bop_result( $cust_pay_pending, $transaction, %options );
1067
1068   {
1069     bill_error => $error,
1070     session_id => $cust_pay_pending->session_id,
1071   }
1072
1073 }
1074
1075 =item default_payment_gateway
1076
1077 DEPRECATED -- use agent->payment_gateway
1078
1079 =cut
1080
1081 sub default_payment_gateway {
1082   my( $self, $method ) = @_;
1083
1084   die "Real-time processing not enabled\n"
1085     unless $conf->exists('business-onlinepayment');
1086
1087   #warn "default_payment_gateway deprecated -- use agent->payment_gateway\n";
1088
1089   #load up config
1090   my $bop_config = 'business-onlinepayment';
1091   $bop_config .= '-ach'
1092     if $method =~ /^(ECHECK|CHEK)$/ && $conf->exists($bop_config. '-ach');
1093   my ( $processor, $login, $password, $action, @bop_options ) =
1094     $conf->config($bop_config);
1095   $action ||= 'normal authorization';
1096   pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
1097   die "No real-time processor is enabled - ".
1098       "did you set the business-onlinepayment configuration value?\n"
1099     unless $processor;
1100
1101   ( $processor, $login, $password, $action, @bop_options )
1102 }
1103
1104 =item realtime_refund_bop METHOD [ OPTION => VALUE ... ]
1105
1106 Refunds a realtime credit card, ACH (electronic check) or phone bill transaction
1107 via a Business::OnlinePayment realtime gateway.  See
1108 L<http://420.am/business-onlinepayment> for supported gateways.
1109
1110 Available methods are: I<CC>, I<ECHECK> and I<LEC>
1111
1112 Available options are: I<amount>, I<reason>, I<paynum>, I<paydate>
1113
1114 Most gateways require a reference to an original payment transaction to refund,
1115 so you probably need to specify a I<paynum>.
1116
1117 I<amount> defaults to the original amount of the payment if not specified.
1118
1119 I<reason> specifies a reason for the refund.
1120
1121 I<paydate> specifies the expiration date for a credit card overriding the
1122 value from the customer record or the payment record. Specified as yyyy-mm-dd
1123
1124 Implementation note: If I<amount> is unspecified or equal to the amount of the
1125 orignal payment, first an attempt is made to "void" the transaction via
1126 the gateway (to cancel a not-yet settled transaction) and then if that fails,
1127 the normal attempt is made to "refund" ("credit") the transaction via the
1128 gateway is attempted.
1129
1130 #The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
1131 #I<zip>, I<payinfo> and I<paydate> are also available.  Any of these options,
1132 #if set, will override the value from the customer record.
1133
1134 #If an I<invnum> is specified, this payment (if successful) is applied to the
1135 #specified invoice.  If you don't specify an I<invnum> you might want to
1136 #call the B<apply_payments> method.
1137
1138 =cut
1139
1140 #some false laziness w/realtime_bop, not enough to make it worth merging
1141 #but some useful small subs should be pulled out
1142 sub realtime_refund_bop {
1143   my $self = shift;
1144
1145   my %options = ();
1146   if (ref($_[0]) eq 'HASH') {
1147     %options = %{$_[0]};
1148   } else {
1149     my $method = shift;
1150     %options = @_;
1151     $options{method} = $method;
1152   }
1153
1154   if ( $DEBUG ) {
1155     warn "$me realtime_refund_bop (new): $options{method} refund\n";
1156     warn "  $_ => $options{$_}\n" foreach keys %options;
1157   }
1158
1159   ###
1160   # look up the original payment and optionally a gateway for that payment
1161   ###
1162
1163   my $cust_pay = '';
1164   my $amount = $options{'amount'};
1165
1166   my( $processor, $login, $password, @bop_options, $namespace ) ;
1167   my( $auth, $order_number ) = ( '', '', '' );
1168
1169   if ( $options{'paynum'} ) {
1170
1171     warn "  paynum: $options{paynum}\n" if $DEBUG > 1;
1172     $cust_pay = qsearchs('cust_pay', { paynum=>$options{'paynum'} } )
1173       or return "Unknown paynum $options{'paynum'}";
1174     $amount ||= $cust_pay->paid;
1175
1176     $cust_pay->paybatch =~ /^((\d+)\-)?(\w+):\s*([\w\-\/ ]*)(:([\w\-]+))?$/
1177       or return "Can't parse paybatch for paynum $options{'paynum'}: ".
1178                 $cust_pay->paybatch;
1179     my $gatewaynum = '';
1180     ( $gatewaynum, $processor, $auth, $order_number ) = ( $2, $3, $4, $6 );
1181
1182     if ( $gatewaynum ) { #gateway for the payment to be refunded
1183
1184       my $payment_gateway =
1185         qsearchs('payment_gateway', { 'gatewaynum' => $gatewaynum } );
1186       die "payment gateway $gatewaynum not found"
1187         unless $payment_gateway;
1188
1189       $processor   = $payment_gateway->gateway_module;
1190       $login       = $payment_gateway->gateway_username;
1191       $password    = $payment_gateway->gateway_password;
1192       $namespace   = $payment_gateway->gateway_namespace;
1193       @bop_options = $payment_gateway->options;
1194
1195     } else { #try the default gateway
1196
1197       my $conf_processor;
1198       my $payment_gateway =
1199         $self->agent->payment_gateway('method' => $options{method});
1200
1201       ( $conf_processor, $login, $password, $namespace ) =
1202         map { my $method = "gateway_$_"; $payment_gateway->$method }
1203           qw( module username password namespace );
1204
1205       @bop_options = $payment_gateway->gatewaynum
1206                        ? $payment_gateway->options
1207                        : @{ $payment_gateway->get('options') };
1208
1209       return "processor of payment $options{'paynum'} $processor does not".
1210              " match default processor $conf_processor"
1211         unless $processor eq $conf_processor;
1212
1213     }
1214
1215
1216   } else { # didn't specify a paynum, so look for agent gateway overrides
1217            # like a normal transaction 
1218  
1219     my $payment_gateway =
1220       $self->agent->payment_gateway( 'method'  => $options{method},
1221                                      #'payinfo' => $payinfo,
1222                                    );
1223     my( $processor, $login, $password, $namespace ) =
1224       map { my $method = "gateway_$_"; $payment_gateway->$method }
1225         qw( module username password namespace );
1226
1227     my @bop_options = $payment_gateway->gatewaynum
1228                         ? $payment_gateway->options
1229                         : @{ $payment_gateway->get('options') };
1230
1231   }
1232   return "neither amount nor paynum specified" unless $amount;
1233
1234   eval "use $namespace";  
1235   die $@ if $@;
1236
1237   my %content = (
1238     'type'           => $options{method},
1239     'login'          => $login,
1240     'password'       => $password,
1241     'order_number'   => $order_number,
1242     'amount'         => $amount,
1243     'referer'        => 'http://cleanwhisker.420.am/', #XXX fix referer :/
1244   );
1245   $content{authorization} = $auth
1246     if length($auth); #echeck/ACH transactions have an order # but no auth
1247                       #(at least with authorize.net)
1248
1249   my $disable_void_after;
1250   if ($conf->exists('disable_void_after')
1251       && $conf->config('disable_void_after') =~ /^(\d+)$/) {
1252     $disable_void_after = $1;
1253   }
1254
1255   #first try void if applicable
1256   if ( $cust_pay && $cust_pay->paid == $amount
1257     && (
1258       ( not defined($disable_void_after) )
1259       || ( time < ($cust_pay->_date + $disable_void_after ) )
1260     )
1261   ) {
1262     warn "  attempting void\n" if $DEBUG > 1;
1263     my $void = new Business::OnlinePayment( $processor, @bop_options );
1264     if ( $void->can('info') ) {
1265       if ( $cust_pay->payby eq 'CARD'
1266            && $void->info('CC_void_requires_card') )
1267       {
1268         $content{'card_number'} = $cust_pay->payinfo;
1269       } elsif ( $cust_pay->payby eq 'CHEK'
1270                 && $void->info('ECHECK_void_requires_account') )
1271       {
1272         ( $content{'account_number'}, $content{'routing_code'} ) =
1273           split('@', $cust_pay->payinfo);
1274         $content{'name'} = $self->get('first'). ' '. $self->get('last');
1275       }
1276     }
1277     $void->content( 'action' => 'void', %content );
1278     $void->test_transaction(1)
1279       if $conf->exists('business-onlinepayment-test_transaction');
1280     $void->submit();
1281     if ( $void->is_success ) {
1282       my $error = $cust_pay->void($options{'reason'});
1283       if ( $error ) {
1284         # gah, even with transactions.
1285         my $e = 'WARNING: Card/ACH voided but database not updated - '.
1286                 "error voiding payment: $error";
1287         warn $e;
1288         return $e;
1289       }
1290       warn "  void successful\n" if $DEBUG > 1;
1291       return '';
1292     }
1293   }
1294
1295   warn "  void unsuccessful, trying refund\n"
1296     if $DEBUG > 1;
1297
1298   #massage data
1299   my $address = $self->address1;
1300   $address .= ", ". $self->address2 if $self->address2;
1301
1302   my($payname, $payfirst, $paylast);
1303   if ( $self->payname && $options{method} ne 'ECHECK' ) {
1304     $payname = $self->payname;
1305     $payname =~ /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
1306       or return "Illegal payname $payname";
1307     ($payfirst, $paylast) = ($1, $2);
1308   } else {
1309     $payfirst = $self->getfield('first');
1310     $paylast = $self->getfield('last');
1311     $payname =  "$payfirst $paylast";
1312   }
1313
1314   my @invoicing_list = $self->invoicing_list_emailonly;
1315   if ( $conf->exists('emailinvoiceautoalways')
1316        || $conf->exists('emailinvoiceauto') && ! @invoicing_list
1317        || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
1318     push @invoicing_list, $self->all_emails;
1319   }
1320
1321   my $email = ($conf->exists('business-onlinepayment-email-override'))
1322               ? $conf->config('business-onlinepayment-email-override')
1323               : $invoicing_list[0];
1324
1325   my $payip = exists($options{'payip'})
1326                 ? $options{'payip'}
1327                 : $self->payip;
1328   $content{customer_ip} = $payip
1329     if length($payip);
1330
1331   my $payinfo = '';
1332   if ( $options{method} eq 'CC' ) {
1333
1334     if ( $cust_pay ) {
1335       $content{card_number} = $payinfo = $cust_pay->payinfo;
1336       (exists($options{'paydate'}) ? $options{'paydate'} : $cust_pay->paydate)
1337         =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/ &&
1338         ($content{expiration} = "$2/$1");  # where available
1339     } else {
1340       $content{card_number} = $payinfo = $self->payinfo;
1341       (exists($options{'paydate'}) ? $options{'paydate'} : $self->paydate)
1342         =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
1343       $content{expiration} = "$2/$1";
1344     }
1345
1346   } elsif ( $options{method} eq 'ECHECK' ) {
1347
1348     if ( $cust_pay ) {
1349       $payinfo = $cust_pay->payinfo;
1350     } else {
1351       $payinfo = $self->payinfo;
1352     } 
1353     ( $content{account_number}, $content{routing_code} )= split('@', $payinfo );
1354     $content{bank_name} = $self->payname;
1355     $content{account_type} = 'CHECKING';
1356     $content{account_name} = $payname;
1357     $content{customer_org} = $self->company ? 'B' : 'I';
1358     $content{customer_ssn} = $self->ss;
1359   } elsif ( $options{method} eq 'LEC' ) {
1360     $content{phone} = $payinfo = $self->payinfo;
1361   }
1362
1363   #then try refund
1364   my $refund = new Business::OnlinePayment( $processor, @bop_options );
1365   my %sub_content = $refund->content(
1366     'action'         => 'credit',
1367     'customer_id'    => $self->custnum,
1368     'last_name'      => $paylast,
1369     'first_name'     => $payfirst,
1370     'name'           => $payname,
1371     'address'        => $address,
1372     'city'           => $self->city,
1373     'state'          => $self->state,
1374     'zip'            => $self->zip,
1375     'country'        => $self->country,
1376     'email'          => $email,
1377     'phone'          => $self->daytime || $self->night,
1378     %content, #after
1379   );
1380   warn join('', map { "  $_ => $sub_content{$_}\n" } keys %sub_content )
1381     if $DEBUG > 1;
1382   $refund->test_transaction(1)
1383     if $conf->exists('business-onlinepayment-test_transaction');
1384   $refund->submit();
1385
1386   return "$processor error: ". $refund->error_message
1387     unless $refund->is_success();
1388
1389   my $paybatch = "$processor:". $refund->authorization;
1390   $paybatch .= ':'. $refund->order_number
1391     if $refund->can('order_number') && $refund->order_number;
1392
1393   while ( $cust_pay && $cust_pay->unapplied < $amount ) {
1394     my @cust_bill_pay = $cust_pay->cust_bill_pay;
1395     last unless @cust_bill_pay;
1396     my $cust_bill_pay = pop @cust_bill_pay;
1397     my $error = $cust_bill_pay->delete;
1398     last if $error;
1399   }
1400
1401   my $cust_refund = new FS::cust_refund ( {
1402     'custnum'  => $self->custnum,
1403     'paynum'   => $options{'paynum'},
1404     'refund'   => $amount,
1405     '_date'    => '',
1406     'payby'    => $bop_method2payby{$options{method}},
1407     'payinfo'  => $payinfo,
1408     'paybatch' => $paybatch,
1409     'reason'   => $options{'reason'} || 'card or ACH refund',
1410   } );
1411   my $error = $cust_refund->insert;
1412   if ( $error ) {
1413     $cust_refund->paynum(''); #try again with no specific paynum
1414     my $error2 = $cust_refund->insert;
1415     if ( $error2 ) {
1416       # gah, even with transactions.
1417       my $e = 'WARNING: Card/ACH refunded but database not updated - '.
1418               "error inserting refund ($processor): $error2".
1419               " (previously tried insert with paynum #$options{'paynum'}" .
1420               ": $error )";
1421       warn $e;
1422       return $e;
1423     }
1424   }
1425
1426   ''; #no error
1427
1428 }
1429
1430 =back
1431
1432 =head1 BUGS
1433
1434 Not autoloaded.
1435
1436 =head1 SEE ALSO
1437
1438 L<FS::cust_main>, L<FS::cust_main::Billing>
1439
1440 =cut
1441
1442 1;