1 package FS::cust_main::Billing_ThirdParty;
4 use vars qw( $DEBUG $me );
5 use FS::Record qw( qsearch qsearchs dbh );
7 use FS::cust_pay_pending;
10 $me = '[FS::cust_main::Billing_ThirdParty]';
11 # arguably doesn't even belong under cust_main...
17 =item create_payment OPTIONS
19 Create a pending payment for a third-party gateway. OPTIONS must include:
20 - method: a Business::OnlineThirdPartyPayment method argument. Currently
22 - amount: a decimal amount. Unlike in Billing_Realtime, there is NO default.
23 - session_id: the customer's self-service session ID.
25 and may optionally include:
26 - invnum: the invoice that this payment will apply to
27 - pkgnum: the package balance that this payment will apply to.
28 - description: the transaction description for the gateway.
29 - payip: the IP address the payment is initiated from
31 On failure, returns a simple string error message. On success, returns
32 a hashref of 'url' => the URL to redirect the user to to complete payment,
33 and optionally 'post_params' => a hashref of name/value pairs to be POSTed
38 my @methods = qw(PAYPAL CC);
39 my %method2payby = ( 'PAYPAL' => 'PPAL', 'CC' => 'MCRD' );
45 # avoid duplicating this--we just need description and invnum
47 $self->_bop_defaults($defaults);
49 my $method = $opt{'method'} or return 'method required';
50 my $amount = $opt{'amount'} or return 'amount required';
51 return "unknown method '$method'" unless grep {$_ eq $method} @methods;
52 return "amount must be > 0" unless $amount > 0;
53 return "session_id required" unless length($opt{'session_id'});
55 my $gateway = $self->agent->payment_gateway(
60 return "no third-party gateway enabled for method $method" if !$gateway;
62 # create pending record
63 $self->select_for_update;
64 my @pending = qsearch('cust_pay_pending', {
65 'custnum' => $self->custnum,
66 'status' => { op=>'!=', value=>'done' }
69 # if there are pending payments in the 'thirdparty' state,
70 # we can safely remove them
72 if ( $_->status eq 'thirdparty' ) {
73 my $error = $_->delete;
74 return "Error deleting unfinished payment #".
75 $_->paypendingnum . ": $error\n" if $error;
77 return "A payment is already being processed for this customer.";
81 my $cpp = FS::cust_pay_pending->new({
82 'custnum' => $self->custnum,
84 'gatewaynum' => $gateway->gatewaynum,
85 'paid' => sprintf('%.2f',$opt{'amount'}),
86 'payby' => $method2payby{ $opt{'method'} },
87 'pkgnum' => $opt{'pkgnum'},
88 'invnum' => $opt{'invnum'} || $defaults->{'invnum'},
89 'session_id' => $opt{'session_id'},
92 my $error = $cpp->insert;
93 return $error if $error;
95 my $transaction = $gateway->processor;
96 # Not included in this content hash:
97 # payinfo, paydate, paycvv, any kind of recurring billing indicator,
98 # paystate, paytype (account type), stateid, ss, payname
100 # Also, unlike bop_realtime, we don't allow the magical %options hash
101 # to override the customer's information. If they need to enter a
102 # different address or something for the billing provider, they can do
103 # that after the redirect.
105 'action' => 'create',
106 'description' => $opt{'description'} || $defaults->{'description'},
108 'customer_id' => $self->custnum,
109 'email' => $self->invoicing_list_emailonly_scalar,
110 'customer_ip' => $opt{'payip'},
111 'first_name' => $self->first,
112 'last_name' => $self->last,
113 'address1' => $self->address1,
114 'address2' => $self->address2,
115 'city' => $self->city,
116 'state' => $self->state,
118 'country' => $self->country,
119 'phone' => ($self->daytime || $self->night),
124 eval { $transaction->create(%content) };
126 warn "ERROR: Executing third-party payment:\n$@\n";
127 return { error => $@ };
131 if ($transaction->is_success) {
132 $cpp->status('thirdparty');
133 # for whatever is most identifiable as the "transaction ID"
134 $cpp->payinfo($transaction->token);
135 # for anything else the transaction needs to remember
136 $cpp->statustext($transaction->statustext);
137 $error = $cpp->replace;
138 return $error if $error;
140 return {url => $transaction->redirect,
141 post_params => $transaction->post_params};
144 $cpp->status('done');
145 $cpp->statustext($transaction->error_message);
146 $error = $cpp->replace;
147 return $error if $error;
149 return $transaction->error_message;
154 =item execute_payment SESSION_ID, PARAMS
156 Complete the payment and get the status. Triggered from the return_url
157 handler; PARAMS are all of the CGI parameters we received in the redirect.
158 On failure, returns an error message. On success, returns a hashref of
159 'paynum', 'paid', 'order_number', and 'auth'.
163 sub execute_payment {
165 my $session_id = shift;
168 my $cpp = qsearchs('cust_pay_pending', {
169 'session_id' => uc($session_id),
170 'custnum' => $self->custnum,
171 'status' => 'thirdparty',
173 or return 'no payment in process for this session';
175 my $gateway = FS::payment_gateway->by_key( $cpp->gatewaynum );
176 my $transaction = $gateway->processor;
177 $transaction->token($cpp->payinfo);
178 $transaction->statustext($cpp->statustext);
182 eval { $transaction->execute(%params) };
184 warn "ERROR: Executing third-party payment:\n$@\n";
185 return { error => $@ };
191 if ( $transaction->is_success ) {
193 $error = $cpp->approve(
194 'processor' => $gateway->gateway_module,
195 'order_number' => $transaction->order_number,
196 'auth' => $transaction->authorization,
200 return $error if $error;
203 'paynum' => $cpp->paynum,
204 'paid' => $cpp->paid,
205 'order_number' => $transaction->order_number,
206 'auth' => $transaction->authorization,
211 my $error = $gateway->gateway_module. " error: ".
212 $transaction->error_message;
214 my $jobnum = $cpp->jobnum;
216 my $placeholder = FS::queue->by_key($jobnum);
218 if ( $placeholder ) {
219 my $e = $placeholder->depended_delete || $placeholder->delete;
220 warn "error removing provisioning jobs after declined paypendingnum ".
221 $cpp->paypendingnum. ": $e\n\n"
224 warn "error finding job $jobnum for declined paypendingnum ".
225 $cpp->paypendingnum. "\n\n";
230 # the raw HTTP response thing when there's no error message
231 # decline notices (the customer has already seen the decline message)
233 # set the pending status
234 my $e = $cpp->decline($error);
236 $e = "WARNING: payment declined but pending payment not resolved - ".
237 "error updating status for pendingnum :".$cpp->paypendingnum.
240 $error = "$e ($error)";
248 =item cancel_payment SESSION_ID
250 Cancel a pending payment attempt. This just cleans up the cust_pay_pending
257 my $session_id = shift;
258 my $cust_pay_pending = qsearchs('cust_pay_pending', {
259 'session_id' => uc($session_id),
260 'status' => 'thirdparty',
262 return { 'error' => $cust_pay_pending->delete };