public function cancelSubscription($subscriptionId, $reason = 'Canceling the subscription via the API. Requested by customer.')
{
$rObject = new stdClass();
$rObject->subscription = new stdClass();
$rObject->subscription->cancellation_message = $reason;
$rObject->subscription->cancel_at_end_of_period = true;
$r = $this->sendRequest("/subscriptions/{$subscriptionId}.json", 'PUT', json_encode($rObject));
if ($r->getResponseCode() == 200) {
return true;
} else {
if ($r->getResponseCode() == 422) {
$response = json_decode($r->getBody()->toString(), true);
throw new Exception($response['errors'][0]);
}
throw new Exception("Cannot cancel subscription. Response code: " . $r->getResponseCode() . ". Response body: " . $r->getBody()->toString());
}
}