Skip to content

Commit

Permalink
BUG: Fixed issues with being able to update the cancel at end of peri…
Browse files Browse the repository at this point in the history
…od flag.
  • Loading branch information
gdey committed Mar 4, 2012
1 parent 57ba08c commit 195c02f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
13 changes: 10 additions & 3 deletions lib/WWW/Chargify/HTTP.pm
Expand Up @@ -91,15 +91,15 @@ sub post {
my $path = join '/', @path;
debug('Path: '.$path.'Body: '.Dumper($body) );

$self->make_request( POST => $path, $options // {}, $body // "{}");
$self->make_request( POST => $path, $options // {}, $body // {});
}
sub put {
my ($self, @path) = @_;
my $body = pop @path if ref($path[-1]) eq 'HASH';
my $options = pop @path if ref($path[-1]) eq 'HASH';
my $path = join '/', grep { defined } @path;
debug('Path: '.$path.'Body: '.Dumper($body) );
$self->make_request( PUT => $path, $options // {}, $body // "{}");
$self->make_request( PUT => $path, $options // {}, $body // {});
}
sub get {

Expand Down Expand Up @@ -140,8 +140,14 @@ sub check_response_code {
$error_type = 'AuthorizationError' if $code eq '403';
$error_type = 'ServerError' if $code eq '500';
$error_type = 'DownForMaintenance' if $code eq '503';
my $content = $response->content;
if( $response->content !~ /^\s*$/ ) {
$errors = JSON::decode_json( $response->content )->{errors};
my $obj = JSON::decode_json( $response->content);
if (ref($obj) eq 'HASH'){
$errors =$obj->{errors};
} elsif ( ref($obj) eq 'ARRAY' ){
$errors = $obj->[1];
};
}
die WWW::Chargify::Exception->new(
response => $response,
Expand Down Expand Up @@ -182,6 +188,7 @@ sub make_request {
return wantarray? ($value,$response) : $value;
}

debug("Body: " . Dumper($content_body)) if $content_body;
#TODO: Need to handle errors here.
$self->check_response_code($response, $content_body);
return wantarray? (undef,$response) : undef;
Expand Down
2 changes: 1 addition & 1 deletion lib/WWW/Chargify/Role/FromHash.pm
Expand Up @@ -87,7 +87,7 @@ sub _to_hash{

my $reader = $attribute->get_read_method;
my $value = $self->$reader;
next unless $value;
next unless defined $value;

my $key = $attribute->has_APIAttributeName
? $attribute->APIAttributeName
Expand Down
20 changes: 19 additions & 1 deletion lib/WWW/Chargify/Subscription.pm
Expand Up @@ -49,7 +49,12 @@ use WWW::Chargify::Migration;
has created_at => ( is => 'rw', isa => 'DateTime', coerce => 1, );
has updated_at => ( is => 'rw', isa => 'DateTime', coerce => 1, );
has canceled_at => ( is => 'rw', isa => 'DateTime', coerce => 1, );
has delayed_cancel_at => ( is => 'rw', isa => 'DateTime', coerce => 1, );
has delayed_cancel_at => ( is => 'rw', isa => 'DateTime', coerce => 1,
predicate => 'has_delayed_cancel_at',
traits => [qw/Chargify::APIAttribute/],
isAPIUpdatable => 0,
);

has customer => ( is => 'rw',
isa => 'WWW::Chargify::Customer' ,
);
Expand Down Expand Up @@ -368,6 +373,19 @@ use WWW::Chargify::Migration;
return $self->_from_hash( http => $http, config => $http->config, hash => $object->{$self->_hash_key} );
}

sub reactivate {
my ($self, %args) = @_;

return if $self->state eq 'active' and !$self->cancel_at_end_of_period;
my $http = $self->http;
my $body = {};
$body->{include_trial} = !!$args{include_trial} if exists $args{include_trial};
my ($object, $response) = $http->put( $self->_resource_key, $self->id, reactivate => $body );
return $self->_from_hash( http => $http, config => $http->config, hash => $object->{$self->_hash_key} );
}




# curl -u $ENV{APIKEY}:x -X PUT "https://$ENV{SUBDOMAIN}.chargify.com/subscriptioF "subscription[id]=1301020" -F "subscription[vault_token]=5436078" -F "subscription[next_billing_at]=2012-02-20T22:40:58

Expand Down

0 comments on commit 195c02f

Please sign in to comment.