Copied to clipboard

Flag this post as spam?

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


  • Bo Jacobsen 597 posts 2395 karma points
    Sep 29, 2021 @ 07:45
    Bo Jacobsen
    0

    Capture payment on completed status

    Hi Matt.

    Is it possible to request to capture a payment when the order change status to completed?

    Or is the auto capture boolean on payment providers used for the completed status?

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Sep 29, 2021 @ 08:06
    Matt Brailsford
    100

    Hi Bo,

    The capture toggle if enabled will capture the payment immediately, if you are wishing to have the capture occur in response to an order changing status then this would need to be done in an event handler.

    In the most recent versions of Vendr there is a new IPaymentService (also available on the IVendrApi facade) which you could inject into an event handler and call it's CaptureOrderPayment method.

    You're event handler would look something like this

    public class CapturePaymentOnOrderStatusChangeHandler : NotificationEventHandlerBase<OrderStatusChangingNotification>
    {
        private IVendrApi _vendrApi;
    
        public CapturePaymentOnOrderStatusChangeHandler(IVendrApi vendrApi)
        {
            _vendrApi = vendrApi;
        }
    
        public override void Handle(OrderStatusChangingNotification evt)
        {
            // Only proceed if the order is currently authorized
            if (evt.Order.TransactionInfo.PaymentStatus == PaymentStatus.Authorized)
            {
                // Check the order status is changing to completed
                var completedOrderStatus = _vendrApi.GetOrderStatus(evt.Order.StoreId, "completed");
                if (evt.OrderStatusId.To.HasValue && evt.OrderStatusId.To.Value == completedOrderStatus.Id) 
                {
                    // Attempt to capture the payment
                    var paymentResult = _vendrApi.CaptureOrderPayment(evt.Order);
                    if (result.Success)
                    {
                        // Apply the changes to the order
                        // --
                        // NB: Because this is a `Changing` event handler
                        // the evt.Order is already writable so not need to
                        // convert it, and also no need to save it as this
                        // will occur automatically.
                        evt.Order.ApplyPaymentChanges(result);
                    }
                }
            }
        }
    }
    

    Which you'd need to hook up in a composer as documented here https://vendr.net/docs/core/1.8.0/key-concepts/events/#registering-a-notification-event-handler

    Hope this helps

    Matt

  • Bo Jacobsen 597 posts 2395 karma points
    Sep 29, 2021 @ 08:24
    Bo Jacobsen
    0

    Thanks for a quick reply.

    We are using Vendr version 1.8.6.

    But I can not find the CaptureOrderPayment in the IVendrApi. Do i miss a using?

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Sep 29, 2021 @ 08:33
    Matt Brailsford
    0

    It's on the web version of the API facade so you'll need using Vendr.Core.Web.Api

  • Bo Jacobsen 597 posts 2395 karma points
    Sep 29, 2021 @ 08:34
    Bo Jacobsen
    0

    And now it works.

    Thanks alot Matt :)

Please Sign in or register to post replies

Write your reply to:

Draft