Hide Payment Gateway based on Shipping Rate

joseabadi2
New Member
3 0 0

Hi,

I created this script based on one that works for hiding payment gateways based on Province selection, but I need to hide Cash on Delivery (COD) for certain shipping rates, the script has no errors but it doesn't work, if anyone has an idea on how to this can be done it will be highly appreciated, here's the script:

HIDE_GATEWAYS_FOR_RATES = [
{
rate_match_type: :include,
rate_names: ["Envío a domicilio"],
gateway_match_type: :exact,
gateway_names: ["Cash on Delivery (COD)"],
},
]
class RateNameSelector
def initialize(match_type, rate_names)
@match_type = match_type
@comparator = match_type == :exact ? '==' : 'include?'
@rate_names = rate_names&.map { |rate_name| rate_name.downcase.strip }
end

def match?(shipping_rate)
(@match_type == :include) == @raten_names.include?(shipping_rate.upcase.strip)
end
end
class GatewayNameSelector
def initialize(match_type, gateway_names)
@comparator = match_type == :exact ? '==' : 'include?'
@gateway_names = gateway_names.map { |name| name.downcase.strip }
end

def match?(payment_gateway)
@gateway_names.any? { |name| payment_gateway.name.downcase.strip.send(@comparator, name) }
end
end
class HideGatewaysForRatesCampaign
def initialize(campaigns)
@campaigns = campaigns
end

def run(cart, shipping_rates)
address = cart.shipping_address

return if address.nil?

@campaigns.each do |campaign|
rate_name_selector = RateNameSelector.new(
campaign[:rate_match_type],
campaign[:rate_names],
)

next unless rate_name_selector.match?(address.rate_names)

gateway_name_selector = GatewayNameSelector.new(
campaign[:gateway_match_type],
campaign[:gateway_names],
)

payment_gateways.delete_if do |payment_gateway|
gateway_name_selector.match?(payment_gateway)
end
end
end
end

CAMPAIGNS = [
HideGatewaysForRatesCampaign.new(HIDE_GATEWAYS_FOR_RATES),
]

CAMPAIGNS.each do |campaign|
campaign.run(Input.cart, Input.payment_gateways)
end

Output.payment_gateways = Input.payment_gateways

0 Likes