+sub check_payinfo_cardtype {
+ my $self = shift;
+
+ return '' if $ignore_cardtype;
+
+ return '' unless $self->payby =~ /^(CARD|CHEK)$/;
+
+ my $payinfo = $self->payinfo;
+ $payinfo =~ s/\D//g;
+
+ # see parallel checks in cust_payby::check & payinfo_Mixin::payinfo_check
+ if ( $self->tokenized($payinfo) ) {
+ $self->set('is_tokenized', 'Y'); #so we don't try to do it again
+ if ( $self->paymask =~ /^\d+x/ ) {
+ $self->set('paycardtype', cardtype($self->paymask));
+ } else {
+ $self->set('paycardtype', '');
+ #return "paycardtype required ".
+ # "(can't derive from a token and no paymask w/prefix provided)";
+ }
+ return '';
+ }
+
+ my %bop_card_types = map { $_=>1 } values %{ card_types() };
+ my $cardtype = cardtype($payinfo);
+ $self->set('paycardtype', $cardtype);
+
+ return "$cardtype not accepted" unless $bop_card_types{$cardtype};
+
+ '';
+
+}
+
+sub _banned_pay_hashref {
+ my $self = shift;
+
+ my %payby2ban = (
+ 'CARD' => 'CARD',
+ 'DCRD' => 'CARD',
+ 'CHEK' => 'CHEK',
+ 'DCHK' => 'CHEK'
+ );
+
+ {
+ 'payby' => $payby2ban{$self->payby},
+ 'payinfo' => $self->payinfo,
+ #don't ever *search* on reason! #'reason' =>
+ };
+}
+
+sub _new_banned_pay_hashref {
+ my $self = shift;
+ my $hr = $self->_banned_pay_hashref;
+ $hr->{payinfo_hash} = 'SHA512';
+ $hr->{payinfo} = sha512_base64($hr->{payinfo});
+ $hr;
+}
+
+=item paydate_mon_year
+
+Returns a two element list consisting of the paydate month and year.
+
+=cut
+
+sub paydate_mon_year {
+ my $self = shift;
+
+ my $date = $self->paydate; # || '12-2037';
+
+ #false laziness w/elements/select-month_year.html
+ if ( $date =~ /^(\d{4})-(\d{1,2})-\d{1,2}$/ ) { #PostgreSQL date format
+ ( $2, $1 );
+ } elsif ( $date =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
+ ( $1, $3 );
+ } else {
+ warn "unrecognized expiration date format: $date";
+ ( '', '' );
+ }
+
+}
+
+=item label
+
+Returns a one line text label for this payment type.
+
+=cut
+
+my %weight = (
+ 1 => 'Primary',
+ 2 => 'Secondary',
+ 3 => 'Tertiary',
+ 4 => 'Fourth',
+ 5 => 'Fifth',
+ 6 => 'Sixth',
+ 7 => 'Seventh',
+);
+
+sub label {
+ my $self = shift;
+
+ my $name = $self->payby =~ /^(CARD|DCRD)$/
+ && $self->paycardtype || FS::payby->shortname($self->payby);
+
+ ( $self->payby =~ /^(CARD|CHEK)$/ ? $weight{$self->weight}. ' automatic '
+ : 'Manual '
+ ).
+ "$name: ". $self->paymask.
+ ( $self->payby =~ /^(CARD|DCRD)$/
+ ? ' Exp '. join('/', $self->paydate_mon_year)
+ : ''
+ );
+
+}
+
+=item realtime_bop
+
+Runs a L<realtime_bop|FS::cust_main::Billing_Realtime::realtime_bop> transaction on this card
+
+=cut
+
+sub realtime_bop {
+ my( $self, %opt ) = @_;
+
+ $self->cust_main->realtime_bop({
+ %opt,
+ 'cust_payby' => $self,
+ });
+
+}
+
+=item tokenize
+
+Runs a L<realtime_tokenize|FS::cust_main::Billing_Realtime::realtime_tokenize> transaction on this card
+
+=cut
+
+sub tokenize {
+ my $self = shift;
+ return '' unless $self->payby =~ /^(CARD|DCRD)$/;
+
+ $self->cust_main->realtime_tokenize({
+ 'cust_payby' => $self,
+ });
+
+}
+
+=item verify
+
+Runs a L<realtime_verify_bop|FS::cust_main::Billing_Realtime/realtime_verify_bop> transaction on this card
+
+=cut
+
+sub verify {
+ my $self = shift;
+ return '' unless $self->payby =~ /^(CARD|DCRD)$/;
+
+ $self->cust_main->realtime_verify_bop({
+ 'cust_payby' => $self,
+ });
+
+}
+
+=item paytypes
+
+Returns a list of valid values for the paytype field (bank account type for
+electronic check payment).
+
+=cut
+
+sub paytypes {
+ #my $class = shift;
+
+ ('', 'Personal checking', 'Personal savings', 'Business checking', 'Business savings');
+}
+
+=item cgi_cust_payby_fields
+
+Returns the field names used in the web interface (including some pseudo-fields).
+
+=cut
+
+sub cgi_cust_payby_fields {
+ #my $class = shift;
+ [qw( payby payinfo paydate_month paydate_year paycvv payname weight
+ payinfo1 payinfo2 payinfo3 paytype paystate payname_CHEK )];
+}
+
+=item cgi_hash_callback HASHREF OLD
+
+Subroutine (not a class or object method). Processes a hash reference
+of web interface contet (transfers the data from pseudo-fields to real fields).
+
+If OLD object is passed, also preserves locationnum, paystart_month, paystart_year,
+payissue and payip. If the new field is blank but the old is not, the old field
+will be preserved.
+
+=cut
+
+sub cgi_hash_callback {
+ my $hashref = shift;
+ my $old = shift;
+
+ my %noauto = (
+ 'CARD' => 'DCRD',
+ 'CHEK' => 'DCHK',
+ );
+ # the payby selector gives the choice of CARD or CHEK (or others, but
+ # those are the ones with auto and on-demand versions). if the user didn't
+ # choose a weight, then they mean DCRD/DCHK.
+ $hashref->{payby} = $noauto{$hashref->{payby}}
+ if ! $hashref->{weight} && exists $noauto{$hashref->{payby}};
+
+ if ( $hashref->{payby} =~ /^(CHEK|DCHK)$/ ) {
+
+ unless ( grep $hashref->{$_}, qw(payinfo1 payinfo2 payinfo3 payname_CHEK)) {
+ %$hashref = ();
+ return;
+ }
+
+ $hashref->{payinfo} = $hashref->{payinfo1}. '@';
+ $hashref->{payinfo} .= $hashref->{payinfo3}.'.'
+ if $conf->config('echeck-country') eq 'CA';
+ $hashref->{payinfo} .= $hashref->{'payinfo2'};
+
+ $hashref->{payname} = $hashref->{'payname_CHEK'};
+
+ } elsif ( $hashref->{payby} =~ /^(CARD|DCRD)$/ ) {
+
+ unless ( grep $hashref->{$_}, qw( payinfo paycvv payname ) ) {
+ %$hashref = ();
+ return;
+ }
+
+ }
+
+ $hashref->{paydate}= $hashref->{paydate_month}. '-'. $hashref->{paydate_year};
+
+ if ($old) {
+ foreach my $field ( qw(locationnum paystart_month paystart_year payissue payip) ) {
+ next if $hashref->{$field};
+ next unless $old->get($field);
+ $hashref->{$field} = $old->get($field);
+ }
+ }
+
+}
+
+=item search_sql
+
+Class method.
+
+Returns a qsearch hash expression to search for parameters specified in HASHREF.
+Valid paramters are:
+
+=over 4
+
+=item payby
+
+listref
+
+=item paydate_year
+
+=item paydate_month
+
+