| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\Api; |
| 4 |
|
| 5 |
use FluentCart\App\Modules\PaymentMethods\Core\GatewayManager; |
| 6 |
use FluentCart\App\Modules\PaymentMethods\StripeGateway\Stripe; |
| 7 |
use FluentCart\Framework\Support\Arr; |
| 8 |
use FluentCart\Framework\Support\Collection; |
| 9 |
|
| 10 |
class PaymentMethods |
| 11 |
{ |
| 12 |
/** |
| 13 |
* @return array |
| 14 |
* |
| 15 |
* All payment methods |
| 16 |
* @throws \Exception |
| 17 |
*/ |
| 18 |
public function getAll(): array |
| 19 |
{ |
| 20 |
$manager = GatewayManager::getInstance(); |
| 21 |
return $manager->getAllMeta(); |
| 22 |
//return apply_filters('fluent_cart/payments/get_global_payment_methods', []); |
| 23 |
} |
| 24 |
|
| 25 |
public static function getLogos(): array |
| 26 |
{ |
| 27 |
$manager = GatewayManager::getInstance(); |
| 28 |
$paymentInstanceMeta = $manager->getAllMeta(); |
| 29 |
return Collection::make($paymentInstanceMeta)->pluck('logo', 'route')->toArray(); |
| 30 |
} |
| 31 |
|
| 32 |
public static function getIcons(): array |
| 33 |
{ |
| 34 |
$manager = GatewayManager::getInstance(); |
| 35 |
$paymentInstanceMeta = $manager->getAllMeta(); |
| 36 |
return Collection::make($paymentInstanceMeta)->pluck('icon', 'route')->toArray(); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* @return array |
| 41 |
* |
| 42 |
* All active payment methods |
| 43 |
*/ |
| 44 |
public static function getActiveMethodInstance($cart = null): array |
| 45 |
{ |
| 46 |
$paymentMethods = GatewayManager::getInstance()->all(); |
| 47 |
$activePaymentMethods = []; |
| 48 |
|
| 49 |
$hasSubscription = $cart ? $cart->hasSubscription() : false; |
| 50 |
|
| 51 |
$hasZeroRecurring = false; |
| 52 |
if ($hasSubscription && $cart) { |
| 53 |
foreach ($cart->cart_data as $item) { |
| 54 |
if (Arr::get($item, 'is_recurring_coupon') === 'yes' && Arr::get($item, 'line_total') === 0) { |
| 55 |
$hasZeroRecurring = true; |
| 56 |
break; |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
foreach ($paymentMethods as $paymentMethod) { |
| 62 |
$settings = $paymentMethod->settings->get(); |
| 63 |
if (Arr::get($settings, 'is_active') === 'yes') { |
| 64 |
$isCurrencySupported = $paymentMethod->isCurrencySupported(); |
| 65 |
if (!$isCurrencySupported || ($hasZeroRecurring && !$paymentMethod->has('zero_recurring'))) { |
| 66 |
continue; |
| 67 |
} |
| 68 |
|
| 69 |
$activePaymentMethods[] = $paymentMethod; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
// Sort by saved order if available |
| 74 |
$savedOrder = get_option('fluent_cart_payment_methods_order', []); |
| 75 |
if (!empty($savedOrder) && is_array($savedOrder)) { |
| 76 |
$orderMap = array_flip($savedOrder); |
| 77 |
usort($activePaymentMethods, function($a, $b) use ($orderMap) { |
| 78 |
$aOrder = $orderMap[$a->getMeta('route')] ?? PHP_INT_MAX; |
| 79 |
$bOrder = $orderMap[$b->getMeta('route')] ?? PHP_INT_MAX; |
| 80 |
return $aOrder <=> $bOrder; |
| 81 |
}); |
| 82 |
} |
| 83 |
|
| 84 |
return $activePaymentMethods; |
| 85 |
} |
| 86 |
|
| 87 |
public static function getActiveMeta(): array |
| 88 |
{ |
| 89 |
$methodInstance = static::getActiveMethodInstance(); |
| 90 |
$activePaymentMethods = []; |
| 91 |
foreach ($methodInstance as $method) { |
| 92 |
$activePaymentMethods[] = $method->getMeta(); |
| 93 |
} |
| 94 |
return $activePaymentMethods; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
/** |
| 99 |
* @param string $method name like stripe, PayPal etc |
| 100 |
* Get payment method to connect info if any method uses connecting |
| 101 |
* @return array |
| 102 |
* |
| 103 |
*/ |
| 104 |
public function getConnectInfo($method) |
| 105 |
{ |
| 106 |
return apply_filters('fluent_cart/get_payment_connect_info_' . sanitize_text_field($method), [], []); |
| 107 |
} |
| 108 |
|
| 109 |
} |
| 110 |
|