SL-371 Auto-refresh account payment methods when settings open#335
Conversation
- Reconcile stored payment methods against the account on page open (persisted, enabled flags preserved, new methods disabled by default) instead of only on save - On API failure, fall back to the last-known stored list instead of an empty page; never wipes stored config (new SaferPayPaymentRepository::getAllPaymentMethodsNames) - Unit test for the refresh reconcile behavior (preserve/added-disabled/removed-dropped + early-return)
There was a problem hiding this comment.
Code Review
This pull request updates the admin controller to automatically refresh and reconcile payment methods from the Saferpay API when opening the settings page, falling back to stored payment methods if the API is unreachable. It also introduces a new unit test suite for the refresh service. The review feedback highlights a performance issue due to redundant back-to-back API requests, suggesting runtime caching, and points out deprecated PHPUnit methods (withConsecutive and setMethods) in the new tests that should be updated for PHPUnit 10 compatibility.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| /** @var SaferPayRefreshPaymentsService $refreshPaymentsService */ | ||
| $refreshPaymentsService = $this->module->getService(SaferPayRefreshPaymentsService::class); | ||
| $refreshPaymentsService->refreshPayments(); | ||
|
|
||
| /** @var SaferPayObtainPaymentMethods $obtainMethods */ | ||
| $obtainMethods = $this->module->getService(SaferPayObtainPaymentMethods::class); | ||
| $paymentMethods = $obtainMethods->obtainPaymentMethodsNamesAsArray(); |
There was a problem hiding this comment.
Every time the Payment Methods settings page is opened, two identical, blocking HTTP requests are made to the Saferpay API back-to-back:
- First, inside
$refreshPaymentsService->refreshPayments(), which calls$this->obtainPayments->obtainPaymentMethodsNamesAsArray(). - Second, immediately after,
$obtainMethods->obtainPaymentMethodsNamesAsArray()is called directly ingetPaymentMethodsData().
This significantly increases the page load time and performs redundant network requests.
Recommendation
To resolve this, implement a simple runtime cache (in-memory caching) inside the SaferPayObtainPaymentMethods service so that subsequent calls within the same request lifecycle return the cached result instantly.
For example, in src/Service/SaferPayObtainPaymentMethods.php:
class SaferPayObtainPaymentMethods
{
/** @var array|null */
private $cachedPaymentMethods;
public function obtainPaymentMethods(): array
{
if ($this->cachedPaymentMethods !== null) {
return $this->cachedPaymentMethods;
}
$paymentMethods = [];
// ... existing fetching logic ...
$this->cachedPaymentMethods = $paymentMethods;
return $paymentMethods;
}
}| $paymentRepository->expects($this->exactly(2)) | ||
| ->method('insertPayment') | ||
| ->withConsecutive( | ||
| [['name' => 'VISA', 'active' => 1]], | ||
| [['name' => 'TWINT', 'active' => 0]] | ||
| ); | ||
| // Custom-form flag preserved for VISA (true -> 1), default 0 for the new TWINT. | ||
| $fieldRepository->expects($this->exactly(2)) | ||
| ->method('insertField') | ||
| ->withConsecutive( | ||
| [['name' => 'VISA', 'active' => 1]], | ||
| [['name' => 'TWINT', 'active' => 0]] | ||
| ); | ||
|
|
||
| $this->makeService($paymentRepository, $obtainPaymentMethods, $fieldRepository)->refreshPayments(); |
There was a problem hiding this comment.
The withConsecutive() method is deprecated in PHPUnit 9.6 and completely removed in PHPUnit 10. Using it will trigger deprecation warnings and block future PHPUnit upgrades.
Recommendation
Instead of withConsecutive(), you can collect the arguments using willReturnCallback() and assert them afterward. This is fully compatible with PHPUnit 10+ and avoids deprecation warnings.
$insertedPayments = [];
$paymentRepository->expects($this->exactly(2))
->method('insertPayment')
->willReturnCallback(function ($data) use (&$insertedPayments) {
$insertedPayments[] = $data;
});
// Custom-form flag preserved for VISA (true -> 1), default 0 for the new TWINT.
$insertedFields = [];
$fieldRepository->expects($this->exactly(2))
->method('insertField')
->willReturnCallback(function ($data) use (&$insertedFields) {
$insertedFields[] = $data;
});
$this->makeService($paymentRepository, $obtainPaymentMethods, $fieldRepository)->refreshPayments();
$this->assertEquals([
['name' => 'VISA', 'active' => 1],
['name' => 'TWINT', 'active' => 0],
], $insertedPayments);
$this->assertEquals([
['name' => 'VISA', 'active' => 1],
['name' => 'TWINT', 'active' => 0],
], $insertedFields);| private function createMockWithMethods($class, array $methods) | ||
| { | ||
| $builder = $this->getMockBuilder($class)->disableOriginalConstructor(); | ||
| if (!empty($methods)) { | ||
| $builder->setMethods($methods); | ||
| } | ||
|
|
||
| return $builder->getMock(); | ||
| } |
There was a problem hiding this comment.
The setMethods() method is deprecated in PHPUnit 9 and removed in PHPUnit 10. It should be replaced with onlyMethods() for existing methods on the class.
private function createMockWithMethods($class, array $methods)
{
$builder = $this->getMockBuilder($class)->disableOriginalConstructor();
if (!empty($methods)) {
$builder->onlyMethods($methods);
}
return $builder->getMock();
}Address review feedback: refreshPayments() already persists the account's methods, so read the list back from storage instead of calling obtainPaymentMethodsNamesAsArray() a second time. Falls back to a live fetch only when nothing was persisted (fresh setup / no-active early return). Removes the redundant back-to-back API request.
c493512
into
feature/react-admin-settings
Part of SL-350 settings redesign. Phase 3 — auto-refresh account config.
Ticket
SaferPayPaymentRepository::getAllPaymentMethodsNames).Tests
New unit test
tests/Unit/Service/SaferPayRefreshPaymentsServiceTest.php(2 tests): reconcile preserves-enabled / adds-new-disabled / drops-removed, and the no-active-methods early return.Verification
Unit tests green;
php -lclean.