From 48f4116f232ab359387bebb95697ec16210eedfd Mon Sep 17 00:00:00 2001 From: Mpande Andrew Date: Sat, 20 Apr 2019 23:17:29 +0300 Subject: [PATCH] added array response for transactions --- composer.lock | 187 ++++++++++++++++- readme.md | 274 +++++++++++++++++++++++++ src/Products/Collection.php | 20 +- src/Products/Disbursement.php | 19 +- src/Products/Product.php | 132 ++++++------ src/Products/Remittance.php | 6 +- src/Responses/ApiKey.php | 26 +++ src/Responses/ApiUser.php | 39 ++++ src/Responses/Balance.php | 48 +++++ src/Responses/Response.php | 17 ++ src/Responses/Token.php | 67 ++++++ src/Responses/TransactionStatus.php | 139 +++++++++++++ src/Traits/Configurations.php | 36 ++++ src/Traits/SandboxUserProvisioning.php | 24 ++- 14 files changed, 935 insertions(+), 99 deletions(-) create mode 100644 readme.md create mode 100644 src/Responses/ApiKey.php create mode 100644 src/Responses/ApiUser.php create mode 100644 src/Responses/Balance.php create mode 100644 src/Responses/Response.php create mode 100644 src/Responses/Token.php create mode 100644 src/Responses/TransactionStatus.php create mode 100644 src/Traits/Configurations.php diff --git a/composer.lock b/composer.lock index 4c37909..f08ee84 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "c9f566c284ab79aab45d7b20c36c956b", + "content-hash": "90470f16c06deffd1db60c205d333b2d", "packages": [ { "name": "guzzlehttp/guzzle", @@ -189,6 +189,51 @@ ], "time": "2018-12-04T20:46:45+00:00" }, + { + "name": "paragonie/random_compat", + "version": "v9.99.99", + "source": { + "type": "git", + "url": "https://github.com/paragonie/random_compat.git", + "reference": "84b4dfb120c6f9b4ff7b3685f9b8f1aa365a0c95" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/paragonie/random_compat/zipball/84b4dfb120c6f9b4ff7b3685f9b8f1aa365a0c95", + "reference": "84b4dfb120c6f9b4ff7b3685f9b8f1aa365a0c95", + "shasum": "" + }, + "require": { + "php": "^7" + }, + "require-dev": { + "phpunit/phpunit": "4.*|5.*", + "vimeo/psalm": "^1" + }, + "suggest": { + "ext-libsodium": "Provides a modern crypto API that can be used to generate random bytes." + }, + "type": "library", + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Paragon Initiative Enterprises", + "email": "security@paragonie.com", + "homepage": "https://paragonie.com" + } + ], + "description": "PHP 5.x polyfill for random_bytes() and random_int() from PHP 7", + "keywords": [ + "csprng", + "polyfill", + "pseudorandom", + "random" + ], + "time": "2018-07-02T15:55:56+00:00" + }, { "name": "psr/http-message", "version": "1.0.1", @@ -278,6 +323,146 @@ ], "description": "A polyfill for getallheaders.", "time": "2016-02-11T07:05:27+00:00" + }, + { + "name": "ramsey/uuid", + "version": "3.8.0", + "source": { + "type": "git", + "url": "https://github.com/ramsey/uuid.git", + "reference": "d09ea80159c1929d75b3f9c60504d613aeb4a1e3" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/ramsey/uuid/zipball/d09ea80159c1929d75b3f9c60504d613aeb4a1e3", + "reference": "d09ea80159c1929d75b3f9c60504d613aeb4a1e3", + "shasum": "" + }, + "require": { + "paragonie/random_compat": "^1.0|^2.0|9.99.99", + "php": "^5.4 || ^7.0", + "symfony/polyfill-ctype": "^1.8" + }, + "replace": { + "rhumsaa/uuid": "self.version" + }, + "require-dev": { + "codeception/aspect-mock": "^1.0 | ~2.0.0", + "doctrine/annotations": "~1.2.0", + "goaop/framework": "1.0.0-alpha.2 | ^1.0 | ~2.1.0", + "ircmaxell/random-lib": "^1.1", + "jakub-onderka/php-parallel-lint": "^0.9.0", + "mockery/mockery": "^0.9.9", + "moontoast/math": "^1.1", + "php-mock/php-mock-phpunit": "^0.3|^1.1", + "phpunit/phpunit": "^4.7|^5.0|^6.5", + "squizlabs/php_codesniffer": "^2.3" + }, + "suggest": { + "ext-ctype": "Provides support for PHP Ctype functions", + "ext-libsodium": "Provides the PECL libsodium extension for use with the SodiumRandomGenerator", + "ext-uuid": "Provides the PECL UUID extension for use with the PeclUuidTimeGenerator and PeclUuidRandomGenerator", + "ircmaxell/random-lib": "Provides RandomLib for use with the RandomLibAdapter", + "moontoast/math": "Provides support for converting UUID to 128-bit integer (in string form).", + "ramsey/uuid-console": "A console application for generating UUIDs with ramsey/uuid", + "ramsey/uuid-doctrine": "Allows the use of Ramsey\\Uuid\\Uuid as Doctrine field type." + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.x-dev" + } + }, + "autoload": { + "psr-4": { + "Ramsey\\Uuid\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Marijn Huizendveld", + "email": "marijn.huizendveld@gmail.com" + }, + { + "name": "Thibaud Fabre", + "email": "thibaud@aztech.io" + }, + { + "name": "Ben Ramsey", + "email": "ben@benramsey.com", + "homepage": "https://benramsey.com" + } + ], + "description": "Formerly rhumsaa/uuid. A PHP 5.4+ library for generating RFC 4122 version 1, 3, 4, and 5 universally unique identifiers (UUID).", + "homepage": "https://github.com/ramsey/uuid", + "keywords": [ + "guid", + "identifier", + "uuid" + ], + "time": "2018-07-19T23:38:55+00:00" + }, + { + "name": "symfony/polyfill-ctype", + "version": "v1.11.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-ctype.git", + "reference": "82ebae02209c21113908c229e9883c419720738a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/82ebae02209c21113908c229e9883c419720738a", + "reference": "82ebae02209c21113908c229e9883c419720738a", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "suggest": { + "ext-ctype": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.11-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Ctype\\": "" + }, + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + }, + { + "name": "Gert de Pagter", + "email": "backendtea@gmail.com" + } + ], + "description": "Symfony polyfill for ctype functions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "ctype", + "polyfill", + "portable" + ], + "time": "2019-02-06T07:57:58+00:00" } ], "packages-dev": [], diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..2df8ba3 --- /dev/null +++ b/readme.md @@ -0,0 +1,274 @@ +## Momo + +Momo is a simple library in PHP for the MTN Mobile Money Open API. + +## Product support + +* Collections +* Disbursements +* Remittances + +## Getting Started + +* Signup For An Account +* Subscribe To Products + +## Signup For An Account + +Follow this link to our developer portal and [signup](https://momodeveloper.mtn.com/signup.html) +for an account. + +## Subscribe To Products + +On the [Products](https://momodeveloper.mtn.com/products.html) page on developer portal you should see items you can subscribe to: + +* Collections +* Disbursements +* Remittances + +## Installation + +The recommended way to install Momo is through [composer](http://getcomposer.org). + +Just create a composer.json file for your project and require it: + +```TERMINAL +composer require fannypack/momo +``` +Now you can add the autoloader, and you will have access to the library: + +```php + '', //(optional) default is http://localhost:8000 + // 'callbackUrl' => '', //(optional) default is http://localhost:8000/callback + // 'environment' => '', //(optional) default is sandbox + // 'accountHolderIdType' => '', //(optional) default is msisdn + 'subscriptionKey' => '', //Product Subscription key + 'xReferenceId' => '', //Api user reference id + 'apiKey' => '', // Api user key + //'preApproval' => '', //(optional) default is false + //'accessToken' => '' //Required for transactions +]; + +// Using collection +$collection = Collection::create($options); + +// Using disbursement +$disbursement = Disbursement::create($options); + +// Using remittance +$remittance = Remittance::create($options); + +``` + +### Sandbox User Provisioning +#### Create API User +```php +createApiUser(); //{"statusCode": 201} + +``` + +#### GET API User Details +```php +getApiUser(); +$apiUser->getProviderCallbackHost(); //http://localhost:8000 +$apiUser->getTargetEnvironment(); //sandbox + +``` + +#### Create API Key +```php +createApiKey(); +$apiKey->getApiKey(); //apiKey + +``` + +### Oauth 2.0 +#### Get token +```php +getToken(); +$token->getAccessToken(); //accessToken +$token->getTokenType(); //tokenType +$token->getExpiresIn(); //expiry in seconds + +``` + +### Transactions +#### Get account balance +```php +getAccountBalance(); +$balance->getAvailableBalance(); //accountBalance +$balance->getCurrency(); //currency + +``` +#### Get account holder status +```php +getAccountHolderInfo($accountHolderId); //{"statusCode": 201} + +``` + +### Collections +#### Request to pay +```php +requestToPay($externalId, $partyId, $amount, $currency, $payerMessage = '', $payeeNote = ''); // {"statusCode": 200, "financialTransactionId": "8f3077a6-ce43-4584-a425-589c50cfcbe4"} + +``` +#### Request to pay status +```php +getRequestToPayStatus($financialTransactionId); +$transactionStatus->getAmount(); //amount +$transactionStatus->getCurrency(); //currency +$transactionStatus->getExternalId(); //externalId +$transactionStatus->getPayer(); //payer object +$transactionStatus->getStatus(); //status + +``` +### Disbursements and Remittances +#### Transfer +```php +transfer($externalId, $partyId, $amount, $currency, $payerMessage = '', $payeeNote = '');// {"statusCode": 200, "financialTransactionId": "8f3077a6-ce43-4584-a425-589c50cfcbe4"} + +``` +#### Transfer status +```php +transferStatus($financialTransactionId); +$transactionStatus->getAmount(); //amount +$transactionStatus->getCurrency(); //currency +$transactionStatus->getExternalId(); //externalId +$transactionStatus->getPayer(); //payer object +$transactionStatus->getStatus(); //status + +``` + +### Bugs +For any bugs found, please email me at andrewmvp007@gmail.com or register an issue at [issues](https://github.com/mpaannddreew/momo/issues) + diff --git a/src/Products/Collection.php b/src/Products/Collection.php index 6954d80..89e81cd 100644 --- a/src/Products/Collection.php +++ b/src/Products/Collection.php @@ -7,6 +7,7 @@ */ namespace FannyPack\Momo\Products; +use FannyPack\Momo\Responses\TransactionStatus; /** * Class Collection @@ -18,13 +19,16 @@ class Collection extends Product const PRE_APPROVAL_URI = "/v1_0/preapproval"; + /** + * @var bool $preApproval + */ protected $preApproval = false; - protected function transactionUrl() { - return $this->getProductBaseUrl() . ($this->preApproval ? self::PRE_APPROVAL_URI: self::REQUEST_TO_PAY_URI); + protected function getProductBaseUrl() { + return self::BASE_URL . "/collection"; } - protected function transactionStatusUrl() { + protected function transactionUrl() { return $this->getProductBaseUrl() . ($this->preApproval ? self::PRE_APPROVAL_URI: self::REQUEST_TO_PAY_URI); } @@ -37,7 +41,7 @@ protected function transactionStatusUrl() { * @param $currency * @param string $payerMessage * @param string $payeeNote - * @return mixed + * @return array * @throws \Exception */ public function requestToPay($externalId, $partyId, $amount, $currency, $payerMessage = '', $payeeNote = '') { @@ -47,11 +51,11 @@ public function requestToPay($externalId, $partyId, $amount, $currency, $payerMe /** * Get request to pay transaction status * - * @param $paymentRef - * @return mixed + * @param $financialTransactionId + * @return TransactionStatus * @throws \Exception */ - public function getRequestToPayStatus($paymentRef) { - return $this->getTransactionStatus($paymentRef); + public function getRequestToPayStatus($financialTransactionId) { + return $this->getTransactionStatus($financialTransactionId); } } \ No newline at end of file diff --git a/src/Products/Disbursement.php b/src/Products/Disbursement.php index 50aaf00..1b0ff49 100644 --- a/src/Products/Disbursement.php +++ b/src/Products/Disbursement.php @@ -7,6 +7,7 @@ */ namespace FannyPack\Momo\Products; +use FannyPack\Momo\Responses\TransactionStatus; /** * Class Disbursement @@ -14,13 +15,13 @@ */ class Disbursement extends Product { - const TRANSFER_URI = "transfer"; + const TRANSFER_URI = "/transfer"; - protected function transactionUrl() { - return $this->getProductBaseUrl() . self::TRANSFER_URI; + protected function getProductBaseUrl() { + return self::BASE_URL . "/disbursement"; } - protected function transactionStatusUrl() { + protected function transactionUrl() { return $this->getProductBaseUrl() . self::TRANSFER_URI; } @@ -33,7 +34,7 @@ protected function transactionStatusUrl() { * @param $currency * @param string $payerMessage * @param string $payeeNote - * @return mixed + * @return array * @throws \Exception */ public function transfer($externalId, $partyId, $amount, $currency, $payerMessage = '', $payeeNote = '') { @@ -43,11 +44,11 @@ public function transfer($externalId, $partyId, $amount, $currency, $payerMessag /** * Get transfer transaction status * - * @param $paymentRef - * @return mixed + * @param $financialTransactionId + * @return TransactionStatus * @throws \Exception */ - public function transferStatus($paymentRef) { - return $this->getTransactionStatus($paymentRef); + public function transferStatus($financialTransactionId) { + return $this->getTransactionStatus($financialTransactionId); } } \ No newline at end of file diff --git a/src/Products/Product.php b/src/Products/Product.php index b837eef..8b6cf24 100644 --- a/src/Products/Product.php +++ b/src/Products/Product.php @@ -5,21 +5,14 @@ * Date: 18/04/2019 * Time: 18:39 * - * ProductInstance::create([ - * 'callbackUrl' => '', - * 'environment' => '', - * 'accountHolderIdType' => '', - * 'subscriptionKey' => '', - * 'xReferenceId' => '', - * 'apiKey' => '', - * 'preApproval' => '' - * ]); - * */ namespace FannyPack\Momo\Products; - +use FannyPack\Momo\Responses\Balance; +use FannyPack\Momo\Responses\Token; +use FannyPack\Momo\Responses\TransactionStatus; +use FannyPack\Momo\Traits\Configurations; use FannyPack\Momo\Traits\SandboxUserProvisioning; use GuzzleHttp\Client; use Ramsey\Uuid\Uuid; @@ -30,11 +23,11 @@ */ abstract class Product { - use SandboxUserProvisioning; + use Configurations, SandboxUserProvisioning; const BASE_URL = "https://ericssonbasicapi2.azure-api.net"; - const TOKEN_URI = "/token"; + const TOKEN_URI = "/token/"; const BALANCE_URI = "/v1_0/account/balance"; @@ -42,10 +35,15 @@ abstract class Product const API_USER_URI = "/v1_0/apiuser"; + /** + * @var string $callbackHost + */ + protected $callbackHost = "http://localhost:8000"; + /** * @var string $callbackUrl */ - protected $callbackUrl = "http://localhost:8000"; + protected $callbackUrl = "http://localhost:8000/callback"; /** * @var string $environment @@ -55,7 +53,7 @@ abstract class Product /** * @var string $accountHolderIdType */ - protected $accountHolderIdType = "MSISDN"; + protected $accountHolderIdType = "msisdn"; /** * @var string $subscriptionKey @@ -72,32 +70,23 @@ abstract class Product */ protected $apiKey; + /** + * @var string $accessToken + */ + protected $accessToken; + /** * Product constructor. * @param $options */ public function __construct($options) { - if(!isset($config['subscriptionKey'])) + if(!isset($options['subscriptionKey'])) throw new \InvalidArgumentException("subscriptionKey should be specified"); - if(!isset($config['xReferenceId'])) + if(!isset($options['xReferenceId'])) throw new \InvalidArgumentException("xReferenceId should be specified"); - foreach ($options as $option => $value) { - try{ - $this->{$option} = $value; - }catch (\Exception $exception) {} - } - } - - /** - * New product instance - * - * @param $options - * @return static - */ - public static function create($options) { - return new static($options); + $this->setOptions($options); } /** @@ -116,35 +105,29 @@ protected function newClient() { ]); } - /** - * Get product base url - * - * @return string - */ - protected function getProductBaseUrl() { - $path = explode('\\', __CLASS__); - return self::BASE_URL . "/" . strtolower(array_pop($path)); - } + abstract protected function getProductBaseUrl(); /** * Get token * - * @return mixed + * @return Token * @throws \Exception */ public function getToken() { + if(!$this->apiKey) + throw new \InvalidArgumentException("apiKey should be specified"); + try { - $resource = $this->getProductBaseUrl() . self::TOKEN_URI; - $response = $this->newClient()->post($resource, [ + $response = $this->newClient()->post($this->getProductBaseUrl() . self::TOKEN_URI, [ 'headers' => [ - 'Authorization' => 'Basic '.base64_encode($this->xReferenceId . ':' . $this->apiKey), + 'Authorization' => 'Basic '.base64_encode($this->xReferenceId . ':' . $this->apiKey) ], 'json' => [ 'grant_type' => 'client_credentials', ], ]); - return json_decode($response->getBody(), true); + return Token::create(json_decode($response->getBody(), true)); } catch (\Exception $exception) { throw new \Exception("Unable to generate token"); } @@ -153,19 +136,22 @@ public function getToken() { /** * Get account balance * - * @return mixed + * @return Balance * @throws \Exception */ public function getAccountBalance() { + if(!$this->accessToken) + throw new \InvalidArgumentException("accessToken should be specified"); + try { - $resource = $this->getProductBaseUrl() . self::BALANCE_URI; - $response = $this->newClient()->get($resource, [ + $response = $this->newClient()->get($this->getProductBaseUrl() . self::BALANCE_URI, [ 'headers' => [ + 'Authorization' => 'Bearer ' . $this->accessToken, 'X-Target-Environment' => $this->environment, - ], + ] ]); - return json_decode($response->getBody(), true); + return Balance::create(json_decode($response->getBody(), true)); } catch (\Exception $exception) { throw new \Exception("Unable to get account balance"); } @@ -175,19 +161,23 @@ public function getAccountBalance() { * Get account holder information * * @param $accountHolderId - * @return mixed + * @return array * @throws \Exception */ public function getAccountHolderInfo($accountHolderId) { - $resource = $this->getProductBaseUrl() . self::ACCOUNT_HOLDER_URI . "/" . $this->accountHolderIdType . "/" . $accountHolderId . "/active"; + if(!$this->accessToken) + throw new \InvalidArgumentException("accessToken should be specified"); + try { - $response = $this->newClient()->get($resource, [ + $url = $this->getProductBaseUrl() . self::ACCOUNT_HOLDER_URI . "/" . $this->accountHolderIdType . "/" . $accountHolderId . "/active"; + $response = $this->newClient()->get($url, [ 'headers' => [ + 'Authorization' => 'Bearer ' . $this->accessToken, 'X-Target-Environment' => $this->environment, ], ]); - return json_decode($response->getBody(), true); + return ['statusCode' => $response->getStatusCode()]; } catch (\Exception $exception) { throw new \Exception("Unable to get account holder information"); } @@ -195,8 +185,6 @@ public function getAccountHolderInfo($accountHolderId) { protected abstract function transactionUrl(); - protected abstract function transactionStatusUrl(); - /** * Start a payment transaction * @@ -206,17 +194,21 @@ protected abstract function transactionStatusUrl(); * @param $currency * @param string $payerMessage * @param string $payeeNote - * @return mixed + * @return array * @throws \Exception */ protected function transact($externalId, $partyId, $amount, $currency, $payerMessage = '', $payeeNote = '') { + if(!$this->accessToken) + throw new \InvalidArgumentException("accessToken should be specified"); + try { - $paymentRef = Uuid::uuid4()->toString(); + $financialTransactionId = Uuid::uuid4()->toString(); $response = $this->newClient()->post($this->transactionUrl(), [ 'headers' => [ - 'X-Reference-Id' => $paymentRef, - 'X-Callback-Url' => $this->callbackUrl, + 'X-Reference-Id' => $financialTransactionId, + 'X-Callback-Url' => $this->callbackHost, 'X-Target-Environment' => $this->environment, + 'Authorization' => 'Bearer ' . $this->accessToken ], 'json' => [ 'amount' => $amount, @@ -228,10 +220,9 @@ protected function transact($externalId, $partyId, $amount, $currency, $payerMes ], 'payerMessage' => $payerMessage, 'payeeNote' => $payeeNote, - ], + ] ]); - - return json_decode($response->getBody(), true); + return ["statusCode" => $response->getStatusCode(), 'financialTransactionId' => $financialTransactionId]; } catch (\Exception $exception) { throw new \Exception("Unable to complete transaction"); } @@ -241,19 +232,22 @@ protected function transact($externalId, $partyId, $amount, $currency, $payerMes * Get transaction status * * @param $paymentRef - * @return mixed + * @return TransactionStatus * @throws \Exception */ protected function getTransactionStatus($paymentRef) { + if(!$this->accessToken) + throw new \InvalidArgumentException("accessToken should be specified"); + try { - $resource = $this->transactionStatusUrl() . "/" . $paymentRef; - $response = $this->newClient()->get($resource, [ + $response = $this->newClient()->get($this->transactionUrl() . "/" . $paymentRef, [ 'headers' => [ 'X-Target-Environment' => $this->environment, - ], + 'Authorization' => 'Bearer ' . $this->accessToken + ] ]); - return json_decode($response->getBody(), true); + return TransactionStatus::create(json_decode($response->getBody(), true)); } catch (\Exception $exception) { throw new \Exception("Unable to get transaction status"); } diff --git a/src/Products/Remittance.php b/src/Products/Remittance.php index db5fd51..f94d9b7 100644 --- a/src/Products/Remittance.php +++ b/src/Products/Remittance.php @@ -13,4 +13,8 @@ * @package FannyPack\Momo\Products */ class Remittance extends Disbursement -{} \ No newline at end of file +{ + protected function getProductBaseUrl() { + return self::BASE_URL . "/remittance"; + } +} \ No newline at end of file diff --git a/src/Responses/ApiKey.php b/src/Responses/ApiKey.php new file mode 100644 index 0000000..88668ac --- /dev/null +++ b/src/Responses/ApiKey.php @@ -0,0 +1,26 @@ +apiKey; + } +} \ No newline at end of file diff --git a/src/Responses/ApiUser.php b/src/Responses/ApiUser.php new file mode 100644 index 0000000..d5651ed --- /dev/null +++ b/src/Responses/ApiUser.php @@ -0,0 +1,39 @@ +providerCallbackHost; + } + + /** + * @return string + */ + public function getTargetEnvironment() + { + return $this->targetEnvironment; + } +} \ No newline at end of file diff --git a/src/Responses/Balance.php b/src/Responses/Balance.php new file mode 100644 index 0000000..1f01d53 --- /dev/null +++ b/src/Responses/Balance.php @@ -0,0 +1,48 @@ +setOptions($options); + } + + /** + * @return string + */ + public function getAvailableBalance() + { + return $this->availableBalance; + } + + /** + * @return string + */ + public function getCurrency() + { + return $this->currency; + } +} \ No newline at end of file diff --git a/src/Responses/Response.php b/src/Responses/Response.php new file mode 100644 index 0000000..27cc343 --- /dev/null +++ b/src/Responses/Response.php @@ -0,0 +1,17 @@ +setOptions($options); + } + + /** + * Access token + * + * @return string + */ + public function getAccessToken() + { + return $this->access_token; + } + + /** + * Token type + * + * @return string + */ + public function getTokenType() + { + return $this->token_type; + } + + /** + * Expiry in seconds + * + * @return int + */ + public function getExpiresIn() + { + return $this->expires_in; + } +} \ No newline at end of file diff --git a/src/Responses/TransactionStatus.php b/src/Responses/TransactionStatus.php new file mode 100644 index 0000000..cbce7f4 --- /dev/null +++ b/src/Responses/TransactionStatus.php @@ -0,0 +1,139 @@ +setOptions($options); + } + + /** + * @return string + */ + public function getAmount() + { + return $this->amount; + } + + /** + * @return string + */ + public function getCurrency() + { + return $this->currency; + } + + /** + * @return string + */ + public function getFinancialTransactionId() + { + return $this->financialTransactionId; + } + + /** + * @return string + */ + public function getExternalId() + { + return $this->externalId; + } + + /** + * @return array + */ + public function getPayer() + { + return $this->payer; + } + + /** + * @return string + */ + public function getPayerMessage() + { + return $this->payerMessage; + } + + /** + * @return string + */ + public function getPayeeNote() + { + return $this->payeeNote; + } + + /** + * @return string + */ + public function getStatus() + { + return $this->status; + } + + /** + * @return array + */ + public function getReason() + { + return $this->reason; + } +} \ No newline at end of file diff --git a/src/Traits/Configurations.php b/src/Traits/Configurations.php new file mode 100644 index 0000000..f4e3fdc --- /dev/null +++ b/src/Traits/Configurations.php @@ -0,0 +1,36 @@ + $value) { + try{ + $this->{$option} = $value; + }catch (\Exception $exception) {} + } + } +} \ No newline at end of file diff --git a/src/Traits/SandboxUserProvisioning.php b/src/Traits/SandboxUserProvisioning.php index 286274c..5f1d142 100644 --- a/src/Traits/SandboxUserProvisioning.php +++ b/src/Traits/SandboxUserProvisioning.php @@ -7,6 +7,8 @@ */ namespace FannyPack\Momo\Traits; +use FannyPack\Momo\Responses\ApiKey; +use FannyPack\Momo\Responses\ApiUser; /** @@ -18,17 +20,17 @@ trait SandboxUserProvisioning /** * Create an api user * - * @return mixed + * @return string * @throws \Exception */ public function createApiUser() { try { $response = $this->newClient()->post(self::BASE_URL . self::API_USER_URI, [ 'headers' => ['X-Reference-Id' => $this->xReferenceId], - 'json' => ['providerCallbackHost' => $this->callbackUrl] + 'json' => ['providerCallbackHost' => $this->callbackHost] ]); - return json_decode($response->getBody(), true); - } catch (\Exception $e) { + return json_encode(['statusCode' => $response->getStatusCode()]); + } catch (\Exception $exception) { throw new \Exception("Unable to create an api user"); } } @@ -36,14 +38,14 @@ public function createApiUser() { /** * Validate api user * - * @return mixed + * @return ApiUser * @throws \Exception */ - public function validateApiUser() { + public function getApiUser() { try { $response = $this->newClient()->get(self::BASE_URL . self::API_USER_URI . "/" . $this->xReferenceId); - return json_decode($response->getBody(), true); - } catch (\Exception $e) { + return ApiUser::create(json_decode($response->getBody(), true)); + } catch (\Exception $exception) { throw new \Exception("Unable to validate api user"); } } @@ -51,14 +53,14 @@ public function validateApiUser() { /** * Create api key * - * @return mixed + * @return ApiKey * @throws \Exception */ public function createApiKey() { try { $response = $this->newClient()->post(self::BASE_URL . self::API_USER_URI . "/" . $this->xReferenceId . "/apikey"); - return json_decode($response->getBody(), true); - } catch (\Exception $e) { + return ApiKey::create(json_decode($response->getBody(), true)); + } catch (\Exception $exception) { throw new \Exception("Unable to create api key"); } }