PluginProbe
MONEI Payments for WooCommerce / 7.2.0
MONEI Payments for WooCommerce v7.2.0
7.3.3 7.3.2 7.3.1 7.3.0 7.2.4 7.2.3 7.2.2 7.2.0 7.2.1 7.1.3 2.1.0 3.0.0 3.1.0 3.1.1 4.0.0 4.1.0 4.1.1 4.2.0 4.2.1 5.0 5.1.0 5.1.1 5.1.2 5.2.2 5.2.3 All 87 releases
monei / src / Repositories / PaymentMethodsRepository.php

PaymentMethodsRepository.php in MONEI Payments for WooCommerce 7.2.0, at src/Repositories/PaymentMethodsRepository.php

84 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Monei\Repositories;
4
5 use Monei\MoneiClient;
6 use Exception;
7
8 class PaymentMethodsRepository implements PaymentMethodsRepositoryInterface {
9 private $accountId;
10 private MoneiClient $moneiClient;
11
12 public function __construct( string $accountId, MoneiClient $moneiClient ) {
13 $this->accountId = $accountId;
14 $this->moneiClient = $moneiClient;
15 }
16
17 /**
18 * Fetch payment methods from the API.
19 */
20 private function fetchFromAPI(): ?array {
21 // The account id no longer reaches the API — getAllowed() derives the account
22 // from the API key. It still gates the call because it is what separates the
23 // test cache from the live one, and because an unset one means the plugin is
24 // not configured yet.
25 if ( ! $this->accountId ) {
26 return null;
27 }
28 try {
29 // /allowed-payment-methods, the API key authenticated replacement for the
30 // deprecated /payment-methods. Amount, currency and country are left out on
31 // purpose: this repository is a container singleton that answers admin
32 // screens as well as the checkout, so it has no one cart to describe.
33 $response = $this->moneiClient->paymentMethods->getAllowed();
34 } catch ( Exception $e ) {
35 $response = null;
36 }
37
38 return $response ? json_decode( $response, true ) : array();
39 }
40
41 /**
42 * Get payment methods (fetch from transient or API).
43 */
44 public function getPaymentMethods(): array {
45 $transientKey = $this->generateTransientKey( $this->accountId );
46 $data = get_transient( $transientKey );
47
48 if ( ! $data ) {
49 $data = $this->fetchFromAPI();
50 if ( $data ) {
51 set_transient( $transientKey, $data, 30 );
52 set_transient( $this->fallbackKey( $transientKey ), $data, HOUR_IN_SECONDS );
53 } else {
54 // An empty answer marks every gateway unavailable, which takes
55 // the payment methods off the checkout. The 30 second cache
56 // makes that one failed call away at any moment, so fall back
57 // to the last answer that worked.
58 $data = get_transient( $this->fallbackKey( $transientKey ) );
59 if ( $data ) {
60 // Without this every request during an outage repeats the
61 // failing call.
62 set_transient( $transientKey, $data, 30 );
63 }
64 }
65 }
66
67 return $data ?: array();
68 }
69
70 /**
71 * Generate a transient key.
72 */
73 private function generateTransientKey( string $key ): string {
74 return 'payment_methods_' . md5( $key );
75 }
76
77 /**
78 * Key of the longer lived copy used when the API call fails.
79 */
80 private function fallbackKey( string $transientKey ): string {
81 return $transientKey . '_last_ok';
82 }
83 }
84