I have a problem with my orders. If a person presses F5 too fast, an order can be sent multiple times. I want to write the order_id
into a file.
Before sending an order and check if the order_id
is already stored and, if it is, dont send the order again.
I get the order_id
, than I try to check if it's in the orders.txt
file. If found in that file redirect to "mysite". If not stored, write it to orders.txt
.
After the order_id
is written to orders.txt
send the order_id-XXXX.xml
to a URL.
The thing is: only the else
part works.
If I press F5 multiple times, it writes the order_id
to the orders.txt
file each time. It is not checking if it's already there. It also sends the order multiple times.
If I remove the inner if (file_exists($filename))else{}
part, it works just fine.
Heres my code :
$order_id= $order->get_order_number();
$filename = $_SERVER['DOCUMENT_ROOT']."/xml/order_id-".$order_id.".xml";
$_cassa_point_url='some_url';
$orders_txt = $_SERVER['DOCUMENT_ROOT']."/xml/orders.txt";
$contents = file_get_contents($orders_txt);
$pattern = preg_quote($order_id, '/');
$pattern = "/^(".$pattern.")/m";
if( strpos(file_get_contents($orders_txt),$order_id) !== false) {
header( "refresh:0;url=mysite );
}else{
$myfile = file_put_contents($orders_txt, $order_id."
" , FILE_APPEND | LOCK_EX);
if (file_exists($filename)) {
$url = $_cassa_point_url . $order_id;
file_get_contents($url);
header( "refresh:0;url=mysite);
return true;
} else {
$url = $_cassa_point_url . $order_id;
file_get_contents($url);
header( "refresh:0;url=mysite );
return true;
}
}