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->payinfo;
278 @return{'month', 'year'} = $cust_main->paydate_monthyear;
282 if ( $cust_main->payby =~ /^(CHEK|DCHK)$/ ) {
283 my ($payinfo1, $payinfo2) = split '@', $cust_main->payinfo;
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'} =~ /^(\d+)$/
335 or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
337 $p->{'payinfo2'} =~ /^(\d+)$/
338 or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
340 $payinfo = $payinfo1. '@'. $payinfo2;
342 } elsif ( $payby eq 'CARD' || $payby eq 'DCRD' ) {
344 $payinfo = $p->{'payinfo'};
346 $payinfo =~ /^(\d{13,16})$/
347 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
350 or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
351 return { 'error' => gettext('unknown_card_type') }
352 if cardtype($payinfo) eq "Unknown";
354 if ( length($p->{'paycvv'}) && $p->{'paycvv'} !~ /^\s*$/ ) {
355 if ( cardtype($payinfo) eq 'American Express card' ) {
356 $p->{'paycvv'} =~ /^\s*(\d{4})\s*$/
357 or return { 'error' => "CVV2 (CID) for American Express cards is four digits." };
360 $p->{'paycvv'} =~ /^\s*(\d{3})\s*$/
361 or return { 'error' => "CVV2 (CVC2/CID) is three digits." };
367 die "unknown payby $payby";
371 'CARD' => [ qw( paystart_month paystart_year payissue address1 address2 city state zip payip ) ],
372 'CHEK' => [ qw( ss paytype paystate stateid stateid_state payip ) ],
375 my $error = $cust_main->realtime_bop( $FS::payby::payby2bop{$payby}, $p->{'amount'},
377 'payinfo' => $payinfo,
378 'paydate' => $p->{'year'}. '-'. $p->{'month'}. '-01',
379 'payname' => $payname,
380 'paybatch' => $paybatch,
382 map { $_ => $p->{$_} } @{ $payby2fields{$payby} }
384 return { 'error' => $error } if $error;
386 $cust_main->apply_payments;
388 if ( $p->{'save'} ) {
389 my $new = new FS::cust_main { $cust_main->hash };
390 if ($payby eq 'CARD' || $payby eq 'DCRD') {
391 $new->set( $_ => $p->{$_} )
392 foreach qw( payname paystart_month paystart_year payissue payip
393 address1 address2 city state zip payinfo );
394 $new->set( 'payby' => $p->{'auto'} ? 'CARD' : 'DCRD' );
395 } elsif ($payby eq 'CHEK' || $payby eq 'DCHK') {
396 $new->set( $_ => $p->{$_} )
397 foreach qw( payname payip paytype paystate
398 stateid stateid_state );
399 $new->set( 'payinfo' => $payinfo );
400 $new->set( 'payby' => $p->{'auto'} ? 'CHEK' : 'DCHK' );
402 $new->set( 'paydate' => $p->{'year'}. '-'. $p->{'month'}. '-01' );
403 my $error = $new->replace($cust_main);
404 return { 'error' => $error } if $error;
408 return { 'error' => '' };
416 my $session = _cache->get($p->{'session_id'})
417 or return { 'error' => "Can't resume session" }; #better error message
421 my $custnum = $session->{'custnum'};
423 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
424 or return { 'error' => "unknown custnum $custnum" };
426 my( $amount, $seconds, $upbytes, $downbytes, $totalbytes ) = ( 0, 0, 0, 0, 0 );
427 my $error = $cust_main->recharge_prepay( $p->{'prepaid_cardnum'},
435 return { 'error' => $error } if $error;
437 return { 'error' => '',
439 'seconds' => $seconds,
440 'duration' => duration_exact($seconds),
441 'upbytes' => $upbytes,
442 'upload' => FS::UI::bytecount::bytecount_unexact($upbytes),
443 'downbytes' => $downbytes,
444 'download' => FS::UI::bytecount::bytecount_unexact($downbytes),
445 'totalbytes'=> $totalbytes,
446 'totalload' => FS::UI::bytecount::bytecount_unexact($totalbytes),
453 my $session = _cache->get($p->{'session_id'})
454 or return { 'error' => "Can't resume session" }; #better error message
456 my $custnum = $session->{'custnum'};
458 my $invnum = $p->{'invnum'};
460 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum,
461 'custnum' => $custnum } )
462 or return { 'error' => "Can't find invnum" };
466 return { 'error' => '',
468 'invoice_text' => join('', $cust_bill->print_text ),
469 'invoice_html' => $cust_bill->print_html,
477 #sessioning for this? how do we get the session id to the backend invoice
478 # template so it can add it to the link, blah
480 my $templatename = $p->{'templatename'};
482 #false laziness-ish w/view/cust_bill-logo.cgi
484 my $conf = new FS::Conf;
485 if ( $templatename =~ /^([^\.\/]*)$/ && $conf->exists("logo_$1.png") ) {
486 $templatename = "_$1";
491 my $filename = "logo$templatename.png";
493 return { 'error' => '',
494 'logo' => $conf->config_binary($filename),
495 'content_type' => 'image/png', #should allow gif, jpg too
502 my $session = _cache->get($p->{'session_id'})
503 or return { 'error' => "Can't resume session" }; #better error message
505 my $custnum = $session->{'custnum'};
507 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
508 or return { 'error' => "unknown custnum $custnum" };
510 my @cust_bill = $cust_main->cust_bill;
512 return { 'error' => '',
513 'invoices' => [ map { { 'invnum' => $_->invnum,
514 '_date' => $_->_date,
523 my $session = _cache->get($p->{'session_id'})
524 or return { 'error' => "Can't resume session" }; #better error message
526 my $custnum = $session->{'custnum'};
528 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
529 or return { 'error' => "unknown custnum $custnum" };
531 my @errors = $cust_main->cancel( 'quiet'=>1 );
533 my $error = scalar(@errors) ? join(' / ', @errors) : '';
535 return { 'error' => $error };
542 my($context, $session, $custnum) = _custoragent_session_custnum($p);
543 return { 'error' => $session } if $context eq 'error';
545 my $search = { 'custnum' => $custnum };
546 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
547 my $cust_main = qsearchs('cust_main', $search )
548 or return { 'error' => "unknown custnum $custnum" };
550 #return { 'cust_pkg' => [ map { $_->hashref } $cust_main->ncancelled_pkgs ] };
552 my $conf = new FS::Conf;
554 { 'svcnum' => $session->{'svcnum'},
555 'custnum' => $custnum,
556 'cust_pkg' => [ map {
560 [ map $_->hashref, $_->available_part_svc ],
562 [ map { my $ref = { $_->hash,
563 label => [ $_->label ],
565 $ref->{_password} = $_->svc_x->_password
566 if $context eq 'agent'
567 && $conf->exists('agent-showpasswords')
568 && $_->part_svc->svcdb eq 'svc_acct';
573 } $cust_main->ncancelled_pkgs
576 small_custview( $cust_main, $conf->config('countrydefault') ),
584 my($context, $session, $custnum) = _custoragent_session_custnum($p);
585 return { 'error' => $session } if $context eq 'error';
587 my $search = { 'custnum' => $custnum };
588 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
589 my $cust_main = qsearchs('cust_main', $search )
590 or return { 'error' => "unknown custnum $custnum" };
593 #foreach my $cust_pkg ( $cust_main->ncancelled_pkgs ) {
594 foreach my $cust_pkg ( $p->{'ncancelled'}
595 ? $cust_main->ncancelled_pkgs
596 : $cust_main->unsuspended_pkgs ) {
597 push @cust_svc, @{[ $cust_pkg->cust_svc ]}; #@{[ ]} to force array context
599 @cust_svc = grep { $_->part_svc->svcdb eq $p->{'svcdb'} } @cust_svc
602 #@svc_x = sort { $a->domain cmp $b->domain || $a->username cmp $b->username }
606 #no#'svcnum' => $session->{'svcnum'},
607 'custnum' => $custnum,
609 my $svc_x = $_->svc_x;
610 my($label, $value) = $_->label;
611 my $part_pkg = $svc_x->cust_svc->cust_pkg->part_pkg;
613 { 'svcnum' => $_->svcnum,
616 'username' => $svc_x->username,
617 'email' => $svc_x->email,
618 'seconds' => $svc_x->seconds,
619 'upbytes' => FS::UI::bytecount::display_bytecount($svc_x->upbytes),
620 'downbytes' => FS::UI::bytecount::display_bytecount($svc_x->downbytes),
621 'totalbytes'=> FS::UI::bytecount::display_bytecount($svc_x->totalbytes),
622 'recharge_amount' => $part_pkg->option('recharge_amount', 1),
623 'recharge_seconds' => $part_pkg->option('recharge_seconds', 1),
624 'recharge_upbytes' => FS::UI::bytecount::display_bytecount($part_pkg->option('recharge_upbytes', 1)),
625 'recharge_downbytes' => FS::UI::bytecount::display_bytecount($part_pkg->option('recharge_downbytes', 1)),
626 'recharge_totalbytes' => FS::UI::bytecount::display_bytecount($part_pkg->option('recharge_totalbytes', 1)),
639 my($context, $session, $custnum) = _custoragent_session_custnum($p);
640 return { 'error' => $session } if $context eq 'error';
642 my $search = { 'svcnum' => $p->{'svcnum'} };
643 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
644 my $svc_acct = qsearchs ( 'svc_acct', $search );
645 return { 'error' => 'No service selected in list_svc_usage' }
648 my $freq = $svc_acct->cust_svc->cust_pkg->part_pkg->freq;
649 my $start = $svc_acct->cust_svc->cust_pkg->setup;
650 #my $end = $svc_acct->cust_svc->cust_pkg->bill; # or time?
653 unless($p->{beginning}){
654 $p->{beginning} = $svc_acct->cust_svc->cust_pkg->last_bill;
659 foreach my $part_export (
660 map { qsearch ( 'part_export', { 'exporttype' => $_ } ) }
661 qw (sqlradius sqlradius_withdomain')
664 push @usage, @ { $part_export->usage_sessions($p->{beginning},
670 #kinda false laziness with FS::cust_main::bill, but perhaps
671 #we should really change this bit to DateTime and DateTime::Duration
673 #change this bit to use Date::Manip? CAREFUL with timezones (see
674 # mailing list archive)
675 my ($nsec,$nmin,$nhour,$nmday,$nmon,$nyear) =
676 (localtime($p->{ending}) )[0,1,2,3,4,5];
677 my ($psec,$pmin,$phour,$pmday,$pmon,$pyear) =
678 (localtime($p->{beginning}) )[0,1,2,3,4,5];
680 if ( $freq =~ /^\d+$/ ) {
682 until ( $nmon < 12 ) { $nmon -= 12; $nyear++; }
684 until ( $pmon >= 0 ) { $pmon += 12; $pyear--; }
685 } elsif ( $freq =~ /^(\d+)w$/ ) {
687 $nmday += $weeks * 7;
688 $pmday -= $weeks * 7;
689 } elsif ( $freq =~ /^(\d+)d$/ ) {
693 } elsif ( $freq =~ /^(\d+)h$/ ) {
698 return { 'error' => "unparsable frequency: ". $freq };
701 my $previous = timelocal_nocheck($psec,$pmin,$phour,$pmday,$pmon,$pyear);
702 my $next = timelocal_nocheck($nsec,$nmin,$nhour,$nmday,$nmon,$nyear);
707 'svcnum' => $p->{svcnum},
708 'beginning' => $p->{beginning},
709 'ending' => $p->{ending},
710 'previous' => ($previous > $start) ? $previous : $start,
711 'next' => ($next < $end) ? $next : $end,
719 my($context, $session, $custnum) = _custoragent_session_custnum($p);
720 return { 'error' => $session } if $context eq 'error';
722 my $search = { 'custnum' => $custnum };
723 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
724 my $cust_main = qsearchs('cust_main', $search )
725 or return { 'error' => "unknown custnum $custnum" };
727 #false laziness w/ClientAPI/Signup.pm
729 my $cust_pkg = new FS::cust_pkg ( {
730 'custnum' => $custnum,
731 'pkgpart' => $p->{'pkgpart'},
733 my $error = $cust_pkg->check;
734 return { 'error' => $error } if $error;
737 unless ( $p->{'svcpart'} eq 'none' ) {
741 if ( $p->{'svcpart'} =~ /^(\d+)$/ ) {
743 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
744 return { 'error' => "Unknown svcpart $svcpart" } unless $part_svc;
745 $svcdb = $part_svc->svcdb;
749 $svcpart ||= $cust_pkg->part_pkg->svcpart($svcdb);
752 'svc_acct' => [ qw( username domsvc _password sec_phrase popnum ) ],
753 'svc_domain' => [ qw( domain ) ],
754 'svc_external' => [ qw( id title ) ],
757 my $svc_x = "FS::$svcdb"->new( {
758 'svcpart' => $svcpart,
759 map { $_ => $p->{$_} } @{$fields{$svcdb}}
762 if ( $svcdb eq 'svc_acct' ) {
765 while ( length($p->{"snarf_machine$snarfnum"}) ) {
766 my $acct_snarf = new FS::acct_snarf ( {
767 'machine' => $p->{"snarf_machine$snarfnum"},
768 'protocol' => $p->{"snarf_protocol$snarfnum"},
769 'username' => $p->{"snarf_username$snarfnum"},
770 '_password' => $p->{"snarf_password$snarfnum"},
773 push @acct_snarf, $acct_snarf;
775 $svc_x->child_objects( \@acct_snarf );
778 my $y = $svc_x->setdefault; # arguably should be in new method
779 return { 'error' => $y } if $y && !ref($y);
781 $error = $svc_x->check;
782 return { 'error' => $error } if $error;
789 tie my %hash, 'Tie::RefHash';
790 %hash = ( $cust_pkg => \@svc );
792 $error = $cust_main->order_pkgs( \%hash, '', 'noexport' => 1 );
793 return { 'error' => $error } if $error;
795 my $conf = new FS::Conf;
796 if ( $conf->exists('signup_server-realtime') ) {
798 my $bill_error = _do_bop_realtime( $cust_main );
801 $cust_pkg->cancel('quiet'=>1);
811 return { error => '', pkgnum => $cust_pkg->pkgnum };
818 my($context, $session, $custnum) = _custoragent_session_custnum($p);
819 return { 'error' => $session } if $context eq 'error';
821 my $search = { 'custnum' => $custnum };
822 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
823 my $cust_main = qsearchs('cust_main', $search )
824 or return { 'error' => "unknown custnum $custnum" };
826 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $p->{pkgnum} } )
827 or return { 'error' => "unknown package $p->{pkgnum}" };
830 my $error = FS::cust_pkg::order( $custnum,
836 my $conf = new FS::Conf;
837 if ( $conf->exists('signup_server-realtime') ) {
839 my $bill_error = _do_bop_realtime( $cust_main );
845 $newpkg[0]->reexport;
849 $newpkg[0]->reexport;
852 return { error => '', pkgnum => $cust_pkg->pkgnum };
859 my($context, $session, $custnum) = _custoragent_session_custnum($p);
860 return { 'error' => $session } if $context eq 'error';
862 my $search = { 'custnum' => $custnum };
863 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
864 my $cust_main = qsearchs('cust_main', $search )
865 or return { 'error' => "unknown custnum $custnum" };
867 my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $p->{'svcnum'} } )
868 or return { 'error' => "unknown service " . $p->{'svcnum'} };
870 my $svc_x = $cust_svc->svc_x;
871 my $part_pkg = $cust_svc->cust_pkg->part_pkg;
874 map { $_ =~ /^recharge_(.*)$/; $1, $part_pkg->option($_, 1) }
875 qw ( recharge_seconds recharge_upbytes recharge_downbytes
876 recharge_totalbytes );
877 my $amount = $part_pkg->option('recharge_amount', 1);
879 my ($l, $v, $d) = $cust_svc->label; # blah
880 my $pkg = "Recharge $v";
882 my $bill_error = $cust_main->charge($amount, $pkg,
883 "time: $vhash{seconds}, up: $vhash{upbytes}," .
884 "down: $vhash{downbytes}, total: $vhash{totalbytes}",
885 $part_pkg->taxclass); #meh
887 my $conf = new FS::Conf;
888 if ( $conf->exists('signup_server-realtime') && !$bill_error ) {
890 $bill_error = _do_bop_realtime( $cust_main );
895 my $error = $svc_x->recharge (\%vhash);
896 return { 'error' => $error } if $error;
900 my $error = $bill_error;
901 $error ||= $svc_x->recharge (\%vhash);
902 return { 'error' => $error } if $error;
905 return { error => '', svc => $cust_svc->part_svc->svc };
909 sub _do_bop_realtime {
910 my ($cust_main) = @_;
912 my $old_balance = $cust_main->balance;
914 my $bill_error = $cust_main->bill;
916 $cust_main->apply_payments_and_credits;
917 $bill_error = $cust_main->collect('realtime' => 1);
919 if ( $cust_main->balance > $old_balance
920 && $cust_main->balance > 0
921 && $cust_main->payby !~ /^(BILL|DCRD|DCHK)$/ ) {
922 #this makes sense. credit is "un-doing" the invoice
923 $cust_main->credit( sprintf("%.2f", $cust_main->balance - $old_balance ),
924 'self-service decline' );
925 $cust_main->apply_credits( 'order' => 'newest' );
927 return { 'error' => '_decline', 'bill_error' => $bill_error };
935 my $session = _cache->get($p->{'session_id'})
936 or return { 'error' => "Can't resume session" }; #better error message
938 my $custnum = $session->{'custnum'};
940 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
941 or return { 'error' => "unknown custnum $custnum" };
943 my $pkgnum = $p->{'pkgnum'};
945 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
946 'pkgnum' => $pkgnum, } )
947 or return { 'error' => "unknown pkgnum $pkgnum" };
949 my $error = $cust_pkg->cancel( 'quiet'=>1 );
950 return { 'error' => $error };
957 return { 'error' => gettext('passwords_dont_match') }
958 if $p->{'_password'} ne $p->{'_password2'};
959 return { 'error' => gettext('empty_password') }
960 unless length($p->{'_password'});
962 _provision( 'FS::svc_acct',
963 [qw(username _password)],
964 [qw(username _password)],
970 sub provision_external {
972 #_provision( 'FS::svc_external', [qw(id title)], [qw(id title)], $p, @_ );
973 _provision( 'FS::svc_external',
982 my( $class, $fields, $return_fields, $p ) = splice(@_, 0, 4);
984 my($context, $session, $custnum) = _custoragent_session_custnum($p);
985 return { 'error' => $session } if $context eq 'error';
987 my $search = { 'custnum' => $custnum };
988 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
989 my $cust_main = qsearchs('cust_main', $search )
990 or return { 'error' => "unknown custnum $custnum" };
992 my $pkgnum = $p->{'pkgnum'};
994 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
997 or return { 'error' => "unknown pkgnum $pkgnum" };
999 my $part_svc = qsearchs('part_svc', { 'svcpart' => $p->{'svcpart'} } )
1000 or return { 'error' => "unknown svcpart $p->{'svcpart'}" };
1002 my $svc_x = $class->new( {
1003 'pkgnum' => $p->{'pkgnum'},
1004 'svcpart' => $p->{'svcpart'},
1005 map { $_ => $p->{$_} } @$fields
1007 my $error = $svc_x->insert;
1008 $svc_x = qsearchs($svc_x->table, { 'svcnum' => $svc_x->svcnum })
1011 return { 'svc' => $part_svc->svc,
1013 map { $_ => $svc_x->get($_) } @$return_fields
1021 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1022 return { 'error' => $session } if $context eq 'error';
1024 my $search = { 'custnum' => $custnum };
1025 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1026 my $cust_main = qsearchs('cust_main', $search )
1027 or return { 'error' => "unknown custnum $custnum" };
1029 my $pkgnum = $p->{'pkgnum'};
1031 my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum,
1032 'pkgnum' => $pkgnum,
1034 or return { 'error' => "unknown pkgnum $pkgnum" };
1036 my $svcpart = $p->{'svcpart'};
1038 my $pkg_svc = qsearchs('pkg_svc', { 'pkgpart' => $cust_pkg->pkgpart,
1039 'svcpart' => $svcpart, } )
1040 or return { 'error' => "unknown svcpart $svcpart for pkgnum $pkgnum" };
1041 my $part_svc = $pkg_svc->part_svc;
1043 my $conf = new FS::Conf;
1046 'svc' => $part_svc->svc,
1047 'svcdb' => $part_svc->svcdb,
1048 'pkgnum' => $pkgnum,
1049 'svcpart' => $svcpart,
1050 'custnum' => $custnum,
1052 'security_phrase' => 0, #XXX !
1053 'svc_acct_pop' => [], #XXX !
1055 'init_popstate' => '',
1060 small_custview( $cust_main, $conf->config('countrydefault') ),
1066 sub unprovision_svc {
1069 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1070 return { 'error' => $session } if $context eq 'error';
1072 my $search = { 'custnum' => $custnum };
1073 $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1074 my $cust_main = qsearchs('cust_main', $search )
1075 or return { 'error' => "unknown custnum $custnum" };
1077 my $svcnum = $p->{'svcnum'};
1079 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svcnum, } )
1080 or return { 'error' => "unknown svcnum $svcnum" };
1082 return { 'error' => "Service $svcnum does not belong to customer $custnum" }
1083 unless $cust_svc->cust_pkg->custnum == $custnum;
1085 my $conf = new FS::Conf;
1087 return { 'svc' => $cust_svc->part_svc->svc,
1088 'error' => $cust_svc->cancel,
1090 small_custview( $cust_main, $conf->config('countrydefault') ),
1095 sub myaccount_passwd {
1097 my($context, $session, $custnum) = _custoragent_session_custnum($p);
1098 return { 'error' => $session } if $context eq 'error';
1100 return { 'error' => "New passwords don't match." }
1101 if $p->{'new_password'} ne $p->{'new_password2'};
1103 return { 'error' => 'Enter new password' }
1104 unless length($p->{'new_password'});
1106 #my $search = { 'custnum' => $custnum };
1107 #$search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent';
1108 $custnum =~ /^(\d+)$/ or die "illegal custnum";
1109 my $search = " AND custnum = $1";
1110 $search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent';
1112 my $svc_acct = qsearchs( {
1113 'table' => 'svc_acct',
1114 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) '.
1115 'LEFT JOIN cust_pkg USING ( pkgnum ) '.
1116 'LEFT JOIN cust_main USING ( custnum ) ',
1117 'hashref' => { 'svcnum' => $p->{'svcnum'}, },
1118 'extra_sql' => $search, #important
1120 or return { 'error' => "Service not found" };
1122 $svc_acct->_password($p->{'new_password'});
1123 my $error = $svc_acct->replace();
1125 my($label, $value) = $svc_acct->cust_svc->label;
1127 return { 'error' => $error,
1136 sub _custoragent_session_custnum {
1139 my($context, $session, $custnum);
1140 if ( $p->{'session_id'} ) {
1142 $context = 'customer';
1143 $session = _cache->get($p->{'session_id'})
1144 or return ( 'error' => "Can't resume session" ); #better error message
1145 $custnum = $session->{'custnum'};
1147 } elsif ( $p->{'agent_session_id'} ) {
1150 my $agent_cache = new FS::ClientAPI_SessionCache( {
1151 'namespace' => 'FS::ClientAPI::Agent',
1153 $session = $agent_cache->get($p->{'agent_session_id'})
1154 or return ( 'error' => "Can't resume session" ); #better error message
1155 $custnum = $p->{'custnum'};
1158 return ( 'error' => "Can't resume session" ); #better error message
1161 ($context, $session, $custnum);