Browse Source

Adding getcatitem() Twig function

Richard Knight 6 years ago
parent
commit
3883312db3
2 changed files with 23 additions and 11 deletions
  1. 11 11
      src/SquarepayExtension.php
  2. 12 0
      src/Twig/SquareTwigRuntime.php

+ 11 - 11
src/SquarepayExtension.php

@@ -112,11 +112,13 @@ class SquarepayExtension extends SimpleExtension
     }
 	
 	protected function registerTwigFunctions() {
-        $getCatalog = [Twig\SquareTwigRuntime::class, 'getCatalog'];
-        $getBasket 	= [Twig\SquareTwigRuntime::class, 'getBasket'];
-        $cardform 	= [Twig\SquareTwigRuntime::class, 'displayCCForm'];
+        $getCatalog 	= [Twig\SquareTwigRuntime::class, 'getCatalog'];
+        $getCatalogItem = [Twig\SquareTwigRuntime::class, 'getCatalogItem'];
+        $getBasket 		= [Twig\SquareTwigRuntime::class, 'getBasket'];
+        $cardform 		= [Twig\SquareTwigRuntime::class, 'displayCCForm'];
 		return [
 			'getcatalog' 	=> [ $getCatalog ],
+			'getcatitem' 	=> [ $getCatalogItem ],
 			'getbasket'		=> [ $getBasket, ['is_safe' => ['html']] ],
 			'cardform' 		=> [ $cardform,  ['is_safe' => ['html']] ]
 		];
@@ -238,7 +240,7 @@ class SquarepayExtension extends SimpleExtension
 			exit;
 		}
 		$orderId = $response['order']['id'];
-		dump($orderId);
+		// dump($orderId);
 
 		// Build transaction request
 		$buyerInfo = [
@@ -271,16 +273,14 @@ class SquarepayExtension extends SimpleExtension
 		$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;
+			return new RedirectResponse('/order-problem');
 		}
 
-        return new RedirectResponse('/basket');
+		// Order succeeded
+		// dump($response);
+		// exit;
+        return new RedirectResponse('/order-complete');
 	}
 
 }

+ 12 - 0
src/Twig/SquareTwigRuntime.php

@@ -40,6 +40,18 @@ class SquareTwigRuntime
 		return $products;
 	}
 	
+	public function getCatalogItem($catId)
+	{
+		try {
+			$catalogApi = new \SquareConnect\Api\CatalogApi($this->apiClient);
+			$response = $catalogApi->retrieveCatalogObject($catId);
+		} catch (Exception $e) {
+			error_log($e->getMessage());
+			return 'ERROR: '.$e->getMessage();
+		}
+		return $response['object'];
+	}
+
 	public function getBasket()
 	{
 		return [