Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Andrew Bond 18 posts 139 karma points
    Feb 15, 2021 @ 09:41
    Andrew Bond
    1

    Correct way to use SetPaymentMethod

    Hello,

    I am trying to remove the payment step from Vendr.Checkout (as we only use one payment provider) by setting the payment method when a product is added to the cart. But Vendr complains that the payment method is not set when we get to the review step.

    Can anyone help with the correct way to setting this?

    [HttpPost]
        public ActionResult AddToBasket(AddToBasketDto postModel)
        {
            try
            {
                using (var uow = _unitOfWorkProvider.Create())
                {
                    var store = CurrentPage.GetStore();                 
                    var order = _sessionManager.GetOrCreateCurrentOrder(store.Id)
                        .AsWritable(uow)
                        .SetProperty("opayoOrderDescription", "Artwork")
                        .SetPaymentMethod(new Guid("436971b5-b711-42ed-8e56-f807f89c8620"))
                        .AddProduct(postModel.ProductReference, postModel.Quantity);
    
                    _orderService.SaveOrder(order);
    
                    uow.Complete();
                }
            }
            catch (ValidationException ex)
            {
                ...
            }
    
            return CurrentUmbracoPage();
    
        }
    
  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Feb 15, 2021 @ 09:53
    Matt Brailsford
    101

    What you have is fine, but it's likely because Vendr.Checkout has a series of event handlers to clear the selected payment / shipping method at various steps of the checkout flow so whilst your code is setting the payment method, these event handlers are likely clearing it

    https://github.com/vendrhub/vendr-checkout/blob/dev/src/Vendr.Checkout/Composing/VendrCheckoutComposer.cs#L14-L30

    We don't currently have an API to remove handlers via Vendr but as Vendr's event registration API is a wrapper around Umbraco's API, you could probably create a composer that runs after VendrCheckouts and use Umbraco's API to remove the handlers

    [ComposeAfter(typeof(VendrCheckoutComposer))]
    public class Composer : IUserComposer
    {
        public void Compose(Composition composition)
        {
            composition.WithCollectionBuilder<EventHandlerCollectionBuilder<OrderProductAddingNotification>>()
                .Remove<OrderProductAddingHandler>();
    
            composition.WithCollectionBuilder<EventHandlerCollectionBuilder<OrderLineChangingNotification>>()
                .Remove<OrderLineChangingHandler>();
    
            composition.WithCollectionBuilder<EventHandlerCollectionBuilder<OrderPaymentCountryRegionChangingNotification>>()
                .Remove<OrderPaymentCountryRegionChangingHandler>();
    
            composition.WithCollectionBuilder<EventHandlerCollectionBuilder<OrderShippingCountryRegionChangingNotification>>()
                .Remove<OrderShippingCountryRegionChangingHandler>();
    
            composition.WithCollectionBuilder<EventHandlerCollectionBuilder<OrderShippingMethodChangingNotification>>()
                .Remove<OrderShippingMethodChangingHandler>();
        }
    }
    

    Obviously, in so doing this, you are removing some of Vendr Checkout's logic, so you'll need to test that the checkout flow calculations are what you expect them to be.

    Matt

  • Andrew Bond 18 posts 139 karma points
    Feb 15, 2021 @ 10:14
    Andrew Bond
    0

    Thanks Matt, that worked a treat.

Please Sign in or register to post replies

Write your reply to:

Draft