Skip to content

Commit

Permalink
Credo payments initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ezeasorekene committed Apr 15, 2023
1 parent f223343 commit a52b55a
Show file tree
Hide file tree
Showing 5 changed files with 312 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor/
66 changes: 65 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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));

```
20 changes: 20 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
77 changes: 77 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

149 changes: 149 additions & 0 deletions src/CredoPay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php

namespace ezeasorekene\CredoPayments;

use Curl\Curl;

class CredoPay
{

protected $publicKey;

protected $secretKey;

protected $baseUrl;

protected $response;

public $debug = false;

public $responseType = 'json';

public function __construct($apiKeys = [], $mode = 'LIVE')
{
$this->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;
}

}

0 comments on commit a52b55a

Please sign in to comment.