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