From 9256aab7cebe2fd42cfd2315dfe1ae4e5733c9d8 Mon Sep 17 00:00:00 2001 From: Jerome Onadja Date: Thu, 23 Dec 2021 20:01:28 +0000 Subject: [PATCH] feat(sdk): v1.0.0 release --- .editorconfig | 10 + .env.example | 4 - .gitignore | 1 - Examples/example.php | 36 --- Examples/example2.php | 33 --- README.md | 77 ++--- composer.json | 86 +++--- .../Exceptions/OrangeMoneyAPIException.php | 12 - .../Exceptions/PaymentSDKException.php | 12 - src/Fasodev/Sdk/OrangeMoneyAPI.php | 268 ------------------ src/Fasodev/Utils/Helpers.php | 48 ---- src/Http/RequestBody.php | 31 ++ src/Http/XMLHttp.php | 44 +++ src/Response/XMLResponse.php | 16 ++ src/Sdk/Config/Credentials.php | 80 ++++++ src/Sdk/Config/TransactionData.php | 102 +++++++ src/Sdk/Exception/TransactionException.php | 10 + src/Sdk/OrangeMoneyAPI.php | 135 +++++++++ src/{Fasodev => }/Sdk/PaymentSDK.php | 10 +- .../Sdk/TransactionInterface.php | 5 +- src/Sdk/TransactionResponse.php | 49 ++++ src/Sdk/TransactionResponseInterface.php | 24 ++ src/Utils/ReferenceGenerator.php | 24 ++ tests/OrangeMoneyAPITest.php | 8 +- tests/PaymentSDKTest.php | 39 +-- tests/TestCase.php | 15 - 26 files changed, 623 insertions(+), 556 deletions(-) create mode 100644 .editorconfig delete mode 100644 .env.example delete mode 100644 Examples/example.php delete mode 100644 Examples/example2.php delete mode 100644 src/Fasodev/Exceptions/OrangeMoneyAPIException.php delete mode 100644 src/Fasodev/Exceptions/PaymentSDKException.php delete mode 100644 src/Fasodev/Sdk/OrangeMoneyAPI.php delete mode 100644 src/Fasodev/Utils/Helpers.php create mode 100644 src/Http/RequestBody.php create mode 100644 src/Http/XMLHttp.php create mode 100644 src/Response/XMLResponse.php create mode 100644 src/Sdk/Config/Credentials.php create mode 100644 src/Sdk/Config/TransactionData.php create mode 100644 src/Sdk/Exception/TransactionException.php create mode 100644 src/Sdk/OrangeMoneyAPI.php rename src/{Fasodev => }/Sdk/PaymentSDK.php (77%) rename src/{Fasodev => }/Sdk/TransactionInterface.php (75%) create mode 100644 src/Sdk/TransactionResponse.php create mode 100644 src/Sdk/TransactionResponseInterface.php create mode 100644 src/Utils/ReferenceGenerator.php delete mode 100644 tests/TestCase.php diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..1a424d5 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,10 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 4 +indent_style = space +insert_final_newline = false +max_line_length = 120 +tab_width = 4 diff --git a/.env.example b/.env.example deleted file mode 100644 index e97eb2f..0000000 --- a/.env.example +++ /dev/null @@ -1,4 +0,0 @@ -APP_ENV=local -ORANGE_MONEY_USERNAME='johndoe' -ORANGE_MONEY_PASSWORD='password' -ORANGE_MONEY_MERCHANT_ID='123456789' \ No newline at end of file diff --git a/.gitignore b/.gitignore index 0a51edc..3e6cc8f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ /.idea /build /vendor -.env .phpunit.result.cache composer.lock diff --git a/Examples/example.php b/Examples/example.php deleted file mode 100644 index a3c00be..0000000 --- a/Examples/example.php +++ /dev/null @@ -1,36 +0,0 @@ - , - * @author S.C Jerôme ONADJA - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -use Fasodev\Exceptions\PaymentSDKException; -use Fasodev\Sdk\OrangeMoneyAPI; -use Fasodev\Sdk\PaymentSDK; - -require_once __DIR__ . '/../vendor/autoload.php'; - -try { - $orangeMoneyAPI = new OrangeMoneyAPI( - 'username', - 'password', - 'merchant_number' - ); - - $orangeMoneyAPI->setAmount(1000) // Montant de la transaction - ->setOTPCode(121212) // Code otp fourni par l'utilisateur - ->setClientNumber(76819212); // Le numero de client - - $sdk = new PaymentSDK($orangeMoneyAPI); - - $result = $sdk->handlePayment(); //Enclenchement du processus de paiement - - echo "paiement effectué"; - echo $result->transID; - -} catch (PaymentSDKException $exception) { - echo "Whoops! Unable to process payment.
Error message returned by request: {$exception->getMessage()}.
Error code returned by request: {$exception->getCode()}"; -} - diff --git a/Examples/example2.php b/Examples/example2.php deleted file mode 100644 index 70e117c..0000000 --- a/Examples/example2.php +++ /dev/null @@ -1,33 +0,0 @@ - , - * @author S.C Jerôme ONADJA - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -use Fasodev\Exceptions\PaymentSDKException; -use Fasodev\Sdk\OrangeMoneyAPI; -use Fasodev\Sdk\PaymentSDK; - -require_once __DIR__ . '/../vendor/autoload.php'; - -try { - $sdk = new PaymentSDK((new OrangeMoneyAPI( - 'username', - 'password', - 'merchant_number' - )) - ->setAmount(1000) // Montant de la transaction - ->setOTPCode(121212) // Code otp fourni par l'utilisateur - ->setClientNumber(76819212)); // Le numero de client - - $result = $sdk->handlePayment(); //Enclenchement du processus de paiement - - echo "paiement effectué"; - echo $result->transID; - -} catch (PaymentSDKException $exception) { - echo "Whoops! Unable to process payment.
Error message returned by request: {$exception->getMessage()}.
Error code returned by request: {$exception->getCode()}"; -} - diff --git a/README.md b/README.md index 7d30625..edb0791 100644 --- a/README.md +++ b/README.md @@ -7,49 +7,35 @@ dans des projets PHP. ## Installation via composer ```shell -composer require faso-dev/orange-money-burkina-sdk v2.beta +composer require faso-dev/orange-money-burkina-sdk ``` ## Cas d'utilisation ```php - use Fasodev\Exceptions\PaymentSDKException; - use Fasodev\Sdk\PaymentSDK; - use \Fasodev\Sdk\OrangeMoneyAPI; - - require_once __DIR__ . '/../vendor/autoload.php'; - - try { - $orangeMoneyAPI = new OrangeMoneyAPI( - 'username', - 'password', - 'merchant_number' - ); - - $orangeMoneyAPI->setAmount(1000) // Montant de la transaction - ->setOTPCode(121212) // Code otp fourni par l'utilisateur - ->setClientNumber(76819212); // Le numero de client - - // Not calling this method will cause the process to default to a test url. - // And the other hand, calling this method and not pass a parameter will - // cause the process to default to the production url as provided by Orange Money - // as of the time of this update. - // If you need to modify the url to make the api request to a custom endpoint, - // you can always provide one as a parameter like so: - // "$orangeMoneyAPI->setUrl('https://custom.orange.bf:9007/payment');" - $orangeMoneyAPI->setUrl(); - - $sdk = new PaymentSDK($orangeMoneyAPI); - - $result = $sdk->handlePayment(); //Enclenchement du processus de paiement - - echo "paiement effectué"; - echo $result->transID; - - } catch (PaymentSDKException $exception) { - echo "Whoops! Unable to process payment.
Error message returned by request: {$exception->getMessage()}.
Error code returned by request: {$exception->getCode()}"; - } +use Fasodev\Sdk\Config\TransactionData; +use Fasodev\Sdk\Exception\TransactionException; +use Fasodev\Sdk\OrangeMoneyAPI; +use Fasodev\Sdk\PaymentSDK; + +require_once __DIR__ . '/vendor/autoload.php'; + +try { + $orangeApi = (new OrangeMoneyAPI("username", "password", "merchant_number")) + ->withTransactionData(TransactionData::from('client_number', 'payment_amount', 'otp_code')) + ->withCustomReference("123456778") //optionnal + ->useProdApi() // for production + ->withoutSSLVerification() //if you have any troubleshoot with ssl verifcation(not recommended) + ; + $response = (new PaymentSDK($orangeApi))->handlePayment(); + echo 'Thank you for your purchasse !'; + echo $response->getTransactionId(); +} catch (TransactionException $exception) { + echo "Whoops! Unable to process payment.
+ Error message returned by request: {$exception->getMessage()}.
+ Error code returned by request: {$exception->getCode()}"; +} ``` ## Testing @@ -66,23 +52,6 @@ ou composer tests ``` -Vous pouvez également exécuter l'exemple de code en procédant comme suit à partir du terminal de commande: - -```bash -php -S localhost:8000 -t Examples/ -``` - -N'oubliez pas de faire une copie du fichier `.env.exampe` et renommez-le en` .env`, puis définissez les variables à utiliser pour exécuter la requête comme ceci: - -``` -APP_ENV=local -ORANGE_MONEY_USERNAME='johndoe' -ORANGE_MONEY_PASSWORD='password' -ORANGE_MONEY_MERCHANT_ID='123456789' -``` - -...puis visitez `http://localhost:8000/example.php` ou ` http://localhost:8000/example2.php` depuis votre navigateur. - ## Authors - https://github.com/faso-dev diff --git a/composer.json b/composer.json index 315121e..f600736 100644 --- a/composer.json +++ b/composer.json @@ -1,48 +1,48 @@ { - "name": "faso-dev/orange-money-burkina-sdk", - "type": "library", - "license": "MIT", - "description": "Ce package est une API qui encapsule l'API de base de Orange Money Burkina", - "authors": [ - { - "name": "Yentema Nadjoari", - "email": "n.yenteck@gmail.com" + "name": "faso-dev/orange-money-burkina-sdk", + "type": "library", + "license": "MIT", + "description": "Ce package est une API qui encapsule l'API de base de Orange Money Burkina", + "authors": [ + { + "name": "Yentema Nadjoari", + "email": "n.yenteck@gmail.com" + }, + { + "name": "S.C Jerôme ONADJA", + "email": "jeromeonadja28@gmail.com" + }, + { + "name": "Faso dev", + "homepage": "https://github.com/faso-dev" + } + ], + "require": { + "php": ">=7.4", + "ext-curl": "*", + "ext-json": "*", + "ext-simplexml": "*" }, - { - "name": "S.C Jerôme ONADJA", - "email": "jeromeonadja28@gmail.com" + "require-dev": { + "phpunit/phpunit": "^9.5" }, - { - "name": "Faso dev", - "homepage": "https://github.com/faso-dev" - } - ], - "require": { - "php": ">=7.3", - "ext-curl": "*", - "ext-json": "*", - "ext-simplexml": "*" - }, - "require-dev": { - "phpunit/phpunit": "^9.5" - }, - "autoload": { - "psr-4": { - "Fasodev\\": "src/Fasodev/" - } - }, - "autoload-dev": { - "psr-4": { - "Fasodev\\Tests\\": "tests" + "autoload": { + "psr-4": { + "Fasodev\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Fasodev\\Tests\\": "tests" + } + }, + "config": { + "sort-packages": true + }, + "minimum-stability": "dev", + "prefer-stable": true, + "scripts": { + "tests": "vendor/bin/phpunit", + "testsVerbose": "@tests -vvv" } - }, - "config": { - "sort-packages": true - }, - "minimum-stability": "dev", - "prefer-stable": true, - "scripts": { - "tests": "vendor/bin/phpunit", - "testsVerbose": "@tests -vvv" - } } diff --git a/src/Fasodev/Exceptions/OrangeMoneyAPIException.php b/src/Fasodev/Exceptions/OrangeMoneyAPIException.php deleted file mode 100644 index e4c6298..0000000 --- a/src/Fasodev/Exceptions/OrangeMoneyAPIException.php +++ /dev/null @@ -1,12 +0,0 @@ - , - * @author S.C Jerôme ONADJA - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -declare(strict_types=1); - -namespace Fasodev\Sdk; - -use Fasodev\Exceptions\OrangeMoneyAPIException; -use Fasodev\Exceptions\PaymentSDKException; -use Fasodev\Utils\Helpers; - -/** - * Class OrangeMoneyAPI - * @package Fasodev\Sdk - */ -class OrangeMoneyAPI implements TransactionInterface -{ - /** - * Transaction amount - * @var float|int|string - */ - protected $amount; - - /** - * @var string - */ - protected $otp; - - /** - * @var string - */ - protected $username; - - /** - * @var string - */ - protected $password; - - /** - * @var - */ - protected $merchantNumber; - - /** - * @var - */ - protected $clientNumber; - - /** - * @var - */ - protected $referenceNumber = ""; - - /** - * @var string - */ - protected $url; - - /** - * OrangeMoneyAPI constructor. - * - * @param string $username - * @param string $password - * @param $merchantNumber - */ - public function __construct(string $username, - string $password, - $merchantNumber) - { - $this->setUsername($username); - $this->setMerchantNumber($merchantNumber); - $this->setPassword($password); - } - - /** - * @return mixed - * @throws PaymentSDKException - */ - public function processPayment() - { - $RQ = $this->requestApi(); - $parsed = Helpers::xmlToObject("" . $RQ . ""); - - // Throw an exception if the request returns any status code that is not 200. - if ($parsed->status != 200) { - throw new OrangeMoneyAPIException((string) $parsed->message, (int) $parsed->status); - } - - return $parsed; - } - - private function requestApi() - { - $xml = " - - OMPREQ - {$this->clientNumber} - {$this->merchantNumber} - {$this->username} - {$this->password} - {$this->amount} - 101 - 101 - 12 - 12 - {$this->otp} - {$this->getReferenceNumber()} - 201500068544 - "; - - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $this->getUrl()); - curl_setopt($ch, CURLOPT_POST, true); - curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml')); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); - $result = curl_exec($ch); - curl_close($ch); - - return $result; - } - - /** - * Set the url to be used to make the API call. - * - * @return string - */ - public function getUrl(): string - { - // Default to a test url if non is set. - return $this->url ?? 'https://testom.orange.bf:9008/payment'; - } - - /** - * Set the url to be used to make the API call. - * - * @param string $url

The URL of Orange Money API as of the time this packages was updated was - * 'https://testom.orange.bf:9008/payment' for testing and 'https://apiom.orange.bf:9007/payment' for production. - * Calling this method without passing a value will default to the production API url.

- * - * @return OrangeMoneyAPI - */ - public function setUrl(string $url = 'https://apiom.orange.bf:9007/payment'): OrangeMoneyAPI - { - $this->url = $url; - return $this; - } - - /** - * @return float|int|string - */ - public function getAmount() - { - return $this->amount; - } - - /** - * @param float|int|string $amount - * @return OrangeMoneyAPI - */ - public function setAmount($amount): self - { - $this->amount = $amount; - return $this; - } - /** - * @param string $otp - * @return OrangeMoneyAPI - */ - public function setOTPCode(string $otp): self - { - $this->otp = $otp; - return $this; - } - - /** - * @param string $username - * @return OrangeMoneyAPI - */ - public function setUsername(string $username): self - { - $this->username = $username; - return $this; - } - - /** - * @return string|null - */ - public function getPassword(): ?string - { - return $this->password; - } - - /** - * @param string $password - * @return OrangeMoneyAPI - */ - public function setPassword(string $password): self - { - $this->password = $password; - return $this; - } - - /** - * @return mixed - */ - public function getMerchantNumber() - { - return $this->merchantNumber; - } - - /** - * @param mixed $merchantNumber - * @return OrangeMoneyAPI - */ - public function setMerchantNumber($merchantNumber): self - { - $this->merchantNumber = $merchantNumber; - return $this; - } - - /** - * @return mixed - */ - public function getClientNumber() - { - return $this->clientNumber; - } - - /** - * @param mixed $clientNumber - * @return OrangeMoneyAPI - */ - public function setClientNumber($clientNumber): self - { - $this->clientNumber = $clientNumber; - return $this; - } - - /** - * Get reference number - * - *

Generate a random string of six digit if referenceNumber is not set

- * - * @return mixed - */ - public function getReferenceNumber(): string - { - return trim($this->referenceNumber) !== '' - ? $this->referenceNumber - : Helpers::randomString(6); - } - - /** - * @param mixed $referenceNumber - * @return OrangeMoneyAPI - */ - public function setReferenceNumber($referenceNumber): self - { - $this->referenceNumber = $referenceNumber; - return $this; - } -} \ No newline at end of file diff --git a/src/Fasodev/Utils/Helpers.php b/src/Fasodev/Utils/Helpers.php deleted file mode 100644 index 645b056..0000000 --- a/src/Fasodev/Utils/Helpers.php +++ /dev/null @@ -1,48 +0,0 @@ - + + OMPREQ + {$transactionData->getClientNumber()} + {$credentials->getMerchant()} + {$credentials->getUsername()} + {$credentials->getPassword()} + {$transactionData->getPaymentAmount()} + 101 + 101 + 12 + 12 + {$transactionData->getOtp()} + {$transactionData->getReferenceNumber()} + 201500068544 + "; + } +} \ No newline at end of file diff --git a/src/Http/XMLHttp.php b/src/Http/XMLHttp.php new file mode 100644 index 0000000..e0c5149 --- /dev/null +++ b/src/Http/XMLHttp.php @@ -0,0 +1,44 @@ +$response") + ]; + } + +} \ No newline at end of file diff --git a/src/Response/XMLResponse.php b/src/Response/XMLResponse.php new file mode 100644 index 0000000..077593a --- /dev/null +++ b/src/Response/XMLResponse.php @@ -0,0 +1,16 @@ +merchant = $merchant; + $this->password = $password; + $this->username = $username; + + } + + /** + * @return string + */ + public function getUsername(): string + { + return $this->username; + } + + /** + * @param string $username + * @return Credentials + */ + public function setUsername(string $username): self + { + $this->username = $username; + return $this; + } + + /** + * @return string + */ + public function getPassword(): string + { + return $this->password; + } + + /** + * @param string $password + * @return Credentials + */ + public function setPassword(string $password): self + { + $this->password = $password; + return $this; + } + + /** + * @return string + */ + public function getMerchant(): string + { + return $this->merchant; + } + + /** + * @param string $merchant + * @return Credentials + */ + public function setMerchant(string $merchant): self + { + $this->merchant = $merchant; + return $this; + } + + + public static function from(string $username, string $password, string $merchant): self + { + return new self($username, $password, $merchant); + } +} \ No newline at end of file diff --git a/src/Sdk/Config/TransactionData.php b/src/Sdk/Config/TransactionData.php new file mode 100644 index 0000000..1804292 --- /dev/null +++ b/src/Sdk/Config/TransactionData.php @@ -0,0 +1,102 @@ +otp = $otp; + $this->paymentAmount = $paymentAmount; + $this->clientNumber = $clientNumber; + } + + public static function from(string $clientNumber, string $paymentAmount, string $otp): self + { + return new self($clientNumber, $paymentAmount, $otp); + } + + /** + * @return string + */ + public function getClientNumber(): string + { + return $this->clientNumber; + } + + /** + * @param string $clientNumber + * @return TransactionData + */ + public function setClientNumber(string $clientNumber): self + { + $this->clientNumber = $clientNumber; + return $this; + } + + /** + * @return string + */ + public function getPaymentAmount(): string + { + return $this->paymentAmount; + } + + /** + * @param string $paymentAmount + * @return TransactionData + */ + public function setAmount(string $paymentAmount): self + { + $this->paymentAmount = $paymentAmount; + return $this; + } + + /** + * @return string + */ + public function getOtp(): string + { + return $this->otp; + } + + /** + * @param string $otp + * @return TransactionData + */ + public function setOtp(string $otp): self + { + $this->otp = $otp; + return $this; + } + + /** + * @return string + */ + public function getReferenceNumber(): string + { + return $this->referenceNumber ?? ReferenceGenerator::token(); + } + + /** + * @param string $referenceNumber + * @return TransactionData + */ + public function setReferenceNumber(string $referenceNumber): TransactionData + { + $this->referenceNumber = $referenceNumber; + return $this; + } + +} \ No newline at end of file diff --git a/src/Sdk/Exception/TransactionException.php b/src/Sdk/Exception/TransactionException.php new file mode 100644 index 0000000..d5b98eb --- /dev/null +++ b/src/Sdk/Exception/TransactionException.php @@ -0,0 +1,10 @@ + , + * @author S.C Jerôme ONADJA + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +declare(strict_types=1); + +namespace Fasodev\Sdk; + +use Fasodev\Http\XMLHttp; +use Fasodev\Http\RequestBody; +use Fasodev\Sdk\Config\Credentials; +use Fasodev\Sdk\Config\TransactionData; +use Fasodev\Sdk\Exception\TransactionException; + +/** + * Class OrangeMoneyAPI + * @package Fasodev\Sdk + */ +class OrangeMoneyAPI implements TransactionInterface +{ + protected const DEV_API_URL = "https://testom.orange.bf:9008/payment"; + protected const PROD_API_URL = "https://apiom.orange.bf:9007/payment"; + + protected TransactionData $transactionData; + + protected Credentials $credentials; + + protected string $apiUrl; + + protected bool $withSSLVerification = XMLHttp::WITH_SSL_ENABLED; + + /** + * @param string $username + * @param string $password + * @param string $merchantNumber + */ + public function __construct(string $username, + string $password, + string $merchantNumber) + { + $this->credentials = Credentials::from($username, $password, $merchantNumber); + $this->useDevApi(); + } + + /** + * @return TransactionResponseInterface + * @throws TransactionException + */ + public function processPayment(): TransactionResponseInterface + { + /** + * @var $errno int + * @var $errorMessage string + * @var $response mixed + */ + [$errno, $errorMessage, $response] = $this->processRequest(); + if ($errno > 0) { + throw new TransactionException($errorMessage, $errno); + } + + $response = TransactionResponse::fromXMLResponse($response); + + if ($response->getStatus() !== 200) { + throw new TransactionException($response->getMessage(), $response->getStatus()); + } + + return $response; + } + + /** + * @return array + */ + private function processRequest(): array + { + return XMLHttp::request($this->apiUrl, [ + + ], RequestBody::from($this->credentials, $this->transactionData), + $this->withSSLVerification + ); + } + + /** + * @param string $reference + * @return $this + */ + public function withCustomReference(string $reference): self + { + $this->transactionData->setReferenceNumber($reference); + return $this; + } + + /** + * @param TransactionData $transactionData + * @return $this + */ + public function withTransactionData(TransactionData $transactionData): self + { + $this->transactionData = $transactionData; + return $this; + } + + /** + * @return $this + */ + public function withoutSSLVerification(): self + { + $this->withSSLVerification = XMLHttp::WITH_SSL_DISABLED; + return $this; + } + + /** + * @param string $devApiUrl + * @return $this + */ + public function useDevApi(string $devApiUrl = self::DEV_API_URL): self + { + + $this->apiUrl = $devApiUrl; + return $this; + } + + /** + * @param string $prodApiUrl + * @return $this + */ + public function useProdApi(string $prodApiUrl = self::PROD_API_URL): self + { + $this->apiUrl = $prodApiUrl; + return $this; + } + +} \ No newline at end of file diff --git a/src/Fasodev/Sdk/PaymentSDK.php b/src/Sdk/PaymentSDK.php similarity index 77% rename from src/Fasodev/Sdk/PaymentSDK.php rename to src/Sdk/PaymentSDK.php index 0c4187f..333e505 100644 --- a/src/Fasodev/Sdk/PaymentSDK.php +++ b/src/Sdk/PaymentSDK.php @@ -9,7 +9,7 @@ namespace Fasodev\Sdk; -use Fasodev\Exceptions\PaymentSDKException; +use Fasodev\Sdk\Exception\TransactionException; /** * Class PaymentSDK @@ -20,7 +20,7 @@ class PaymentSDK /** * @var TransactionInterface */ - protected $transaction; + protected TransactionInterface $transaction; /** * PaymentSDK constructor. @@ -32,10 +32,10 @@ public function __construct(TransactionInterface $transaction) } /** - * @return mixed - * @throws PaymentSDKException + * @return TransactionResponse + * @throws TransactionException */ - public function handlePayment() + public function handlePayment(): TransactionResponse { return $this->transaction->processPayment(); } diff --git a/src/Fasodev/Sdk/TransactionInterface.php b/src/Sdk/TransactionInterface.php similarity index 75% rename from src/Fasodev/Sdk/TransactionInterface.php rename to src/Sdk/TransactionInterface.php index 9f89975..e9ac457 100644 --- a/src/Fasodev/Sdk/TransactionInterface.php +++ b/src/Sdk/TransactionInterface.php @@ -10,6 +10,7 @@ namespace Fasodev\Sdk; use Fasodev\Exceptions\PaymentSDKException; +use Fasodev\Sdk\Exception\TransactionException; /** * Interface TransactionInterface @@ -19,7 +20,7 @@ interface TransactionInterface { /** * @return mixed - * @throws PaymentSDKException + * @throws TransactionException */ - public function processPayment(); + public function processPayment() : TransactionResponseInterface; } \ No newline at end of file diff --git a/src/Sdk/TransactionResponse.php b/src/Sdk/TransactionResponse.php new file mode 100644 index 0000000..149374b --- /dev/null +++ b/src/Sdk/TransactionResponse.php @@ -0,0 +1,49 @@ +transactionId = $transactionId; + $this->message = $message; + $this->status = $status; + } + + public static function fromXMLResponse($xmlResponse): self + { + return new self((int)$xmlResponse->status, (string)$xmlResponse->message, (string)$xmlResponse->transID); + } + + /** + * @inheritdoc + */ + public function getStatus(): int + { + return $this->status; + } + + /** + * @inheritdoc + */ + public function getTransactionId(): string + { + return $this->transactionId; + } + + + /** + * @inheritdoc + */ + public function getMessage(): string + { + return $this->message; + } + + +} \ No newline at end of file diff --git a/src/Sdk/TransactionResponseInterface.php b/src/Sdk/TransactionResponseInterface.php new file mode 100644 index 0000000..3dac6bc --- /dev/null +++ b/src/Sdk/TransactionResponseInterface.php @@ -0,0 +1,24 @@ +assertInstanceOf(OrangeMoneyAPI::class, $this->orangeMoneyAPI); } diff --git a/tests/PaymentSDKTest.php b/tests/PaymentSDKTest.php index 31fada8..c7aef08 100644 --- a/tests/PaymentSDKTest.php +++ b/tests/PaymentSDKTest.php @@ -2,8 +2,12 @@ namespace Fasodev\Tests; +use Exception; +use Fasodev\Sdk\Config\TransactionData; +use Fasodev\Sdk\Exception\TransactionException; use Fasodev\Sdk\OrangeMoneyAPI; use Fasodev\Sdk\PaymentSDK; +use PHPUnit\Framework\TestCase; /** * Class PaymentSDKTest @@ -12,33 +16,32 @@ */ class PaymentSDKTest extends TestCase { - /** - * @var PaymentSDK - */ - private $paymentSDK; protected function setUp(): void { parent::setUp(); - - $orangeMoneyApi = $this->createStub(OrangeMoneyAPI::class); - - $this->paymentSDK = new PaymentSDK($orangeMoneyApi); } - /** - * @test - */ - public function canInstantiateWithoutAnError() + + public function testHandlePaymentWillTrowTransactionExceptionWithInvalidData() { - $this->assertInstanceOf(PaymentSDK::class, $this->paymentSDK); + $this->expectException(TransactionException::class); + $orangeApi = (new OrangeMoneyAPI("", "", "")) + ->withTransactionData(TransactionData::from("00000000", "1000", "124893")) + ->withCustomReference("124869354") + ->useDevApi(); + $paymentSDK = new PaymentSDK($orangeApi); + $paymentSDK->handlePayment(); } - /** - * @test - */ - public function hasAttributeTransaction() + public function testHandlePaymentWillReturnTransactionResponse() { - $this->assertClassHasAttribute('transaction',PaymentSDK::class); + $orangeApi = (new OrangeMoneyAPI("", "", "")) + ->withTransactionData(TransactionData::from("00000000", "1000", "124893")) + ->withCustomReference("124869354") + ->withoutSSLVerification() + ->useDevApi(); + $paymentSDK = new PaymentSDK($orangeApi); + $paymentSDK->handlePayment(); } } diff --git a/tests/TestCase.php b/tests/TestCase.php deleted file mode 100644 index ebd8a7b..0000000 --- a/tests/TestCase.php +++ /dev/null @@ -1,15 +0,0 @@ -