SquareReaderSDK Android webhook

So we are taking in-person payments using SquareReaderSDK on both IOS and Android devices. After the payment we receive a payment.created webhook. In order to get the note field we take the order_id found in the payment webhook and then send a request to receive the order details in php as follows:

$order_response = $client->getOrdersApi()->retrieveOrder($orderId);

So if the payment was taken using an IOS device the process works perfectly and the full order details are received and the note field is stored in the database. However if the payment was taken using an Android device the exact same code in the php backend to obtain the order details

$order_response = $client->getOrdersApi()->retrieveOrder($orderId);

results in the following error message.

[2022-01-11 13:02:07] development.ERROR: [
{
“category”: “INVALID_REQUEST_ERROR”,
“code”: “NOT_FOUND”,
“detail”: “Order not found for id 1pLZAmBObFNAZERFdgEXNFweV”
}
]

and

[2022-01-11 12:36:05] development.ERROR: [
{
“category”: “INVALID_REQUEST_ERROR”,
“code”: “NOT_FOUND”,
“detail”: “Order not found for id FZJEb81DESN2JKLue0qD2z2eV”
}
]
I have checked and found that the payments have been processed correctly and appear in our account. If I use the API Explorer and enter the same order_id it retrieves the full order details correctly.

Application Id: sq0idp-xikX7cnH-tSB59hpB-AoSA

Any idea why we cannot receive the order details from the order_id if the payment was processed using an Android device?

:wave: I do see the errors in the logs. Not sure why this is happening. If you retry the request after getting this error does it succeed?

Yes. It does succeed when retrying later for both order numbers tested above.

However the problem is still happening today.

[2022-01-12 12:29:53] development.ERROR: [
{
“category”: “INVALID_REQUEST_ERROR”,
“code”: “NOT_FOUND”,
“detail”: “Order not found for id xpOestyYG7HbOn4cFqF9efpeV”
}
]

I take it you are calling the API as soon as you are getting the order_id? This is due to some asynchronous operations that is happening within Square. Id recommend implementing a retry method when you get this error. :slightly_smiling_face:

Ok. Thanks for this. I will try to implement the retry method. Wouldn’t it make sense to send out the note field in the initial payment.created webhook to negate the need for this?

So now I have implemented the retry method for retrieving the order details.

This has raised another issue with retrieving the order details. If the payment is unsuccessful due to insufficient funds for example an order_id will still be created and sent out in the payment.created webhook but the order details will never be retrieved and an error message will be received “order not found” as above continuously. So it is necessary to deal with this situation otherwise a continuous loop will be created.

So far I have found the best way to deal with the asynchronous nature of retrieving orders is to implement a 5 second delay after receiving the payment.created webhook before requesting the order details.

sleep(5);
$order_response = $client->getOrdersApi()->retrieveOrder($orderId);

So far this has resulted in no more error messages as long as the unsuccessful payment due to insufficient funds is dealt with. And does not affect the user experience. Perhaps this could help someone else.

May I still ask if it would be possible in the future to send out the note field in the initial payment.created webhook rather needing to make an extra request to retrieve the order?

Thanks for sharing your solution with the community @alipete, much appreciated!

I don’t have a public roadmap I can share regarding what may be included in the future for webhooks, but I’ll definitely make a note for the team that our developers are interested in getting more information with fewer API calls.