Copied to clipboard

Flag this post as spam?

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


  • Nguyen Dung Tri 106 posts 606 karma points
    Nov 03, 2016 @ 09:55
    Nguyen Dung Tri
    0

    How to get current invoice?

    Hello,

    Is there a way to get current invoice object during checkout?

    From BasketCheckoutPaymentManager.cs file, there is a method (AuthorizePayment). Inside this method, there is a way to get invoice object:

    /// <summary>
            /// Attempts to process a payment
            /// </summary>
            /// <param name="paymentGatewayMethod">The <see cref="IPaymentGatewayMethod"/> to use in processing the payment</param>
            /// <param name="args">Additional arguments required by the payment processor</param>
            /// <returns>The <see cref="IPaymentResult"/></returns>
            public override IPaymentResult AuthorizePayment(IPaymentGatewayMethod paymentGatewayMethod, ProcessorArgumentCollection args)
            {
                Mandate.ParameterNotNull(paymentGatewayMethod, "paymentGatewayMethod");
    
                if (!this.IsReadyToInvoice()) return new PaymentResult(Attempt<IPayment>.Fail(new InvalidOperationException("SalesPreparation is not ready to invoice")), null, false);
    
                // invoice
                var invoice = this.PrepareInvoice(this.InvoiceBuilder);
    
                this.Context.Services.InvoiceService.Save(invoice);
    
                var result = invoice.AuthorizePayment(paymentGatewayMethod, args);
    
                if (result.Payment.Success && this.Context.Settings.EmptyBasketOnPaymentSuccess) this.Context.Customer.Basket().Empty();
                this.OnFinalizing(result);
    
                return result;
            }
    

    var invoice = this.PrepareInvoice(this.InvoiceBuilder);
    

    My work is focus on My Payment Controller Base. So how can I get invoice object in Process method?

    Note: When user click Finish & Pay. The debug line will stop at Process method inside my payment controller base class. From there, I want to get current invoice to do some stuff like retrieve order total, order items, etc...

  • Nguyen Dung Tri 106 posts 606 karma points
    Nov 03, 2016 @ 20:35
    Nguyen Dung Tri
    100

    From other post, I have got a way to prepare an invoice in my payment base class.

    var checkoutMgr = this.Basket.GetCheckoutManager();
    var invoice = checkoutMgr.Payment.PrepareInvoice();
    

    If the payment is success, don't forget to then save the invoice after you are done with it.

    See the following resources for additional information which might help you:

    https://merchello.readme.io/docs/checkoutmanager

    https://merchello.readme.io/docs/checkoutmanager#icheckoutpaymentmanager

  • Rusty Swayne 1655 posts 4993 karma points c-trib
    Nov 04, 2016 @ 15:40
    Rusty Swayne
    0

    Hey Nguyen,

    As you've found, the PrepareInvoice method builds an invoice from the checkout manager data but does not save it. This is the method of generating an sales summary without generating a bunch of invoice numbers and persisted records.

    However, to clarify your finding above, the invoice IS saved internally in the CheckoutManager payment methods -

    • CheckoutManager.Payment.Authorize
    • CheckoutManager.Payment.AuthorizeCapture

    so you should not need to save again once you get the payment result.

Please Sign in or register to post replies

Write your reply to:

Draft