Skip to content
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
2 changes: 1 addition & 1 deletion captainhook.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}
},
{
"action": "vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php --diff",
"action": "vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php --diff",
"options": {}
}
],
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
],
"require": {
"php": "^7.4",
"php": ">=7.4",
"payplug/payplug-php": "^4.0"
},
"require-dev": {
Expand Down
36 changes: 19 additions & 17 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ parameters:
paths:
- src
- tests
ignoreErrors:
- '#Access to an undefined property Payplug\\Resource\\Payment::\$#'
stubFiles:
- stubs/PayplugPayment.stub
treatPhpDocTypesAsCertain: false
16 changes: 15 additions & 1 deletion src/Actions/PaymentAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function refundAction(RefundInputDTO $refund_inputDTO): ?RefundOutputDTO
$validator = new PaymentResourceValidator();
$validator->validate($resource);
$validator->validateIsPaid($resource);
$resource_id = $resource->id;
$resource_id = (string) $resource->id;

// Format the attributes for the refund request
$refund_gateway = $this->get_refund_gateway();
Expand All @@ -94,6 +94,20 @@ public function refundAction(RefundInputDTO $refund_inputDTO): ?RefundOutputDTO
return RefundOutputDTO::create($refund);
}

/**
* @param string $resource_id
* @param string $api_bearer
*
* @return ?PaymentOutputDTO
* @throws \Exception
*/
public function retrieveAction(string $resource_id, string $api_bearer): ?PaymentOutputDTO
{
$resource = $this->get_api()->load($api_bearer)->retrievePaymentResource($resource_id);

return PaymentOutputDTO::create($resource);
}

public function get_payment_gateway(): PaymentGatewayManager
{
return new PaymentGatewayManager();
Expand Down
28 changes: 28 additions & 0 deletions src/Utilities/Services/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,34 @@ public function refundPaymentResource(string $resource_id, array $datas): array
return $response;
}

/**
* @param string $resource_id
* @return array<string, mixed>
*/
public function retrievePaymentResource(string $resource_id): array
{
try {
if (null === $this->payplug_api) {
throw new \RuntimeException('API Payplug must be initialized.');
}
$response = [
'code' => 200,
'message' => 'OK',
'resource' => Payment::retrieve($resource_id, $this->payplug_api),
'result' => true,
];
} catch (\Exception $e) {
$response = [
'code' => $e->getCode(),
'message' => $e->getMessage(),
'resource' => null,
'result' => false,
];
}

return $response;
}

public function getBearerToken(): string
{
return $this->bearer_token;
Expand Down
66 changes: 66 additions & 0 deletions stubs/PayplugPayment.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace Payplug\Resource;

/**
* @property string|null $id
* @property bool|null $is_live
* @property bool|null $is_paid
* @property bool|null $is_refunded
* @property int $amount
* @property int $amount_refunded
* @property string|null $currency
* @property int|null $paid_at
* @property int|null $refundable_after
* @property int|null $refundable_until
* @property object|null $failure
* @property object|null $card
* @property object|null $billing
* @property object|null $metadata
*/
class Payment
{
/** @var string|null */
public $id;

/** @var bool|null */
public $is_live;

/** @var bool|null */
public $is_paid;

/** @var bool|null */
public $is_refunded;

/** @var int */
public $amount;

/** @var int */
public $amount_refunded;

/** @var string|null */
public $currency;

/** @var int|null */
public $paid_at;

/** @var int|null */
public $refundable_after;

/** @var int|null */
public $refundable_until;

/** @var object|null */
public $failure;

/** @var object|null */
public $card;

/** @var object|null */
public $billing;

/** @var object|null */
public $metadata;
}
70 changes: 70 additions & 0 deletions tests/Integration/PaymentAction/retrieveActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace PayPlugPluginMcp\Tests\Integration\PaymentAction;

use Mockery;
use Mockery\MockInterface;
use PayPlugPluginMcp\Actions\PaymentAction;
use PayPlugPluginMcp\Tests\Mock\PaymentMock;
use PayPlugPluginMcp\Tests\Mock\PaymentOutputDTOMock;
use PHPUnit\Framework\TestCase;

/**
* @group integration
*/
class retrieveActionTest extends TestCase
{
/** @var PaymentAction&MockInterface */
private $action;

/** @var MockInterface */
private $payment_api;

public function setUp(): void
{
$this->action = Mockery::mock(PaymentAction::class, [])->makePartial();
$this->payment_api = \Mockery::mock('alias:Payplug\Payment');
}

public function tearDown(): void
{
Mockery::close();
}

public function testWhenResourceCantBeRetrieved(): void
{
$error_msg = 'An error occurred during the process';
$error_code = 500;
$this->payment_api
->shouldReceive('retrieve')
->once()
->andThrow(new \Exception($error_msg, $error_code));

$error_output_props = [
'code' => $error_code,
'message' => $error_msg,
'result' => false,
'resource' => null,
];
$this->assertEquals(
PaymentOutputDTOMock::get($error_output_props),
$this->action->retrieveAction('pay_5iHMDxy4ABR4YBVW4UscIn', 'sk_test_bearer_token')
);
}

public function testWhenResourceIsRetrieved(): void
{
$resource = PaymentMock::getStandard(['is_paid' => true]);
$this->payment_api
->shouldReceive('retrieve')
->once()
->andReturn($resource);

$this->assertEquals(
PaymentOutputDTOMock::get(['resource' => $resource]),
$this->action->retrieveAction('pay_5iHMDxy4ABR4YBVW4UscIn', 'sk_test_bearer_token')
);
}
}
Loading
Loading