Hello,
In my code I want to link an order_id to a payment, but the order is created only if the payment is successful.
Is there a way to do an update request on an already existing payment ?
I thought about setting autocomplete to false, creating the order, linking the order_id to the payment, then process the payment.
Code example below :
process_payment.php
→ file where I check if the client can afford the order
//Step 1 : create a payment
$create_payment_request = new CreatePaymentRequest($token, uniqid(), $money);
$create_payment_request->setAutocomplete(false);
$_SESSION['square_payment_request'] = $create_payment_request; // to use on process_order.php
$response = $payments_api->createPayment($create_payment_request);
//Step 2: Check if payment success
if($response->isSuccess()){
$_SESSION['square_payment_id'] = $response->getResult()->getPayment()->getId();
echo json_encode($response->getResult());
}
process_order.php
→ goal of this file would be to set the order_id AND complete the payment on the client’s card
//i have an Ajax query that loads this file if process_payment has worked.
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
//error_log('Received a non-POST request');
echo 'Request not allowed';
return;
}
$payment_request = $_SESSION['square_payment_request'];
$square_order_id = $_SESSION['square_order_id']; // generated in another file
$client = $_SESSION['square_client'];
$payment_request->setOrderId($square_order_id); //**This line is the most important**
$body = new \Square\Models\CompletePaymentRequest();
$api_response = $client->getPaymentsApi()->completePayment($_SESSION['square_payment_id'],$body);
But it doesn’t seem to work.
Is there a way how I could set the order_id on the payment after I created the payment but BEFORE the payment is completed?
This is very important to me because I can’t see the client’s ordered items on the square transaction.
Thank you for your help.