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