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