Skip to content

Commit

Permalink
Merge pull request #3 from MyOnlineStore/void
Browse files Browse the repository at this point in the history
Implement void method for order API
  • Loading branch information
Frank Verhoeven authored Aug 22, 2019
2 parents 5776f3a + b0de21b commit 3bd40df
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 1 deletion.
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());
}
}

0 comments on commit 3bd40df

Please sign in to comment.