From b5a250106d50ffe285488038296647274261be69 Mon Sep 17 00:00:00 2001 From: Michael Roterman Date: Thu, 29 Oct 2020 10:17:13 +0100 Subject: [PATCH 1/4] [CORE-258] SDK modifications for CSE. --- src/Api/Api.php | 2 +- src/Api/Encryption/PublicKeys.php | 22 +++++++ src/Api/Transaction/EncryptedTransaction.php | 69 ++++++++++++++++++++ src/Encryption.php | 25 +++++++ src/Transaction.php | 58 ++++++++++++---- 5 files changed, 162 insertions(+), 14 deletions(-) create mode 100644 src/Api/Encryption/PublicKeys.php create mode 100644 src/Api/Transaction/EncryptedTransaction.php create mode 100644 src/Encryption.php diff --git a/src/Api/Api.php b/src/Api/Api.php index 90102584..db4986e8 100644 --- a/src/Api/Api.php +++ b/src/Api/Api.php @@ -68,7 +68,7 @@ public function doRequest($endpoint, $version = null) $curl->setOpt(CURLOPT_SSL_VERIFYPEER, Config::getVerifyPeer()); $result = $curl->post($uri, $data); - + if (isset($result->status) && $result->status === 'FALSE') { throw new Error\Api($result->error); } diff --git a/src/Api/Encryption/PublicKeys.php b/src/Api/Encryption/PublicKeys.php new file mode 100644 index 00000000..92347fa2 --- /dev/null +++ b/src/Api/Encryption/PublicKeys.php @@ -0,0 +1,22 @@ + + */ +class EncryptedTransaction 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 + */ + public function getTransactionId() + { + return $this->transactionId; + } + + /** + * @param mixed $transactionId + */ + public function setTransactionId($transactionId) + { + $this->transactionId = $transactionId; + } + + /** + * @return mixed + */ + public function getPayload() + { + return $this->payload; + } + + /** + * @param mixed $payload + */ + public function setPayload($payload) + { + $this->payload = $payload; + } + + /** + * @inheritdoc + */ + public function doRequest($endpoint = null, $version = null) + { + return parent::doRequest('creditcard/captureWithEncryptedData'); + } +} diff --git a/src/Encryption.php b/src/Encryption.php new file mode 100644 index 00000000..222f1472 --- /dev/null +++ b/src/Encryption.php @@ -0,0 +1,25 @@ +doRequest(); + } +} diff --git a/src/Transaction.php b/src/Transaction.php index 7bbde549..392cbfa0 100644 --- a/src/Transaction.php +++ b/src/Transaction.php @@ -37,7 +37,7 @@ class Transaction /** @var string Payment fees */ const PRODUCT_TYPE_PAYMENT = 'PAYMENT'; /** @var string An extra order line added by PAY. if the total amount does not match the total of the product lines */ - const PRODUCT_TYPE_ROUNDING = 'ROUNDING'; + const PRODUCT_TYPE_ROUNDING = 'ROUNDING'; /** @var string Costs for shipment */ const PRODUCT_TYPE_SHIPPING = 'SHIPPING'; /** @var string Ticket for events, festivals or theaters */ @@ -49,7 +49,6 @@ class Transaction /** @var string Voucher for a free article or discount for next order */ const PRODUCT_TYPE_VOUCHER = 'VOUCHER'; - /** * Start a new transaction * @@ -318,26 +317,26 @@ public static function status($transactionId) * @return Result\Details * @throws Error\Api * @throws Error\Error - * @throws Error\Required\ApiToken + * @throws Error\Required\ApiToken */ public static function details( $transactionId, $entranceCode = null ) { - + $api = new Api\Details(); - + $api->setTransactionId($transactionId); - + if ($entranceCode !== null) { $api->setEntranceCode($entranceCode); - } - $result = $api->doRequest(); - + } + $result = $api->doRequest(); + return new Result\Details($result); } - + /** * Get the transaction in an exchange script. * This will work for all kinds of exchange calls (GET, POST AND POST_XML) @@ -425,7 +424,7 @@ public static function refund( * @return Result\Cancel * @throws Error\Api * @throws Error\Error - * @throws Error\Required\ApiToken + * @throws Error\Required\ApiToken */ public static function cancel( $transactionId, @@ -434,11 +433,11 @@ public static function cancel( { $api = new Api\Cancel(); $api->setTransactionId($transactionId); - + if ($entranceCode !== null) { $api->setEntranceCode($entranceCode); } - + $result = $api->doRequest(); return new Result\Cancel($result); @@ -664,4 +663,37 @@ public static function QRPayment($options = array()) return new Result\QRPayment($result); } + + /** + * Attempt to capture an encrypted transaction. + * + * @param string $transactionId + * @param string $payload + * + * @return Result\Details + * @throws Error\Api + * @throws Error\Error + * @throws Error\Required\ApiToken + */ + public static function captureWithEncryptedData( + $transactionId, + $payload + ) + { + $api = new Api\EncryptedTransaction(); + $api->setTransactionId($transactionId); + $api->setPayload($payload); + + try { + return $api->doRequest(); + } catch (\Exception $e) { + return array( + 'type' => 'error', + 'message' => $e->getMessage(), + 'file' => $e->getFile(), + 'line' => $e->getLine(), + 'trace' => $e->getTraceAsString() + ); + } + } } From 3909e5792abf9216946c8dfa06f05ca37cfa35bd Mon Sep 17 00:00:00 2001 From: Michael Roterman Date: Wed, 4 Nov 2020 11:50:17 +0100 Subject: [PATCH 2/4] [CORE-258] Move some methods to the creditcard namespace --- .../EncryptedTransaction.php | 2 +- .../{Encryption => Creditcard}/PublicKeys.php | 4 +- src/Creditcard.php | 62 +++++++++++++++++++ src/Encryption.php | 25 -------- 4 files changed, 65 insertions(+), 28 deletions(-) rename src/Api/{Transaction => Creditcard}/EncryptedTransaction.php (97%) rename src/Api/{Encryption => Creditcard}/PublicKeys.php (76%) create mode 100644 src/Creditcard.php delete mode 100644 src/Encryption.php diff --git a/src/Api/Transaction/EncryptedTransaction.php b/src/Api/Creditcard/EncryptedTransaction.php similarity index 97% rename from src/Api/Transaction/EncryptedTransaction.php rename to src/Api/Creditcard/EncryptedTransaction.php index 0f588ba0..dfc82906 100644 --- a/src/Api/Transaction/EncryptedTransaction.php +++ b/src/Api/Creditcard/EncryptedTransaction.php @@ -1,6 +1,6 @@ + */ +class Creditcard +{ + /** + * Attempt to capture an encrypted transaction. + * + * @param string $transactionId + * @param string $payload + * + * @return array|Result\Details + * @throws Error\Api + * @throws Error\Error + * @throws Error\Required\ApiToken + */ + public static function captureWithEncryptedData( + $transactionId, + $payload + ) + { + $api = new Api\EncryptedTransaction(); + $api->setTransactionId($transactionId); + $api->setPayload($payload); + + 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. + * + * @return array + * @throws Error\Api + * @throws Error\Error + * @throws Error\Required\ApiToken + */ + public static function publicKeys() + { + $api = new Api\PublicKeys(); + + return $api->doRequest(); + } +} diff --git a/src/Encryption.php b/src/Encryption.php deleted file mode 100644 index 222f1472..00000000 --- a/src/Encryption.php +++ /dev/null @@ -1,25 +0,0 @@ -doRequest(); - } -} From b90e1cf5fcca70ddc4f0a33c9bf7638c2daafa83 Mon Sep 17 00:00:00 2001 From: Michael Roterman Date: Mon, 21 Dec 2020 12:27:45 +0100 Subject: [PATCH 3/4] Remove capture encrypted transaction method from the transaction class. --- src/Transaction.php | 33 --------------------------------- 1 file changed, 33 deletions(-) diff --git a/src/Transaction.php b/src/Transaction.php index 392cbfa0..8d4aaf4e 100644 --- a/src/Transaction.php +++ b/src/Transaction.php @@ -663,37 +663,4 @@ public static function QRPayment($options = array()) return new Result\QRPayment($result); } - - /** - * Attempt to capture an encrypted transaction. - * - * @param string $transactionId - * @param string $payload - * - * @return Result\Details - * @throws Error\Api - * @throws Error\Error - * @throws Error\Required\ApiToken - */ - public static function captureWithEncryptedData( - $transactionId, - $payload - ) - { - $api = new Api\EncryptedTransaction(); - $api->setTransactionId($transactionId); - $api->setPayload($payload); - - try { - return $api->doRequest(); - } catch (\Exception $e) { - return array( - 'type' => 'error', - 'message' => $e->getMessage(), - 'file' => $e->getFile(), - 'line' => $e->getLine(), - 'trace' => $e->getTraceAsString() - ); - } - } } From 482dbb411eb0b7c378ff1c4d569cfdf43c170d8c Mon Sep 17 00:00:00 2001 From: Michael Roterman Date: Wed, 24 Feb 2021 17:08:22 +0100 Subject: [PATCH 4/4] [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. *