/** * Returns allowed payment methods. * * @param string $shippingCountry The SilvercartCountry to check the payment methods for. * @param SilvercartShoppingCart $shoppingCart The shopping cart object * @param Boolean $forceAnonymousCustomerIfNotExist When true, an anonymous customer will be created when no customer exists * * @return ArrayList * * @author Sebastian Diel <*****@*****.**>, * Sascha Koehler <*****@*****.**> * @since 15.11.2014 */ public static function getAllowedPaymentMethodsFor($shippingCountry, $shoppingCart, $forceAnonymousCustomerIfNotExist = false) { $allowedPaymentMethods = array(); if (!$shippingCountry) { return $allowedPaymentMethods; } $paymentMethods = $shippingCountry->SilvercartPaymentMethods('isActive = 1'); $member = SilvercartCustomer::currentUser(); if (!$member && $forceAnonymousCustomerIfNotExist) { $member = new Member(); $anonymousGroup = Group::get()->filter('Code', 'anonymous')->first(); $memberGroups = new ArrayList(); $memberGroups->push($anonymousGroup); } else { $memberGroups = $member->Groups(); } $shippingMethodID = null; if (Controller::curr() instanceof SilvercartCheckoutStep_Controller) { $checkoutData = Controller::curr()->getCombinedStepData(); if (array_key_exists('ShippingMethod', $checkoutData)) { $shippingMethodID = $checkoutData['ShippingMethod']; } } if ($paymentMethods) { foreach ($paymentMethods as $paymentMethod) { $assumePaymentMethod = true; $containedInGroup = false; $containedInUsers = false; $doAccessChecks = true; // ------------------------------------------------------------ // Basic checks // ------------------------------------------------------------ if ($paymentMethod->enableActivationByOrderRestrictions) { $assumePaymentMethod = $paymentMethod->isActivationByOrderRestrictionsPossible($member); $doAccessChecks = false; } $checkAmount = $shoppingCart->getAmountTotalWithoutFees()->getAmount(); if (!$paymentMethod->isAvailableForAmount($checkAmount)) { $assumePaymentMethod = false; $doAccessChecks = false; } // ------------------------------------------------------------ // Shipping method check // ------------------------------------------------------------ if (!is_null($shippingMethodID) && $paymentMethod->SilvercartShippingMethods()->exists() && !$paymentMethod->SilvercartShippingMethods()->find('ID', $shippingMethodID)) { $assumePaymentMethod = false; $doAccessChecks = false; } // ------------------------------------------------------------ // Access checks // ------------------------------------------------------------ if ($doAccessChecks) { // Check if access for groups or is set positively if ($paymentMethod->ShowOnlyForGroups()->exists()) { foreach ($paymentMethod->ShowOnlyForGroups() as $paymentGroup) { if ($memberGroups->find('ID', $paymentGroup->ID)) { $containedInGroup = true; break; } } if ($containedInGroup) { $assumePaymentMethod = true; } else { $assumePaymentMethod = false; } } // Check if access for users or is set positively if ($paymentMethod->ShowOnlyForUsers()->exists()) { if ($paymentMethod->ShowOnlyForUsers()->find('ID', $member->ID)) { $containedInUsers = true; } if ($containedInUsers) { $assumePaymentMethod = true; } else { if (!$containedInGroup) { $assumePaymentMethod = false; } } } // Check if access for groups is set negatively if ($paymentMethod->ShowNotForGroups()->exists()) { foreach ($paymentMethod->ShowNotForGroups() as $paymentGroup) { if ($memberGroups->find('ID', $paymentGroup->ID)) { if (!$containedInUsers) { $assumePaymentMethod = false; } } } } // Check if access for users is set negatively if ($paymentMethod->ShowNotForUsers()->exists()) { if ($paymentMethod->ShowNotForUsers()->find('ID', $member->ID)) { if (!$containedInUsers) { $assumePaymentMethod = false; } } } } if ($assumePaymentMethod) { $allowedPaymentMethods[] = $paymentMethod; } } } $allowedPaymentMethods = new ArrayList($allowedPaymentMethods); return $allowedPaymentMethods; }