Skip to content

Commit

Permalink
Merge pull request #747 from sandervanhooft/add_testmode_support_to_r…
Browse files Browse the repository at this point in the history
…efunds

Add testmode support to refunds
  • Loading branch information
sandervanhooft authored Oct 30, 2024
2 parents 0a26175 + 781a0f5 commit e81713f
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 6 deletions.
43 changes: 43 additions & 0 deletions src/Endpoints/PaymentRefundEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
19 changes: 13 additions & 6 deletions src/Resources/Refund.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,26 @@

namespace Mollie\Api\Resources;

use Mollie\Api\MollieApiClient;
use Mollie\Api\Types\RefundStatus;

class Refund extends BaseResource
{
use HasPresetOptions;

/**
* Id of the payment method.
*
* @var string
*/
public $id;

/**
* Mode of the refund, either "live" or "test".
*
* @var string
*/
public $mode;

/**
* The $amount that was refunded.
*
Expand Down Expand Up @@ -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;
}
}
21 changes: 21 additions & 0 deletions tests/Mollie/API/Endpoints/PaymentRefundEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
83 changes: 83 additions & 0 deletions tests/Mollie/API/Resources/RefundTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e81713f

Please sign in to comment.