Skip to content

Instantly share code, notes, and snippets.

@Sidsector9
Last active May 16, 2023 17:31
Show Gist options
  • Select an option

  • Save Sidsector9/b9d2e8b1a40533a05643140c5765fc11 to your computer and use it in GitHub Desktop.

Select an option

Save Sidsector9/b9d2e8b1a40533a05643140c5765fc11 to your computer and use it in GitHub Desktop.
<?php
/**
* Gift card balance: : $5
* Order total before applying gift card : $30
* Order total after applying gift card : $25 (to be paid using Credit card.)
*/
// Create an order for $25
$base_price_money = new \Square\Models\Money();
$base_price_money->setAmount(2500);
$base_price_money->setCurrency('USD');
$order_line_item = new \Square\Models\OrderLineItem('1');
$order_line_item->setName('25 dollar total');
$order_line_item->setItemType('ITEM');
$order_line_item->setBasePriceMoney($base_price_money);
$line_items = [$order_line_item];
$order = new \Square\Models\Order( $location_id );
$order->setLineItems($line_items);
$body = new \Square\Models\CreateOrderRequest();
$body->setOrder($order);
$body->setIdempotencyKey( uniqid() );
$api_response = $client->getOrdersApi()->createOrder($body);
if ($api_response->isSuccess()) {
$result = $api_response->getResult();
} else {
$errors = $api_response->getErrors();
}
$order_id = $result->getOrder()->getId();
$order_version = $result->getOrder()->getVersion();
// Create payment $5 - Gift card
$amount_money = new \Square\Models\Money();
$amount_money->setAmount(500);
$amount_money->setCurrency('USD');
$body = new \Square\Models\CreatePaymentRequest('cnon:gift-card-nonce-ok', uniqid(), $amount_money);
$body->setAutocomplete(false);
$body->setOrderId( $order_id );
$body->setLocationId( $location_id );
$body->setAcceptPartialAuthorization(true);
$api_response = $client->getPaymentsApi()->createPayment($body);
if ($api_response->isSuccess()) {
$result = $api_response->getResult();
} else {
$errors = $api_response->getErrors();
}
$gift_card_payment_id = $result->getPayment()->getId();
// Create payment $25 - Credit card
$amount_money = new \Square\Models\Money();
$amount_money->setAmount(2500);
$amount_money->setCurrency('USD');
$body = new \Square\Models\CreatePaymentRequest('cnon:card-nonce-ok', uniqid(), $amount_money);
$body->setAutocomplete(false);
$body->setOrderId( $order_id );
$body->setLocationId( $location_id );
$body->setAcceptPartialAuthorization(true);
$api_response = $client->getPaymentsApi()->createPayment($body);
if ($api_response->isSuccess()) {
$result = $api_response->getResult();
} else {
$errors = $api_response->getErrors();
}
$credit_card_payment_id = $result->getPayment()->getId();
// Completing order.
$payment_ids = [ $gift_card_payment_id, $credit_card_payment_id ];
$body = new \Square\Models\PayOrderRequest( uniqid() );
$body->setPaymentIds($payment_ids);
$api_response = $client->getOrdersApi()->payOrder( $order_id, $body);
if ($api_response->isSuccess()) {
$result = $api_response->getResult();
} else {
$errors = $api_response->getErrors();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment