mollie / Shopware6

47 stars 52 forks source link

The customer id is invalid. #802

Closed carstenjaksch closed 1 week ago

carstenjaksch commented 1 month ago

I activated Mollie on one of our production sales channels with live API key and got an error after a test order (registered user).

Logs:

[ERROR] Error when starting Mollie payment: Could not create Mollie order {"function":"order-prepare","exception":"[object] (RuntimeException(code: 422): Could not create Mollie order at vendor/store.shopware.com/molliepayments/src/Service/MollieApi/Order.php:209)\n[previous exception] [object] (Mollie\\Api\\Exceptions\\ApiException(code: 422): [2024-08-07T12:58:01+0000] Error executing API call (422: Unprocessable Entity): The customer id is invalid. Documentation: https://docs.mollie.com/reference/v2/orders-api/create-order. Field: payment.customerId at vendor/store.shopware.com/molliepayments/src/Service/MollieApi/Client/MollieHttpClient.php:150)"} {"file":"/home/shokwxbk/www.shops.art2.media/releases/12/vendor/store.shopware.com/molliepayments/src/Handler/PaymentHandler.php","line":127,"class":"Kiener\\MolliePayments\\Handler\\PaymentHandler","callType":"->","function":"pay","url":"/account/order/update/01912ce75ab071ab84251ba71d22446f","ip":"x","http_method":"POST","server":"x","referrer":"/account/order/edit/01912ce75ab071ab84251ba71d22446f"}
[CRITICAL] Could not create Mollie order {"function":"finalize-payment","exception":"[object] (Mollie\\Api\\Exceptions\\ApiException(code: 422): [2024-08-07T12:58:01+0000] Error executing API call (422: Unprocessable Entity): The customer id is invalid. Documentation: https://docs.mollie.com/reference/v2/orders-api/create-order. Field: payment.customerId at vendor/store.shopware.com/molliepayments/src/Service/MollieApi/Client/MollieHttpClient.php:150)"} {"file":"vendor/store.shopware.com/molliepayments/src/Service/MollieApi/Order.php","line":201,"class":"Kiener\\MolliePayments\\Service\\MollieApi\\Order","callType":"->","function":"createOrder","url":"/account/order/update/01912ce75ab071ab84251ba71d22446f","ip":"x","http_method":"POST","server":"x","referrer":"/account/order/edit/01912ce75ab071ab84251ba71d22446f"}
[WARNING] No customer found for the current mollie id and sales channel combination, creating a new one. {"salesChannel":"019049e2b26c73f4ab60b8f0571b9cc4","mollieCustomerId":"cst_VAPnXu7SQu"} {"file":"vendor/store.shopware.com/molliepayments/src/Service/CustomerService.php","line":571,"class":"Kiener\\MolliePayments\\Service\\CustomerService","callType":"->","function":"createMollieCustomer","url":"/account/order/update/01912ce75ab071ab84251ba71d22446f","ip":"x","http_method":"POST","server":"x","referrer":"/account/order/edit/01912ce75ab071ab84251ba71d22446f"}
[INFO] Starting Checkout for order B100014 with payment: paypal {"saleschannel":"x","cart":{"amount":76.84}} {"file":"vendor/store.shopware.com/molliepayments/src/Handler/PaymentHandler.php","line":105,"class":"Kiener\\MolliePayments\\Handler\\PaymentHandler","callType":"->","function":"pay","url":"/account/order/update/01912ce75ab071ab84251ba71d22446f","ip":"x","http_method":"POST","server":"x","referrer":"/account/order/edit/01912ce75ab071ab84251ba71d22446f"}

Shopware: 6.6.4.1 Mollie: 4.9.3 API keys test shows green.

The Mollie dashboard shows nothing: No order, no payment attempt.

Edit: I found that the change logs of 3.1.0 mention a rare error where "Customer ID is invalid when creating and order". Maybe the error is not completely fixed.

carstenjaksch commented 1 month ago

I found the PR that should add customer creation if no one is found. But ... it only adds the log output and no logic to actually create a customer?

Interestingly, I tried the same order payment two times. The first try, "mollieCustomerId" was empty. The second try (from accounts page > finish payment), same warning about no customer found, but with an id. The logs from above are the second try.

It seems the first time the customer was created.

Edit: Now it gets really strange. We have another Mollie account. I found the customer id of my second payment attempt there in the test mode. The creation date is from my attempt today!

I double checked API keys and test mode switch. The sales channel is definitely configured for the new Mollie account in live mode.

How can the logs return the customer id of another Mollie account (and from another mode)?

carstenjaksch commented 1 month ago

I tried reinstalling the plugin, resetting the API keys and disabling the option to create customers in Mollie.

Nothing worked so far.

fjbender commented 1 month ago

I think we have a strict 1-1 relation between a Mollie cst_ ID and a Shopware customer record. When you have a Mollie customer entity associated with a Shopware customer and then switch API Keys (test/live) or even accounts, the Mollie customer ID remains associated with the Shopware customer. Would this explain your situation?

E.g. you have Shopware customer 1, and do a test payment with Mollie Account A, resulting in the Mollie customer cst_deadbeef, the association remains, even if you switch to Mollie Account B (which then won't work).

I would assume though that turning off "Create customers in Mollie" would fix this situation in most situations (except maybe for recurring products where a Mollie customer is required).

carstenjaksch commented 1 month ago

Thank your for the answer @fjbender!

The Mollie customer in the other account was created after my tests yesterday. So there was no existing record.

And I tried disabling the option to create customers in Mollie. Doesn't work either.

I created the profile in the old account, deleted it and created it again in the new account (internal communication issues). The Mollie plugin in Shopware was configured for the old account. But all of that should not matter, because the API is from the new account profile and I reinstalled the plugin today – deleting all data. So there should be no orphan data left, if any.

Also, I tried another profile API key from the new account and reset all keys. Nothing works so far ...

I now deleted the customer whose id gets returned by the plugin. But the plugin returns the same id and no customer is created. Another issue is that the new account is still in onboarding mode: No orders, no customers. If I try to get to the returned customer id in the new Mollie account:

https://my.mollie.com/dashboard/org_xxxxxxxx/customers/cst_BMcsW9wovj

The dashboard redirects me to the onboarding screen.

Edit: I see now that the customer id has changed. But the issue persists.

And the error has changed: "The customer id is no longer available." What the hell is happening here ....

So I tried a new order in an incognito tab with a new email address. That seems to work. But there is a critical bug in the customer management I think. I will leave this open for now.

fjbender commented 1 month ago

I can see that that particular customer has been deleted today, for some reason. It's all a bit mysterious.

Can you join our Discord and raise the issue there? I'd like to look at this in a bit more detail and that's easier in a chat :)

carstenjaksch commented 1 month ago

@fjbender Discord doesn't let me DM you. I have sent a friend request.

BlackScorp commented 1 week ago

@carstenjaksch can you check out this code?

https://github.com/mollie/Shopware6/commit/894c1fc16fb71bc94caa6163442a773213389883

it seems like we are using the saleschannelid from customer to find out the related api key. maybe tihs solves your problem?

carstenjaksch commented 1 week ago

Thank you for the update @BlackScorp. I don't know. Currently we have no issues. I will let you know if anything regarding this issue happens again. Closing this for now.