Skip to content

Commit

Permalink
Cancel subscription at period end
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineLemaire committed Sep 4, 2020
1 parent fbf57b3 commit f052f84
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 71 deletions.
47 changes: 13 additions & 34 deletions src/Message/CancelSubscriptionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Stripe Cancel Subscription Request.
*
* @see \Omnipay\Stripe\Gateway
* @link https://stripe.com/docs/api/#cancel_subscription
* @link https://stripe.com/docs/api/subscriptions/cancel
*/
class CancelSubscriptionRequest extends AbstractRequest
{
Expand All @@ -35,49 +35,28 @@ public function setSubscriptionReference($value)
return $this->setParameter('subscriptionReference', $value);
}

/**
* Set whether or not to cancel the subscription at period end.
*
* @param bool $value
*
* @return CancelSubscriptionRequest provides a fluent interface.
*/
public function setAtPeriodEnd($value)
{
return $this->setParameter('atPeriodEnd', $value);
}

/**
* Get whether or not to cancel the subscription at period end.
*
* @return bool
*/
public function getAtPeriodEnd()
{
return $this->getParameter('atPeriodEnd');
}

public function getData()
{
$this->validate('customerReference', 'subscriptionReference');
$this->validate('subscriptionReference');

$data = array();
if ($this->parameters->has('invoice_now')) {
$data['invoice_now'] = $this->getParameter('invoice_now') ? 'true' : 'false';
}

// NOTE: Boolean must be passed as string
// Otherwise it will be converted to numeric 0 or 1
// Causing an error with the API
if ($this->getAtPeriodEnd()) {
$data['at_period_end'] = 'true';
if ($this->parameters->has('prorate')) {
$data['prorate'] = $this->getParameter('prorate') ? 'true' : 'false';
}

return $data;
return array();
}

public function getEndpoint()
{
return $this->endpoint
.'/customers/'.$this->getCustomerReference()
.'/subscriptions/'.$this->getSubscriptionReference();
return $this->getCustomerReference() ?
$this->endpoint
.'/customers/'.$this->getCustomerReference()
.'/subscriptions/'.$this->getSubscriptionReference() :
$this->endpoint.'/subscriptions/'.$this->getSubscriptionReference();
}

public function getHttpMethod()
Expand Down
8 changes: 5 additions & 3 deletions src/Message/FetchSubscriptionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,17 @@ public function setSubscriptionReference($value)

public function getData()
{
$this->validate('customerReference', 'subscriptionReference');
$this->validate('subscriptionReference');

return array();
}

public function getEndpoint()
{
return $this->endpoint.'/customers/'.$this->getCustomerReference()
.'/subscriptions/'.$this->getSubscriptionReference();
return $this->getCustomerReference() ?
$this->endpoint.'/customers/'.$this->getCustomerReference()
.'/subscriptions/'.$this->getSubscriptionReference() :
$this->endpoint.'/subscriptions/'.$this->getSubscriptionReference();
}

public function getHttpMethod()
Expand Down
27 changes: 23 additions & 4 deletions src/Message/UpdateSubscriptionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Stripe Update Subscription Request
*
* @see \Omnipay\Stripe\Gateway
* @link https://stripe.com/docs/api#update_subscription
* @link https://stripe.com/docs/api/subscriptions/update
*/
class UpdateSubscriptionRequest extends AbstractRequest
{
Expand Down Expand Up @@ -74,9 +74,22 @@ public function setSubscriptionReference($value)
return $this->setParameter('subscriptionReference', $value);
}

/**
* @return bool
*/
public function getCancelAtPeriodEnd()
{
return $this->getParameter('cancel_at_period_end');
}

public function setCancelAtPeriodEnd($value)
{
return $this->setParameter('cancel_at_period_end', $value);
}

public function getData()
{
$this->validate('customerReference', 'subscriptionReference', 'plan');
$this->validate('subscriptionReference', 'plan');

$data = array(
'plan' => $this->getPlan()
Expand All @@ -86,6 +99,10 @@ public function getData()
$data['tax_percent'] = (float)$this->getParameter('tax_percent');
}

if ($this->parameters->has('cancel_at_period_end')) {
$data['cancel_at_period_end'] = $this->getCancelAtPeriodEnd() ? 'true' : 'false';
}

if ($this->getMetadata()) {
$data['metadata'] = $this->getMetadata();
}
Expand All @@ -95,7 +112,9 @@ public function getData()

public function getEndpoint()
{
return $this->endpoint.'/customers/'.$this->getCustomerReference()
.'/subscriptions/'.$this->getSubscriptionReference();
return $this->getCustomerReference() ?
$this->endpoint.'/customers/'.$this->getCustomerReference()
.'/subscriptions/'.$this->getSubscriptionReference() :
$this->endpoint.'/subscriptions/'.$this->getSubscriptionReference();
}
}
10 changes: 2 additions & 8 deletions tests/Message/CancelSubscriptionRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,12 @@ class CancelSubscriptionRequestTest extends TestCase
public function setUp()
{
$this->request = new CancelSubscriptionRequest($this->getHttpClient(), $this->getHttpRequest());
$this->request->setCustomerReference('cus_7lfqk3Om3t4xSU');
$this->request->setSubscriptionReference('sub_7mU0FokE8GQZFW');
$this->request->setAtPeriodEnd(true);
}

public function testEndpoint()
{
$this->assertSame('https://api.stripe.com/v1/customers/cus_7lfqk3Om3t4xSU/subscriptions/sub_7mU0FokE8GQZFW', $this->request->getEndpoint());
$this->assertSame(true, $this->request->getAtPeriodEnd());

$data = $this->request->getData();
$this->assertSame('true', $data['at_period_end']);
$this->assertSame('https://api.stripe.com/v1/subscriptions/sub_7mU0FokE8GQZFW', $this->request->getEndpoint());
}

public function testSendSuccess()
Expand All @@ -48,6 +42,6 @@ public function testSendError()
$this->assertFalse($response->isRedirect());
$this->assertNull($response->getSubscriptionReference());
$this->assertNull($response->getPlan());
$this->assertSame('Customer cus_7lqqgOm33t4xSU does not have a subscription with ID sub_7mU0DonX8GQZFW', $response->getMessage());
$this->assertSame("No such subscription: 'sub_7mU0FokE8GQZFW'", $response->getMessage());
}
}
7 changes: 2 additions & 5 deletions tests/Message/UpdateSubscriptionRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ class UpdateSubscriptionRequestTest extends TestCase
public function setUp()
{
$this->request = new UpdateSubscriptionRequest($this->getHttpClient(), $this->getHttpRequest());
$this->request->setCustomerReference('cus_7lqqgOm33t4xSU');
$this->request->setSubscriptionReference('sub_7uNSBwlTzGjYWw');
$this->request->setPlan('basic');
}

public function testEndpoint()
{
$endpoint = 'https://api.stripe.com/v1/customers/cus_7lqqgOm33t4xSU/subscriptions/sub_7uNSBwlTzGjYWw';
$endpoint = 'https://api.stripe.com/v1/subscriptions/sub_7uNSBwlTzGjYWw';
$this->assertSame($endpoint, $this->request->getEndpoint());
}

Expand Down Expand Up @@ -50,12 +49,10 @@ public function testSendError()
$this->assertNull($response->getSubscriptionReference());
$this->assertNull($response->getPlan());

$customerReference = $this->request->getCustomerReference();
$subscriptionReference = $this->request->getSubscriptionReference();

$message = sprintf(
'Customer %s does not have a subscription with ID %s',
$customerReference,
"No such subscription: '%s'",
$subscriptionReference
);
$this->assertSame($message, $response->getMessage());
Expand Down
24 changes: 16 additions & 8 deletions tests/Mock/CancelSubscriptionFailure.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
HTTP/1.1 404 Not Found
HTTP/1.1 404
Server: nginx
Date: Sun, 24 Jan 2016 22:29:41 GMT
Date: Fri, 04 Sep 2020 09:19:40 GMT
Content-Type: application/json
Content-Length: 188
Connection: keep-alive
Content-Length: 240
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, POST, HEAD, OPTIONS, DELETE
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: Request-Id, Stripe-Manage-Version, X-Stripe-External-Auth-Required, X-Stripe-Privileged-Session-Required
Access-Control-Max-age: 300
Cache-Control: no-cache, no-store
Request-Id: req_T3w109w5clA85G
Stripe-Version: 2020-08-27
Strict-Transport-security: max-age=31556926; includeSubDomains; preload

{
"error": {
"type": "invalid_request_error",
"message": "Customer cus_7lqqgOm33t4xSU does not have a subscription with ID sub_7mU0DonX8GQZFW",
"param": "subscription"
"code": "resource_missing",
"doc_url": "https://stripe.com/docs/error-codes/resource-missing",
"message": "No such subscription: 'sub_7mU0FokE8GQZFW'",
"param": "id",
"type": "invalid_request_error"
}
}
}
25 changes: 16 additions & 9 deletions tests/Mock/UpdateSubscriptionFailure.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
HTTP/1.1 400 Bad Request
HTTP/1.1 404
Server: nginx
Date: Sun, 14 Feb 2016 23:05:08 GMT
Content-Type: application/json
Content-Length: 188
Connection: keep-alive
Date: Fri, 04 Sep 2020 09:16:28 GMT
Content-type: application/json
Content-length: 240
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, POST, HEAD, OPTIONS, DELETE
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: Request-Id, Stripe-Manage-Version, X-Stripe-External-Auth-Required, X-Stripe-Privileged-Session-Required
Access-Control-Max-Age: 300
Cache-Control: no-cache, no-store
Request-Id: req_aXzizOODI7qOZx
Stripe-Version: 2020-08-27
Strict-Transport-Security: max-age=31556926; includeSubDomains; preload

{
"error": {
"type": "invalid_request_error",
"message": "Customer cus_7lqqgOm33t4xSU does not have a subscription with ID sub_7uNSBwlTzGjYWw",
"param": "subscription"
"code": "resource_missing",
"doc_url": "https://stripe.com/docs/error-codes/resource-missing",
"message": "No such subscription: 'sub_7uNSBwlTzGjYWw'",
"param": "id",
"type": "invalid_request_error"
}
}
}

0 comments on commit f052f84

Please sign in to comment.