Skip to content
Nikola Gavrić edited this page Oct 23, 2019 · 5 revisions

Examples of charging, creating and saving customers

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

$amount = 5000; //Is in USD currency and is in smallest denomination (cents). ($amount = 5000 == 50 Dollars)
$formNonce = 'some nonce'; //nonce reference => https://docs.connect.squareup.com/articles/adding-payment-form
$location_id = 'some location id'; //$location_id is id of a location from Square
$currency = 'USD'; //available currencies => https://docs.connect.squareup.com/api/connect/v2/?q=currency#type-currency
$note = 'Note: This payment has been paid in full';
$options = [
  'amount' => $amount,
  'source_id' => $formNonce,
  'location_id' => $location_id,
  'currency' => $currency,
  'note' => $note
];

$customer = array(
  'first_name' => 'John',
  'last_name' => 'Doe',
  'company_name' => 'ABC Company', //Optional
  'nickname' => 'Johny boy', //Optional
  'email' => 'john.doe@gmail.com',
  'phone' => '+389651989221', //Optional
  'note' => 'Great guy',
);

//$merchant is instance of User

Create and save customers

Facade approach
//Create and save a customer
Square::setMerchant($merchant)->setCustomer($customer)->save();
Trait approach
//Let's assume you pulled User model from database and stored it inside $merchant
...
//Create and save a customer
$merchant->saveCustomer($customer);

Charge

Facade approach
//Simple charge
Square::charge($options);

//Charging with different currency
$currency = 'RSD';
$options['currency'] = $currency;
Square::charge($options);
//IMPORTANT NOTE: Your location might not allow some currencies
//example: when your location is in USA you can't use RSD currency
//This is a restriction from Square.

//Charging with reference to another object/resource
$image = 'my-image.jpg'; // ex. Name of image customer is buying
// or
$image = 25; // Id of image customer is buying from database
$reference_id = $image;
$options['reference_id'] = $reference_id;
Square::charge($options);

//Create a customer, save it and charge him
Square::setMerchant($merchant)->setCustomer($customer)->charge($options);
Trait approach
// Default test data
$options = [
  'note' => 'Customer bought a book',
  'reference_id' => '5'
];

//Simple charge
$merchant->charge($amount, $formNonce, $location_id);

//Simple charge with additional options
$merchant->charge($amount, $formNonce, $location_id, $options);

//Creating and charging a customer in one without options
$merchant->charge($amount, $formNonce, $location_id, [], $customer);

//Creating and charging a customer in one with options
$merchant->charge($amount, $formNonce, $location_id, $options, $customer);

//Charging with different currency
$currency = 'RSD';
$merchant->charge($amount, $formNonce, $location_id, [], $customer, $currency);
//IMPORTANT NOTE: Your location might not allow some currencies
//example: when your location is in USA you can't use RSD currency
//This is a restriction from Square.

User (merchant) relationships and other functions inside HasCustomers trait

Check if merchant has a customer already
//$customer can be either false or \Nikolag\Square\Models\Customer
$customer = $merchant->hasCustomer('john.doe@gmail.com');
Retrieve transactions by status
//$passed is Illuminate\Database\Eloquent\Collection of Nikolag\Square\Models\Transaction
$passed = $merchant->passedTransactions;

//$failed is Illuminate\Database\Eloquent\Collection of Nikolag\Square\Models\Transaction
$failed = $merchant->failedTransactions;

//$opened is Illuminate\Database\Eloquent\Collection of Nikolag\Square\Models\Transaction
$opened = $merchant->openedTransactions
Retrieve all transactions
//$transactions is Illuminate\Database\Eloquent\Collection of Nikolag\Square\Models\Transaction
$transactions = $merchant->transactions;
Retrieve all customers
//$customers is Illuminate\Database\Eloquent\Collection of Nikolag\Square\Models\Customer
$customers = $merchant->customers;

Clone this wiki locally