use FS::CGI qw(small_custview); #doh
use FS::Conf;
use FS::Record qw(qsearch qsearchs);
+use FS::Msgcat qw(gettext);
use FS::svc_acct;
use FS::svc_domain;
use FS::cust_main;
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/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
);
#store in db?
-my $cache = new Cache::SharedMemoryCache();
+my $cache = new Cache::SharedMemoryCache( {
+ 'namespace' => 'FS::ClientAPI::MyAccount',
+} );
-#false laziness w/FS::ClientAPI::passwd::passwd (needs to handle encrypted pw)
+#false laziness w/FS::ClientAPI::passwd::passwd
sub login {
my $p = shift;
my $svc_domain = qsearchs('svc_domain', { 'domain' => $p->{'domain'} } )
- or return { error => "Domain not found" };
+ or return { error => 'Domain '. $p->{'domain'}. ' not found' };
- my $svc_acct =
- ( length($p->{'password'}) < 13
- && qsearchs( 'svc_acct', { 'username' => $p->{'username'},
- 'domsvc' => $svc_domain->svcnum,
- '_password' => $p->{'password'} } )
- )
- || qsearchs( 'svc_acct', { 'username' => $p->{'username'},
- 'domsvc' => $svc_domain->svcnum,
- '_password' => $p->{'password'} } );
-
- unless ( $svc_acct ) { return { error => 'Incorrect password.' } }
+ my $svc_acct = qsearchs( 'svc_acct', { 'username' => $p->{'username'},
+ 'domsvc' => $svc_domain->svcnum, }
+ );
+ return { error => 'User not found.' } unless $svc_acct;
+ return { error => 'Incorrect password.' }
+ unless $svc_acct->check_password($p->{'password'});
my $session = {
'svcnum' => $svc_acct->svcnum,
$return{name} = $cust_main->first. ' '. $cust_main->get('last');
+ for (@cust_main_editable_fields) {
+ $return{$_} = $cust_main->get($_);
+ }
+
} else { #no customer record
my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $session->{'svcnum'} } )
}
+sub edit_info {
+ my $p = shift;
+ my $session = $cache->get($p->{'session_id'})
+ or return { 'error' => "Can't resume session" }; #better error message
+
+ my $custnum = $session->{'custnum'}
+ or return { 'error' => "no customer record" };
+
+ my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
+ or return { 'error' => "unknown custnum $custnum" };
+
+ my $new = new FS::cust_main { $cust_main->hash };
+ $new->set( $_ => $p->{$_} )
+ foreach grep { exists $p->{$_} } @cust_main_editable_fields;
+ my $error = $new->replace($cust_main);
+ return { 'error' => $error } if $error;
+ #$cust_main = $new;
+
+ return { 'error' => '' };
+}
+
sub payment_info {
my $p = shift;
my $session = $cache->get($p->{'session_id'})
warn $return{card_type} = cardtype($cust_main->payinfo);
$return{payinfo} = $cust_main->payinfo;
- if ( $cust_main->paydate =~ /^(\d{4})-(\d{2})-\d{2}$/ ) { #Pg date format
- @return{'month', 'year'} = ( $2, $1 );
- } elsif ( $cust_main->paydate =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
- @return{'month', 'year'} = ( $1, $3 );
- }
+ @return{'month', 'year'} = $cust_main->paydate_monthyear;
}
};
+#some false laziness with httemplate/process/payment.cgi - look there for
+#ACH and CVV support stuff
sub process_payment {
+
my $p = shift;
my $session = $cache->get($p->{'session_id'})
my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
or return { 'error' => "unknown custnum $custnum" };
+ $p->{'payname'} =~ /^([\w \,\.\-\']+)$/
+ or return { 'error' => gettext('illegal_name'). " payname: ". $p->{'payname'} };
+ my $payname = $1;
+
+ $p->{'paybatch'} =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
+ or return { 'error' => gettext('illegal_text'). " paybatch: ". $p->{'paybatch'} };
+ my $paybatch = $1;
+
+ my $payinfo;
+ my $paycvv = '';
+ #if ( $payby eq 'CHEK' ) {
+ #
+ # $p->{'payinfo1'} =~ /^(\d+)$/
+ # or return { 'error' => "illegal account number ". $p->{'payinfo1'} };
+ # my $payinfo1 = $1;
+ # $p->{'payinfo2'} =~ /^(\d+)$/
+ # or return { 'error' => "illegal ABA/routing number ". $p->{'payinfo2'} };
+ # my $payinfo2 = $1;
+ # $payinfo = $payinfo1. '@'. $payinfo2;
+ #
+ #} elsif ( $payby eq 'CARD' ) {
+
+ $payinfo = $p->{'payinfo'};
+ $payinfo =~ s/\D//g;
+ $payinfo =~ /^(\d{13,16})$/
+ or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
+ $payinfo = $1;
+ validate($payinfo)
+ or return { 'error' => gettext('invalid_card') }; # . ": ". $self->payinfo
+ return { 'error' => gettext('unknown_card_type') }
+ if cardtype($payinfo) eq "Unknown";
+
+ if ( defined $cust_main->dbdef_table->column('paycvv') ) {
+ if ( length($p->{'paycvv'} ) ) {
+ if ( cardtype($payinfo) eq 'American Express card' ) {
+ $p->{'paycvv'} =~ /^(\d{4})$/
+ or return { 'error' => "CVV2 (CID) for American Express cards is four digits." };
+ $paycvv = $1;
+ } else {
+ $p->{'paycvv'} =~ /^(\d{3})$/
+ or return { 'error' => "CVV2 (CVC2/CID) is three digits." };
+ $paycvv = $1;
+ }
+ }
+ }
+
+ #} else {
+ # die "unknown payby $payby";
+ #}
+
+ my $error = $cust_main->realtime_bop( 'CC', $p->{'amount'},
+ 'quiet' => 1,
+ 'payinfo' => $payinfo,
+ 'paydate' => $p->{'year'}. '-'. $p->{'month'}. '-01',
+ 'payname' => $payname,
+ 'paybatch' => $paybatch,
+ 'paycvv' => $paycvv,
+ map { $_ => $p->{$_} } qw( address1 address2 city state zip )
+ );
+ return { 'error' => $error } if $error;
+
+ $cust_main->apply_payments;
+
if ( $p->{'save'} ) {
my $new = new FS::cust_main { $cust_main->hash };
$new->set( $_ => $p->{$_} )
$cust_main = $new;
}
- my $error = $cust_main->realtime_bop( 'CC', $p->{'amount'}, quiet=>1,
- 'paydate' => $p->{'year'}. '-'. $p->{'month'}. '-01',
- map { $_ => $p->{$_} }
- qw( payname address1 address2 city state zip payinfo paybatch )
- );
- return { 'error' => $error } if $error;
-
- $cust_main->apply_payments;
-
return { 'error' => '' };
}