Skip to content

Commit

Permalink
Use methods/all to retrieve active
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaymo committed Dec 18, 2023
1 parent 606a579 commit 7f0e332
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/Shared/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ public function getPayment($payment_id, $apiKey, $use_cache = true): ?\Mollie\Ap
*/
public function getAllPaymentMethods($apiKey, $test_mode = false, $use_cache = true)
{
if(!$apiKey) {
$apiKey = $this->getApiKey($test_mode);
}
$result = $this->getRegularPaymentMethods($apiKey, $test_mode, $use_cache);
if (!is_array($result)) {
$result = unserialize($result);
Expand Down Expand Up @@ -312,8 +315,17 @@ public function getRegularPaymentMethods($apiKey, $test_mode = false, $use_cache
if ($use_cache && ! empty(self::$regular_api_methods)) {
return self::$regular_api_methods;
}

self::$regular_api_methods = $this->getApiPaymentMethods($use_cache);
$test_mode = $this->isTestModeEnabled();
$methods = $this->getAllAvailablePaymentMethods($use_cache);
// We cannot access allActive for all methods so we filter them out here
$filtered_methods = array_filter($methods, function ($method) use ($test_mode) {
if ($test_mode === "live") {
return $method['status'] === "activated";
} else {
return in_array($method['status'], ["activated", "pending-review"]);
}
});
self::$regular_api_methods = $filtered_methods;

return self::$regular_api_methods;
}
Expand Down Expand Up @@ -679,14 +691,21 @@ public function isSubscription($orderId)
return apply_filters($this->pluginId . '_is_subscription_payment', $isSubscription, $orderId);
}

public function getAllAvailablePaymentMethods($use_cache = true)
public function getAllAvailablePaymentMethods($use_cache = true, $filters = [])
{
$apiKey = $this->settingsHelper->getApiKey();
$methods = false;
$locale = $this->getPaymentLocale();
$test_mode = $this->isTestModeEnabled();
$filters_key = [];
$filters_key['locale'] = $locale;
$filters_key['mode'] = ( $test_mode ? 'test' : 'live' );
$filters_key['api'] = 'methods';
$transient_id = $this->getTransientId(md5(http_build_query($filters_key)));
$filters['include'] = 'issuers';
$filters['locale'] = $locale;
$keysAllowed = ['amount' => '', 'locale' => '', 'issuers' => ''];
$filters = array_intersect_key($filters, $keysAllowed);

try {
if ($use_cache) {
Expand All @@ -701,8 +720,7 @@ public function getAllAvailablePaymentMethods($use_cache = true)
if (!$apiKey) {
return [];
}
$methods = $this->api_helper->getApiClient($apiKey)->methods->allAvailable($filters_key);

$methods = $this->api_helper->getApiClient($apiKey)->methods->allAvailable($filters);
$methods_cleaned = [];

foreach ($methods as $method) {
Expand Down

0 comments on commit 7f0e332

Please sign in to comment.