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