1 package FS::ClientAPI::MyAccount;
6 use Digest::MD5 qw(md5_hex);
8 use Business::CreditCard;
10 use FS::CGI qw(small_custview); #doh
12 use FS::UI::bytecount;
14 use FS::Record qw(qsearch qsearchs);
15 use FS::Msgcat qw(gettext);
16 use FS::Misc qw(card_types);
17 use FS::ClientAPI_SessionCache;
24 use FS::cust_main_county;
29 #false laziness with FS::cust_main
31 eval "use Time::Local;";
32 die "Time::Local minimum version 1.05 required with Perl versions before 5.6"
33 if $] < 5.006 && !defined($Time::Local::VERSION);
34 eval "use Time::Local qw(timelocal_nocheck);";
37 use vars qw( @cust_main_editable_fields );
38 @cust_main_editable_fields = qw(
39 first last company address1 address2 city
40 county state zip country daytime night fax
41 ship_first ship_last ship_company ship_address1 ship_address2 ship_city
42 ship_state ship_zip ship_country ship_daytime ship_night ship_fax
43 payby payinfo payname paystart_month paystart_year payissue payip
46 use subs qw(_provision);
49 $cache ||= new FS::ClientAPI_SessionCache( {
50 'namespace' => 'FS::ClientAPI::MyAccount',
54 #false laziness w/FS::ClientAPI::passwd::passwd
58 my $svc_domain = qsearchs('svc_domain', { 'domain' => $p->{'domain'} } )
59 or return { error => 'Domain '. $p->{'domain'}. ' not found' };
61 my $svc_acct = qsearchs( 'svc_acct', { 'username' => $p->{'username'},
62 'domsvc' => $svc_domain->svcnum, }
64 return { error => 'User not found.' } unless $svc_acct;
66 my $conf = new FS::Conf;
67 my $pkg_svc = $svc_acct->cust_svc->pkg_svc;
68 return { error => 'Only primary user may log in.' }
69 if $conf->exists('selfservice_server-primary_only')
70 && ( ! $pkg_svc || $pkg_svc->primary_svc ne 'Y' );
72 return { error => 'Incorrect password.' }
73 unless $svc_acct->check_password($p->{'password'});
76 'svcnum' => $svc_acct->svcnum,
79 my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
81 my $cust_main = $cust_pkg->cust_main;
82 $session->{'custnum'} = $cust_main->custnum;
87 $session_id = md5_hex(md5_hex(time(). {}. rand(). $$))
88 } until ( ! defined _cache->get($session_id) ); #just in case
90 _cache->set( $session_id, $session, '1 hour' );
92 return { 'error' => '',
93 'session_id' => $session_id,
99 if ( $p->{'session_id'} ) {
100 _cache->remove($p->{'session_id'});
101 return { 'error' => '' };
103 return { 'error' => "Can't resume session" }; #better error message
110 my($context, $session, $custnum) = _custoragent_session_custnum($p);
111 return { 'error' => $session } if $context eq 'error';
114 if ( $custnum ) { #customer record
116 my $search = { 'custnum' => $custnum };
117 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
118 my $cust_main = qsearchs('cust_main', $search )
119 or return { 'error' => "unknown custnum $custnum" };
121 $return{balance} = $cust_main->balance;
125 invnum => $_->invnum,
126 date => time2str("%b %o, %Y", $_->_date),
129 } $cust_main->open_cust_bill;
130 $return{open_invoices} = \@open;
132 my $conf = new FS::Conf;
133 $return{small_custview} =
134 small_custview( $cust_main, $conf->config('countrydefault') );
136 $return{name} = $cust_main->first. ' '. $cust_main->get('last');
138 for (@cust_main_editable_fields) {
139 $return{$_} = $cust_main->get($_);
142 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
143 $return{payinfo} = $cust_main->paymask;
144 @return{'month', 'year'} = $cust_main->paydate_monthyear;
147 $return{'invoicing_list'} =
148 join(', ', grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list );
149 $return{'postal_invoicing'} =
150 0 < ( grep { $_ eq 'POST' } $cust_main->invoicing_list );
152 } elsif ( $session->{'svcnum'} ) { #no customer record
154 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $session->{'svcnum'} } )
155 or die "unknown svcnum";
156 $return{name} = $svc_acct->email;
160 return { 'error' => 'Expired session' }; #XXX redirect to login w/this err!
164 return { 'error' => '',
165 'custnum' => $custnum,
173 my $session = _cache->get($p->{'session_id'})
174 or return { 'error' => "Can't resume session" }; #better error message
176 my $custnum = $session->{'custnum'}
177 or return { 'error' => "no customer record" };
179 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
180 or return { 'error' => "unknown custnum $custnum" };
182 my $new = new FS::cust_main { $cust_main->hash };
183 $new->set( $_ => $p->{$_} )
184 foreach grep { exists $p->{$_} } @cust_main_editable_fields;
186 if ( $p->{'payby'} =~ /^(CARD|DCRD)$/ ) {
187 $new->paydate($p->{'year'}. '-'. $p->{'month'}. '-01');
188 if ( $new->payinfo eq $cust_main->paymask ) {
189 $new->payinfo($cust_main->payinfo);
191 $new->paycvv($p->{'paycvv'});
196 if ( exists $p->{'invoicing_list'} || exists $p->{'postal_invoicing'} ) {
197 #false laziness with httemplate/edit/process/cust_main.cgi
198 @invoicing_list = split( /\s*\,\s*/, $p->{'invoicing_list'} );
199 push @invoicing_list, 'POST' if $p->{'postal_invoicing'};
201 @invoicing_list = $cust_main->invoicing_list;
204 my $error = $new->replace($cust_main, \@invoicing_list);
205 return { 'error' => $error } if $error;
208 return { 'error' => '' };
213 my $session = _cache->get($p->{'session_id'})
214 or return { 'error' => "Can't resume session" }; #better error message
220 use vars qw($payment_info); #cache for performance
221 unless ( $payment_info ) {
223 my $conf = new FS::Conf;
224 my %states = map { $_->state => 1 }
225 qsearch('cust_main_county', {
226 'country' => $conf->config('countrydefault') || 'US'
231 #list all counties/states/countries
232 'cust_main_county' =>
233 [ map { $_->hashref } qsearch('cust_main_county', {}) ],
235 #shortcut for one-country folks
237 [ sort { $a cmp $b } keys %states ],
239 'card_types' => card_types(),
241 'paytypes' => [ @FS::cust_main::paytypes ],
243 'stateid_label' => FS::Msgcat::_gettext('stateid'),
244 'stateid_state_label' => FS::Msgcat::_gettext('stateid_state'),
246 'show_ss' => $conf->exists('show_ss'),
247 'show_stateid' => $conf->exists('show_stateid'),
248 'show_paystate' => $conf->exists('show_bankstate'),
257 my %return = %$payment_info;
259 my $custnum = $session->{'custnum'};
261 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
262 or return { 'error' => "unknown custnum $custnum" };
264 $return{balance} = $cust_main->balance;
266 $return{payname} = $cust_main->payname
267 || ( $cust_main->first. ' '. $cust_main->get('last') );
269 $return{$_} = $cust_main->get($_) for qw(address1 address2 city state zip);
271 $return{payby} = $cust_main->payby;
272 $return{stateid_state} = $cust_main->stateid_state;
274 if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
275 $return{card_type} = cardtype($cust_main->payinfo);
276 $return{payinfo} = $cust_main->paymask;
278 @return{'month', 'year'} = $cust_main->paydate_monthyear;
282 if ( $cust_main->payby =~ /^(CHEK|DCHK)$/ ) {
283 my ($payinfo1, $payinfo2) = split '@', $cust_main->paymask;
284 $return{payinfo1} = $payinfo1;
285 $return{payinfo2} = $payinfo2;
286 $return{paytype} = $cust_main->paytype;
287 $return{paystate} = $cust_main->paystate;
291 #doubleclick protection
293 $return{paybatch} = "webui-MyAccount-$_date-$$-". rand() * 2**32;
295 return { 'error' => '',
301 #some false laziness with httemplate/process/payment.cgi - look there for
302 #ACH and CVV support stuff
303 sub process_payment {
307 my $session = _cache->get($p->{'session_id'})
308 or return { 'error' => "Can't resume session" }; #better error message
312 my $custnum = $session->{'custnum'};
314 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
315 or return { 'error' => "unknown custnum $custnum" };
317 $p->{'payname'} =~ /^([\w \,\.\-\']+)$/
318 or return { 'error' => gettext('illegal_name'). " payname: ". $p->{'payname'} };
321 $p->{'paybatch'} =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
322 or return { 'error' => gettext('illegal_text'). " paybatch: ". $p->{'paybatch'} };
325 $p->{'payby'} = 'CARD' unless exists($p->{'payby'});
326 $p->{'payby'} =~ /^([A-Z]{4})$/
327 or return { 'error' => "illegal_payby " . $p->{'payby'} };
332 if ( $payby eq 'CHEK' || $payby eq 'DCHK' ) {
334 $p->{'payinfo1'} =~ /^([\dx]+)$/
335 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
337 $p->{'payinfo2'} =~ /^([\dx]+)$/
338 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
340 $payinfo = $payinfo1. '@'. $payinfo2;
342 $payinfo = $cust_main->payinfo
343 if $cust_main->paymask eq $payinfo;
345 } elsif ( $payby eq 'CARD' || $payby eq 'DCRD' ) {
347 $payinfo = $p->{'payinfo'};
348 $payinfo =~ s/[^\dx]//g;
349 $payinfo =~ /^(\d{13,16})$/
350 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
353 $payinfo = $cust_main->payinfo
354 if $cust_main->paymask eq $payinfo;
357 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
358 return { 'error' => gettext('unknown_card_type') }
359 if cardtype($payinfo) eq "Unknown";
361 if ( length($p->{'paycvv'}) && $p->{'paycvv'} !~ /^\s*$/ ) {
362 if ( cardtype($payinfo) eq 'American Express card' ) {
363 $p->{'paycvv'} =~ /^\s*(\d{4})\s*$/
364 or return { 'error' => "CVV2 (CID) for American Express cards is four digits." };
367 $p->{'paycvv'} =~ /^\s*(\d{3})\s*$/
368 or return { 'error' => "CVV2 (CVC2/CID) is three digits." };
374 die "unknown payby $payby";
378 'CARD' => [ qw( paystart_month paystart_year payissue address1 address2 city state zip payip ) ],
379 'CHEK' => [ qw( ss paytype paystate stateid stateid_state payip ) ],
382 my $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $p->{'amount'},
384 'payinfo' => $payinfo,
385 'paydate' => $p->{'year'}. '-'. $p->{'month'}. '-01',
386 'payname' => $payname,
387 'paybatch' => $paybatch,
389 map { $_ => $p->{$_} } @{ $payby2fields{$payby} }
391 return { 'error' => $error } if $error;
393 $cust_main->apply_payments;
395 if ( $p->{'save'} ) {
396 my $new = new FS::cust_main { $cust_main->hash };
397 if ($payby eq 'CARD' || $payby eq 'DCRD') {
398 $new->set( $_ => $p->{$_} )
399 foreach qw( payname paystart_month paystart_year payissue payip
400 address1 address2 city state zip payinfo );
401 $new->set( 'payby' => $p->{'auto'} ? 'CARD' : 'DCRD' );
402 } elsif ($payby eq 'CHEK' || $payby eq 'DCHK') {
403 $new->set( $_ => $p->{$_} )
404 foreach qw( payname payip paytype paystate
405 stateid stateid_state );
406 $new->set( 'payinfo' => $payinfo );
407 $new->set( 'payby' => $p->{'auto'} ? 'CHEK' : 'DCHK' );
409 $new->set( 'paydate' => $p->{'year'}. '-'. $p->{'month'}. '-01' );
410 my $error = $new->replace($cust_main);
411 return { 'error' => $error } if $error;
415 return { 'error' => '' };
419 sub process_payment_order_pkg {
422 my $hr = process_payment($p);
423 return $hr if $hr->{'error'};
432 my $session = _cache->get($p->{'session_id'})
433 or return { 'error' => "Can't resume session" }; #better error message
437 my $custnum = $session->{'custnum'};
439 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
440 or return { 'error' => "unknown custnum $custnum" };
442 my( $amount, $seconds, $upbytes, $downbytes, $totalbytes ) = ( 0, 0, 0, 0, 0 );
443 my $error = $cust_main->recharge_prepay( $p->{'prepaid_cardnum'},
451 return { 'error' => $error } if $error;
453 return { 'error' => '',
455 'seconds' => $seconds,
456 'duration' => duration_exact($seconds),
457 'upbytes' => $upbytes,
458 'upload' => FS::UI::bytecount::bytecount_unexact($upbytes),
459 'downbytes' => $downbytes,
460 'download' => FS::UI::bytecount::bytecount_unexact($downbytes),
461 'totalbytes'=> $totalbytes,
462 'totalload' => FS::UI::bytecount::bytecount_unexact($totalbytes),
469 my $session = _cache->get($p->{'session_id'})
470 or return { 'error' => "Can't resume session" }; #better error message
472 my $custnum = $session->{'custnum'};
474 my $invnum = $p->{'invnum'};
476 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum,
477 'custnum' => $custnum } )
478 or return { 'error' => "Can't find invnum" };
482 return { 'error' => '',
484 'invoice_text' => join('', $cust_bill->print_text ),
485 'invoice_html' => $cust_bill->print_html,
493 #sessioning for this? how do we get the session id to the backend invoice
494 # template so it can add it to the link, blah
496 my $templatename = $p->{'templatename'};
498 #false laziness-ish w/view/cust_bill-logo.cgi
500 my $conf = new FS::Conf;
501 if ( $templatename =~ /^([^\.\/]*)$/ && $conf->exists("logo_$1.png") ) {
502 $templatename = "_$1";
507 my $filename = "logo$templatename.png";
509 return { 'error' => '',
510 'logo' => $conf->config_binary($filename),
511 'content_type' => 'image/png', #should allow gif, jpg too
518 my $session = _cache->get($p->{'session_id'})
519 or return { 'error' => "Can't resume session" }; #better error message
521 my $custnum = $session->{'custnum'};
523 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
524 or return { 'error' => "unknown custnum $custnum" };
526 my @cust_bill = $cust_main->cust_bill;
528 return { 'error' => '',
529 'invoices' => [ map { { 'invnum' => $_->invnum,
530 '_date' => $_->_date,
539 my $session = _cache->get($p->{'session_id'})
540 or return { 'error' => "Can't resume session" }; #better error message
542 my $custnum = $session->{'custnum'};
544 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
545 or return { 'error' => "unknown custnum $custnum" };
547 my @errors = $cust_main->cancel( 'quiet'=>1 );
549 my $error = scalar(@errors) ? join(' / ', @errors) : '';
551 return { 'error' => $error };
558 my($context, $session, $custnum) = _custoragent_session_custnum($p);
559 return { 'error' => $session } if $context eq 'error';
561 my $search = { 'custnum' => $custnum };
562 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
563 my $cust_main = qsearchs('cust_main', $search )
564 or return { 'error' => "unknown custnum $custnum" };
566 #return { 'cust_pkg' => [ map { $_->hashref } $cust_main->ncancelled_pkgs ] };
568 my $conf = new FS::Conf;
570 { 'svcnum' => $session->{'svcnum'},
571 'custnum' => $custnum,
572 'cust_pkg' => [ map {
576 [ map $_->hashref, $_->available_part_svc ],
578 [ map { my $ref = { $_->hash,
579 label => [ $_->label ],
581 $ref->{_password} = $_->svc_x->_password
582 if $context eq 'agent'
583 && $conf->exists('agent-showpasswords')
584 && $_->part_svc->svcdb eq 'svc_acct';
589 } $cust_main->ncancelled_pkgs
592 small_custview( $cust_main, $conf->config('countrydefault') ),
600 my($context, $session, $custnum) = _custoragent_session_custnum($p);
601 return { 'error' => $session } if $context eq 'error';
603 my $search = { 'custnum' => $custnum };
604 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
605 my $cust_main = qsearchs('cust_main', $search )
606 or return { 'error' => "unknown custnum $custnum" };
609 #foreach my $cust_pkg ( $cust_main->ncancelled_pkgs ) {
610 foreach my $cust_pkg ( $p->{'ncancelled'}
611 ? $cust_main->ncancelled_pkgs
612 : $cust_main->unsuspended_pkgs ) {
613 push @cust_svc, @{[ $cust_pkg->cust_svc ]}; #@{[ ]} to force array context
615 @cust_svc = grep { $_->part_svc->svcdb eq $p->{'svcdb'} } @cust_svc
618 #@svc_x = sort { $a->domain cmp $b->domain || $a->username cmp $b->username }
622 #no#'svcnum' => $session->{'svcnum'},
623 'custnum' => $custnum,
625 my $svc_x = $_->svc_x;
626 my($label, $value) = $_->label;
627 my $part_pkg = $svc_x->cust_svc->cust_pkg->part_pkg;
629 { 'svcnum' => $_->svcnum,
632 'username' => $svc_x->username,
633 'email' => $svc_x->email,
634 'seconds' => $svc_x->seconds,
635 'upbytes' => FS::UI::bytecount::display_bytecount($svc_x->upbytes),
636 'downbytes' => FS::UI::bytecount::display_bytecount($svc_x->downbytes),
637 'totalbytes'=> FS::UI::bytecount::display_bytecount($svc_x->totalbytes),
638 'recharge_amount' => $part_pkg->option('recharge_amount', 1),
639 'recharge_seconds' => $part_pkg->option('recharge_seconds', 1),
640 'recharge_upbytes' => FS::UI::bytecount::display_bytecount($part_pkg->option('recharge_upbytes', 1)),
641 'recharge_downbytes' => FS::UI::bytecount::display_bytecount($part_pkg->option('recharge_downbytes', 1)),
642 'recharge_totalbytes' => FS::UI::bytecount::display_bytecount($part_pkg->option('recharge_totalbytes', 1)),
655 my($context, $session, $custnum) = _custoragent_session_custnum($p);
656 return { 'error' => $session } if $context eq 'error';
658 my $search = { 'svcnum' => $p->{'svcnum'} };
659 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
660 my $svc_acct = qsearchs ( 'svc_acct', $search );
661 return { 'error' => 'No service selected in list_svc_usage' }
664 my $freq = $svc_acct->cust_svc->cust_pkg->part_pkg->freq;
665 my $start = $svc_acct->cust_svc->cust_pkg->setup;
666 #my $end = $svc_acct->cust_svc->cust_pkg->bill; # or time?
669 unless($p->{beginning}){
670 $p->{beginning} = $svc_acct->cust_svc->cust_pkg->last_bill;
675 foreach my $part_export (
676 map { qsearch ( 'part_export', { 'exporttype' => $_ } ) }
677 qw (sqlradius sqlradius_withdomain')
680 push @usage, @ { $part_export->usage_sessions($p->{beginning},
686 #kinda false laziness with FS::cust_main::bill, but perhaps
687 #we should really change this bit to DateTime and DateTime::Duration
689 #change this bit to use Date::Manip? CAREFUL with timezones (see
690 # mailing list archive)
691 my ($nsec,$nmin,$nhour,$nmday,$nmon,$nyear) =
692 (localtime($p->{ending}) )[0,1,2,3,4,5];
693 my ($psec,$pmin,$phour,$pmday,$pmon,$pyear) =
694 (localtime($p->{beginning}) )[0,1,2,3,4,5];
696 if ( $freq =~ /^\d+$/ ) {
698 until ( $nmon < 12 ) { $nmon -= 12; $nyear++; }
700 until ( $pmon >= 0 ) { $pmon += 12; $pyear--; }
701 } elsif ( $freq =~ /^(\d+)w$/ ) {
703 $nmday += $weeks * 7;
704 $pmday -= $weeks * 7;
705 } elsif ( $freq =~ /^(\d+)d$/ ) {
709 } elsif ( $freq =~ /^(\d+)h$/ ) {
714 return { 'error' => "unparsable frequency: ". $freq };
717 my $previous = timelocal_nocheck($psec,$pmin,$phour,$pmday,$pmon,$pyear);
718 my $next = timelocal_nocheck($nsec,$nmin,$nhour,$nmday,$nmon,$nyear);
723 'svcnum' => $p->{svcnum},
724 'beginning' => $p->{beginning},
725 'ending' => $p->{ending},
726 'previous' => ($previous > $start) ? $previous : $start,
727 'next' => ($next < $end) ? $next : $end,
735 my($context, $session, $custnum) = _custoragent_session_custnum($p);
736 return { 'error' => $session } if $context eq 'error';
738 my $search = { 'custnum' => $custnum };
739 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
740 my $cust_main = qsearchs('cust_main', $search )
741 or return { 'error' => "unknown custnum $custnum" };
743 #false laziness w/ClientAPI/Signup.pm
745 my $cust_pkg = new FS::cust_pkg ( {
746 'custnum' => $custnum,
747 'pkgpart' => $p->{'pkgpart'},
749 my $error = $cust_pkg->check;
750 return { 'error' => $error } if $error;
753 unless ( $p->{'svcpart'} eq 'none' ) {
757 if ( $p->{'svcpart'} =~ /^(\d+)$/ ) {
759 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
760 return { 'error' => "Unknown svcpart $svcpart" } unless $part_svc;
761 $svcdb = $part_svc->svcdb;
765 $svcpart ||= $cust_pkg->part_pkg->svcpart($svcdb);
768 'svc_acct' => [ qw( username domsvc _password sec_phrase popnum ) ],
769 'svc_domain' => [ qw( domain ) ],
770 'svc_external' => [ qw( id title ) ],
773 my $svc_x = "FS::$svcdb"->new( {
774 'svcpart' => $svcpart,
775 map { $_ => $p->{$_} } @{$fields{$svcdb}}
778 if ( $svcdb eq 'svc_acct' ) {
781 while ( length($p->{"snarf_machine$snarfnum"}) ) {
782 my $acct_snarf = new FS::acct_snarf ( {
783 'machine' => $p->{"snarf_machine$snarfnum"},
784 'protocol' => $p->{"snarf_protocol$snarfnum"},
785 'username' => $p->{"snarf_username$snarfnum"},
786 '_password' => $p->{"snarf_password$snarfnum"},
789 push @acct_snarf, $acct_snarf;
791 $svc_x->child_objects( \@acct_snarf );
794 my $y = $svc_x->setdefault; # arguably should be in new method
795 return { 'error' => $y } if $y && !ref($y);
797 $error = $svc_x->check;
798 return { 'error' => $error } if $error;
805 tie my %hash, 'Tie::RefHash';
806 %hash = ( $cust_pkg => \@svc );
808 $error = $cust_main->order_pkgs( \%hash, '', 'noexport' => 1 );
809 return { 'error' => $error } if $error;
811 my $conf = new FS::Conf;
812 if ( $conf->exists('signup_server-realtime') ) {
814 my $bill_error = _do_bop_realtime( $cust_main );
817 $cust_pkg->cancel('quiet'=>1);
827 return { error => '', pkgnum => $cust_pkg->pkgnum };
834 my($context, $session, $custnum) = _custoragent_session_custnum($p);
835 return { 'error' => $session } if $context eq 'error';
837 my $search = { 'custnum' => $custnum };
838 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
839 my $cust_main = qsearchs('cust_main', $search )
840 or return { 'error' => "unknown custnum $custnum" };
842 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $p->{pkgnum} } )
843 or return { 'error' => "unknown package $p->{pkgnum}" };
846 my $error = FS::cust_pkg::order( $custnum,
852 my $conf = new FS::Conf;
853 if ( $conf->exists('signup_server-realtime') ) {
855 my $bill_error = _do_bop_realtime( $cust_main );
861 $newpkg[0]->reexport;
865 $newpkg[0]->reexport;
868 return { error => '', pkgnum => $cust_pkg->pkgnum };
875 my($context, $session, $custnum) = _custoragent_session_custnum($p);
876 return { 'error' => $session } if $context eq 'error';
878 my $search = { 'custnum' => $custnum };
879 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
880 my $cust_main = qsearchs('cust_main', $search )
881 or return { 'error' => "unknown custnum $custnum" };
883 my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $p->{'svcnum'} } )
884 or return { 'error' => "unknown service " . $p->{'svcnum'} };
886 my $svc_x = $cust_svc->svc_x;
887 my $part_pkg = $cust_svc->cust_pkg->part_pkg;
890 map { $_ =~ /^recharge_(.*)$/; $1, $part_pkg->option($_, 1) }
891 qw ( recharge_seconds recharge_upbytes recharge_downbytes
892 recharge_totalbytes );
893 my $amount = $part_pkg->option('recharge_amount', 1);
895 my ($l, $v, $d) = $cust_svc->label; # blah
896 my $pkg = "Recharge $v";
898 my $bill_error = $cust_main->charge($amount, $pkg,
899 "time: $vhash{seconds}, up: $vhash{upbytes}," .
900 "down: $vhash{downbytes}, total: $vhash{totalbytes}",
901 $part_pkg->taxclass); #meh
903 my $conf = new FS::Conf;
904 if ( $conf->exists('signup_server-realtime') && !$bill_error ) {
906 $bill_error = _do_bop_realtime( $cust_main );
911 my $error = $svc_x->recharge (\%vhash);
912 return { 'error' => $error } if $error;
916 my $error = $bill_error;
917 $error ||= $svc_x->recharge (\%vhash);
918 return { 'error' => $error } if $error;
921 return { error => '', svc => $cust_svc->part_svc->svc };
925 sub _do_bop_realtime {
926 my ($cust_main) = @_;
928 my $old_balance = $cust_main->balance;
930 my $bill_error = $cust_main->bill;
932 $cust_main->apply_payments_and_credits;
933 $bill_error = $cust_main->collect('realtime' => 1);
935 if ( $cust_main->balance > $old_balance
936 && $cust_main->balance > 0
937 && $cust_main->payby !~ /^(BILL|DCRD|DCHK)$/ ) {
938 #this makes sense. credit is "un-doing" the invoice
939 $cust_main->credit( sprintf("%.2f", $cust_main->balance - $old_balance ),
940 'self-service decline' );
941 $cust_main->apply_credits( 'order' => 'newest' );
943 return { 'error' => '_decline', 'bill_error' => $bill_error };
951 my $session = _cache->get($p->{'session_id'})
952 or return { 'error' => "Can't resume session" }; #better error message
954 my $custnum = $session->{'custnum'};
956 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
957 or return { 'error' => "unknown custnum $custnum" };
959 my $pkgnum = $p->{'pkgnum'};
961 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
962 'pkgnum' => $pkgnum, } )
963 or return { 'error' => "unknown pkgnum $pkgnum" };
965 my $error = $cust_pkg->cancel( 'quiet'=>1 );
966 return { 'error' => $error };
973 return { 'error' => gettext('passwords_dont_match') }
974 if $p->{'_password'} ne $p->{'_password2'};
975 return { 'error' => gettext('empty_password') }
976 unless length($p->{'_password'});
978 _provision( 'FS::svc_acct',
979 [qw(username _password)],
980 [qw(username _password)],
986 sub provision_external {
988 #_provision( 'FS::svc_external', [qw(id title)], [qw(id title)], $p, @_ );
989 _provision( 'FS::svc_external',
998 my( $class, $fields, $return_fields, $p ) = splice(@_, 0, 4);
1000 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1001 return { 'error' => $session } if $context eq 'error';
1003 my $search = { 'custnum' => $custnum };
1004 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1005 my $cust_main = qsearchs('cust_main', $search )
1006 or return { 'error' => "unknown custnum $custnum" };
1008 my $pkgnum = $p->{'pkgnum'};
1010 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
1011 'pkgnum' => $pkgnum,
1013 or return { 'error' => "unknown pkgnum $pkgnum" };
1015 my $part_svc = qsearchs('part_svc', { 'svcpart' => $p->{'svcpart'} } )
1016 or return { 'error' => "unknown svcpart $p->{'svcpart'}" };
1018 my $svc_x = $class->new( {
1019 'pkgnum' => $p->{'pkgnum'},
1020 'svcpart' => $p->{'svcpart'},
1021 map { $_ => $p->{$_} } @$fields
1023 my $error = $svc_x->insert;
1024 $svc_x = qsearchs($svc_x->table, { 'svcnum' => $svc_x->svcnum })
1027 return { 'svc' => $part_svc->svc,
1029 map { $_ => $svc_x->get($_) } @$return_fields
1037 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1038 return { 'error' => $session } if $context eq 'error';
1040 my $search = { 'custnum' => $custnum };
1041 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1042 my $cust_main = qsearchs('cust_main', $search )
1043 or return { 'error' => "unknown custnum $custnum" };
1045 my $pkgnum = $p->{'pkgnum'};
1047 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
1048 'pkgnum' => $pkgnum,
1050 or return { 'error' => "unknown pkgnum $pkgnum" };
1052 my $svcpart = $p->{'svcpart'};
1054 my $pkg_svc = qsearchs('pkg_svc', { 'pkgpart' => $cust_pkg->pkgpart,
1055 'svcpart' => $svcpart, } )
1056 or return { 'error' => "unknown svcpart $svcpart for pkgnum $pkgnum" };
1057 my $part_svc = $pkg_svc->part_svc;
1059 my $conf = new FS::Conf;
1062 'svc' => $part_svc->svc,
1063 'svcdb' => $part_svc->svcdb,
1064 'pkgnum' => $pkgnum,
1065 'svcpart' => $svcpart,
1066 'custnum' => $custnum,
1068 'security_phrase' => 0, #XXX !
1069 'svc_acct_pop' => [], #XXX !
1071 'init_popstate' => '',
1076 small_custview( $cust_main, $conf->config('countrydefault') ),
1082 sub unprovision_svc {
1085 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1086 return { 'error' => $session } if $context eq 'error';
1088 my $search = { 'custnum' => $custnum };
1089 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1090 my $cust_main = qsearchs('cust_main', $search )
1091 or return { 'error' => "unknown custnum $custnum" };
1093 my $svcnum = $p->{'svcnum'};
1095 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svcnum, } )
1096 or return { 'error' => "unknown svcnum $svcnum" };
1098 return { 'error' => "Service $svcnum does not belong to customer $custnum" }
1099 unless $cust_svc->cust_pkg->custnum == $custnum;
1101 my $conf = new FS::Conf;
1103 return { 'svc' => $cust_svc->part_svc->svc,
1104 'error' => $cust_svc->cancel,
1106 small_custview( $cust_main, $conf->config('countrydefault') ),
1111 sub myaccount_passwd {
1113 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1114 return { 'error' => $session } if $context eq 'error';
1116 return { 'error' => "New passwords don't match." }
1117 if $p->{'new_password'} ne $p->{'new_password2'};
1119 return { 'error' => 'Enter new password' }
1120 unless length($p->{'new_password'});
1122 #my $search = { 'custnum' => $custnum };
1123 #$search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1124 $custnum =~ /^(\d+)$/ or die "illegal custnum";
1125 my $search = " AND custnum = $1";
1126 $search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
1128 my $svc_acct = qsearchs( {
1129 'table' => 'svc_acct',
1130 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) '.
1131 'LEFT JOIN cust_pkg USING ( pkgnum ) '.
1132 'LEFT JOIN cust_main USING ( custnum ) ',
1133 'hashref' => { 'svcnum' => $p->{'svcnum'}, },
1134 'extra_sql' => $search, #important
1136 or return { 'error' => "Service not found" };
1138 $svc_acct->_password($p->{'new_password'});
1139 my $error = $svc_acct->replace();
1141 my($label, $value) = $svc_acct->cust_svc->label;
1143 return { 'error' => $error,
1152 sub _custoragent_session_custnum {
1155 my($context, $session, $custnum);
1156 if ( $p->{'session_id'} ) {
1158 $context = 'customer';
1159 $session = _cache->get($p->{'session_id'})
1160 or return ( 'error' => "Can't resume session" ); #better error message
1161 $custnum = $session->{'custnum'};
1163 } elsif ( $p->{'agent_session_id'} ) {
1166 my $agent_cache = new FS::ClientAPI_SessionCache( {
1167 'namespace' => 'FS::ClientAPI::Agent',
1169 $session = $agent_cache->get($p->{'agent_session_id'})
1170 or return ( 'error' => "Can't resume session" ); #better error message
1171 $custnum = $p->{'custnum'};
1174 return ( 'error' => "Can't resume session" ); #better error message
1177 ($context, $session, $custnum);