Code Cookbook

Itemize Transactions

Itemize transactions by linking them to Order objects.

You can itemize your transactions by linking them to existing order objects which capture itemized details. Linking orders and transaction enables Square Dashboard reporting on purchase items, taxes, and discounts. If the Order object references catalog items and the catalog items have inventory tracking enabled, you can track product inventory through the Square Dashboard.

Before you start

Step 1: Reference the order ID in your transaction request

To link an order to your transaction, you need to set the order_id field in your Charge request to a valid order ID.

//Make sure you have your order ID and location ID
$orderId = "{ORDER_ID}";
$locationId = "{LOCATION_ID}";

//Make your Charge request object
$chargeRequest = new \SquareConnect\Model\ChargeRequest();

// Link an order ID
$chargeRequest->setOrderId($value) = $orderId;

For more information on building the rest of the Charge request, see the Transactions API Setup Guide.

Step 2: Complete the transaction

Call the Charge endpoint.

try {

  $apiResponse = $transactionsApi->charge($locationId, $chargeRequest);

  // For now, just print the result to the screen
  echo "<pre>" . print_r($apiResponse, true) . "</pre>";

} catch (\SquareConnect\ApiException $e) {
  echo "<hr>" .
       "The SquareConnect\Api\TransactionsApi object threw an exception.<br>" .
       "API call: <code>TransactionsApi->charge</code>" .
       "<pre>" . var_dump($e) . "</pre>";
  throw $e;
}

Contact Developer Support, join our Slack channel, or ask for help on Stack Overflow