Skip to content

Commit 5e045eb

Browse files
committed
Change payment methods check to sdk
Also update the SDK used and remove caching of payment methods
1 parent 636bbda commit 5e045eb

File tree

5 files changed

+75
-83
lines changed

5 files changed

+75
-83
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"require": {
33
"php": ">=7.4",
4-
"monei/monei-php-sdk": "^2.4.0",
4+
"monei/monei-php-sdk": "^2.6",
55
"php-di/php-di": "^6.4"
66
},
77
"autoload": {

composer.lock

Lines changed: 53 additions & 49 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Core/container-definitions.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@
4545
)
4646
),
4747
ApiKeyService::class => DI\autowire( ApiKeyService::class ),
48+
MoneiSdkClientFactory::class => DI\autowire( MoneiSdkClientFactory::class )
49+
->constructor( DI\get( ApiKeyService::class ) ),
4850
PaymentMethodsRepository::class => DI\factory(
49-
function ( ApiKeyService $apiKeyService ) {
50-
return new Monei\Repositories\PaymentMethodsRepository( $apiKeyService->get_account_id() );
51+
function ( ApiKeyService $apiKeyService, MoneiSdkClientFactory $sdkClientFactory ) {
52+
return new Monei\Repositories\PaymentMethodsRepository( $apiKeyService->get_account_id(), $sdkClientFactory->get_client() );
5153
}
5254
),
5355
PaymentMethodsService::class => DI\create( PaymentMethodsService::class )
@@ -57,9 +59,6 @@ function ( ApiKeyService $apiKeyService ) {
5759
->constructor( $blocksPath, $blockNamespacePrefix ),
5860
MoneiApplePayVerificationService::class => DI\autowire( MoneiApplePayVerificationService::class )
5961
->constructor( DI\get( MoneiPaymentServices::class ) ),
60-
61-
MoneiSdkClientFactory::class => DI\autowire( MoneiSdkClientFactory::class )
62-
->constructor( DI\get( ApiKeyService::class ) ),
6362
WooCommerceSubscriptionsHandler::class => \DI\create(
6463
WooCommerceSubscriptionsHandler::class,
6564
)->constructor(

src/Repositories/PaymentMethodsRepository.php

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,37 @@
22

33
namespace Monei\Repositories;
44

5-
use Monei\Repositories\PaymentMethodsRepositoryInterface;
5+
use Monei\MoneiClient;
66

77
class PaymentMethodsRepository implements PaymentMethodsRepositoryInterface {
88
private $accountId;
9+
private MoneiClient $moneiClient;
910

10-
public function __construct( string $accountId ) {
11+
public function __construct( string $accountId, MoneiClient $moneiClient) {
1112
$this->accountId = $accountId;
13+
$this->moneiClient = $moneiClient;
1214
}
1315

1416
/**
1517
* Fetch payment methods from the API.
1618
*/
1719
private function fetchFromAPI(): ?array {
18-
$response = wp_remote_get( 'https://api.monei.com/v1/payment-methods?accountId=' . $this->accountId );
19-
if ( is_wp_error( $response ) ) {
20-
return null;
21-
}
22-
23-
return json_decode( wp_remote_retrieve_body( $response ), true );
24-
}
25-
26-
/**
27-
* Generate a transient key.
28-
*/
29-
private function generateTransientKey( string $key ): string {
30-
return 'payment_methods_' . md5( $key );
20+
if ( ! $this->accountId ) {
21+
return null;
22+
}
23+
try {
24+
$response = $this->moneiClient->paymentMethods->get( $this->accountId );
25+
} catch ( \Exception $e ) {
26+
$response = null;
27+
}
28+
29+
return json_decode($response, true);
3130
}
3231

3332
/**
3433
* Get payment methods (fetch from transient or API).
3534
*/
3635
public function getPaymentMethods(): array {
37-
$transientKey = $this->generateTransientKey( $this->accountId );
38-
$data = get_transient( $transientKey );
39-
40-
if ( ! $data ) {
41-
$data = $this->fetchFromAPI();
42-
if ( $data ) {
43-
set_transient( $transientKey, $data, DAY_IN_SECONDS );
44-
}
45-
}
46-
47-
return $data ?: array();
36+
return $this->fetchFromAPI() ?: array();
4837
}
4938
}

0 commit comments

Comments
 (0)