Skip to content

Commit 6eab75f

Browse files
authored
Merge pull request #55 from WellingGuzman/dev
Ability to cancel subscriptions at the end of its period
2 parents f2c5b48 + eaa9949 commit 6eab75f

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/Message/CancelSubscriptionRequest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,50 @@ public function getSubscriptionReference()
2626
/**
2727
* Set the set subscription reference.
2828
*
29+
* @param string $value
30+
*
2931
* @return CancelSubscriptionRequest provides a fluent interface.
3032
*/
3133
public function setSubscriptionReference($value)
3234
{
3335
return $this->setParameter('subscriptionReference', $value);
3436
}
3537

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+
3660
public function getData()
3761
{
3862
$this->validate('customerReference', 'subscriptionReference');
3963

4064
$data = array();
4165

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+
}
72+
4273
return $data;
4374
}
4475

tests/Message/CancelSubscriptionRequestTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,16 @@ public function setUp()
1111
$this->request = new CancelSubscriptionRequest($this->getHttpClient(), $this->getHttpRequest());
1212
$this->request->setCustomerReference('cus_7lfqk3Om3t4xSU');
1313
$this->request->setSubscriptionReference('sub_7mU0FokE8GQZFW');
14+
$this->request->setAtPeriodEnd(true);
1415
}
1516

1617
public function testEndpoint()
1718
{
1819
$this->assertSame('https://api.stripe.com/v1/customers/cus_7lfqk3Om3t4xSU/subscriptions/sub_7mU0FokE8GQZFW', $this->request->getEndpoint());
20+
$this->assertSame(true, $this->request->getAtPeriodEnd());
21+
22+
$data = $this->request->getData();
23+
$this->assertSame('true', $data['at_period_end']);
1924
}
2025

2126
public function testSendSuccess()

0 commit comments

Comments
 (0)