Last active
June 19, 2023 03:06
-
-
Save MCKLtech/0551dc9d6f86d7921d67665b8e1d0e5c to your computer and use it in GitHub Desktop.
This gist gives a high level overview of how to create an order in Square using the PHP SDK. For an order to show in the Dashboard you must create, fulfill AND pay for the order.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$body = new \Square\Models\CreateOrderRequest; | |
//Add 5 of the same line items | |
//We are creating them dynamically | |
$qty = 5; | |
$lineItem = new \Square\Models\OrderLineItem($qty); | |
$lineItem->setName('Latte'); | |
$money = new \Square\Models\Money; | |
$money->setCurrency('CAD'); | |
//Amount in Cents e.g. $5.50 | |
$money->setAmount(550); | |
$lineItem->setBasePriceMoney($money); | |
//You MUST set a location ID | |
$order = new \Square\Models\Order($locationId->id); | |
$order->setCustomerId($customer->id); | |
$order->setLineItems([$lineItem]); | |
//Set Fullfillment | |
//You MUST set fulfillment! | |
$fullfillment = new \Square\Models\OrderFulfillment; | |
$fullfillment->setType('SHIPMENT'); | |
$fullfillment->setState('PROPOSED'); | |
//Add some demo details, they are not verified by Square | |
$pickupDetails = new \Square\Models\OrderFulfillmentShipmentDetails; | |
$pickupDetails->setShippingType('Express'); | |
$pickupDetails->setCarrier('UPS'); | |
//You must set the Shipment receiver with a valid Square customer | |
$recipient = new \Square\Models\OrderFulfillmentRecipient; | |
$recipient->setCustomerId($customer->id); | |
$pickupDetails->setRecipient($recipient); | |
$fullfillment->setShipmentDetails($pickupDetails); | |
$order->setFulfillments([$fullfillment]); | |
//Ready | |
$body->setOrder($order); | |
//Very important, you must set a unique key | |
$body->setIdempotencyKey(uniqid()); | |
$ordersApi = $this->square->getOrdersApi(); | |
$apiResponse = $ordersApi->createOrder($locationId->id, $body); | |
if ($apiResponse->isSuccess()) { | |
$order = $apiResponse->getResult(); | |
$order = $order->getOrder(); | |
error_log('Square Order Creation::' . print_r($order, true)); | |
//Now, we MUST Pay for the Order | |
//In the sandbox, you can use this demo source. | |
$body_sourceId = 'ccof:customer-card-id-ok'; | |
$body_idempotencyKey = uniqid(); | |
$body_amountMoney = new \Square\Models\Money; | |
$body_amountMoney->setAmount($order->getTotalMoney()->getAmount()); | |
$body_amountMoney->setCurrency(\Square\Models\Currency::CAD); | |
$body = new \Square\Models\CreatePaymentRequest( | |
$body_sourceId, | |
$body_idempotencyKey, | |
$body_amountMoney | |
); | |
$body->setAutocomplete(true); | |
$body->setCustomerId($customer->id); | |
$body->setLocationId($locationId->id); | |
$body->setOrderId($order->getId()); | |
$paymentsApi = $this->square->getPaymentsApi(); | |
$apiResponse = $paymentsApi->createPayment($body); | |
error_log('Payment Response::' . print_r($apiResponse, true)); | |
} else { | |
$errors = $apiResponse->getErrors(); | |
error_log('Errors for Square Order Creation::' . print_r($errors, true)); | |
} | |
//End Order Creation |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment