Business-Stripe-Subscription

 view release on metacpan or  search on metacpan

lib/Business/Stripe/Subscription.pm  view on Meta::CPAN

    }
    $self->{'error'} = 'Failed to update checkout session';
    return undef;
}

# Retrieve subscription object from Stripe
sub get_subscription {
    my ($self, $subscription) = @_;
    
    if (!$subscription) {
        $self->{'error'} = 'Subscription missing';
        $self->_error('Subscription missing');
        return undef;
    }

    return $http->get("https://api.stripe.com/v1/subscriptions/$subscription", $self->_get_header);
}

# Cancel subscription at end of current period
sub cancel {
    my ($self, $subscription, $cancel) = @_;
    
    $self->{'error'} = '';
    $self->{'error'} = 'Subscription missing' unless $subscription;
    return undef if $self->{'error'};
    
    $cancel = 1 unless defined $cancel;
    my $state = $cancel ? 'true' : 'false';
    
    my $vars = {
        'cancel_at_period_end'  => $state,
    };
    
    my $response = $http->post_form("https://api.stripe.com/v1/subscriptions/$subscription", $vars, $self->_get_header);

    if ($response->{'success'}) {
        return $cancel;
    }
    $self->{'error'} = 'Failed to set cancellation';
    return undef;
}

# Cancel subscription immediately
sub cancel_now {
    my ($self, $subscription) = @_;

    $self->{'error'} = '';
    $self->{'error'} = 'Subscription missing' unless $subscription;
    return undef if $self->{'error'};
    
    my $response = $http->delete("https://api.stripe.com/v1/subscriptions/$subscription",  $self->_get_header);
        
    if ($response->{'success'}) {
        return $response->{'content'}->{'id'} eq $subscription;
    }
    $self->{'error'} = 'Cancellation failed';
    return undef;
}

# Change subscripotion to a different price plan
sub update {
    my ($self, $subscription, $plan) = @_;
    
    $self->{'error'} = '';
    $self->{'error'} = 'Subscription missing'      unless $subscription;
    $self->{'error'} = 'Subscription plan missing' unless $plan;
    return undef if $self->{'error'};
    
    my $res = $http->post_form("https://api.stripe.com/v1/subscriptions/$subscription", {}, $self->_get_header);
    my $payload = decode_json($res->{'content'});

    # Don't change to the same plan
    if ($payload->{'items'}->{'data'}[0]->{'price'}->{'id'} eq $plan) {
        $self->{'error'} = 'Cannot change to the same price plan';
        return 0;
    }

    my $vars = {
        'items[0][id]'             => $payload->{'items'}->{'data'}[0]->{'id'},
        'items[0][price]'          => $plan,
        'proration_behavior'       => 'create_prorations',
        'cancel_at_period_end'     => 'false',
    };

    my $response = $http->post_form("https://api.stripe.com/v1/subscriptions/$subscription", $vars, $self->_get_header);
    
    if ($response->{'success'}) {
        return $response->{'content'}->{'id'} eq $subscription;
    }
    $self->{'error'} = 'Update failed';
    return undef;
}

# Update card details
sub new_card {
    my ($self, $customer, $subscription) = @_;
    
    $self->{'error'} = '';
    $self->{'error'} = 'Customer missing'       unless $customer;
    $self->{'error'} = 'Subscription missing'   unless $subscription;
    return undef if $self->{'error'};

    my $session = {
        'success_url'                                   => $self->{'success_url'},
        'cancel_url'                                    => $self->{'cancel_url'},
        'payment_method_types[0]'                       => 'card',
        'mode'                                          => 'setup',
        'customer'                                      => $customer,
        'setup_intent_data[metadata][subscription_id]'  => $subscription,
    };
    
    my $response = $http->post_form($self->{'url'} . 'checkout/sessions', $session, $self->_get_header);
    if ($response->{'success'}) {
        return decode_json $response->{'content'};
    }
    
    $self->{'error'} = 'Failed to obtain card update URL';
    return undef;
}

# Set default card



( run in 0.839 second using v1.01-cache-2.11-cpan-7fcb06a456a )