diff --git a/README.md b/README.md index 92ed42a..cf0d2c8 100755 --- a/README.md +++ b/README.md @@ -9,7 +9,13 @@ Install Community Store first Register your application with square to generate Application ID / Token / Location (https://docs.connect.squareup.com/) Install this package +## Errata +vendor/apimatic/jsonmapper/src/JsonMapper.php +May throw an exception on line 119 on some hosting configurations. Simply commenting it out seems to resolve the issue. +//$this->config = parse_ini_file($iniPath); + ## Contributors Thanks both Mirko (bulli1979) and Christian (guedeWebGate) for their help in developing this add-on. +Updated to latest API version by Jeff Paetkau Dec 2022 Also, I would like to thank Ryan (Mesuva) for the review. Thanks guys for your input and guidance! diff --git a/composer.json b/composer.json index 584f637..daccb67 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "require": { - "square/connect": "*" - } + "require": { + "square/square": "24.0.0.20221116" + } } diff --git a/composer.lock b/composer.lock index d89162b..b91d009 100644 --- a/composer.lock +++ b/composer.lock @@ -1,61 +1,279 @@ { "_readme": [ "This file locks the dependencies of your project to a known state", - "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "3c8d7004f8c835a89030dcac74982fed", + "content-hash": "96683862f30eed587065f5c60fed54c2", "packages": [ { - "name": "square/connect", - "version": "2.0.0.1", + "name": "apimatic/core", + "version": "0.1.3", "source": { "type": "git", - "url": "https://github.com/square/connect-php-sdk.git", - "reference": "949202cb41c8df51b6f70a88d3649679905b9bd0" + "url": "https://github.com/apimatic/core-lib-php.git", + "reference": "514093bea77f1ed6efaa12c9e8bb94c6adafe26c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/square/connect-php-sdk/zipball/949202cb41c8df51b6f70a88d3649679905b9bd0", - "reference": "949202cb41c8df51b6f70a88d3649679905b9bd0", + "url": "https://api.github.com/repos/apimatic/core-lib-php/zipball/514093bea77f1ed6efaa12c9e8bb94c6adafe26c", + "reference": "514093bea77f1ed6efaa12c9e8bb94c6adafe26c", "shasum": "" }, "require": { + "apimatic/core-interfaces": "^0.1.0", + "apimatic/jsonmapper": "^3.0.4", "ext-curl": "*", + "ext-dom": "*", "ext-json": "*", - "ext-mbstring": "*", - "php": ">=5.3.3" + "ext-libxml": "*", + "php": ">=7.2 <8.2" }, "require-dev": { - "phpunit/phpunit": "~4.0", - "satooshi/php-coveralls": "~0.6.1", - "squizlabs/php_codesniffer": "~2.0" + "phan/phan": "5.3.1", + "phpunit/phpunit": "^7.5 || ^8.5 || ^9.5", + "squizlabs/php_codesniffer": "^3.5" }, "type": "library", "autoload": { "psr-4": { - "SquareConnect\\": "lib/" + "Core\\": "src/" } }, "notification-url": "https://packagist.org/downloads/", "license": [ - "Apache v2" + "proprietary" + ], + "description": "Core logic and the utilities for the Apimatic's PHP SDK", + "homepage": "https://github.com/apimatic/core-lib-php", + "keywords": [ + "apimatic", + "core", + "corelib", + "php" + ], + "support": { + "issues": "https://github.com/apimatic/core-lib-php/issues", + "source": "https://github.com/apimatic/core-lib-php/tree/0.1.3" + }, + "time": "2022-10-12T10:17:49+00:00" + }, + { + "name": "apimatic/core-interfaces", + "version": "0.1.0", + "source": { + "type": "git", + "url": "https://github.com/apimatic/core-interfaces-php.git", + "reference": "51ac49e29598cf30cdecb23436b93e94db30b223" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/apimatic/core-interfaces-php/zipball/51ac49e29598cf30cdecb23436b93e94db30b223", + "reference": "51ac49e29598cf30cdecb23436b93e94db30b223", + "shasum": "" + }, + "require": { + "php": ">=7.2 <8.2" + }, + "type": "library", + "autoload": { + "psr-4": { + "CoreInterfaces\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "proprietary" + ], + "description": "Definition of the behavior of apimatic/core, apimatic/unirest-php and Apimatic's PHP SDK", + "homepage": "https://github.com/apimatic/core-interfaces-php", + "keywords": [ + "apimatic", + "core", + "corelib", + "interface", + "php", + "unirest" + ], + "support": { + "issues": "https://github.com/apimatic/core-interfaces-php/issues", + "source": "https://github.com/apimatic/core-interfaces-php/tree/0.1.0" + }, + "time": "2022-09-30T10:15:48+00:00" + }, + { + "name": "apimatic/jsonmapper", + "version": "v3.0.5", + "source": { + "type": "git", + "url": "https://github.com/apimatic/jsonmapper.git", + "reference": "7f998fba2a5ad4a41d9c010e89c2f0922f249111" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/apimatic/jsonmapper/zipball/7f998fba2a5ad4a41d9c010e89c2f0922f249111", + "reference": "7f998fba2a5ad4a41d9c010e89c2f0922f249111", + "shasum": "" + }, + "require": { + "ext-json": "*" + }, + "require-dev": { + "phpunit/phpunit": "^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0 || ^9.0.0", + "squizlabs/php_codesniffer": "^3.0.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "apimatic\\jsonmapper\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "OSL-3.0" + ], + "authors": [ + { + "name": "Christian Weiske", + "email": "christian.weiske@netresearch.de", + "homepage": "http://www.netresearch.de/", + "role": "Developer" + }, + { + "name": "Mehdi Jaffery", + "email": "mehdi.jaffery@apimatic.io", + "homepage": "http://apimatic.io/", + "role": "Developer" + } + ], + "description": "Map nested JSON structures onto PHP classes", + "support": { + "email": "mehdi.jaffery@apimatic.io", + "issues": "https://github.com/apimatic/jsonmapper/issues", + "source": "https://github.com/apimatic/jsonmapper/tree/v3.0.5" + }, + "time": "2022-12-06T15:07:38+00:00" + }, + { + "name": "apimatic/unirest-php", + "version": "4.0.0", + "source": { + "type": "git", + "url": "https://github.com/apimatic/unirest-php.git", + "reference": "70ccda3d712cdc395d39004b8ef619837f23994b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/apimatic/unirest-php/zipball/70ccda3d712cdc395d39004b8ef619837f23994b", + "reference": "70ccda3d712cdc395d39004b8ef619837f23994b", + "shasum": "" + }, + "require": { + "apimatic/core-interfaces": "^0.1.0", + "ext-curl": "*", + "ext-json": "*", + "php": ">=7.2 <8.2" + }, + "require-dev": { + "phan/phan": "5.3.1", + "phpunit/phpunit": "^7.5 || ^8.5 || ^9.5", + "squizlabs/php_codesniffer": "^3.5" + }, + "type": "library", + "autoload": { + "psr-4": { + "Unirest\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Mashape", + "email": "opensource@mashape.com", + "homepage": "https://www.mashape.com", + "role": "Developer" + }, + { + "name": "APIMATIC", + "email": "opensource@apimatic.io", + "homepage": "https://www.apimatic.io", + "role": "Developer" + } + ], + "description": "Unirest PHP", + "homepage": "https://github.com/apimatic/unirest-php", + "keywords": [ + "client", + "curl", + "http", + "https", + "rest" + ], + "support": { + "email": "opensource@apimatic.io", + "issues": "https://github.com/apimatic/unirest-php/issues", + "source": "https://github.com/apimatic/unirest-php/tree/4.0.0" + }, + "time": "2022-09-30T10:57:16+00:00" + }, + { + "name": "square/square", + "version": "24.0.0.20221116", + "source": { + "type": "git", + "url": "https://github.com/square/square-php-sdk.git", + "reference": "4aea41708d3c4d48bcf49b067b3e56ad5757464a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/square/square-php-sdk/zipball/4aea41708d3c4d48bcf49b067b3e56ad5757464a", + "reference": "4aea41708d3c4d48bcf49b067b3e56ad5757464a", + "shasum": "" + }, + "require": { + "apimatic/core": "~0.1.0", + "apimatic/core-interfaces": "~0.1.0", + "apimatic/unirest-php": "^4.0.0", + "ext-json": "*", + "php": ">=7.2 <8.2" + }, + "require-dev": { + "phan/phan": "5.3.1", + "phpunit/phpunit": "^7.5 || ^8.5 || ^9.5", + "squizlabs/php_codesniffer": "^3.5" + }, + "type": "library", + "autoload": { + "psr-4": { + "Square\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" ], "authors": [ { - "name": "Square, Inc.", - "homepage": "https://github.com/Square/connect-php-sdk" + "name": "Square Developer Platform", + "email": "developers@squareup.com", + "homepage": "https://squareup.com/developers" } ], - "description": "PHP client library for the Square Connect v2 API", - "homepage": "http://swagger.io", + "description": "Use Square APIs to manage and run business including payment, customer, product, inventory, and employee management.", + "homepage": "https://squareup.com/developers", "keywords": [ "api", - "php", "sdk", - "swagger" + "square" ], - "time": "2016-05-20T01:19:58+00:00" + "support": { + "issues": "https://github.com/square/square-php-sdk/issues", + "source": "https://github.com/square/square-php-sdk/tree/24.0.0.20221116" + }, + "time": "2022-11-16T18:46:02+00:00" } ], "packages-dev": [], @@ -65,5 +283,6 @@ "prefer-stable": false, "prefer-lowest": false, "platform": [], - "platform-dev": [] + "platform-dev": [], + "plugin-api-version": "2.3.0" } diff --git a/controller.php b/controller.php index c04a9d7..a31ca41 100755 --- a/controller.php +++ b/controller.php @@ -12,7 +12,7 @@ class Controller extends Package { protected $pkgHandle = 'community_store_square'; protected $appVersionRequired = '5.7.2'; - protected $pkgVersion = '0.9.1'; + protected $pkgVersion = '0.10.0'; public function on_start() { diff --git a/elements/community_store_square/checkout_form.php b/elements/community_store_square/checkout_form.php index d6a411b..536050b 100644 --- a/elements/community_store_square/checkout_form.php +++ b/elements/community_store_square/checkout_form.php @@ -2,119 +2,109 @@ extract($vars); ?> - - +' : '' ?> -
-
- -
-
-
- -
-
-
-
-
-
-
- -
-
-
-
-
- -
-
-
-
-
-
-
- -
-
-
-
-
-
- -

+
+
+
+ +
\ No newline at end of file diff --git a/src/CommunityStore/Payment/Methods/CommunityStoreSquare/CommunityStoreSquarePaymentMethod.php b/src/CommunityStore/Payment/Methods/CommunityStoreSquare/CommunityStoreSquarePaymentMethod.php index cb5df4d..40ad127 100644 --- a/src/CommunityStore/Payment/Methods/CommunityStoreSquare/CommunityStoreSquarePaymentMethod.php +++ b/src/CommunityStore/Payment/Methods/CommunityStoreSquare/CommunityStoreSquarePaymentMethod.php @@ -6,8 +6,10 @@ use Log; use Config; use Exception; -use \Square\Charge; -use Square\Error; +use Square\SquareClient; +use Square\Environment; +use Square\Models; +use Square\Exceptions\ApiException; use \Concrete\Package\CommunityStore\Src\CommunityStore\Payment\Method as StorePaymentMethod; use \Concrete\Package\CommunityStore\Src\CommunityStore\Utilities\Calculator as StoreCalculator; @@ -93,72 +95,44 @@ public function submitPayment() $customer = new StoreCustomer(); $currency = Config::get('community_store_square.currency'); $mode = Config::get('community_store_square.mode'); - + + $client = null; if ($mode == 'sandbox') { $privateKey = Config::get('community_store_square.sandboxAccessToken'); $locationKey = Config::get('community_store_square.sandboxLocation'); + $client = new SquareClient([ + 'accessToken' => $privateKey, + 'environment' => Environment::SANDBOX, + ]); } else { $privateKey = Config::get('community_store_square.liveAccessToken'); $locationKey = Config::get('community_store_square.liveLocation'); + $client = new SquareClient([ + 'accessToken' => $privateKey, + 'environment' => Environment::PRODUCTION, + ]); } - - $token = $_POST['squareToken']; - $nonce = $_POST['nonce']; - - // Alert for debugging purposes only - // Log::addEntry("Nonce: " . $nonce , t('Community Store Square')); - - $genericError = false; - if (is_null($nonce)) { - echo "Invalid card data"; - http_response_code(422); - return; - } - $transaction_api = new TransactionApi(); - $request_body = array ( - "card_nonce" => $nonce, - # Monetary amounts are specified in the smallest unit of the applicable currency. - # This amount is in cents. It's also hard-coded for $1.00, which isn't very useful. - "amount_money" => array ( - "amount" => StoreCalculator::getGrandTotal()*100, - "currency" => $currency - ), - # Every payment you process with the SDK must have a unique idempotency key. - # If you're unsure whether a particular payment succeeded, you can reattempt - # it with the same idempotency key without worrying about double charging - # the buyer. - "idempotency_key" => uniqid() - ); - - try { - // Alert for debugging purposes only - // Log::addEntry("Init actual credit card payment", t('Community Store Square')); - $result = $transaction_api->charge($privateKey, $locationKey, $request_body); - - // Alert for debugging purposes only - Log::addEntry('Square info.'."\n".'Result is:' . $result . "\n", t('Community Store Square')); - - // credit card payment was successful - updating database - order record - return array('error'=>0, 'transactionReference'=>$result); - } catch (\SquareConnect\ApiException $e) { - $respBody = $e->getResponseBody(); - if (is_object($respBody)) { - $errTxt = ''; - foreach($respBody->errors as $respError) { - $errTxt = $errTxt . $respError->detail; - } - $totalError = json_encode($respBody); - // Log error to backend (Reports - Logs) - Log::addEntry ("JSON from response body: ".$totalError, t('Community Store Square')); - // Display error to frontend - return array('error'=>1,'errorMessage'=> 'Payment Error: '.$errTxt); - } else { - // Log error to backend (Reports - Logs) - Log::addEntry("Something went wrong during the payment process:". $e, t('Community Store Square')); - // Display error to frontend - return array ('error'=>1,'errorMessage'=>'Something went wrong during the payment process!'); - } - } + + $paymentsApi = $client->getPaymentsApi(); + $body_sourceId = $_POST['nonce']; + $body_idempotencyKey = uniqid(); + $body_amountMoney = new Models\Money(); + $body_amountMoney->setAmount(StoreCalculator::getGrandTotal()*100); + $body_amountMoney->setCurrency($currency); + $body = new Models\CreatePaymentRequest( + $body_sourceId, + $body_idempotencyKey, + $body_amountMoney + ); + $body->setAutocomplete(true); + $apiResponse = $paymentsApi->createPayment($body); + + if ($apiResponse->isSuccess()) { + $createPaymentResponse = $apiResponse->getResult(); + return array('error'=>0, 'transactionReference'=>$createPaymentResponse->getPayment()->getId()); + } else { + return array ('error'=>1,'errorMessage'=>$apiResponse->getErrors()); + } } public function getPaymentMethodName(){