Tim
Tim

Reputation: 5933

How to: get the end of a subscription after it has been canceled with Paymill

I have set up a monthly subscription with no date of validity. If someone cancels his subscription which calls the following function behind the scenes

$subscription->setId('sub_dea86e5c65b2087202e3');
             ->setRemove(false);

I get no information about the current period that was booked. If someone subscribes on Sept 1st and cancels on Sept 2nd I have no way to find out, if his subscription is still valid.

Upvotes: 1

Views: 50

Answers (1)

Tim
Tim

Reputation: 5933

Since there is no built-in way to query the Paymill-API I've done the following:

$subscription = $this->getClientSubscriptions(); //Get subscriptions for the client

if ($subscription)
{
    if ( ! $subscription['is_canceled'])
    {
        $this->client->end_of_period = date('Y-m-d H:i:s', $subscription['next_capture_at']);
        /* Set end of period within the database if
           subscription is not canceled */
    } elseif ($this->client->end_of_period < date('Y-m-d H:i:s'))
    {
        /* If the subscription has been canceled we can
           compare the end of period to the current date
           and set the subscription to false since it expired */
        $this->client->end_of_period = null;
        $subscription = false;
    }

    $this->client->save();
    $this->cache->put($subscription_cache, $subscription, 1440);
    /* Save and put it to cache to prevent excessive calls to the paymill-api */
}

If there are any question for this solution I'll be happy to answer them.

Upvotes: 1

Related Questions