Copied to clipboard

Flag this post as spam?

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


  • Pantelis 53 posts 107 karma points
    Nov 24, 2015 @ 11:19
    Pantelis
    0

    Properly capture a custom payment

    Hi,

    I created a simple controller to process payments (the gateway uses jquery and it's rather simple so I didn't create a payment gateway, just a custom form).

    I then use the GatewayProviderService to create an IPayment, save it and apply it using ApplyPaymentToInvoice (I saw these from the payment providers' demo).

    The payment shows up fine in Merchello, and balance is zero, but in the sales listing the invoice still shows an Unpaid Status. How do we change this to correctly reflect the payment?

    Thank you, Pantelis

  • Rusty Swayne 1655 posts 4993 karma points c-trib
    Nov 24, 2015 @ 17:26
    Rusty Swayne
    0

    @Pantelis - typically this is done in the PaymentGatewayMethodBase which internally uses a private method to assert the invoice status:

    I think in your case, the method can be simplified since you know it is an authorize or capture by your result during execution, but the entire method is

     /// <summary>
        /// Validates the invoice status is correct after a payment request
        /// </summary>
        /// <param name="invoice">
        /// The invoice.
        /// </param>
        private void AssertInvoiceStatus(IInvoice invoice)
        {
            var appliedPayments = _gatewayProviderService.GetAppliedPaymentsByInvoiceKey(invoice.Key).ToArray();
    
            var appliedTotal = 
                appliedPayments.Where(x => x.TransactionType == AppliedPaymentType.Debit).Sum(x => x.Amount) - 
                appliedPayments.Where(x => x.TransactionType == AppliedPaymentType.Credit).Sum(x => x.Amount);
    
            var statuses = GatewayProviderService.GetAllInvoiceStatuses().ToArray();
    
            if (invoice.Total > appliedTotal && invoice.InvoiceStatusKey != Core.Constants.DefaultKeys.InvoiceStatus.Partial)
                invoice.InvoiceStatus = statuses.First(x => x.Key == Core.Constants.DefaultKeys.InvoiceStatus.Partial);
            if (appliedTotal == 0 && invoice.InvoiceStatusKey != Core.Constants.DefaultKeys.InvoiceStatus.Unpaid)
                invoice.InvoiceStatus = statuses.First(x => x.Key == Core.Constants.DefaultKeys.InvoiceStatus.Unpaid);
            if (invoice.Total <= appliedTotal && invoice.InvoiceStatusKey != Core.Constants.DefaultKeys.InvoiceStatus.Paid)
                invoice.InvoiceStatus = statuses.First(x => x.Key == Core.Constants.DefaultKeys.InvoiceStatus.Paid);
    
            if(invoice.IsDirty()) GatewayProviderService.Save(invoice);
    
        }        
    
Please Sign in or register to post replies

Write your reply to:

Draft