Making payment methods dependent on shipping options

If you want to limit the availability of payment methods based on a specified shipping option, shipping country or a different attribute from the shopping cart, you need to customize the PaymentOptionProvider class from the CMS.Ecommerce namespace.

The following example disables the PayPal payment method when the customer selects USA in the shipping address or selects USPS as the shipping option.

  1. Open your Kentico project in Visual Studio.

  2. Create a new class in the App_Code folder (or Old_App_Code on web application installations). For example, name the class CustomPaymentOptionInfoProvider.

  3. Make the class inherit from the PaymentOptionInfoProvider class.

  4. Add using statements for the CMS, CMS.Base and CMS.Ecommerce namespaces.

  5. Register the new InfoProvider class:

    
    
    
     [assembly: RegisterCustomProvider(typeof(CustomPaymentOptionInfoProvider))]
    
    
     
  6. Override the IsPaymentOptionApplicablemethod:

    
    
    
     using CMS;
     using CMS.Ecommerce;
     using CMS.Base;
    
     [assembly: RegisterCustomProvider(typeof(CustomPaymentOptionInfoProvider))]
     public class CustomPaymentOptionInfoProvider : PaymentOptionInfoProvider
     {
         protected override bool IsPaymentOptionApplicableInternal(ShoppingCartInfo cart, PaymentOptionInfo paymentOption)
         {
             // Does not check availability if the shopping cart or payment option is not available.
             if ((cart == null) || (paymentOption == null))
             {
                 return true;
             }
    
             // Allows all methods except PayPal.
             if (!paymentOption.PaymentOptionName.EqualsCSafe("PayPal"))
             {
                 return true;
             }
    
             // Forbids PayPal within USA.
             if ((cart.ShoppingCartBillingAddress != null) &&
                 cart.ShoppingCartBillingAddress.GetCountryThreeLetterCode().EqualsCSafe("USA"))
             {
                 return false;
             }
    
             // Forbids PayPal if the USPS shipping method is selected.
             return (cart.ShippingOption == null) || !cart.ShippingOption.ShippingOptionName.EqualsCSafe("USPS");
         }
     }
    
    
     
  7. Save your project and reload you Kentico website.

If a customer now selects the USA as their country or selects USPS as their shipping option, they will not be able to use PayPal as the payment method.

If you want to use the user interface for setting the limitations, you can either create a user interface or create a custom table and use the custom table API to add the values for the Dictionary variable with the limitations. Do not forget to set caching (you can use the CacheHelper class).