|
@@ -24,8 +24,7 @@ class SquarepayExtension extends SimpleExtension
|
|
|
|
|
|
protected function registerServices(Application $app)
|
|
|
{
|
|
|
- $app['square.apiClient'] = $app->share(function($app) {
|
|
|
-
|
|
|
+ $app['square.apiClient'] = $app->share(function($app) {
|
|
|
// Create and configure a new Square API client using the OAuth token
|
|
|
$manager = $app['filesystem'];
|
|
|
if (!$manager->hasFilesystem('extensions_config')) {
|
|
@@ -45,6 +44,17 @@ class SquarepayExtension extends SimpleExtension
|
|
|
return new \SquareConnect\ApiClient($apiConfig);
|
|
|
});
|
|
|
|
|
|
+ $config = $this->getConfig();
|
|
|
+ if ($config['testmode']) {
|
|
|
+ $app['square.apiSandboxClient'] = $app->share(function($app) use ($config) {
|
|
|
+ // Create and configure a new Square API client using the OAuth token
|
|
|
+ $apiConfig = new \SquareConnect\Configuration();
|
|
|
+ $apiConfig->setAccessToken($config['sq_sandbox_token']);
|
|
|
+ $apiClient = new \SquareConnect\ApiClient($apiConfig);
|
|
|
+ return new \SquareConnect\ApiClient($apiConfig);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
$app['twig.runtime.square'] = $app->share(function ($app) {
|
|
|
return new Twig\SquareTwigRuntime(
|
|
|
$app['square.apiClient'],
|
|
@@ -121,6 +131,7 @@ class SquarepayExtension extends SimpleExtension
|
|
|
$sku = $request->request->get('sku');
|
|
|
$name = $request->request->get('name');
|
|
|
$price = $request->request->get('price');
|
|
|
+ $squid = $request->request->get('squid'); // Square Unique Id (for CatalogItemVariation)
|
|
|
$key = substr(md5($sku), 0, 10);
|
|
|
if (isset($basket[$key])) {
|
|
|
$basket[$key]['quantity']++;
|
|
@@ -131,7 +142,8 @@ class SquarepayExtension extends SimpleExtension
|
|
|
'name' => $name,
|
|
|
'price' => $price,
|
|
|
'quantity' => 1,
|
|
|
- 'total' => $price
|
|
|
+ 'total' => $price,
|
|
|
+ 'squid' => $squid
|
|
|
];
|
|
|
}
|
|
|
$app['session']->set('basket', $basket);
|
|
@@ -171,15 +183,101 @@ class SquarepayExtension extends SimpleExtension
|
|
|
|
|
|
public function basketClear(Application $app)
|
|
|
{
|
|
|
- $app['session']->remove('basket');
|
|
|
+ $app['session']->set('basket', []);
|
|
|
return new RedirectResponse('/basket');
|
|
|
}
|
|
|
|
|
|
// -----------------------------------------------------------------------------------------------------------------
|
|
|
// SQUARE PAYMENT PROCESSING
|
|
|
|
|
|
- public function processSquarePaymentResponse() {
|
|
|
- // TODO
|
|
|
+ public function processSquarePaymentResponse(Application $app, Request $request) {
|
|
|
+
|
|
|
+ // Grab the card nonce, name and email
|
|
|
+ $cardNonce = $request->request->get('nonce');
|
|
|
+ $name = $request->request->get('name');
|
|
|
+ $email = $request->request->get('email');
|
|
|
+
|
|
|
+ $config = $this->getConfig();
|
|
|
+ $testmode = $config['testmode'];
|
|
|
+ $locationId = $testmode ? $config['sq_sandbox_location_id'] : $config['sq_location_id'];
|
|
|
+ $apiClient = $testmode ? $app['square.apiSandboxClient'] : $app['square.apiClient'];
|
|
|
+
|
|
|
+ // Create the order
|
|
|
+ // NOTE: The Orders API has sandbox support for ad-hoc items only. There is no sandbox support for orders built from Catalog items.
|
|
|
+ $orderId = null;
|
|
|
+ $basket = $app['session']->get('basket');
|
|
|
+ $lineItems = [];
|
|
|
+ foreach($basket as $line) {
|
|
|
+ $sqitem = new \SquareConnect\Model\CreateOrderRequestLineItem;
|
|
|
+ if ($config['testmode']) {
|
|
|
+ $sqitem->setName($line['name']);
|
|
|
+ } else {
|
|
|
+ $sqitem->setCatalogObjectId($line['squid']);
|
|
|
+ }
|
|
|
+ $sqitem->setQuantity((string) $line['quantity']);
|
|
|
+ $price = new \SquareConnect\Model\Money;
|
|
|
+ $price->setAmount((int) $line['price']);
|
|
|
+ $price->setCurrency('GBP');
|
|
|
+ $sqitem->setBasePriceMoney($price);
|
|
|
+ array_push($lineItems, $sqitem);
|
|
|
+ }
|
|
|
+ $request = new \SquareConnect\Model\CreateOrderRequest();
|
|
|
+ $request->setIdempotencyKey(uniqid()); // uniqid() generates a random string
|
|
|
+ $request->setLineItems($lineItems);
|
|
|
+ try {
|
|
|
+ $ordersApi = new \SquareConnect\Api\OrdersApi($apiClient);
|
|
|
+ $response = $ordersApi->createOrder($locationId, $request);
|
|
|
+ dump($response);
|
|
|
+ }
|
|
|
+ catch (Exception $e) {
|
|
|
+ // TODO: Add better error handling
|
|
|
+ echo '<pre>';
|
|
|
+ echo 'Exception when calling OrdersApi->createOrder:', $e->getMessage(), PHP_EOL;
|
|
|
+ echo '</pre>';
|
|
|
+ exit;
|
|
|
+ }
|
|
|
+ $orderId = $response['order']['id'];
|
|
|
+ dump($orderId);
|
|
|
+
|
|
|
+ // Build transaction request
|
|
|
+ $buyerInfo = [
|
|
|
+ 'buyer_email_address' => $email
|
|
|
+ ];
|
|
|
+
|
|
|
+ $idempotencyKey = uniqid();
|
|
|
+ $basketTotal = array_sum(array_column($basket, 'total'));
|
|
|
+ $paymentInfo = [
|
|
|
+ 'idempotency_key' => $idempotencyKey,
|
|
|
+ 'amount_money' => [
|
|
|
+ 'amount' => $basketTotal,
|
|
|
+ 'currency' => 'GBP'
|
|
|
+ ],
|
|
|
+ 'card_nonce' => $cardNonce,
|
|
|
+ ];
|
|
|
+ if ($orderId) {
|
|
|
+ $paymentInfo['order_id'] = $orderId;
|
|
|
+ }
|
|
|
+
|
|
|
+ $referenceInfo = [
|
|
|
+ 'buyer_name' => $name,
|
|
|
+ 'buyer_email_address' => $email
|
|
|
+ ];
|
|
|
+
|
|
|
+ // $chargeRequest = new \SquareConnect\Model\ChargeRequest();
|
|
|
+ // $chargeRequest->setOrderId($orderId); // Link an order ID
|
|
|
+
|
|
|
+ $tranactionRequest = array_merge($buyerInfo, $paymentInfo, $referenceInfo);
|
|
|
+ $transactionsApi = new \SquareConnect\Api\TransactionsApi($apiClient);
|
|
|
+ try {
|
|
|
+ $response = $transactionsApi->charge($locationId, $tranactionRequest);
|
|
|
+ dump($response);
|
|
|
+ } 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;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
}
|