Skip to content
Nikola Gavrić edited this page Mar 26, 2020 · 5 revisions

Examples of charging, creating and saving orders

Imagine we have the following data available (don't skip this part)

//$amount is in USD currency and is in smallest denomination (cents). ($amount = 200 == 2 Dollars)
$amount = 1000;

//nonce reference => https://docs.connect.squareup.com/articles/adding-payment-form
$formNonce = 'some nonce';

//$location_id is id of a location from Square
$location_id = 'some location id';

//note about this order
$note = 'My first order made with Square';

//some kind of reference id to an object or resource
$reference_id = 'client_1_bought_file.pdf';

$options = array(
  'amount' => $amount,
  'source_id' => $formNonce,
  'location_id' => $location_id,
  'note' => $note,
  'reference_id' => $reference_id
);

$products = array(
  [
    'name' => 'Shirt',
    'variation_name' => 'Large white',
    'note' => 'This note can have maximum of 50 characters.',
    'price' => 440.99,
    'quantity' => 2,
    'reference_id' => '5' //An optional ID to associate the product with an entity ID in your own table
  ],
  [
    'name' => 'Shirt',
    'variation_name' => 'Mid-size yellow',
    'note' => 'This note can have maximum of 50 characters.',
    'quantity' => 1,
    'price' => 118.02
  ],
);

$tax = array(
  'name' => 'Shipping tax',
  'type' => 'INCLUSIVE', //https://docs.connect.squareup.com/api/connect/v2#type-taxinclusiontype
  'percentage' => 5.0,
  'reference_id' => '25' //An optional ID to associate the tax with an entity ID in your own table
);

$discount = array(
  'name' => 'Sale Mania -5 USD',
  'amount' => 500 //It is in the smallest denomination of currency provided (if currency = USD then amount = 500 cents == 5 USD)
);

$order = array(
  'products' => $products
);

Create and save an order

Facade approach
//Simple create and save
$square = Square::setOrder($order, $location_id)->save();

//Create and save with currency other than USD
$square = Square::setOrder($order, $location_id, 'RSD')->save();

//Create and save order and apply a tax to the order
$order['taxes'] = array($tax);
$square = Square::setOrder($order, $location_id)->save();

//Create and save order and apply a discount to the order
$order['discounts'] = array($discount);
$square = Square::setOrder($order, $location_id)->save();

//Create the order, add additional product and save the order
$additionalProduct = array(
  'name' => 'Shirt',
  'variation_name' => 'Large-size yellow',
  'note' => 'This note can have maximum of 50 characters.',
  'quantity' => 1,
  'price' => 100
);
$square = Square::setOrder($order, $location_id)
                  ->addProduct($additionalProduct)
                  ->save();

Charge against the order

Facade approach
//Let's assume you pulled Order model from database and stored it inside $order
...
//Simple charge
$square = Square::setOrder($order, $location_id)->charge($options);

//Add a User who is a merchant/seller to the order and charge
$square = Square::setOrder($order, $location_id)->setMerchant($merchant)->charge($options);

//Add a User who is a customer/buyer to the order and charge him
$square = Square::setOrder($order, $location_id)
                ->setMerchant($merchant)
                ->setCustomer($customer)
                ->charge($options);
Trait approach
//Let's assume you pulled Order model from database and stored it inside $order
...
//Simple charge
$order->charge($amount, $formNonce, $location_id);

//Add a User who is a merchant/seller to the order and charge
$order->charge($amount, $formNonce, $location_id, $merchant);

//Add a User who is a merchant/seller to the order and charge with options
$order->charge($amount, $formNonce, $location_id, $merchant, $options);

//Add a User who is a customer/buyer to the order and charge him without options
$order->charge($amount, $formNonce, $location_id, $merchant, [], $customer);

//Change currency to something other than USD
$order->charge($amount, $formNonce, $location_id, $merchant, [], $customer, 'RSD');

Order relationships and other functions inside HasProduct trait

Check if order contains a tax
//$tax can be either array or \Nikolag\Square\Models\Tax
$order->hasTax($tax);
Check if order contains a discount
//$discount can be either array or \Nikolag\Square\Models\Discount
$order->hasDiscount($discount);
Check if order contains a product
//$product can be either array or \Nikolag\Square\Models\Product
$order->hasProduct($product);
Retrieve all taxes for order
$order->taxes;
Retrieve all discounts for order
$order->discounts;
Retrieve all products for order
$order->products;
Clone this wiki locally