Skip to content
Browse files

ENH: Added credit card methods to customer. Made it easier to get an …

…active credit card.
  • Loading branch information...
1 parent e2ac4a5 commit c4111abb2add51ca0b9319607c1f6d2de53e8fdd @gdey committed Mar 4, 2012
Showing with 37 additions and 4 deletions.
  1. +2 −0 dist.ini
  2. +14 −1 lib/WWW/Chargify/CreditCard.pm
  3. +20 −2 lib/WWW/Chargify/Customer.pm
  4. +1 −1 lib/WWW/Chargify/Subscription.pm
View
2 dist.ini
@@ -23,6 +23,8 @@ JSON = 2.21
JSON::XS = 2.29
HTTP::Request = 5.827
LWP::UserAgent = 5.835
+Date::Manip = 0
+DateTime::Format::DateManip = 0
[@Git]
View
15 lib/WWW/Chargify/CreditCard.pm
@@ -23,6 +23,7 @@ no warnings qw/uninitialized/;
use WWW::Chargify;
use WWW::Chargify::Customer;
use WWW::Chargify::Meta::Attribute::Trait::APIAttribute;
+use DateTime;
use namespace::autoclean;
@@ -82,9 +83,21 @@ has [qw[ expiration_month expiration_year ]] => (
sub _hash_key { 'credit_card' };
-
sub customer { my $self = shift; WWW::Chargify::Customer->find_by_id( http => $self->http, id => $self->customer_id ) }
+sub expire_date {
+ my $self = shift;
+ return DateTime->new(
+ year => $self->expiration_year,
+ month => $self->expiration_month
+ );
+}
+sub is_expired {
+ my $self = shift;
+ return DateTime->compare( DateTime->now, $self->expire_date ) == 1;
+}
+
+
#}
1;
View
22 lib/WWW/Chargify/Customer.pm
@@ -107,7 +107,9 @@ package WWW::Chargify::Customer;
my $subscription_resource_key = WWW::Chargify::Subscription->_resource_key;
my ($objects, $response) = $http->get($resource_key,$id,$subscription_resource_key);
- return map { WWW::Chargify::Subscription->_from_hash(
+ return sort {
+ $b->id <=> $a->id
+ } map { WWW::Chargify::Subscription->_from_hash(
config => $config,
http => $http,
hash => $_->{$subscription_hash_key}
@@ -123,7 +125,6 @@ package WWW::Chargify::Customer;
my ($self, %args) = @_;
my $product = $args{product} || confess "product is requried.";
-
my %hash = (
creditcard => $args{creditcard},
next_billing_at => $args{next_billing_at},
@@ -144,4 +145,21 @@ package WWW::Chargify::Customer;
}
+ sub all_credit_cards {
+ my $self = shift;
+ return map { $_->credit_card } grep {
+ defined $_->has_credit_card
+ } $self->subscriptions
+ }
+ sub credit_cards {
+ my $self = shift;
+ return grep { !$_->is_expired } $self->all_credit_cards;
+ }
+
+ sub credit_card {
+ my $self = shift;
+ my ($cc, @i) = $self->credit_cards;
+ return $cc;
+ }
+
1;
View
2 lib/WWW/Chargify/Subscription.pm
@@ -67,7 +67,7 @@ use WWW::Chargify::Migration;
- has credit_card => ( is => 'rw', isa => 'WWW::Chargify::CreditCard' );
+ has credit_card => ( is => 'rw', isa => 'WWW::Chargify::CreditCard', predicate => 'has_credit_card' );
has cancellation_message => ( is => 'rw', isa => 'Str' );
has signup_revenue => ( is => 'rw', isa => 'Num' );
has signup_payment_id => ( is => 'rw', isa => 'Num' );

0 comments on commit c4111ab

Please sign in to comment.
Something went wrong with that request. Please try again.