diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..57872d0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/vendor/ diff --git a/README.md b/README.md index 45259ac..33ed0a7 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,66 @@ -# credo-payments +# Credo Payments + This package is for communicating with credocentral.com and credodemo.com API + +## Getting Started + +Install the package using composer. + +Run `composer require ezeasorekene/credo-payments` + +### Define your api keys + +Define your `$apiKeys` variable as an array with the public and private keys. You should get the keys from environment variables. Also define `CREDO_MODE` environment key with parameters `DEMO | LIVE` + +```php + $apiKeys = [ + 'publicKey' => getenv("CREDO_MODE") == 'LIVE' ? getenv("CREDO_LIVE_PUBLIC_KEY") : getenv("CREDO_TEST_PUBLIC_KEY"), + 'secretKey' => getenv("CREDO_MODE") == 'LIVE' ? getenv("CREDO_LIVE_SECRET_KEY") : getenv("CREDO_TEST_SECRET_KEY"), + ]; +``` + +### Instantiating Transaction + +```php + $credo = new CredoPay( $apiKeys, getenv("CREDO_MODE") ); +``` + +Set data to post using array + +```php + $credodata = [ + 'reference' => uniqid(), + 'amount' => 20000, //in kobo + 'bearer' => 0, + 'callbackUrl' => 'http://example.com/callback/credo', + 'currency' => 'NGN', + 'customerFirstName' => 'Credo', + 'customerLastName' => 'Package', + 'customerPhoneNumber' => '234080111111111', + 'email' => 'credotest@gmail.com', + 'channels' => array('card', 'bank'), + "metadata" => [ + "customFields" => [ + [ + 'display_name' => 'Custom Field Display 1', + 'variable_name' => 'Custom Field Variable 1', + 'value' => 'Custom Field Value 1' + ] + ] + ], + ]; +``` + +Response is a JSON encoded object. So to decode, perform a `json_decode` + +```php + $response = json_decode($credo->initialize($credodata)); +``` + +### Verifying Transaction + +```php + $credo = new CredoPay( $apiKeys, getenv("CREDO_MODE") ); + $verify = json_decode($credo->verify($reference)); + +``` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..7af427f --- /dev/null +++ b/composer.json @@ -0,0 +1,20 @@ +{ + "name": "ezeasorekene/credo-payments", + "description": "This package is for communicating with credocentral.com and credodemo.com API", + "type": "library", + "require": { + "curl/curl": "^2.5" + }, + "license": "MIT", + "autoload": { + "psr-4": { + "Ezeasorekene\\CredoPayments\\": "src/" + } + }, + "authors": [ + { + "name": "Ekene Ezeasor", + "email": "ezeasorekene@gmail.com" + } + ] +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..1f27a3e --- /dev/null +++ b/composer.lock @@ -0,0 +1,77 @@ +{ + "_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#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "f5e724367c25f9076ea01f5e3f734b49", + "packages": [ + { + "name": "curl/curl", + "version": "2.5.0", + "source": { + "type": "git", + "url": "https://github.com/php-mod/curl.git", + "reference": "c4f8799c471e43b7c782c77d5c6e178d0465e210" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-mod/curl/zipball/c4f8799c471e43b7c782c77d5c6e178d0465e210", + "reference": "c4f8799c471e43b7c782c77d5c6e178d0465e210", + "shasum": "" + }, + "require": { + "ext-curl": "*", + "php": "^5.6 | ^7.0 | ^8.0" + }, + "require-dev": { + "yoast/phpunit-polyfills": "^0.2.0" + }, + "type": "library", + "autoload": { + "psr-0": { + "Curl": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "php-curl-class", + "homepage": "https://github.com/php-curl-class" + }, + { + "name": "Hassan Amouhzi", + "email": "hassan@anezi.net", + "homepage": "http://hassan.amouhzi.com" + }, + { + "name": "user52", + "homepage": "https://github.com/user52" + } + ], + "description": "cURL class for PHP", + "homepage": "https://github.com/php-mod/curl", + "keywords": [ + "curl", + "dot" + ], + "support": { + "issues": "https://github.com/php-mod/curl/issues", + "source": "https://github.com/php-mod/curl/tree/2.5.0" + }, + "time": "2022-12-14T13:27:59+00:00" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [], + "plugin-api-version": "2.3.0" +} diff --git a/src/CredoPay.php b/src/CredoPay.php new file mode 100644 index 0000000..7314174 --- /dev/null +++ b/src/CredoPay.php @@ -0,0 +1,149 @@ +publicKey = isset($apiKeys['publicKey']) ? $apiKeys['publicKey'] : null; + $this->secretKey = isset($apiKeys['secretKey']) ? $apiKeys['secretKey'] : null; + $this->baseUrl = $mode == 'LIVE' ? 'https://api.credocentral.com' : 'https://api.public.credodemo.com'; + } + + public function initialize(array $data = []) + { + $initialize = new Curl(); + + $initialize->setHeader('Authorization', $this->publicKey); + $initialize->setHeader('Content-Type', 'application/json'); + + $initialize->post($this->baseUrl . "/transaction/initialize", json_encode($data)); + + $responseObject = json_decode($initialize->response); + $responseArray = json_decode($initialize->response, true); + + if ($responseObject->status == 200) { + if ($this->responseType == 'array') { + $data = [ + 'status' => $responseArray['status'], + 'message' => $responseArray['message'], + 'data' => [ + 'authorizationUrl' => $responseArray['data']['authorizationUrl'], + 'reference' => $responseArray['data']['reference'], + 'credoReference' => $responseArray['data']['credoReference'], + ] + ]; + } else { + $data = json_encode([ + 'status' => $responseObject->status, + 'message' => $responseObject->message, + 'data' => [ + 'authorizationUrl' => $responseObject->data->authorizationUrl, + 'reference' => $responseObject->data->reference, + 'credoReference' => $responseObject->data->credoReference, + ] + ]); + } + } else { + if ($this->responseType == 'array') { + $data = [ + 'status' => $responseArray['status'], + 'message' => $responseArray['message'], + 'error' => isset($responseArray['error']) ? $responseArray['error'] : '', + ]; + } else { + $data = json_encode([ + 'status' => $responseObject->status, + 'message' => $responseObject->message, + 'error' => isset($responseObject->error) ? $responseObject->error : '', + ]); + } + } + + return $data; + } + + public function verify(string $reference) + { + $verify = new Curl(); + + $verify->setHeader('Authorization', $this->secretKey); + $verify->setHeader('Content-Type', 'application/json'); + + $verify->get($this->baseUrl . "/transaction/{$reference}/verify"); + + $responseObject = json_decode($verify->response); + $responseArray = json_decode($verify->response, true); + + if ($responseObject->status == 200) { + if ($this->responseType == 'array') { + $data = [ + 'status' => $responseArray['status'], + 'message' => $responseArray['message'], + 'data' => [ + 'email' => $responseArray['data']['customerId'], + 'transactionDate' => isset($responseArray['data']['transactionDate']) ? $responseArray['data']['transactionDate'] : '', + 'reference' => $responseArray['data']['businessRef'], + 'credoReference' => $responseArray['data']['transRef'], + 'currencyCode' => $responseArray['data']['currencyCode'], + 'debitedAmount' => $responseArray['data']['debitedAmount'], + 'transAmount' => $responseArray['data']['transAmount'], + 'transFeeAmount' => $responseArray['data']['transFeeAmount'], + 'metadata' => $responseArray['data']['metadata'], + 'status' => $responseArray['data']['status'], + ] + ]; + } else { + $data = json_encode([ + 'status' => $responseObject->status, + 'message' => $responseObject->message, + 'data' => [ + 'email' => $responseObject->data->customerId, + 'transactionDate' => isset($responseObject->data->transactionDate) ? $responseObject->data->transactionDate : '', + 'reference' => $responseObject->data->businessRef, + 'credoReference' => $responseObject->data->transRef, + 'currencyCode' => $responseObject->data->currencyCode, + 'debitedAmount' => $responseObject->data->debitedAmount, + 'transAmount' => $responseObject->data->transAmount, + 'transFeeAmount' => $responseObject->data->transFeeAmount, + 'metadata' => $responseObject->data->metadata, + 'status' => $responseObject->data->status, + ] + ]); + } + } else { + if ($this->responseType == 'array') { + $data = [ + 'status' => $responseArray['status'], + 'message' => $responseArray['message'], + 'error' => isset($responseArray['error']) ? $responseArray['error'] : '', + ]; + } else { + $data = json_encode([ + 'status' => $responseObject->status, + 'message' => $responseObject->message, + 'error' => isset($responseObject->error) ? $responseObject->error : '', + ]); + } + } + + return $data; + } + +} \ No newline at end of file