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

Implementation refunds #434

Merged
merged 7 commits into from
Aug 25, 2023
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
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<testsuite name="Unit Tests">
<directory>./tests/MercadoPago</directory>
<exclude>./tests/MercadoPago/Client/Payment/PaymentClientITTest.php</exclude>
<exclude>./tests/MercadoPago/Client/Payment/PaymentRefundClientITTest.php</exclude>
<exclude>./tests/MercadoPago/Client/CardToken/CardTokenClientITTest.php</exclude>
<exclude>./tests/MercadoPago/Client/PaymentMethod/PaymentMethodClientITTest.php</exclude>
<exclude>./tests/MercadoPago/Client/Preference/PreferenceClientITTest.php</exclude>
Expand Down
109 changes: 109 additions & 0 deletions src/MercadoPago/Client/Payment/PaymentRefundClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace MercadoPago\Client\Payment;

use MercadoPago\Client\MercadoPagoClient;
use MercadoPago\Core\MPRequestOptions;
use MercadoPago\Exceptions\MPApiException;
use MercadoPago\MercadoPagoConfig;
use MercadoPago\Net\HttpMethod;
use MercadoPago\Resources\PaymentRefund;
use MercadoPago\Resources\PaymentRefundResult;
use MercadoPago\Serialization\Serializer;

/** Client responsible for performing payment refunds actions. */
class PaymentRefundClient extends MercadoPagoClient
{
private static $URL_WITH_PAYMENT_ID = "/v1/payments/%s/refunds";

private static $URL_WITH_REFUND_ID = "/v1/payments/%s/refunds/%s";

/** Default constructor. Uses the default http client used by the SDK. */
public function __construct()
{
parent::__construct(MercadoPagoConfig::getHttpClient());
}

/**
* Method responsible for refunding a payment.
* @param int $payment_id payment id.
* @param float $amount refund amount.
* @param mixed $request_options request options to be sent.
* @return \MercadoPago\Resources\PaymentRefund payment refunded.
* @see https://www.mercadopago.com.br/developers/en/reference/chargebacks/_payments_id_refunds/post
*/
public function refund(int $payment_id, float $amount, ?MPRequestOptions $request_options = null): PaymentRefund
{
try {
$payload = new PaymentRefundCreateRequest();
$payload -> amount = $amount;
$response = parent::send(sprintf(self::$URL_WITH_PAYMENT_ID, strval($payment_id)), HttpMethod::POST, json_encode($payload), null, $request_options);
$result = Serializer::deserializeFromJson(PaymentRefund::class, $response->getContent());
$result->setResponse($response);
return $result;
} catch (MPApiException | \Exception $e) {
throw $e;
}
}

/**
* Method responsible for refunding a payment.
* @param int $payment_id payment id.
* @param float $amount refund amount.
* @param mixed $request_options request options to be sent.
* @return \MercadoPago\Resources\PaymentRefund payment refunded.
* @see https://www.mercadopago.com.br/developers/en/reference/chargebacks/_payments_id_refunds/post
*/
public function refundTotal(int $payment_id, ?MPRequestOptions $request_options = null): PaymentRefund
{
try {
$response = parent::send(sprintf(self::$URL_WITH_PAYMENT_ID, strval($payment_id)), HttpMethod::POST, null, null, $request_options);
$result = Serializer::deserializeFromJson(PaymentRefund::class, $response->getContent());
$result->setResponse($response);
return $result;
} catch (MPApiException | \Exception $e) {
throw $e;
}
}

/**
* Method responsible for getting payment.
* @param int $payment_id payment id.
* @param int $refund_id refund id.
* @param mixed $request_options request options to be sent.
* @return \MercadoPago\Resources\PaymentRefund refund found.
* @see https://www.mercadopago.com.br/developers/en/reference/chargebacks/_payments_id_refunds_refund_id/get
*/

public function get(int $payment_id, int $refund_id, ?MPRequestOptions $request_options = null): PaymentRefund
{
try {
$response = parent::send(sprintf(self::$URL_WITH_REFUND_ID, strval($payment_id), strval($refund_id)), HttpMethod::GET, null, null, $request_options);
$result = Serializer::deserializeFromJson(PaymentRefund::class, $response->getContent());
$result->setResponse($response);
return $result;
} catch (MPApiException | \Exception $e) {
throw $e;
}
}

/**
* Method responsible for getting payment.
* @param int $payment_id payment id.
* @param mixed $request_options request options to be sent.
* @return \MercadoPago\Resources\PaymentRefundResult refund found.
* @see https://www.mercadopago.com.br/developers/en/reference/chargebacks/_payments_id_refunds/get
*/
public function list(int $payment_id, ?MPRequestOptions $request_options = null): PaymentRefundResult
{
try {
$response = parent::send(sprintf(self::$URL_WITH_PAYMENT_ID, strval($payment_id)), HttpMethod::GET, null, null, $request_options);
$result = new PaymentRefundResult();
$result->data = $response->getContent();
$result->setResponse($response);
return $result;
} catch (MPApiException | \Exception $e) {
throw $e;
}
}
}
10 changes: 10 additions & 0 deletions src/MercadoPago/Client/Payment/PaymentRefundCreateRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace MercadoPago\Client\Payment;

/** PaymentRefundCreateRequest class. */
class PaymentRefundCreateRequest
{
/** Amount to be refunded. */
public float $amount;
}
16 changes: 16 additions & 0 deletions src/MercadoPago/Resources/Common/Source.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace MercadoPago\Resources\Common;

/** Source class. */
class Source
{
/** Source id. */
public ?string $id;

/** Source name. */
public ?string $name;

/** Source type. */
public ?string $type;
}
55 changes: 55 additions & 0 deletions src/MercadoPago/Resources/PaymentRefund.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace MercadoPago\Resources;

use MercadoPago\Net\MPResource;
use MercadoPago\Serialization\Mapper;

/** Payment Refund class. */
class PaymentRefund extends MPResource
{
/** Class mapper. */
use Mapper;

/** Refund id. */
public ?int $id;

/** ID of the refunded payment. */
public ?int $payment_id;

/** Amount refunded. */
public ?float $amount;

/** Adjustment amount. */
public ?float $adjustment_amount;

/** Refund status. */
public ?string $status;

/** Refund mode. */
public ?string $refund_mode;

/** Date of creation. */
public ?string $date_created;

/** Refund reason. */
public ?string $reason;

/** Unique sequence number. */
public ?string $unique_sequence_number;

/** Source of the refund. */
public object|array|null $source;

public $map = [
"source" => "MercadoPago\Resources\Common\Source"
];

/**
* Method responsible for getting map of entities.
*/
public function getMap(): array
{
return $this->map;
}
}
11 changes: 11 additions & 0 deletions src/MercadoPago/Resources/PaymentRefundResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace MercadoPago\Resources;

use MercadoPago\Net\MPResource;

/** PaymentRefundResult class. */
class PaymentRefundResult extends MPResource
{
public array $data;
}
22 changes: 22 additions & 0 deletions tests/MercadoPago/Client/Base/BaseClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace MercadoPago\Client\Base;

use PHPUnit\Framework\TestCase;

/**
* Base Client.
*/
abstract class BaseClient extends TestCase
{
protected function mockHttpRequest(string $filepath, int $status_code): \PHPUnit\Framework\MockObject\MockObject|\MercadoPago\Net\HttpRequest
{
/** @var \PHPUnit\Framework\MockObject\MockObject|\MercadoPago\Net\HttpRequest $mock_http_request */
$mock_http_request = $this->getMockBuilder(\MercadoPago\Net\HttpRequest::class)->getMock();

$response_json = file_get_contents(__DIR__ . $filepath);
$mock_http_request->method('execute')->willReturn($response_json);
$mock_http_request->method('getInfo')->willReturn($status_code);
return $mock_http_request;
}
}
51 changes: 20 additions & 31 deletions tests/MercadoPago/Client/Payment/PaymentClientUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@

use MercadoPago\MercadoPagoConfig;
use MercadoPago\Net\MPDefaultHttpClient;
use PHPUnit\Framework\TestCase;
use MercadoPago\Client\Base\BaseClient;

/**
* PaymentClient unit tests.
* Payment Client unit tests.
*/
final class PaymentClientUnitTest extends TestCase
final class PaymentClientUnitTest extends BaseClient
{
public function testCreateSuccess(): void
{
$filepath = '../../../Resources/Mocks/Response/Payment/payment_base.json';
$mockHttpRequest = $this->mockHttpRequest($filepath, 201);
$mock_http_request = $this->mockHttpRequest($filepath, 201);

$httpClient = new MPDefaultHttpClient($mockHttpRequest);
MercadoPagoConfig::setHttpClient($httpClient);
$http_client = new MPDefaultHttpClient($mock_http_request);
MercadoPagoConfig::setHttpClient($http_client);

$client = new PaymentClient();
$payment = $client->create($this->createRequest());
Expand All @@ -34,10 +34,10 @@ public function testCreateSuccess(): void
public function testGetSuccess(): void
{
$filepath = '../../../Resources/Mocks/Response/Payment/payment_base.json';
$mockHttpRequest = $this->mockHttpRequest($filepath, 200);
$mock_http_request = $this->mockHttpRequest($filepath, 200);

$httpClient = new MPDefaultHttpClient($mockHttpRequest);
MercadoPagoConfig::setHttpClient($httpClient);
$http_client = new MPDefaultHttpClient($mock_http_request);
MercadoPagoConfig::setHttpClient($http_client);

$client = new PaymentClient();
$payment_id = 17014025134;
Expand All @@ -49,10 +49,10 @@ public function testGetSuccess(): void
public function testCancelSuccess(): void
{
$filepath = '../../../Resources/Mocks/Response/Payment/payment_cancelled.json';
$mockHttpRequest = $this->mockHttpRequest($filepath, 200);
$mock_http_request = $this->mockHttpRequest($filepath, 200);

$httpClient = new MPDefaultHttpClient($mockHttpRequest);
MercadoPagoConfig::setHttpClient($httpClient);
$http_client = new MPDefaultHttpClient($mock_http_request);
MercadoPagoConfig::setHttpClient($http_client);

$client = new PaymentClient();
$payment_id = 17014025134;
Expand All @@ -65,10 +65,10 @@ public function testCancelSuccess(): void
public function testCaptureSuccess(): void
{
$filepath = '../../../Resources/Mocks/Response/Payment/payment_captured.json';
$mockHttpRequest = $this->mockHttpRequest($filepath, 200);
$mock_http_request = $this->mockHttpRequest($filepath, 200);

$httpClient = new MPDefaultHttpClient($mockHttpRequest);
MercadoPagoConfig::setHttpClient($httpClient);
$http_client = new MPDefaultHttpClient($mock_http_request);
MercadoPagoConfig::setHttpClient($http_client);

$client = new PaymentClient();
$payment_id = 17014025134;
Expand All @@ -81,18 +81,18 @@ public function testCaptureSuccess(): void
public function testSearchSuccess(): void
{
$filepath = '../../../Resources/Mocks/Response/Payment/payment_search.json';
$mockHttpRequest = $this->mockHttpRequest($filepath, 200);
$mock_http_request = $this->mockHttpRequest($filepath, 200);

$httpClient = new MPDefaultHttpClient($mockHttpRequest);
MercadoPagoConfig::setHttpClient($httpClient);
$http_client = new MPDefaultHttpClient($mock_http_request);
MercadoPagoConfig::setHttpClient($http_client);

$client = new PaymentClient();
$search_request = new \MercadoPago\Net\MPSearchRequest(5, 0, []);
$search_result = $client->search($search_request);
$this->assertEquals(200, $search_result->getResponse()->getStatusCode());
$this->assertEquals(5, $search_result->paging["limit"]);
$this->assertNotNull(0, $search_result->paging["offset"]);
$this->assertNotNull(102, $search_result->paging["total"]);
$this->assertEquals(0, $search_result->paging["offset"]);
$this->assertEquals(102, $search_result->paging["total"]);
$this->assertEquals(5, count($search_result->results));
$this->assertEquals(1241012238, $search_result->results[0]["id"]);
}
Expand All @@ -109,15 +109,4 @@ private function createRequest(): array
];
return $request;
}

private function mockHttpRequest(string $filepath, int $status_code): \PHPUnit\Framework\MockObject\MockObject|\MercadoPago\Net\HttpRequest
{
/** @var \PHPUnit\Framework\MockObject\MockObject|\MercadoPago\Net\HttpRequest $mockHttpRequest */
$mockHttpRequest = $this->getMockBuilder(\MercadoPago\Net\HttpRequest::class)->getMock();

$responseJson = file_get_contents(__DIR__ . $filepath);
$mockHttpRequest->method('execute')->willReturn($responseJson);
$mockHttpRequest->method('getInfo')->willReturn($status_code);
return $mockHttpRequest;
}
}
Loading
Loading