| 1 | package Net::Async::Webservice::UPS::CreditCard; | ||
| 2 | $Net::Async::Webservice::UPS::CreditCard::VERSION = '1.1.4'; | ||
| 3 | { | ||
| 4 | $Net::Async::Webservice::UPS::CreditCard::DIST = 'Net-Async-Webservice-UPS'; | ||
| 5 | } | ||
| 6 | use Moo; | ||
| 7 | use 5.010; | ||
| 8 | use Types::Standard qw(Str Int); | ||
| 9 | use Net::Async::Webservice::UPS::Types ':types'; | ||
| 10 | use namespace::autoclean; | ||
| 11 | |||
| 12 | # ABSTRACT: a credit card to pay UPS shipments with | ||
| 13 | |||
| 14 | |||
| 15 | has code => ( | ||
| 16 | is => 'ro', | ||
| 17 | isa => CreditCardCode, | ||
| 18 | required => 1, | ||
| 19 | ); | ||
| 20 | |||
| 21 | |||
| 22 | has type => ( | ||
| 23 | is => 'ro', | ||
| 24 | isa => CreditCardType, | ||
| 25 | required => 1, | ||
| 26 | ); | ||
| 27 | |||
| 28 | |||
| 29 | has number => ( | ||
| 30 | is => 'ro', | ||
| 31 | isa => Str, | ||
| 32 | required => 1, | ||
| 33 | ); | ||
| 34 | |||
| 35 | |||
| 36 | has expiration_year => ( | ||
| 37 | is => 'ro', | ||
| 38 | isa => Int, | ||
| 39 | required => 1, | ||
| 40 | ); | ||
| 41 | |||
| 42 | |||
| 43 | has expiration_month => ( | ||
| 44 | is => 'ro', | ||
| 45 | isa => Int, | ||
| 46 | required => 1, | ||
| 47 | ); | ||
| 48 | |||
| 49 | |||
| 50 | has security_code => ( | ||
| 51 | is => 'ro', | ||
| 52 | isa => Str, | ||
| 53 | required => 0, | ||
| 54 | ); | ||
| 55 | |||
| 56 | |||
| 57 | has address => ( | ||
| 58 | is => 'ro', | ||
| 59 | isa => Address, | ||
| 60 | required => 1, | ||
| 61 | ); | ||
| 62 | |||
| 63 | my %code_for_type = ( | ||
| 64 | AMEX => '01', | ||
| 65 | Discover => '03', | ||
| 66 | MasterCard => '04', | ||
| 67 | Optima => '05', | ||
| 68 | VISA => '06', | ||
| 69 | Bravo => '07', | ||
| 70 | Diners => '08', | ||
| 71 | ); | ||
| 72 | my %type_for_code = reverse %code_for_type; | ||
| 73 | |||
| 74 | |||
| 75 | sub type_for_code { | ||
| 76 | my ($code) = @_; | ||
| 77 | return $type_for_code{$code}; | ||
| 78 | } | ||
| 79 | |||
| 80 | |||
| 81 | around BUILDARGS => sub { | ||
| 82 | my ($orig,$class,@etc) = @_; | ||
| 83 | my $args = $class->$orig(@etc); | ||
| 84 | if ($args->{code} and not $args->{type}) { | ||
| 85 | $args->{type} = $type_for_code{$args->{code}}; | ||
| 86 | if (!defined $args->{type}) { | ||
| 87 | require Carp; | ||
| 88 | Carp::croak "Bad credit card code $args->{code}"; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | elsif ($args->{type} and not $args->{code}) { | ||
| 92 | $args->{code} = $code_for_type{$args->{type}}; | ||
| 93 | if (!defined $args->{code}) { | ||
| 94 | require Carp; | ||
| 95 | Carp::croak "Bad credit card type $args->{type}"; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | return $args; | ||
| 99 | }; | ||
| 100 | |||
| 101 | |||
| 102 | sub as_hash { | ||
| 103 | my ($self) = @_; | ||
| 104 | |||
| 105 | return { | ||
| 106 | Type => $self->code, | ||
| 107 | Number => $self->number, | ||
| 108 | ExpirationDate => sprintf('%02d%02d',$self->expiration_month,$self->expiration_year), | ||
| 109 | ( $self->security_code ? ( SecurityCode => $self->security_code ) : () ), | ||
| 110 | %{$self->address->as_hash('Ship')}, | ||
| 111 | }; | ||
| 112 | } | ||
| 113 | |||
| 114 | |||
| 115 | sub cache_id { return $_[0]->number } | ||
| 116 | |||
| 117 | 1; | ||
| 118 | |||
| 119 | __END__ |