Klarna Docs - Indicating an invoice/refund reference in settlement reports

Indicating an invoice/refund reference in settlement reports

Summary

In this post you will learn where to hook into the module to Indicate an invoice/refund reference when sending a Klarna capture and refund reuqest in settlement reports for Klarna Checkout orders.

Why this can be useful

Magento itself does not provide by default the respective IDs before the the invoice or refund was created. But this is not the case for settlement reports (ERP). For this case you need to hook into the module and indicate the respective reference.

Steps to indicate the reference for the Klarna capture and refund API request

Checkout where to hook:

1. The Klarna Checkout module provides two methods to hook into. For the invoice its the method \Klarna\Backend\Model\Api\Ordermanagement::getInvoiceId() and for the refund its the method \Klarna\Backend\Model\Api\Ordermanagement::getRefundId(). These methods are just returning a null value since the IDs are just created after the invoice/refund was generated (vanilla Magento). For settlement reports a plugin must be created around them where the own logic will be added.

2. Create a plugin to return the ID:In the following just an example for the invoice id will be shown. This solution works similiar for the refund id. At first you need to create the plugin entry in your module A\B\etc\di.xml:

MARKUP
<type name="Klarna\Backend\Model\Api\Ordermanagement">
      <plugin name="afterGetMerchantReferences" type="A\B\Plugin\GetInvoiceIdPlugin"/>
</type>

After that you need to create the plugin class:

PHP
namespace A\B\Plugin;

class GetInvoiceIdPlugin
{

    public function afterGetInvoiceId(
        \Klarna\Backend\Model\Api\Ordermanagement $klarnaClass,
        $result,
        \Magento\Sales\Api\Data\InvoiceInterface $invoice
    ) {

3. Run the Magento cli commands so that Magento knows about this new entry.

After these steps you can now indicate the invoice/refund it when the respective Klarna capture/refund API request will be send.