Set order_id before completing payment

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.

Orders can only be added to payments at the time of creation. If no order_id is passed in at the time of payment Square will automatically generate an order for the payment. :slightly_smiling_face:

Would it be a bad practice to create orders before the payment ?
I don’t want to create an order if no payment is made.

That’s our recommended flow. With our Orders API you can first CalculateOrder which enables applications to preview prices without creating an order. Then you can CreateOrder with all the calculated order. :slightly_smiling_face:

Okay, thanks.

Wouldn’t it be an issue for let’s say an online store where clients are clicking checkout, but not putting their credit card info, with the recommended flow

Recommended flow:
1- Calculate Order
2- Create Order (even if client has no intention of paying/buying the order)
3- Create a payment with said Order id from #2

I think that would create a whole lot of orders which are not valid, not paid and that the data is there even though we don’t want to process the unpaid order

Should we consider every unpaid order as an abandoned cart ?

Unfortunately we buyer’s will abandon carts and orders will end up in an unpaid state. This is part of eCommerce. How you choose to interpret the order is entirely up to you and your business use case. For example you could consider a cart abandoned after 24 or 48 hours. :slightly_smiling_face:

1 Like