From 482dbb411eb0b7c378ff1c4d569cfdf43c170d8c Mon Sep 17 00:00:00 2001 From: Michael Roterman Date: Wed, 24 Feb 2021 17:08:22 +0100 Subject: [PATCH] [CORE-258] Refactor CSE SDK implementation for 3dsv2 --- src/Api/Creditcard/AbstractCseRequest.php | 84 ++++++++++++++++++ src/Api/Creditcard/CseAuthenticate.php | 21 +++++ src/Api/Creditcard/CseAuthorize.php | 21 +++++ ...ryptedTransaction.php => CseTdsStatus.php} | 48 ++++------- src/Api/Creditcard/PublicKeys.php | 2 +- src/Creditcard.php | 86 +++++++++++++++++-- 6 files changed, 223 insertions(+), 39 deletions(-) create mode 100644 src/Api/Creditcard/AbstractCseRequest.php create mode 100644 src/Api/Creditcard/CseAuthenticate.php create mode 100644 src/Api/Creditcard/CseAuthorize.php rename src/Api/Creditcard/{EncryptedTransaction.php => CseTdsStatus.php} (59%) diff --git a/src/Api/Creditcard/AbstractCseRequest.php b/src/Api/Creditcard/AbstractCseRequest.php new file mode 100644 index 00000000..a6a0cf39 --- /dev/null +++ b/src/Api/Creditcard/AbstractCseRequest.php @@ -0,0 +1,84 @@ + + */ +abstract class AbstractCseRequest extends Api +{ + private $orderId; + private $payload; + private $threeDSTransactionId; + + /** + * @var int the version of the api + */ + protected $version = 2; + + /** + * @var bool Is the ApiToken required for this API + */ + protected $apiTokenRequired = true; + + public function getData() + { + return array_merge(parent::getData(), array( + 'orderId' => $this->getOrderId(), + 'threeDSTransactionId' => $this->getThreeDSTransactionId(), + 'payload' => $this->getPayload() + )); + } + + /** + * @return string + */ + public function getOrderId() + { + return $this->orderId; + } + + /** + * @param string $orderId + */ + public function setOrderId($orderId) + { + $this->orderId = $orderId; + } + + /** + * @return string + */ + public function getPayload() + { + return $this->payload; + } + + /** + * @param string $payload + */ + public function setPayload($payload) + { + $this->payload = $payload; + } + + /** + * @return string|null + */ + public function getThreeDSTransactionId() + { + return $this->threeDSTransactionId; + } + + /** + * @param string|null $threeDSTransactionId + */ + public function setThreeDSTransactionId($threeDSTransactionId) + { + $this->threeDSTransactionId = $threeDSTransactionId; + } +} diff --git a/src/Api/Creditcard/CseAuthenticate.php b/src/Api/Creditcard/CseAuthenticate.php new file mode 100644 index 00000000..b34334fd --- /dev/null +++ b/src/Api/Creditcard/CseAuthenticate.php @@ -0,0 +1,21 @@ + + */ +class CseAuthenticate extends AbstractCseRequest +{ + /** + * @inheritdoc + */ + public function doRequest($endpoint = null, $version = null) + { + return parent::doRequest('creditcard/cseAuthenticate'); + } +} diff --git a/src/Api/Creditcard/CseAuthorize.php b/src/Api/Creditcard/CseAuthorize.php new file mode 100644 index 00000000..04ea8eb0 --- /dev/null +++ b/src/Api/Creditcard/CseAuthorize.php @@ -0,0 +1,21 @@ + + */ +class CseAuthorize extends AbstractCseRequest +{ + /** + * @inheritdoc + */ + public function doRequest($endpoint = null, $version = null) + { + return parent::doRequest('creditcard/cseAuthorize'); + } +} diff --git a/src/Api/Creditcard/EncryptedTransaction.php b/src/Api/Creditcard/CseTdsStatus.php similarity index 59% rename from src/Api/Creditcard/EncryptedTransaction.php rename to src/Api/Creditcard/CseTdsStatus.php index dfc82906..99589282 100644 --- a/src/Api/Creditcard/EncryptedTransaction.php +++ b/src/Api/Creditcard/CseTdsStatus.php @@ -9,61 +9,49 @@ * * @author Michael Roterman */ -class EncryptedTransaction extends Api +class CseTdsStatus extends Api { - private $transactionId; - private $payload; - /** * @var int the version of the api */ protected $version = 2; - public function getData() - { - return array_merge(parent::getData(), array( - 'transactionId' => $this->transactionId, - 'payload' => $this->payload - )); - } - /** - * @return mixed + * @var string */ - public function getTransactionId() - { - return $this->transactionId; - } - + private $transactionId; + /** - * @param mixed $transactionId + * @inheritdoc */ - public function setTransactionId($transactionId) + public function doRequest($endpoint = null, $version = null) { - $this->transactionId = $transactionId; + return parent::doRequest('creditcard/cseTdsStatus'); } /** - * @return mixed + * {@inheritDoc} */ - public function getPayload() + public function getData() { - return $this->payload; + return array_merge(parent::getData(), array( + 'transactionId' => $this->transactionId + )); } /** - * @param mixed $payload + * @return string */ - public function setPayload($payload) + public function getTransactionId() { - $this->payload = $payload; + return $this->transactionId; } /** - * @inheritdoc + * @param string $transactionId */ - public function doRequest($endpoint = null, $version = null) + public function setTransactionId($transactionId) { - return parent::doRequest('creditcard/captureWithEncryptedData'); + $this->transactionId = $transactionId; } } diff --git a/src/Api/Creditcard/PublicKeys.php b/src/Api/Creditcard/PublicKeys.php index 7f4ad039..f296f440 100644 --- a/src/Api/Creditcard/PublicKeys.php +++ b/src/Api/Creditcard/PublicKeys.php @@ -17,6 +17,6 @@ class PublicKeys extends Api */ public function doRequest($endpoint = null, $version = null) { - return parent::doRequest('creditcard/publicKeys'); + return parent::doRequest('creditcard/cseGetPublicKeys'); } } diff --git a/src/Creditcard.php b/src/Creditcard.php index e7126b08..9c0b91ef 100644 --- a/src/Creditcard.php +++ b/src/Creditcard.php @@ -13,9 +13,10 @@ class Creditcard { /** - * Attempt to capture an encrypted transaction. + * Attempt to authorize a encrypted transaction. + * + * @param string $orderId * - * @param string $transactionId * @param string $payload * * @return array|Result\Details @@ -23,13 +24,15 @@ class Creditcard * @throws Error\Error * @throws Error\Required\ApiToken */ - public static function captureWithEncryptedData( - $transactionId, + public static function cseAuthorize( + $orderId, + $threeDSTransactionId, $payload - ) - { - $api = new Api\EncryptedTransaction(); - $api->setTransactionId($transactionId); + ) { + $api = new Api\CseAuthorize(); + + $api->setOrderId($orderId); + $api->setThreeDSTransactionId($threeDSTransactionId); $api->setPayload($payload); try { @@ -45,6 +48,73 @@ public static function captureWithEncryptedData( } } + /** + * Attempt to authenticate a encrypted transaction. + * + * @param string $orderId + * @param string $payload + * @param string|null $threeDSTransactionId + * + * @return array|Result\Details + * @throws Error\Api + * @throws Error\Error + * @throws Error\Required\ApiToken + */ + public static function cseAuthenticate( + $orderId, + $payload, + $threeDSTransactionId = null + ) { + $api = new Api\CseAuthenticate(); + + $api->setOrderId($orderId); + $api->setPayload($payload); + $api->setThreeDSTransactionId($threeDSTransactionId); + + try { + return $api->doRequest(); + } catch (\Exception $e) { + return array( + 'type' => 'error', + 'message' => $e->getMessage(), + 'file' => $e->getFile(), + 'line' => $e->getLine(), + 'trace' => $e->getTraceAsString() + ); + } + } + + + /** + * Attempt to authenticate a encrypted transaction. + * + * @param string $transactionId + * + * @return array|Result\Details + * @throws Error\Api + * @throws Error\Error + * @throws Error\Required\ApiToken + */ + public static function cseTdsStatus( + $transactionId + ) { + $api = new Api\CseTdsStatus(); + $api->setTransactionId($transactionId); + + try { + return $api->doRequest(); + } catch (\Exception $e) { + return array( + 'type' => 'error', + 'message' => $e->getMessage(), + 'file' => $e->getFile(), + 'line' => $e->getLine(), + 'trace' => $e->getTraceAsString() + ); + } + } + + /** * Obtain cryptographic keys to use. *