VERSION_MISMATCH when trying to cancel invoice

I’m trying to use the API example from this URL:

I’ve successfully deployed MANY API calls to Square, but for some strange reason, this one requires “version: 0” to be sent, unlike almost every other call I’ve used.

I’m using curl in PHP to make the call:

$postfields = ‘{“version”: “0”}’;

$curl_url = “https://connect.squareup.com/v2/invoices/” . $invoice_id . “/cancel”;

$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $curl_url,
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POSTFIELDS => $postfields,
CURLOPT_HTTPHEADER => array(
‘Square-Version: 2024-12-18’,
'Authorization: Bearer ’ . $square_access_token,
‘Content-Type: application/json’
)
));

$response = curl_exec($ch);
$parsed_response = json_decode($response, true);

When that code is ran, it gives me this error message:

{“errors”:[{“code”:“VERSION_MISMATCH”,“detail”:“Version is not up to date”,“field”:“version”,“category”:“INVALID_REQUEST_ERROR”}]}

The even more bizarre thing is that it was working perfectly fine (literally running the script with a while loop of people to cancel invoices for) and all of a sudden, using the exact same code, it stopped working in the middle of the script running.

I’m guessing it’s just something stupid I’m doing in the PHP code when trying to pass the value for “version”, but I could be wrong. Any help would be greatly appreciated!

Thanks,

Andy

:wave: I’m Square Dev AI, here to help answer your question!
Based on the information you’ve provided, the following documentation will help answer your question:

Additional Documentation

Update Invoices
Invoices API
Using the REST API

If this documentation helped you find a solution, please check the box indicating that this reply solves the problem.
Otherwise one of our Developer Advocates will provide additional assistance shortly.

What’s your application ID and what’s the invoice_id your trying to cancel? When you GetInvoice what version is being returned? :slightly_smiling_face:

So, the version number that’s passed has to match what’s received in the GetInvoice call? That seems very strange…

But it seems to have worked!

That’s correct for Optimistic Concurrency. :slightly_smiling_face: