diff --git a/src/Message/CancelSubscriptionRequest.php b/src/Message/CancelSubscriptionRequest.php index 27ef6e2f..fdf5000b 100644 --- a/src/Message/CancelSubscriptionRequest.php +++ b/src/Message/CancelSubscriptionRequest.php @@ -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 { @@ -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() diff --git a/src/Message/FetchSubscriptionRequest.php b/src/Message/FetchSubscriptionRequest.php index dc1b1f16..bc369f1c 100644 --- a/src/Message/FetchSubscriptionRequest.php +++ b/src/Message/FetchSubscriptionRequest.php @@ -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() diff --git a/src/Message/UpdateSubscriptionRequest.php b/src/Message/UpdateSubscriptionRequest.php index 25b98503..a41bad6f 100644 --- a/src/Message/UpdateSubscriptionRequest.php +++ b/src/Message/UpdateSubscriptionRequest.php @@ -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 { @@ -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() @@ -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(); } @@ -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(); } } diff --git a/tests/Message/CancelSubscriptionRequestTest.php b/tests/Message/CancelSubscriptionRequestTest.php index 5924a8ba..8bee8591 100644 --- a/tests/Message/CancelSubscriptionRequestTest.php +++ b/tests/Message/CancelSubscriptionRequestTest.php @@ -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() @@ -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()); } } diff --git a/tests/Message/UpdateSubscriptionRequestTest.php b/tests/Message/UpdateSubscriptionRequestTest.php index dc8bc3a2..ca1f8927 100644 --- a/tests/Message/UpdateSubscriptionRequestTest.php +++ b/tests/Message/UpdateSubscriptionRequestTest.php @@ -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()); } @@ -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()); diff --git a/tests/Mock/CancelSubscriptionFailure.txt b/tests/Mock/CancelSubscriptionFailure.txt index 52a72675..f40330ed 100644 --- a/tests/Mock/CancelSubscriptionFailure.txt +++ b/tests/Mock/CancelSubscriptionFailure.txt @@ -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" } -} \ No newline at end of file +} diff --git a/tests/Mock/UpdateSubscriptionFailure.txt b/tests/Mock/UpdateSubscriptionFailure.txt index 65dd4a43..2b5c4272 100644 --- a/tests/Mock/UpdateSubscriptionFailure.txt +++ b/tests/Mock/UpdateSubscriptionFailure.txt @@ -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" } -} \ No newline at end of file +}