wallee-payment / magento-2

Wallee integration for Magento 2.3 and later
https://wallee.com
Apache License 2.0
3 stars 3 forks source link

Different results for identical transactions #21

Closed Leone closed 1 year ago

Leone commented 1 year ago

Hi, We have a problem in Magento because Wallee sets different statuses for identical orders.

Preconditions:

Steps to reproduce:

Expectations:

Actual results:

I debugged this in Magento and found the reason (on Magento side):

When a customer creates an order, it has "pending_payment" status and state in Magento. Order is correctly transferred to Wallee and processed there. Walle sends a request to Magento - json similar to this

{
  "eventId": 180000000,
  "entityId": 95000000,
  "listenerEntityId": 1472000000000,
  "listenerEntityTechnicalName": "Transaction",
  "spaceId": 20000,
  "webhookListenerId": 200000,
  "timestamp": "2023-02-08T13:37:03+0000"
}

The request goes to \Wallee\Payment\Controller\Webhook\Index::execute() method, it is processed and because "listenerEntityTechnicalName" is "Transaction", \Wallee\Payment\Model\Webhook\Listener\TransactionListener class is used to process the request further.

First, \Wallee\Payment\Model\Webhook\Listener\TransactionListener::loadEntity($request) method is executed to load the entity from Wallee system (because in the first request, we had only entityId). Next, \Wallee\Payment\Model\Webhook\Listener\TransactionListener::process($entity, $order) is executed. In this method, script compares entity and transactionInfo statuses and if they are different, execute the parent method.

if ($transactionInfo->getState() != $entity->getState()) {
    parent::process($entity, $order);
}

In the parent method (\Wallee\Payment\Model\Webhook\Listener\AbstractOrderRelatedListener::process($entity, $order)) Magento checks entityState, gets the appropriate command and executes it.

$this->commandPool->get(\strtolower($entity->getState()))->execute($entity, $order);

If $entity->getState() is AUTHORIZED everything works correctly, order status is changed to processing_wallee. But if getStatus() method returns anything else, \Wallee\Payment\Model\Webhook\Listener\Transaction\AuthorizedCommand isn't executed and the status of the order in Magento isn't changed.

We did some tests in Magento and we noticed that with identical orders, entityState can be different. We added logger in \Wallee\Payment\Model\Webhook\Listener\TransactionListener::process() method and saved $transactionInfo->getState() and $entity->getState(). Results:

TransactionListener::process() - transactionInfoState: PROCESSING, entityState: COMPLETED, orderIncrementId: 100429926
TransactionListener::process() - transactionInfoState: COMPLETED, entityState: COMPLETED, orderIncrementId: 100429926

TransactionListener::process() - transactionInfoState: PROCESSING, entityState: AUTHORIZED, orderIncrementId: 100429927
TransactionListener::process() - transactionInfoState: AUTHORIZED, entityState: COMPLETED, orderIncrementId: 100429927

Both orders (100429926 and 100429927) were created by the same user, the same product was used. The second order was created less than a minute after the first one. In the first order, entityState is COMPLETED so order state in Magento was pending_payment. However, in the second order we had entityState: AUTHORIZED, so AuthorizedCommand was executed and order had state = processing.

Probably, the first order was processed really fast in Wallee (and had COMPLETED state) and the other was still being processed. But this is just a guess, as I don't have access to the Wallee panel.

theodorspringwallee commented 1 year ago

Dear Leone

Many thanks for bringing up this issue, including this detailed technical analysis.

I forwarded your analysis already to our developers.

If I may ask, I have some questions on this.

About:

Both orders have the same data in Wallee panel

Do you mean with this, when the mentioned issue appears, these two separate orders has the same linked wallee transaction?

About the general issue:

Is that correct?

And for those two magento orders with the different states, are they then persisting on these states or are they changing later to their correct (same) states after a while (e.g., through further webhooks)?

Since it seems to be a complex topic, would it be possible for you to provide us a reproduction video?

In the meanwhile we will check on our side, if we can reproduce this issue on our showcase.

I also tried to visualize your analysis: Please feel free to correct me, if I forgot something here. image

Thanks for your answers.

Best regards

Leone commented 1 year ago

Dear @theodorspringwallee Thank you for the answer and for forwarding the information to the developers.

Do you mean with this, when the mentioned issue appears, these two separate orders has the same linked wallee transaction?

No. I meant that they were processed in the same way. They have the same product, total amount, customer data - which is correct. Also, they have the same states/comments in the "Transaction Information" section - the same, which means in both statuses are in the same order:

1. The processing of the transaction was started.
2. The attempt to authorize the transaction succeeded.
3. The amount of CHF40.50 was authorized.
4. The amount of CHF40.50 was completed.
5. The delivery indication is pending.

Of course, transaction id, invoice id are different. And separate requests were sent to Magento for them.

You assume that there is a timing issue (e.g., when one order is faster in the completion, the sent webhook contains another transaction state, what ends then in a different magento 2 order state result?

Yes, exactly. However, I could only check this from Magento side, I don't know how exactly the logic works on the Wallee side, so this is just an assumption that may turn out to be wrong.

And for those two magento orders with the different states, are they then persisting on these states or are they changing later to their correct (same) states after a while (e.g., through further webhooks)?

States do not change unless the customer pays the invoice. So we have two identical orders (the same products, customer data, etc but different order/transaction/invoice ids), one with pending_payment and one with processing_wallee statuses and when the customer pays, they correctly change the state to processing.

Since it seems to be a complex topic, would it be possible for you to provide us a reproduction video?

Unfortunately not. This error does not happen, for example, in every second order. Sometimes it is one order out of 3, sometimes out of 5, and sometimes there are two of them in a row. Besides, some of the data can be seen in the Magento panel, some on the Wallee side, and some in the console directly on the server with the Magento instance. Preparing a video with all this information would require a lot of time, and I think that the results wouldn't be more useful than describing them here.

Thank you for the graph, it looks great :) Everything looks correct on it. And for clarification - all this logic works correctly. I have described the whole path to more easily show where the problem is. It could also be shortened to a simple message

AuthorizedCommand isn't executed because Entity loaded from Wallee has a different state than AUTHORIZED

However, I think it is better to send more information.

theodorspringwallee commented 1 year ago

Dear Leone

Thank you for your answers.

I did now about 15 orders in our Magento 2 showcase (https://magento-2.showcase-wallee.com/) with PostFinance ISR Bank Transfer in immediate completion mode.

After this, I was able to reproduce and received one order with "Hold Delivery" (processing_wallee) instead of the correct "Pending Payment" (pending_payment) status.

I've created for this a reproduction video for this and forwarded it to our developers.

We will let you know, when we made progress on this.

Best regards

sprankhub commented 1 year ago

Any news here, @theodorspringwallee? We might have the same issue.

theodorspringwallee commented 1 year ago

Hi sprankhub, our developers have now a possible fix for it, which we are testing currently.

sprankhub commented 1 year ago

But I guess the fix is not part of 1.3.20 yet? Any update?

theodorspringwallee commented 1 year ago

Hi sprankhub

This fix should be released tomorrow.

Best regards

sprankhub commented 1 year ago

Any update?

theodorspringwallee commented 1 year ago

Dear sprankhub

Yes, it should be fixed now (in v1.3.21) and downloadable here: https://github.com/wallee-payment/magento-2/releases

Best regards