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