# monei/7.3.0/src/Repositories/PaymentMethodsRepository.php

MONEI Payments for WooCommerce, version 7.3.0. 84 lines.

- Page: https://pluginprobe.com/plugins/monei/7.3.0/code/src/Repositories/PaymentMethodsRepository.php
- Raw: https://pluginprobe.com/plugins/monei/7.3.0/raw/src/Repositories/PaymentMethodsRepository.php
- Modified: 2026-08-25T11:57:56+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/monei/7.3.0/code/src/Repositories/PaymentMethodsRepository.php#L10-L20`.

```php
<?php

namespace Monei\Repositories;

use Monei\MoneiClient;
use Exception;

class PaymentMethodsRepository implements PaymentMethodsRepositoryInterface {
	private $accountId;
	private MoneiClient $moneiClient;

	public function __construct( string $accountId, MoneiClient $moneiClient ) {
		$this->accountId   = $accountId;
		$this->moneiClient = $moneiClient;
	}

	/**
	 * Fetch payment methods from the API.
	 */
	private function fetchFromAPI(): ?array {
		// The account id no longer reaches the API — getAllowed() derives the account
		// from the API key. It still gates the call because it is what separates the
		// test cache from the live one, and because an unset one means the plugin is
		// not configured yet.
		if ( ! $this->accountId ) {
			return null;
		}
		try {
			// /allowed-payment-methods, the API key authenticated replacement for the
			// deprecated /payment-methods. Amount, currency and country are left out on
			// purpose: this repository is a container singleton that answers admin
			// screens as well as the checkout, so it has no one cart to describe.
			$response = $this->moneiClient->paymentMethods->getAllowed();
		} catch ( Exception $e ) {
			$response = null;
		}

		return $response ? json_decode( $response, true ) : array();
	}

	/**
	 * Get payment methods (fetch from transient or API).
	 */
	public function getPaymentMethods(): array {
		$transientKey = $this->generateTransientKey( $this->accountId );
		$data         = get_transient( $transientKey );

		if ( ! $data ) {
			$data = $this->fetchFromAPI();
			if ( $data ) {
				set_transient( $transientKey, $data, 30 );
				set_transient( $this->fallbackKey( $transientKey ), $data, HOUR_IN_SECONDS );
			} else {
				// An empty answer marks every gateway unavailable, which takes
				// the payment methods off the checkout. The 30 second cache
				// makes that one failed call away at any moment, so fall back
				// to the last answer that worked.
				$data = get_transient( $this->fallbackKey( $transientKey ) );
				if ( $data ) {
					// Without this every request during an outage repeats the
					// failing call.
					set_transient( $transientKey, $data, 30 );
				}
			}
		}

		return $data ?: array();
	}

	/**
	 * Generate a transient key.
	 */
	private function generateTransientKey( string $key ): string {
		return 'payment_methods_' . md5( $key );
	}

	/**
	 * Key of the longer lived copy used when the API call fails.
	 */
	private function fallbackKey( string $transientKey ): string {
		return $transientKey . '_last_ok';
	}
}

```
