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