buckaroo-it / Magento2

Repository containing the Magento2 plugin by Buckaroo
MIT License
28 stars 35 forks source link

[BP-569] Afterpay (old) refunds with third party gift card gives error #48

Closed boldsidney closed 3 years ago

boldsidney commented 4 years ago

Buckaroo version v1.15.0 added support for amasty gift cards. This works great when placing orders, however when creating a credit memo we get the error image

This is due to the grand total excluding gift card value being send as reference value, which is then compared to the grand total on the order in magento which is including gift card value image

I had a look at what is send to the gateway, and it's something like this

["Currency"]=> string(3) "EUR" 
["AmountDebit"]=> int(0) 
["AmountCredit"]=> float(34.99) 
["Invoice"]=> string(13) "302029436" 
["Order"]=> string(13) "302029436" 
["Services"]=> object(stdClass)#7933 (1) { 
    ["Service"]=> array(4) { 
        ["Name"]=> string(18) "afterpaydigiaccept" 
        ["Action"]=> string(6) "Refund" 
        ["Version"]=> int(1) 
        ["RequestParameter"]=> array(6) { 
            [0]=> array(3) { 
                ["_"]=> string(15) "1 x product_name" 
                ["Name"]=> string(18) "ArticleDescription" 
                ["GroupID"]=> int(1) 
            } 
            [1]=> array(3) { 
                ["_"]=> string(5) "26931" 
                ["Name"]=> string(9) "ArticleId" 
                ["GroupID"]=> int(1) 
            } 
            [2]=> array(3) { 
                ["_"]=> int(1) 
                ["Name"]=> string(15) "ArticleQuantity" 
                ["GroupID"]=> int(1) 
            } 
            [3]=> array(3) { ["_"]=> float(40) ["Name"]=> string(16) "ArticleUnitPrice" ["GroupID"]=> int(1) } 
            [4]=> array(3) { ["_"]=> int(1) ["Name"]=> string(18) "ArticleVatCategory" ["GroupID"]=> int(1) } 
            [5]=> array(2) { ["_"]=> float(4.99) ["Name"]=> string(13) "ShippingCosts" } 
        } 
    } 
} 
["AdditionalParameters"]=> object(stdClass)#7935 (1) { 
    ["AdditionalParameter"]=> array(2) { 
        [0]=> array(2) { ["_"]=> string(6) "refund" ["Name"]=> string(27) "service_action_from_magento" } 
        [1]=> array(2) { ["_"]=> int(1) ["Name"]=> string(20) "initiated_by_magento" } 
    }
}

There is no mention of the giftcard leading to the issue that amountCredit !== (ArticleUnitPrice + ShippingCosts)

In the payment method you see in the getRequestArticlesData method that third party giftcards are added to the order https://github.com/buckaroo-it/Magento2/blob/v1.25.4-RC1/Model/Method/Afterpay.php#L740

However when looking at the getCreditmemoArticleData method, we see no such mention. It seems to me that this method should include the used giftcard for use in refund as well.

Putting the remaining value from the gift card as adjustment refund doesnt work either because then you get a Magento error stating that the total refund value exceeds the grand total.

steps to reproduce

  1. create an amasty giftcard
  2. add that giftcard to your cart
  3. place an order with afterpay old
  4. try to create a creditmemo with refund online for that order

expected result

actual result

Ideally the balance on the used giftcard would also be restored after a successful creditmemo. But perhaps that is more specific to the amasty extension and out of scope for the buckaroo integration.

We're currently running v1.24.0 on production, but I've also had a look at the more recent gifcard related changes between 1.24 and master, but those seemed to be for buckaroo giftcards like fashion cheque and had no effect for third party amasty giftcards when i tried applying those changes as patches on our test environment.

Buckaroo-Rens commented 4 years ago

Thank you for your issue. We will start researching this in our next sprint (5oct - 16 oct).

Buckaroo-Rens commented 4 years ago

@boldsidney We made a chance in our plugin. Are you able to test is? Since we do not have licence to this plugin and still awaiting response from Amasty. https://github.com/buckaroo-it/Magento2/commit/1d84a7c8c482620fa3f2351424b875d0d07639a4

Buckaroo-Rens commented 4 years ago

@boldsidney Did you do any tests allready?

boldsidney commented 4 years ago

Hi @Buckaroo-Rens ,

I've tested it on magento 2.3.5-p2 with buckaroo version 1.26.0. Unfortunately it's still not working.

Looking at the code changes, it would seem that the fix was done by making use of the discount amount data. However, the amasty giftcard doesn't work like that. Rather than working with the core magento discount fields, it just records the discount separately in its own table and directly removes the giftcard value from the grand total.

With you not having access to the extension, perhaps it's easier if you were to provide guidance on what changes should be done to the soap packet to process the refund and that we find a way to implement it ourselves?

sample order placement

      <ns1:Currency>EUR</ns1:Currency>
      <ns1:AmountDebit>79.97</ns1:AmountDebit>
      <ns1:AmountCredit>0</ns1:AmountCredit>
     <ns1:Services>
        <ns1:Service Name="afterpaydigiaccept" Action="Pay" Version="1">
          ... address data ...
          <ns1:RequestParameter Name="CustomerIPAddress">127.0.0.1</ns1:RequestParameter>
          <ns1:RequestParameter Name="Accept">true</ns1:RequestParameter>
          <ns1:RequestParameter Name="B2B">false</ns1:RequestParameter>
          <ns1:RequestParameter Name="ArticleDescription" GroupID="1">1 x Product 1</ns1:RequestParameter>
          <ns1:RequestParameter Name="ArticleId" GroupID="1">27909</ns1:RequestParameter>
          <ns1:RequestParameter Name="ArticleQuantity" GroupID="1">1</ns1:RequestParameter>
          <ns1:RequestParameter Name="ArticleUnitPrice" GroupID="1">49.99</ns1:RequestParameter>
          <ns1:RequestParameter Name="ArticleVatCategory" GroupID="1">1</ns1:RequestParameter>
          <ns1:RequestParameter Name="ArticleDescription" GroupID="2">1 x Product 2</ns1:RequestParameter>
          <ns1:RequestParameter Name="ArticleId" GroupID="2">5540</ns1:RequestParameter>
          <ns1:RequestParameter Name="ArticleQuantity" GroupID="2">1</ns1:RequestParameter>
          <ns1:RequestParameter Name="ArticleUnitPrice" GroupID="2">34.99</ns1:RequestParameter>
          <ns1:RequestParameter Name="ArticleVatCategory" GroupID="2">1</ns1:RequestParameter>
          <ns1:RequestParameter Name="ShippingCosts">4.99</ns1:RequestParameter>
          <ns1:RequestParameter Name="ArticleDescription" GroupID="3">Betaald bedrag</ns1:RequestParameter>
          <ns1:RequestParameter Name="ArticleId" GroupID="3">BETAALD</ns1:RequestParameter>
          <ns1:RequestParameter Name="ArticleQuantity" GroupID="3">1</ns1:RequestParameter>
          <ns1:RequestParameter Name="ArticleUnitPrice" GroupID="3">-10</ns1:RequestParameter>
          <ns1:RequestParameter Name="ArticleVatCategory" GroupID="3">4</ns1:RequestParameter>
        </ns1:Service>
      </ns1:Services>

following creditmemo request

      <ns1:Currency>EUR</ns1:Currency>
      <ns1:AmountDebit>0</ns1:AmountDebit>
      <ns1:AmountCredit>79.97</ns1:AmountCredit>
      <ns1:Services>
        <ns1:Service Name="afterpaydigiaccept" Action="Refund" Version="1">
          <ns1:RequestParameter Name="ArticleDescription" GroupID="1">1 x Product 1</ns1:RequestParameter>
          <ns1:RequestParameter Name="ArticleId" GroupID="1">27909</ns1:RequestParameter>
          <ns1:RequestParameter Name="ArticleQuantity" GroupID="1">1</ns1:RequestParameter>
          <ns1:RequestParameter Name="ArticleUnitPrice" GroupID="1">49.99</ns1:RequestParameter>
          <ns1:RequestParameter Name="ArticleVatCategory" GroupID="1">1</ns1:RequestParameter>
          <ns1:RequestParameter Name="ArticleDescription" GroupID="2">1 x Product 2</ns1:RequestParameter>
          <ns1:RequestParameter Name="ArticleId" GroupID="2">5540</ns1:RequestParameter>
          <ns1:RequestParameter Name="ArticleQuantity" GroupID="2">1</ns1:RequestParameter>
          <ns1:RequestParameter Name="ArticleUnitPrice" GroupID="2">34.99</ns1:RequestParameter>
          <ns1:RequestParameter Name="ArticleVatCategory" GroupID="2">1</ns1:RequestParameter>
          <ns1:RequestParameter Name="ShippingCosts">4.99</ns1:RequestParameter>
        </ns1:Service>
      </ns1:Services>

error message

     <Status>
        <Code Code="690">Rejected</Code>
        <SubCode Code="S996">An error occurred while processing the transaction: Refund rejected. The following validation errors occurred: 
TotalGrossAmount (79.97) is not equal to the sum of the articles (89.97).</SubCode>
        <DateTime>2020-10-19T13:38:09</DateTime>
      </Status>

Seeing as the payment request adds an additional discount article with a minus value, would our issue be solved if we were to do the same when doing the refund? And when doing partial refunds, I would assume that we have to split the giftcard amount over the articles like the way magento does with the discount amount. Furthermore, does it matter for Afterpay what we specify as description for the giftcard discount, or can it be anything?

Buckaroo-Rens commented 3 years ago

We made some final changes in version 1.30 which is already released.