Skip to content

Commit 8afb5e3

Browse files
Cancel subscription at period end
1 parent fbf57b3 commit 8afb5e3

7 files changed

+72
-74
lines changed

src/Message/CancelSubscriptionRequest.php

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Stripe Cancel Subscription Request.
1010
*
1111
* @see \Omnipay\Stripe\Gateway
12-
* @link https://stripe.com/docs/api/#cancel_subscription
12+
* @link https://stripe.com/docs/api/subscriptions/cancel
1313
*/
1414
class CancelSubscriptionRequest extends AbstractRequest
1515
{
@@ -35,49 +35,20 @@ public function setSubscriptionReference($value)
3535
return $this->setParameter('subscriptionReference', $value);
3636
}
3737

38-
/**
39-
* Set whether or not to cancel the subscription at period end.
40-
*
41-
* @param bool $value
42-
*
43-
* @return CancelSubscriptionRequest provides a fluent interface.
44-
*/
45-
public function setAtPeriodEnd($value)
46-
{
47-
return $this->setParameter('atPeriodEnd', $value);
48-
}
49-
50-
/**
51-
* Get whether or not to cancel the subscription at period end.
52-
*
53-
* @return bool
54-
*/
55-
public function getAtPeriodEnd()
56-
{
57-
return $this->getParameter('atPeriodEnd');
58-
}
59-
6038
public function getData()
6139
{
62-
$this->validate('customerReference', 'subscriptionReference');
63-
64-
$data = array();
65-
66-
// NOTE: Boolean must be passed as string
67-
// Otherwise it will be converted to numeric 0 or 1
68-
// Causing an error with the API
69-
if ($this->getAtPeriodEnd()) {
70-
$data['at_period_end'] = 'true';
71-
}
40+
$this->validate('subscriptionReference');
7241

73-
return $data;
42+
return array();
7443
}
7544

7645
public function getEndpoint()
7746
{
78-
return $this->endpoint
79-
.'/customers/'.$this->getCustomerReference()
80-
.'/subscriptions/'.$this->getSubscriptionReference();
47+
return $this->getCustomerReference() ?
48+
$this->endpoint
49+
.'/customers/'.$this->getCustomerReference()
50+
.'/subscriptions/'.$this->getSubscriptionReference() :
51+
$this->endpoint.'/subscriptions/'.$this->getSubscriptionReference();
8152
}
8253

8354
public function getHttpMethod()

src/Message/FetchSubscriptionRequest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,17 @@ public function setSubscriptionReference($value)
3636

3737
public function getData()
3838
{
39-
$this->validate('customerReference', 'subscriptionReference');
39+
$this->validate('subscriptionReference');
4040

4141
return array();
4242
}
4343

4444
public function getEndpoint()
4545
{
46-
return $this->endpoint.'/customers/'.$this->getCustomerReference()
47-
.'/subscriptions/'.$this->getSubscriptionReference();
46+
return $this->getCustomerReference() ?
47+
$this->endpoint.'/customers/'.$this->getCustomerReference()
48+
.'/subscriptions/'.$this->getSubscriptionReference() :
49+
$this->endpoint.'/subscriptions/'.$this->getSubscriptionReference();
4850
}
4951

5052
public function getHttpMethod()

src/Message/UpdateSubscriptionRequest.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Stripe Update Subscription Request
1111
*
1212
* @see \Omnipay\Stripe\Gateway
13-
* @link https://stripe.com/docs/api#update_subscription
13+
* @link https://stripe.com/docs/api/subscriptions/update
1414
*/
1515
class UpdateSubscriptionRequest extends AbstractRequest
1616
{
@@ -74,9 +74,22 @@ public function setSubscriptionReference($value)
7474
return $this->setParameter('subscriptionReference', $value);
7575
}
7676

77+
/**
78+
* @return bool
79+
*/
80+
public function getCancelAtPeriodEnd()
81+
{
82+
return $this->getParameter('cancel_at_period_end');
83+
}
84+
85+
public function setCancelAtPeriodEnd($value)
86+
{
87+
return $this->setParameter('cancel_at_period_end', $value);
88+
}
89+
7790
public function getData()
7891
{
79-
$this->validate('customerReference', 'subscriptionReference', 'plan');
92+
$this->validate('subscriptionReference', 'plan');
8093

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

102+
if ($this->parameters->has('cancel_at_period_end')) {
103+
$data['cancel_at_period_end'] = $this->getCancelAtPeriodEnd() ? 'true' : 'false';
104+
}
105+
89106
if ($this->getMetadata()) {
90107
$data['metadata'] = $this->getMetadata();
91108
}
@@ -95,7 +112,9 @@ public function getData()
95112

96113
public function getEndpoint()
97114
{
98-
return $this->endpoint.'/customers/'.$this->getCustomerReference()
99-
.'/subscriptions/'.$this->getSubscriptionReference();
115+
return $this->getCustomerReference() ?
116+
$this->endpoint.'/customers/'.$this->getCustomerReference()
117+
.'/subscriptions/'.$this->getSubscriptionReference() :
118+
$this->endpoint.'/subscriptions/'.$this->getSubscriptionReference();
100119
}
101120
}

tests/Message/CancelSubscriptionRequestTest.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,12 @@ class CancelSubscriptionRequestTest extends TestCase
1414
public function setUp()
1515
{
1616
$this->request = new CancelSubscriptionRequest($this->getHttpClient(), $this->getHttpRequest());
17-
$this->request->setCustomerReference('cus_7lfqk3Om3t4xSU');
1817
$this->request->setSubscriptionReference('sub_7mU0FokE8GQZFW');
19-
$this->request->setAtPeriodEnd(true);
2018
}
2119

2220
public function testEndpoint()
2321
{
24-
$this->assertSame('https://api.stripe.com/v1/customers/cus_7lfqk3Om3t4xSU/subscriptions/sub_7mU0FokE8GQZFW', $this->request->getEndpoint());
25-
$this->assertSame(true, $this->request->getAtPeriodEnd());
26-
27-
$data = $this->request->getData();
28-
$this->assertSame('true', $data['at_period_end']);
22+
$this->assertSame('https://api.stripe.com/v1/subscriptions/sub_7mU0FokE8GQZFW', $this->request->getEndpoint());
2923
}
3024

3125
public function testSendSuccess()
@@ -48,6 +42,6 @@ public function testSendError()
4842
$this->assertFalse($response->isRedirect());
4943
$this->assertNull($response->getSubscriptionReference());
5044
$this->assertNull($response->getPlan());
51-
$this->assertSame('Customer cus_7lqqgOm33t4xSU does not have a subscription with ID sub_7mU0DonX8GQZFW', $response->getMessage());
45+
$this->assertSame("No such subscription: 'sub_7mU0FokE8GQZFW'", $response->getMessage());
5246
}
5347
}

tests/Message/UpdateSubscriptionRequestTest.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@ class UpdateSubscriptionRequestTest extends TestCase
1414
public function setUp()
1515
{
1616
$this->request = new UpdateSubscriptionRequest($this->getHttpClient(), $this->getHttpRequest());
17-
$this->request->setCustomerReference('cus_7lqqgOm33t4xSU');
1817
$this->request->setSubscriptionReference('sub_7uNSBwlTzGjYWw');
1918
$this->request->setPlan('basic');
2019
}
2120

2221
public function testEndpoint()
2322
{
24-
$endpoint = 'https://api.stripe.com/v1/customers/cus_7lqqgOm33t4xSU/subscriptions/sub_7uNSBwlTzGjYWw';
23+
$endpoint = 'https://api.stripe.com/v1/subscriptions/sub_7uNSBwlTzGjYWw';
2524
$this->assertSame($endpoint, $this->request->getEndpoint());
2625
}
2726

@@ -50,12 +49,10 @@ public function testSendError()
5049
$this->assertNull($response->getSubscriptionReference());
5150
$this->assertNull($response->getPlan());
5251

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

5654
$message = sprintf(
57-
'Customer %s does not have a subscription with ID %s',
58-
$customerReference,
55+
"No such subscription: '%s'",
5956
$subscriptionReference
6057
);
6158
$this->assertSame($message, $response->getMessage());
Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
1-
HTTP/1.1 404 Not Found
1+
HTTP/1.1 404
22
Server: nginx
3-
Date: Sun, 24 Jan 2016 22:29:41 GMT
3+
Date: Fri, 04 Sep 2020 09:19:40 GMT
44
Content-Type: application/json
5-
Content-Length: 188
6-
Connection: keep-alive
5+
Content-Length: 240
76
Access-Control-Allow-Credentials: true
7+
Access-Control-Allow-Methods: GET, POST, HEAD, OPTIONS, DELETE
8+
Access-Control-Allow-Origin: *
9+
Access-Control-Expose-Headers: Request-Id, Stripe-Manage-Version, X-Stripe-External-Auth-Required, X-Stripe-Privileged-Session-Required
10+
Access-Control-Max-age: 300
811
Cache-Control: no-cache, no-store
12+
Request-Id: req_T3w109w5clA85G
13+
Stripe-Version: 2020-08-27
14+
Strict-Transport-security: max-age=31556926; includeSubDomains; preload
915

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

1116
{
1217
"error": {
13-
"type": "invalid_request_error",
14-
"message": "Customer cus_7lqqgOm33t4xSU does not have a subscription with ID sub_7uNSBwlTzGjYWw",
15-
"param": "subscription"
18+
"code": "resource_missing",
19+
"doc_url": "https://stripe.com/docs/error-codes/resource-missing",
20+
"message": "No such subscription: 'sub_7uNSBwlTzGjYWw'",
21+
"param": "id",
22+
"type": "invalid_request_error"
1623
}
17-
}
24+
}

0 commit comments

Comments
 (0)