Skip to content

Commit e3f60e7

Browse files
authored
Merge pull request #1 from jtad009/develop
PayStack Cakephp Plugin
2 parents 23a78be + fc6222f commit e3f60e7

File tree

7 files changed

+244
-2
lines changed

7 files changed

+244
-2
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,18 @@ Step 1: Include the following code into your AppController.php to load the payst
5151
$this->loadComponent('PayStack.CurlConnection');
5252
?>
5353

54-
55-
Step 2: in your controller create an action, mine will be PurchaseSMS()
54+
Step 2:create view using the following code
55+
56+
<?php
57+
//Note This form will be setup as per you requirement. in my case i needed to pay for sms units
58+
echo $this->Form->create(null,['url'=>['controller'=>'as-per-requirement','action'=>'purchase-sms']]);
59+
echo $this->Form->input('amount',['templates'=>['inputContainer'=>'<div class="form-group">{{content}}<p class=" mb-3 mt-2"> <span id="allocatedUnits" class="text-danger pull-right small ">0 </span><span class="small pull-right text-muted mr-2 ">UNIT(S) Worth: </span><span class="small text-muted mr-2"> Send to </span><span id="reach" class="text-danger small ">0 </span></p></div>'],'class'=>'form-control','style'=>'resize:none','maxlength'=>"290",'options'=>['500'=>'500','1000'=>'1000','1500'=>'1500','2000'=>'2000','3000'=>'3000','5000'=>'5000','7000'=>'7000','10000'=>'10000'],'empty'=>'Select amount you want to pay','id'=>'sms-amount']);
60+
echo $this->Form->submit('PURCHASE UNITS',['class'=>'btn btn-sm btn-danger btn-block mt-2 ']);
61+
echo $this->Form->end();
62+
63+
?>
64+
65+
Step 3: in your controller create an action, mine will be PurchaseSMS()
5666

5767
<?php
5868

composer.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "jtad009/PayStack",
3+
"description": "Paystack plugin for CakePHP",
4+
"type": "cakephp-plugin",
5+
"require": {
6+
"php": ">=5.5.9",
7+
"cakephp/cakephp": ">=3.3.2 <4.0.0"
8+
},
9+
"require-dev": {
10+
"phpunit/phpunit": "*"
11+
},
12+
"autoload": {
13+
"psr-4": {
14+
"CurlManager\\": "src"
15+
}
16+
},
17+
"autoload-dev": {
18+
"psr-4": {
19+
"CurlManager\\Test\\": "tests",
20+
"Cake\\Test\\": "./vendor/cakephp/cakephp/tests"
21+
}
22+
}
23+
}

config/routes.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
use Cake\Routing\RouteBuilder;
3+
use Cake\Routing\Router;
4+
use Cake\Routing\Route\DashedRoute;
5+
6+
Router::plugin(
7+
'CurlManager',
8+
['path' => '/curl-manager'],
9+
function (RouteBuilder $routes) {
10+
$routes->fallbacks(DashedRoute::class);
11+
}
12+
);

phpunit.xml.dist

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
colors="true"
4+
processIsolation="false"
5+
stopOnFailure="false"
6+
syntaxCheck="false"
7+
bootstrap="./tests/bootstrap.php"
8+
>
9+
<php>
10+
<ini name="memory_limit" value="-1"/>
11+
<ini name="apc.enable_cli" value="1"/>
12+
</php>
13+
14+
<!-- Add any additional test suites you want to run here -->
15+
<testsuites>
16+
<testsuite name="CurlManager Test Suite">
17+
<directory>./tests/TestCase</directory>
18+
</testsuite>
19+
</testsuites>
20+
21+
<!-- Setup a listener for fixtures -->
22+
<listeners>
23+
<listener
24+
class="\Cake\TestSuite\Fixture\FixtureInjector"
25+
file="./vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureInjector.php">
26+
<arguments>
27+
<object class="\Cake\TestSuite\Fixture\FixtureManager" />
28+
</arguments>
29+
</listener>
30+
</listeners>
31+
32+
<!-- Prevent coverage reports from looking in tests and vendors -->
33+
<filter>
34+
<blacklist>
35+
<directory suffix=".php">./vendor/</directory>
36+
<directory suffix=".ctp">./vendor/</directory>
37+
38+
<directory suffix=".php">./tests/</directory>
39+
<directory suffix=".ctp">./tests/</directory>
40+
</blacklist>
41+
</filter>
42+
43+
</phpunit>

src/Controller/AppController.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace PayStack\Controller;
4+
5+
use App\Controller\AppController as BaseController;
6+
7+
class AppController extends BaseController
8+
{
9+
10+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
namespace PayStack\Controller\Component;
3+
4+
use Cake\Controller\Component;
5+
use Cake\Controller\ComponentRegistry;
6+
7+
/**
8+
* CurlConnection component
9+
*/
10+
class CurlConnectionComponent extends Component
11+
{
12+
13+
/**
14+
* Default configuration.
15+
*
16+
* @var array
17+
*/
18+
protected $_defaultConfig = [];
19+
20+
21+
public function urlOpen($url) {
22+
// Fake the browser type
23+
ini_set('user_agent', 'MSIE 4\.0b2;');
24+
$dh = fopen("$url", 'r') != FALSE ? fopen("$url", 'r') : "0";
25+
$result = fread($dh, 8192);
26+
return $result;
27+
}
28+
/**
29+
* Using CURL to open connection outside of this aplication
30+
* @param array $options indicates the data sent out of this App
31+
*/
32+
public function payStackConnection($options){
33+
$curl = curl_init();
34+
curl_setopt_array($curl,$options);
35+
$response = curl_exec($curl);
36+
$err = curl_error($curl);
37+
curl_close($curl);
38+
if($err){
39+
// there was an error contacting the Paystack API
40+
die('Curl returned error: ' . $err);
41+
}else{
42+
return $response;
43+
}
44+
}
45+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/*
3+
* This file is part of the Laravel Paystack package.
4+
*
5+
* (c) israel Edet <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PayStack\Controller\Component;
11+
12+
use Cake\Controller\Component;
13+
use Cake\Controller\ComponentRegistry;
14+
15+
/**
16+
* PayStack component
17+
*/
18+
class PayStackComponent extends Component
19+
{
20+
21+
/**
22+
* Default configuration.
23+
*
24+
* @var array
25+
*/
26+
protected $_defaultConfig = [];
27+
public $components = ['CurlConnection'];
28+
29+
private $secretKey = PaystackTestSecretKey;
30+
private $publicKey = PaystackTestPublicKey;
31+
private $url = "https://api.paystack.co/transaction/initialize";
32+
private $verifyUrl = "https://api.paystack.co/transaction/verify/";
33+
34+
/* Initiate a payment request to Paystack
35+
* Included the option to pass the payload to this method for situations
36+
* when the payload is built on the fly (not passed to the controller from a view)
37+
* @param payload to be sent to paystack
38+
* @return array|false
39+
*/
40+
public function payWithPaystack($postdata){
41+
$result = array();
42+
43+
$options = array(
44+
CURLOPT_URL => $this->url,
45+
CURLOPT_POST => 1,
46+
CURLOPT_POSTFIELDS=>json_encode($postdata),
47+
CURLOPT_RETURNTRANSFER=>true,
48+
CURLOPT_HTTPHEADER => [
49+
'Authorization: Bearer '.$this->secretKey,
50+
'Content-Type: application/json',
51+
],
52+
);
53+
$request = $this->CurlConnection->payStackConnection($options); //make reques via Curl
54+
if ($request) {
55+
$result = json_decode($request);
56+
57+
if(!$result->status){
58+
// there was an error from the API
59+
die('API returned error: ' . $result->message);
60+
}else{
61+
62+
return $result;
63+
}
64+
}
65+
}
66+
67+
68+
/**
69+
* Hit Paystack Gateway to Verify that the transaction is valid
70+
* @param String $reference
71+
* @return array|false
72+
*/
73+
public function callback($reference,$user_id){
74+
$options = array(
75+
CURLOPT_URL => $this->verifyUrl . rawurlencode($reference),
76+
CURLOPT_RETURNTRANSFER => true,
77+
CURLOPT_HTTPHEADER => [
78+
"accept: application/json",
79+
"authorization: Bearer ".$this->secretKey,
80+
"cache-control: no-cache"
81+
],
82+
);
83+
84+
$response = $this->CurlConnection->payStackConnection($options);
85+
$tranx = json_decode($response);
86+
87+
if(!$tranx->status){
88+
// there was an error from the API
89+
die('API returned error: ' . $tranx->message);
90+
}
91+
92+
if('success' == $tranx->data->status){
93+
// transaction was successful...
94+
return $transx;
95+
}
96+
97+
}
98+
99+
}

0 commit comments

Comments
 (0)