From 232360adef20199c37d5ce0290cb3e20da132266 Mon Sep 17 00:00:00 2001 From: Mike Feijs Date: Thu, 18 Apr 2019 18:42:19 +0200 Subject: [PATCH] Implement void method for order API --- README.md | 6 ++ src/Gateway.php | 11 ++- src/Message/Request/CancelOrderRequest.php | 35 ++++++++ src/Message/Response/CancelOrderResponse.php | 17 ++++ tests/GatewayTest.php | 6 ++ tests/Message/CancelOrderRequestTest.php | 92 ++++++++++++++++++++ 6 files changed, 166 insertions(+), 1 deletion(-) create mode 100755 src/Message/Request/CancelOrderRequest.php create mode 100644 src/Message/Response/CancelOrderResponse.php create mode 100644 tests/Message/CancelOrderRequestTest.php diff --git a/README.md b/README.md index 0fa3f29..24489de 100644 --- a/README.md +++ b/README.md @@ -177,6 +177,12 @@ $response = $gateway->createShipment( )->send(); ``` +4. As long as the order is `created`, `authorized` or `shipping`, it may be cancelled (voided) + +```php +$response = $gateway->void(["transactionReference" => "ord_xxx"])->send(); +``` + ## Support If you are having general issues with Omnipay, we suggest posting on diff --git a/src/Gateway.php b/src/Gateway.php index e9756db..1c39f49 100644 --- a/src/Gateway.php +++ b/src/Gateway.php @@ -4,6 +4,7 @@ use Omnipay\Common\AbstractGateway; use Omnipay\Common\Message\RequestInterface; +use Omnipay\Mollie\Message\Request\CancelOrderRequest; use Omnipay\Mollie\Message\Request\CompleteOrderRequest; use Omnipay\Mollie\Message\Request\CompletePurchaseRequest; use Omnipay\Mollie\Message\Request\CreateCustomerMandateRequest; @@ -31,7 +32,6 @@ * @method RequestInterface authorize(array $options = array()) * @method RequestInterface completeAuthorize(array $options = array()) * @method RequestInterface capture(array $options = array()) - * @method RequestInterface void(array $options = array()) * @method RequestInterface createCard(array $options = array()) * @method RequestInterface updateCard(array $options = array()) * @method RequestInterface deleteCard(array $options = array()) @@ -259,4 +259,13 @@ public function revokeCustomerMandate(array $parameters = []) { return $this->createRequest(RevokeCustomerMandateRequest::class, $parameters); } + + /** + * @param array $parameters + * @return CancelOrderRequest + */ + public function void(array $parameters = []) + { + return $this->createRequest(CancelOrderRequest::class, $parameters); + } } diff --git a/src/Message/Request/CancelOrderRequest.php b/src/Message/Request/CancelOrderRequest.php new file mode 100755 index 0000000..aea7915 --- /dev/null +++ b/src/Message/Request/CancelOrderRequest.php @@ -0,0 +1,35 @@ +validate('apiKey', 'transactionReference'); + + return []; + } + + /** + * @inheritdoc + */ + public function sendData($data) + { + return $this->response = new CancelOrderResponse( + $this, + $this->sendRequest(self::DELETE, '/orders/'.$this->getTransactionReference(), $data) + ); + } +} diff --git a/src/Message/Response/CancelOrderResponse.php b/src/Message/Response/CancelOrderResponse.php new file mode 100644 index 0000000..ee055ac --- /dev/null +++ b/src/Message/Response/CancelOrderResponse.php @@ -0,0 +1,17 @@ +data['status'] ?? ''; + } +} diff --git a/tests/GatewayTest.php b/tests/GatewayTest.php index cd50484..cbd4e41 100644 --- a/tests/GatewayTest.php +++ b/tests/GatewayTest.php @@ -4,6 +4,7 @@ use Omnipay\Common\Exception\InvalidRequestException; use Omnipay\Mollie\Gateway; +use Omnipay\Mollie\Message\Request\CancelOrderRequest; use Omnipay\Mollie\Message\Request\CompletePurchaseRequest; use Omnipay\Mollie\Message\Request\CreateCustomerMandateRequest; use Omnipay\Mollie\Message\Request\CreateCustomerRequest; @@ -212,4 +213,9 @@ public function testCreateCustomerMandate() $this->assertInstanceOf(CreateCustomerMandateRequest::class, $request); } + + public function testVoid() + { + $this->assertInstanceOf(CancelOrderRequest::class, $this->gateway->void()); + } } diff --git a/tests/Message/CancelOrderRequestTest.php b/tests/Message/CancelOrderRequestTest.php new file mode 100644 index 0000000..cbf98a6 --- /dev/null +++ b/tests/Message/CancelOrderRequestTest.php @@ -0,0 +1,92 @@ +httpClient = $this->createMock(ClientInterface::class); + $this->request = new CancelOrderRequest($this->httpClient, $this->getHttpRequest()); + } + + public function insufficientDataProvider(): array + { + return [ + [['apiKey' => 'mykey']], + [['transactionReference' => 'myref']], + ]; + } + + public function responseDataProvider(): array + { + return [ + [['id' => 'ord_kEn1PlbGa'], false], + [['status' => 'paid', 'id' => 'ord_kEn1PlbGa'], false], + [['status' => 'canceled', 'id' => 'ord_kEn1PlbGa'], true], + ]; + } + + /** + * @dataProvider insufficientDataProvider + * + * @expectedException \Omnipay\Common\Exception\InvalidRequestException + * + * @param array $input + */ + public function testGetDataWillValidateRequiredData(array $input) + { + $this->request->initialize($input); + $this->request->getData(); + } + + public function testGetDataWillReturnEmptyArray() + { + $this->request->initialize(['apiKey' => 'mykey', 'transactionReference' => 'myref']); + self::assertEquals([], $this->request->getData()); + } + + /** + * @dataProvider responseDataProvider + */ + public function testSendData(array $responseData, bool $success) + { + $response = $this->createMock(ResponseInterface::class); + $response->expects(self::once()) + ->method('getBody') + ->willReturn(\json_encode($responseData)); + + $this->httpClient->expects(self::once()) + ->method('request') + ->with( + 'DELETE', + 'https://api.mollie.com/v2/orders/ord_kEn1PlbGa', + ['Authorization' => 'Bearer mykey'] + )->willReturn($response); + + + $this->request->initialize(['apiKey' => 'mykey', 'transactionReference' => 'ord_kEn1PlbGa']); + $voidResponse = $this->request->sendData([]); + + $this->assertInstanceOf(CancelOrderResponse::class, $voidResponse); + $this->assertEquals($success, $voidResponse->isSuccessful()); + $this->assertSame('ord_kEn1PlbGa', $voidResponse->getTransactionReference()); + } + +}