X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2FClientAPI%2FMyAccount.pm;h=54b8a9979c92724de9b22e377f364c6200eac8f1;hb=97316d268e5751a1d08a0a37e5a0456f2ce4815c;hp=77c1fc88931a2b5fac945f3e3f5859184d766d1d;hpb=a2746023c2b286d67ab5e4d5777e8cb271525a13;p=freeside.git diff --git a/FS/FS/ClientAPI/MyAccount.pm b/FS/FS/ClientAPI/MyAccount.pm index 77c1fc889..54b8a9979 100644 --- a/FS/FS/ClientAPI/MyAccount.pm +++ b/FS/FS/ClientAPI/MyAccount.pm @@ -2,14 +2,16 @@ package FS::ClientAPI::MyAccount; use strict; use vars qw($cache); +use subs qw(_cache); use Digest::MD5 qw(md5_hex); use Date::Format; use Business::CreditCard; -use Cache::SharedMemoryCache; #store in db? +use Time::Duration; use FS::CGI qw(small_custview); #doh use FS::Conf; use FS::Record qw(qsearch qsearchs); use FS::Msgcat qw(gettext); +use FS::ClientAPI_SessionCache; use FS::svc_acct; use FS::svc_domain; use FS::svc_external; @@ -19,34 +21,22 @@ use FS::cust_bill; use FS::cust_main_county; use FS::cust_pkg; -use FS::ClientAPI; #hmm -FS::ClientAPI->register_handlers( - 'MyAccount/login' => \&login, - 'MyAccount/customer_info' => \&customer_info, - 'MyAccount/edit_info' => \&edit_info, - 'MyAccount/invoice' => \&invoice, - 'MyAccount/cancel' => \&cancel, - 'MyAccount/payment_info' => \&payment_info, - 'MyAccount/process_payment' => \&process_payment, - 'MyAccount/list_pkgs' => \&list_pkgs, - 'MyAccount/order_pkg' => \&order_pkg, - 'MyAccount/cancel_pkg' => \&cancel_pkg, - 'MyAccount/charge' => \&charge, -); - use vars qw( @cust_main_editable_fields ); @cust_main_editable_fields = qw( first last company address1 address2 city county state zip country daytime night fax ship_first ship_last ship_company ship_address1 ship_address2 ship_city ship_state ship_zip ship_country ship_daytime ship_night ship_fax - payby payinfo payname + payby payinfo payname paystart_month paystart_year payissue payip ); -#store in db? -my $cache = new Cache::SharedMemoryCache( { - 'namespace' => 'FS::ClientAPI::MyAccount', -} ); +use subs qw(_provision); + +sub _cache { + $cache ||= new FS::ClientAPI_SessionCache( { + 'namespace' => 'FS::ClientAPI::MyAccount', + } ); +} #false laziness w/FS::ClientAPI::passwd::passwd sub login { @@ -59,6 +49,13 @@ sub login { 'domsvc' => $svc_domain->svcnum, } ); return { error => 'User not found.' } unless $svc_acct; + + my $conf = new FS::Conf; + my $pkg_svc = $svc_acct->cust_svc->pkg_svc; + return { error => 'Only primary user may log in.' } + if $conf->exists('selfservice_server-primary_only') + && ( ! $pkg_svc || $pkg_svc->primary_svc ne 'Y' ); + return { error => 'Incorrect password.' } unless $svc_acct->check_password($p->{'password'}); @@ -75,27 +72,37 @@ sub login { my $session_id; do { $session_id = md5_hex(md5_hex(time(). {}. rand(). $$)) - } until ( ! defined $cache->get($session_id) ); #just in case + } until ( ! defined _cache->get($session_id) ); #just in case - $cache->set( $session_id, $session, '1 hour' ); + _cache->set( $session_id, $session, '1 hour' ); return { 'error' => '', 'session_id' => $session_id, }; } -sub customer_info { +sub logout { my $p = shift; - my $session = $cache->get($p->{'session_id'}) - or return { 'error' => "Can't resume session" }; #better error message + if ( $p->{'session_id'} ) { + _cache->remove($p->{'session_id'}); + return { 'error' => '' }; + } else { + return { 'error' => "Can't resume session" }; #better error message + } +} - my %return; +sub customer_info { + my $p = shift; - my $custnum = $session->{'custnum'}; + my($context, $session, $custnum) = _custoragent_session_custnum($p); + return { 'error' => $session } if $context eq 'error'; + my %return; if ( $custnum ) { #customer record - my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } ) + my $search = { 'custnum' => $custnum }; + $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent'; + my $cust_main = qsearchs('cust_main', $search ) or return { 'error' => "unknown custnum $custnum" }; $return{balance} = $cust_main->balance; @@ -111,7 +118,7 @@ sub customer_info { my $conf = new FS::Conf; $return{small_custview} = - small_custview( $cust_main, $conf->config('defaultcountry') ); + small_custview( $cust_main, $conf->config('countrydefault') ); $return{name} = $cust_main->first. ' '. $cust_main->get('last'); @@ -125,16 +132,20 @@ sub customer_info { } $return{'invoicing_list'} = - join(', ', grep { $_ ne 'POST' } $cust_main->invoicing_list ); + join(', ', grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list ); $return{'postal_invoicing'} = 0 < ( grep { $_ eq 'POST' } $cust_main->invoicing_list ); - } else { #no customer record + } elsif ( $session->{'svcnum'} ) { #no customer record my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $session->{'svcnum'} } ) or die "unknown svcnum"; $return{name} = $svc_acct->email; + } else { + + return { 'error' => 'Expired session' }; #XXX redirect to login w/this err! + } return { 'error' => '', @@ -146,7 +157,7 @@ sub customer_info { sub edit_info { my $p = shift; - my $session = $cache->get($p->{'session_id'}) + my $session = _cache->get($p->{'session_id'}) or return { 'error' => "Can't resume session" }; #better error message my $custnum = $session->{'custnum'} @@ -186,10 +197,46 @@ sub edit_info { sub payment_info { my $p = shift; - my $session = $cache->get($p->{'session_id'}) + my $session = _cache->get($p->{'session_id'}) or return { 'error' => "Can't resume session" }; #better error message - my %return; + ## + #generic + ## + + my $conf = new FS::Conf; + my %states = map { $_->state => 1 } + qsearch('cust_main_county', { + 'country' => $conf->config('countrydefault') || 'US' + } ); + + use vars qw($payment_info); #cache for performance + $payment_info ||= { + + #list all counties/states/countries + 'cust_main_county' => + [ map { $_->hashref } qsearch('cust_main_county', {}) ], + + #shortcut for one-country folks + 'states' => + [ sort { $a cmp $b } keys %states ], + + 'card_types' => { + 'VISA' => 'VISA card', + 'MasterCard' => 'MasterCard', + 'Discover' => 'Discover card', + 'American Express' => 'American Express card', + 'Switch' => 'Switch', + 'Solo' => 'Solo', + }, + + }; + + ## + #customer-specific + ## + + my %return = %$payment_info; my $custnum = $session->{'custnum'}; @@ -213,25 +260,7 @@ sub payment_info { } - #list all counties/states/countries - $return{'cust_main_county'} = - [ map { $_->hashref } qsearch('cust_main_county', {}) ], - - #shortcut for one-country folks - my $conf = new FS::Conf; - my %states = map { $_->state => 1 } - qsearch('cust_main_county', { - 'country' => $conf->config('defaultcountry') || 'US' - } ); - $return{'states'} = [ sort { $a cmp $b } keys %states ]; - - $return{card_types} = { - 'VISA' => 'VISA card', - 'MasterCard' => 'MasterCard', - 'Discover' => 'Discover card', - 'American Express' => 'American Express card', - }; - + #doubleclick protection my $_date = time; $return{paybatch} = "webui-MyAccount-$_date-$$-". rand() * 2**32; @@ -247,7 +276,7 @@ sub process_payment { my $p = shift; - my $session = $cache->get($p->{'session_id'}) + my $session = _cache->get($p->{'session_id'}) or return { 'error' => "Can't resume session" }; #better error message my %return; @@ -314,7 +343,8 @@ sub process_payment { 'payname' => $payname, 'paybatch' => $paybatch, 'paycvv' => $paycvv, - map { $_ => $p->{$_} } qw( address1 address2 city state zip ) + map { $_ => $p->{$_} } qw( paystart_month paystart_year payissue payip + address1 address2 city state zip ) ); return { 'error' => $error } if $error; @@ -323,7 +353,8 @@ sub process_payment { if ( $p->{'save'} ) { my $new = new FS::cust_main { $cust_main->hash }; $new->set( $_ => $p->{$_} ) - foreach qw( payname address1 address2 city state zip payinfo ); + foreach qw( payname paystart_month paystart_year payissue payip + address1 address2 city state zip payinfo ); $new->set( 'paydate' => $p->{'year'}. '-'. $p->{'month'}. '-01' ); $new->set( 'payby' => $p->{'auto'} ? 'CARD' : 'DCRD' ); my $error = $new->replace($cust_main); @@ -335,9 +366,39 @@ sub process_payment { } +sub process_prepay { + + my $p = shift; + + my $session = _cache->get($p->{'session_id'}) + or return { 'error' => "Can't resume session" }; #better error message + + my %return; + + my $custnum = $session->{'custnum'}; + + my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } ) + or return { 'error' => "unknown custnum $custnum" }; + + my( $amount, $seconds ) = ( 0, 0 ); + my $error = $cust_main->recharge_prepay( $p->{'prepaid_cardnum'}, + \$amount, + \$seconds + ); + + return { 'error' => $error } if $error; + + return { 'error' => '', + 'amount' => $amount, + 'seconds' => $seconds, + 'duration' => duration_exact($seconds), + }; + +} + sub invoice { my $p = shift; - my $session = $cache->get($p->{'session_id'}) + my $session = _cache->get($p->{'session_id'}) or return { 'error' => "Can't resume session" }; #better error message my $custnum = $session->{'custnum'}; @@ -353,13 +414,61 @@ sub invoice { return { 'error' => '', 'invnum' => $invnum, 'invoice_text' => join('', $cust_bill->print_text ), + 'invoice_html' => $cust_bill->print_html, }; } +sub invoice_logo { + my $p = shift; + + #sessioning for this? how do we get the session id to the backend invoice + # template so it can add it to the link, blah + + my $templatename = $p->{'templatename'}; + + #false laziness-ish w/view/cust_bill-logo.cgi + + my $conf = new FS::Conf; + if ( $templatename =~ /^([^\.\/]*)$/ && $conf->exists("logo_$1.png") ) { + $templatename = "_$1"; + } else { + $templatename = ''; + } + + my $filename = "logo$templatename.png"; + + return { 'error' => '', + 'logo' => $conf->config_binary($filename), + 'content_type' => 'image/png', #should allow gif, jpg too + }; +} + + +sub list_invoices { + my $p = shift; + my $session = _cache->get($p->{'session_id'}) + or return { 'error' => "Can't resume session" }; #better error message + + my $custnum = $session->{'custnum'}; + + my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } ) + or return { 'error' => "unknown custnum $custnum" }; + + my @cust_bill = $cust_main->cust_bill; + + return { 'error' => '', + 'invoices' => [ map { { 'invnum' => $_->invnum, + '_date' => $_->_date, + } + } @cust_bill + ] + }; +} + sub cancel { my $p = shift; - my $session = $cache->get($p->{'session_id'}) + my $session = _cache->get($p->{'session_id'}) or return { 'error' => "Can't resume session" }; #better error message my $custnum = $session->{'custnum'}; @@ -377,26 +486,100 @@ sub cancel { sub list_pkgs { my $p = shift; - my $session = $cache->get($p->{'session_id'}) - or return { 'error' => "Can't resume session" }; #better error message - my $custnum = $session->{'custnum'}; + my($context, $session, $custnum) = _custoragent_session_custnum($p); + return { 'error' => $session } if $context eq 'error'; - my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } ) + my $search = { 'custnum' => $custnum }; + $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent'; + my $cust_main = qsearchs('cust_main', $search ) or return { 'error' => "unknown custnum $custnum" }; - return { 'cust_pkg' => [ map { $_->hashref } $cust_main->ncancelled_pkgs ] }; + #return { 'cust_pkg' => [ map { $_->hashref } $cust_main->ncancelled_pkgs ] }; + + my $conf = new FS::Conf; + + { 'svcnum' => $session->{'svcnum'}, + 'custnum' => $custnum, + 'cust_pkg' => [ map { + { $_->hash, + $_->part_pkg->hash, + part_svc => + [ map $_->hashref, $_->available_part_svc ], + cust_svc => + [ map { my $ref = { $_->hash, + label => [ $_->label ], + }; + $ref->{_password} = $_->svc_x->_password + if $context eq 'agent' + && $conf->exists('agent-showpasswords') + && $_->part_svc->svcdb eq 'svc_acct'; + $ref; + } $_->cust_svc + ], + }; + } $cust_main->ncancelled_pkgs + ], + 'small_custview' => + small_custview( $cust_main, $conf->config('countrydefault') ), + }; + +} + +sub list_svcs { + my $p = shift; + + use Data::Dumper; + + my($context, $session, $custnum) = _custoragent_session_custnum($p); + return { 'error' => $session } if $context eq 'error'; + + my $search = { 'custnum' => $custnum }; + $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent'; + my $cust_main = qsearchs('cust_main', $search ) + or return { 'error' => "unknown custnum $custnum" }; + + my @cust_svc = (); + #foreach my $cust_pkg ( $cust_main->ncancelled_pkgs ) { + foreach my $cust_pkg ( $cust_main->unsuspended_pkgs ) { + push @cust_svc, @{[ $cust_pkg->cust_svc ]}; #@{[ ]} to force array context + } + @cust_svc = grep { $_->part_svc->svcdb eq $p->{'svcdb'} } @cust_svc + if $p->{'svcdb'}; + + #@svc_x = sort { $a->domain cmp $b->domain || $a->username cmp $b->username } + # @svc_x; + + { + #no#'svcnum' => $session->{'svcnum'}, + 'custnum' => $custnum, + 'svcs' => [ map { + my $svc_x = $_->svc_x; + my($label, $value) = $_->label; + + { 'svcnum' => $_->svcnum, + 'label' => $label, + 'value' => $value, + 'username' => $svc_x->username, + 'email' => $svc_x->email, + # more... + }; + } + @cust_svc + ], + }; } sub order_pkg { my $p = shift; - my $session = $cache->get($p->{'session_id'}) - or return { 'error' => "Can't resume session" }; #better error message - my $custnum = $session->{'custnum'}; + my($context, $session, $custnum) = _custoragent_session_custnum($p); + return { 'error' => $session } if $context eq 'error'; - my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } ) + my $search = { 'custnum' => $custnum }; + $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent'; + my $cust_main = qsearchs('cust_main', $search ) or return { 'error' => "unknown custnum $custnum" }; #false laziness w/ClientAPI/Signup.pm @@ -477,10 +660,16 @@ sub order_pkg { $cust_main->apply_credits; $bill_error = $cust_main->collect; - if ( $cust_main->balance > $old_balance + if ( $cust_main->balance > $old_balance + && $cust_main->balance > 0 && $cust_main->payby !~ /^(BILL|DCRD|DCHK)$/ ) { + #this makes sense. credit is "un-doing" the invoice + $cust_main->credit( sprintf("%.2f", $cust_main->balance - $old_balance ), + 'self-service decline' ); + $cust_main->apply_credits( 'order' => 'newest' ); + $cust_pkg->cancel('quiet'=>1); - return { 'error' => '_decline' }; + return { 'error' => '_decline', 'bill_error' => $bill_error }; } else { $cust_pkg->reexport; } @@ -495,7 +684,7 @@ sub order_pkg { sub cancel_pkg { my $p = shift; - my $session = $cache->get($p->{'session_id'}) + my $session = _cache->get($p->{'session_id'}) or return { 'error' => "Can't resume session" }; #better error message my $custnum = $session->{'custnum'}; @@ -503,16 +692,227 @@ sub cancel_pkg { my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } ) or return { 'error' => "unknown custnum $custnum" }; - my $pkgnum = $session->{'pkgnum'}; + my $pkgnum = $p->{'pkgnum'}; my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum, 'pkgnum' => $pkgnum, } ) or return { 'error' => "unknown pkgnum $pkgnum" }; - my $error = $cust_main->cancel( 'quiet'=>1 ); + my $error = $cust_pkg->cancel( 'quiet'=>1 ); return { 'error' => $error }; } +sub provision_acct { + my $p = shift; + + return { 'error' => gettext('passwords_dont_match') } + if $p->{'_password'} ne $p->{'_password2'}; + return { 'error' => gettext('empty_password') } + unless length($p->{'_password'}); + + _provision( 'FS::svc_acct', + [qw(username _password)], + [qw(username _password)], + $p, + @_ + ); +} + +sub provision_external { + my $p = shift; + #_provision( 'FS::svc_external', [qw(id title)], [qw(id title)], $p, @_ ); + _provision( 'FS::svc_external', + [], + [qw(id title)], + $p, + @_ + ); +} + +sub _provision { + my( $class, $fields, $return_fields, $p ) = splice(@_, 0, 4); + + my($context, $session, $custnum) = _custoragent_session_custnum($p); + return { 'error' => $session } if $context eq 'error'; + + my $search = { 'custnum' => $custnum }; + $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent'; + my $cust_main = qsearchs('cust_main', $search ) + or return { 'error' => "unknown custnum $custnum" }; + + my $pkgnum = $p->{'pkgnum'}; + + my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum, + 'pkgnum' => $pkgnum, + } ) + or return { 'error' => "unknown pkgnum $pkgnum" }; + + my $part_svc = qsearchs('part_svc', { 'svcpart' => $p->{'svcpart'} } ) + or return { 'error' => "unknown svcpart $p->{'svcpart'}" }; + + my $svc_x = $class->new( { + 'pkgnum' => $p->{'pkgnum'}, + 'svcpart' => $p->{'svcpart'}, + map { $_ => $p->{$_} } @$fields + } ); + my $error = $svc_x->insert; + $svc_x = qsearchs($svc_x->table, { 'svcnum' => $svc_x->svcnum }) + unless $error; + + return { 'svc' => $part_svc->svc, + 'error' => $error, + map { $_ => $svc_x->get($_) } @$return_fields + }; + +} + +sub part_svc_info { + my $p = shift; + + my($context, $session, $custnum) = _custoragent_session_custnum($p); + return { 'error' => $session } if $context eq 'error'; + + my $search = { 'custnum' => $custnum }; + $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent'; + my $cust_main = qsearchs('cust_main', $search ) + or return { 'error' => "unknown custnum $custnum" }; + + my $pkgnum = $p->{'pkgnum'}; + + my $cust_pkg = qsearchs('cust_pkg', { 'custnum' => $custnum, + 'pkgnum' => $pkgnum, + } ) + or return { 'error' => "unknown pkgnum $pkgnum" }; + + my $svcpart = $p->{'svcpart'}; + + my $pkg_svc = qsearchs('pkg_svc', { 'pkgpart' => $cust_pkg->pkgpart, + 'svcpart' => $svcpart, } ) + or return { 'error' => "unknown svcpart $svcpart for pkgnum $pkgnum" }; + my $part_svc = $pkg_svc->part_svc; + + my $conf = new FS::Conf; + + return { + 'svc' => $part_svc->svc, + 'svcdb' => $part_svc->svcdb, + 'pkgnum' => $pkgnum, + 'svcpart' => $svcpart, + 'custnum' => $custnum, + + 'security_phrase' => 0, #XXX ! + 'svc_acct_pop' => [], #XXX ! + 'popnum' => '', + 'init_popstate' => '', + 'popac' => '', + 'acstate' => '', + + 'small_custview' => + small_custview( $cust_main, $conf->config('countrydefault') ), + + }; + +} + +sub unprovision_svc { + my $p = shift; + + my($context, $session, $custnum) = _custoragent_session_custnum($p); + return { 'error' => $session } if $context eq 'error'; + + my $search = { 'custnum' => $custnum }; + $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent'; + my $cust_main = qsearchs('cust_main', $search ) + or return { 'error' => "unknown custnum $custnum" }; + + my $svcnum = $p->{'svcnum'}; + + my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svcnum, } ) + or return { 'error' => "unknown svcnum $svcnum" }; + + return { 'error' => "Service $svcnum does not belong to customer $custnum" } + unless $cust_svc->cust_pkg->custnum == $custnum; + + my $conf = new FS::Conf; + + return { 'svc' => $cust_svc->part_svc->svc, + 'error' => $cust_svc->cancel, + 'small_custview' => + small_custview( $cust_main, $conf->config('countrydefault') ), + }; + +} + +sub myaccount_passwd { + my $p = shift; + my($context, $session, $custnum) = _custoragent_session_custnum($p); + return { 'error' => $session } if $context eq 'error'; + + return { 'error' => "New passwords don't match." } + if $p->{'new_password'} ne $p->{'new_password2'}; + + return { 'error' => 'Enter new password' } + unless length($p->{'new_password'}); + + #my $search = { 'custnum' => $custnum }; + #$search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent'; + $custnum =~ /^(\d+)$/ or die "illegal custnum"; + my $search = " AND custnum = $1"; + $search .= " AND agentnum = ". $session->{'agentnum'} if $context eq 'agent'; + + my $svc_acct = qsearchs( { + 'table' => 'svc_acct', + 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum ) '. + 'LEFT JOIN cust_pkg USING ( pkgnum ) '. + 'LEFT JOIN cust_main USING ( custnum ) ', + 'hashref' => { 'svcnum' => $p->{'svcnum'}, }, + 'extra_sql' => $search, #important + } ) + or return { 'error' => "Service not found" }; + + $svc_acct->_password($p->{'new_password'}); + my $error = $svc_acct->replace(); + + my($label, $value) = $svc_acct->cust_svc->label; + + return { 'error' => $error, + 'label' => $label, + 'value' => $value, + }; + +} + +#-- + +sub _custoragent_session_custnum { + my $p = shift; + + my($context, $session, $custnum); + if ( $p->{'session_id'} ) { + + $context = 'customer'; + $session = _cache->get($p->{'session_id'}) + or return ( 'error' => "Can't resume session" ); #better error message + $custnum = $session->{'custnum'}; + + } elsif ( $p->{'agent_session_id'} ) { + + $context = 'agent'; + my $agent_cache = new FS::ClientAPI_SessionCache( { + 'namespace' => 'FS::ClientAPI::Agent', + } ); + $session = $agent_cache->get($p->{'agent_session_id'}) + or return ( 'error' => "Can't resume session" ); #better error message + $custnum = $p->{'custnum'}; + + } else { + return ( 'error' => "Can't resume session" ); #better error message + } + + ($context, $session, $custnum); + +} + 1;