Order items in a payment plugin

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
7 years ago
I am writing a payment plugin and wonder how I can get order items from within IPaymentMethod.ProcessPayment().

this.orderService.GetOrderByGuid(processPaymentRequest.OrderGuid) returns null as apparently the order isn't created until paid.

Thanks!
7 years ago
muriwai wrote:
I am writing a payment plugin and wonder how I can get order items from within IPaymentMethod.ProcessPayment().

You have the CustomerId in the ProcessPaymentRequest so you can get them from the customer's cart. See how it's done in the PayPalDirect plugin:

var customer = _customerService.GetCustomerById(processPaymentRequest.CustomerId);
if (customer == null)
    throw new Exception("Customer cannot be loaded");

//get current shopping cart
var shoppingCart = customer.ShoppingCartItems
    .Where(shoppingCartItem => shoppingCartItem.ShoppingCartType == ShoppingCartType.ShoppingCart)
    .LimitPerStore(processPaymentRequest.StoreId).ToList();

//items
var items = GetItems(shoppingCart, customer, processPaymentRequest.StoreId, currency.CurrencyCode);

The GetItems function is defined in the plugin so you'd need to implement that in whatever way makes sense for you're doing.
7 years ago
Great, thank you.

I was looking at Authorize.NET plugin, which doesn't have shopping cart code. I assumed the other payment plugins are similarly lean.
3 years ago
petemitch wrote:
You have the CustomerId in the ProcessPaymentRequest so you can get them from the customer's cart. See how it's done in the PayPalDirect plugin:

var customer = _customerService.GetCustomerById(processPaymentRequest.CustomerId);
if (customer == null)
    throw new Exception("Customer cannot be loaded");

//get current shopping cart
var shoppingCart = customer.ShoppingCartItems
    .Where(shoppingCartItem => shoppingCartItem.ShoppingCartType == ShoppingCartType.ShoppingCart)
    .LimitPerStore(processPaymentRequest.StoreId).ToList();

//items
var items = GetItems(shoppingCart, customer, processPaymentRequest.StoreId, currency.CurrencyCode);

The GetItems function is defined in the plugin so you'd need to implement that in whatever way makes sense for you're doing.


The ShoppingCartItems property is not available in v4.40
3 years ago
danushka-vp wrote:
The ShoppingCartItems property is not available in v4.40


For 4.40, you'd use this:


//Shopping Cart
var shoppingCart = (await _shoppingCartService
                    .GetShoppingCartAsync(await _workContext.GetCurrentCustomerAsync(), Core.Domain.Orders.ShoppingCartType.ShoppingCart, (await _storeContext.GetCurrentStoreAsync()).Id))
                    .ToList();

//Tax
var taxTotal = Math.Round((await _orderTotalCalculationService.GetTaxTotalAsync(shoppingCart, false)).taxTotal, 2);

//Shipping Total
var shippingTotal = Math.Round(await _orderTotalCalculationService.GetShoppingCartShippingTotalAsync(shoppingCart) ?? decimal.Zero, 2);

//Order Total
var orderTotal = Math.Round((await _orderTotalCalculationService.GetShoppingCartTotalAsync(shoppingCart, usePaymentMethodAdditionalFee: false)).shoppingCartTotal ?? decimal.Zero, 2);
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.