Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement void method for order API #3

Merged
merged 2 commits into from
Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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);
}
}
35 changes: 35 additions & 0 deletions src/Message/Request/CancelOrderRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Omnipay\Mollie\Message\Request;

use Omnipay\Mollie\Message\Response\CancelOrderResponse;

/**
* Cancel an order with the Mollie API.
*
* @see https://docs.mollie.com/reference/v2/orders-api/cancel-order
* @method CancelOrderResponse send()
*/
final class CancelOrderRequest extends AbstractMollieRequest
{
/**
* @inheritdoc
*/
public function getData()
{
$this->validate('apiKey', 'transactionReference');

return [];
}

/**
* @inheritdoc
*/
public function sendData($data)
{
return $this->response = new CancelOrderResponse(
$this,
$this->sendRequest(self::DELETE, '/orders/'.$this->getTransactionReference(), $data)
);
}
}
21 changes: 21 additions & 0 deletions src/Message/Response/CancelOrderResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Omnipay\Mollie\Message\Response;

/**
* @see https://docs.mollie.com/reference/v2/orders-api/cancel-order
*/
final class CancelOrderResponse extends FetchOrderResponse
{
/**
* @return bool
*/
public function isSuccessful()
{
if (!isset($this->data['status'])) {
return false;
}

return 'canceled' === $this->data['status'];
}
}
6 changes: 6 additions & 0 deletions tests/GatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -212,4 +213,9 @@ public function testCreateCustomerMandate()

$this->assertInstanceOf(CreateCustomerMandateRequest::class, $request);
}

public function testVoid()
{
$this->assertInstanceOf(CancelOrderRequest::class, $this->gateway->void());
}
}
90 changes: 90 additions & 0 deletions tests/Message/CancelOrderRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace Omnipay\Mollie\Test\Message;

use Omnipay\Common\Http\ClientInterface;
use Omnipay\Mollie\Message\Request\CancelOrderRequest;
use Omnipay\Mollie\Message\Response\CancelOrderResponse;
use Omnipay\Tests\TestCase;
use Psr\Http\Message\ResponseInterface;

final class CancelOrderRequestTest extends TestCase
{
/**
* @var ClientInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $httpClient;

/**
* @var CancelOrderRequest
*/
private $request;

public function setUp()
{
$this->httpClient = $this->createMock(ClientInterface::class);
$this->request = new CancelOrderRequest($this->httpClient, $this->getHttpRequest());
}

public function insufficientDataProvider()
{
return [
[['apiKey' => 'mykey']],
[['transactionReference' => 'myref']],
];
}

public function responseDataProvider()
{
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, $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());
}
}