diff --git a/src/Endpoints/PaymentRefundEndpoint.php b/src/Endpoints/PaymentRefundEndpoint.php index 6110cdb57..844330b7e 100644 --- a/src/Endpoints/PaymentRefundEndpoint.php +++ b/src/Endpoints/PaymentRefundEndpoint.php @@ -154,4 +154,47 @@ public function createForId(string $paymentId, array $data, array $filters = []) return parent::rest_create($data, $filters); } + + /** + * @param \Mollie\Api\Resources\Payment $payment + * @param string $refundId + * @param array $parameters + * @return null + * + * @throws \Mollie\Api\Exceptions\ApiException + */ + public function cancelForPayment(Payment $payment, string $refundId, array $parameters = []) + { + $this->parentId = $payment->id; + + return $this->cancelForId($payment->id, $refundId, $parameters); + } + + /** + * @param string $paymentId + * @param string $refundId + * @param array $parameters + * @return null + * + * @throws \Mollie\Api\Exceptions\ApiException + */ + public function cancelForId(string $paymentId, string $refundId, array $parameters = []) + { + $this->parentId = $paymentId; + + $body = null; + if (count($parameters) > 0) { + $body = json_encode($parameters); + } + + $this->client->performHttpCall( + EndpointAbstract::REST_DELETE, + $this->getResourcePath() . '/' . $refundId, + $body + ); + + $this->getResourcePath(); + + return null; + } } diff --git a/src/Resources/Refund.php b/src/Resources/Refund.php index 651b13f13..88f3e4ae2 100644 --- a/src/Resources/Refund.php +++ b/src/Resources/Refund.php @@ -2,11 +2,12 @@ namespace Mollie\Api\Resources; -use Mollie\Api\MollieApiClient; use Mollie\Api\Types\RefundStatus; class Refund extends BaseResource { + use HasPresetOptions; + /** * Id of the payment method. * @@ -14,6 +15,13 @@ class Refund extends BaseResource */ public $id; + /** + * Mode of the refund, either "live" or "test". + * + * @var string + */ + public $mode; + /** * The $amount that was refunded. * @@ -167,11 +175,10 @@ public function isCanceled() */ public function cancel() { - $this->client->performHttpCallToFullUrl( - MollieApiClient::HTTP_DELETE, - $this->_links->self->href + return $this->client->paymentRefunds->cancelForId( + $this->paymentId, + $this->id, + $this->getPresetOptions() ); - - return null; } } diff --git a/tests/Mollie/API/Endpoints/PaymentRefundEndpointTest.php b/tests/Mollie/API/Endpoints/PaymentRefundEndpointTest.php index 34bdd4df2..e3b2eeddb 100644 --- a/tests/Mollie/API/Endpoints/PaymentRefundEndpointTest.php +++ b/tests/Mollie/API/Endpoints/PaymentRefundEndpointTest.php @@ -516,6 +516,27 @@ public function testListRefundsOnPaymentResource() $this->assertEquals($paymentLink, $refund->_links->payment); } + public function testCancelRefundForPayment() + { + $this->mockApiCall( + new Request( + "DELETE", + "/v2/payments/tr_44aKxzEbr8/refunds/re_4qqhO89gsT", + [], + '{"testmode":true}' + ), + new Response(204) + ); + + $response = $this->apiClient->paymentRefunds->cancelForPayment( + $this->getPayment(), + "re_4qqhO89gsT", + [ 'testmode' => true ] + ); + + $this->assertNull($response); + } + /** * @return Payment */ diff --git a/tests/Mollie/API/Resources/RefundTest.php b/tests/Mollie/API/Resources/RefundTest.php index 7ea14163b..b8cdc706c 100644 --- a/tests/Mollie/API/Resources/RefundTest.php +++ b/tests/Mollie/API/Resources/RefundTest.php @@ -2,12 +2,95 @@ namespace Tests\Mollie\Api\Resources; +use Mollie\Api\Endpoints\PaymentRefundEndpoint; use Mollie\Api\MollieApiClient; use Mollie\Api\Resources\Refund; use Mollie\Api\Types\RefundStatus; class RefundTest extends \PHPUnit\Framework\TestCase { + public function testCancelRefundUsingOauthTestmode() + { + $endpoint = $this->createMock(PaymentRefundEndpoint::class); + $endpoint->expects($this->once()) + ->method('cancelForId') + ->with( + $this->equalTo('tr_abc'), // paymentId + $this->equalTo('re_123'), // refundId + $this->equalTo(['testmode' => true]) + ); + + $apiClient = $this->createMock(MollieApiClient::class); + $apiClient->paymentRefunds = $endpoint; + $apiClient->expects($this->once()) + ->method('usesOAuth') + ->willReturn(true); + + $refund = new Refund($apiClient); + $refund->id = 're_123'; + $refund->paymentId = 'tr_abc'; + $refund->mode = 'test'; + + $response = $refund->cancel(); + + $this->assertNull($response); + } + + public function testCancelRefundUsingOauthLivemode() + { + $endpoint = $this->createMock(PaymentRefundEndpoint::class); + $endpoint->expects($this->once()) + ->method('cancelForId') + ->with( + $this->equalTo('tr_abc'), // paymentId + $this->equalTo('re_123'), // refundId + $this->equalTo(['testmode' => false]) + ); + + $apiClient = $this->createMock(MollieApiClient::class); + $apiClient->paymentRefunds = $endpoint; + $apiClient->expects($this->once()) + ->method('usesOAuth') + ->willReturn(true); + + $refund = new Refund($apiClient); + $refund->id = 're_123'; + $refund->paymentId = 'tr_abc'; + $refund->mode = 'live'; + + $response = $refund->cancel(); + + $this->assertNull($response); + } + + public function testCancelRefundWithoutOauth() + { + $endpoint = $this->createMock(PaymentRefundEndpoint::class); + $endpoint->expects($this->once()) + ->method('cancelForId') + ->with( + $this->equalTo('tr_abc'), // paymentId + $this->equalTo('re_123'), // refundId + $this->equalTo([]) + ); + + $apiClient = $this->createMock(MollieApiClient::class); + $apiClient->paymentRefunds = $endpoint; + $apiClient->expects($this->once()) + ->method('usesOAuth') + ->willReturn(false); + + $refund = new Refund($apiClient); + $refund->id = 're_123'; + $refund->paymentId = 'tr_abc'; + $refund->mode = 'test'; + + $response = $refund->cancel(); + + $this->assertNull($response); + } + + /** * @param string $status * @param string $function