Skip to content

Commit f70c479

Browse files
feat(paystack): Generate unique secure transaction reference hash
1 parent 06cc780 commit f70c479

File tree

3 files changed

+81
-29
lines changed

3 files changed

+81
-29
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
"require": {
1414
"php": ">=5.5.9",
1515
"illuminate/support": "5.2.*",
16-
"guzzlehttp/guzzle": "~5.0",
17-
"ramsey/uuid": "3.1.*"
16+
"guzzlehttp/guzzle": "~5.0"
1817
},
1918
"require-dev": {
2019
"phpunit/phpunit" : "4.*",

src/Paystack.php

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
namespace Unicodeveloper\Paystack;
44

55
use GuzzleHttp\Client;
6-
use Ramsey\Uuid\Uuid;
7-
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
86

97
class Paystack {
108

@@ -210,30 +208,7 @@ public function getAccessCode()
210208
*/
211209
public function genTranxRef()
212210
{
213-
try {
214-
215-
// Generate a version 1 (time-based) UUID object
216-
$uuid1 = Uuid::uuid1();
217-
echo $uuid1->toString() . "\n"; // i.e. e4eaaaf2-d142-11e1-b3e4-080027620cdd
218-
219-
// // Generate a version 3 (name-based and hashed with MD5) UUID object
220-
// $uuid3 = Uuid::uuid3(Uuid::NAMESPACE_DNS, 'php.net');
221-
// echo $uuid3->toString() . "\n"; // i.e. 11a38b9a-b3da-360f-9353-a5a725514269
222-
223-
// // Generate a version 4 (random) UUID object
224-
// $uuid4 = Uuid::uuid4();
225-
// echo $uuid4->toString() . "\n"; // i.e. 25769c6c-d34d-4bfe-ba98-e0ee856f3e7a
226-
227-
// // Generate a version 5 (name-based and hashed with SHA1) UUID object
228-
// $uuid5 = Uuid::uuid5(Uuid::NAMESPACE_DNS, 'php.net');
229-
// echo $uuid5->toString() . "\n"; // i.e. c4a760a8-dbcf-5254-a0d9-6a4474bd1b62
230-
231-
} catch (UnsatisfiedDependencyException $e) {
232-
233-
// Some dependency was not met. Either the method cannot be called on a
234-
// 32-bit system, or it can, but it relies on Moontoast\Math to be present.
235-
echo 'Caught exception: ' . $e->getMessage() . "\n";
236-
237-
}
211+
return TransRef::getHashedToken();
238212
}
213+
239214
}

src/TransRef.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Laravel Paystack package.
5+
*
6+
* (c) Prosper Otemuyiwa <[email protected]>
7+
*
8+
* Source http://stackoverflow.com/a/13733588/179104
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Unicodeveloper\Paystack;
15+
16+
class TransRef {
17+
18+
public static function getPool( $type = 'alnum')
19+
{
20+
switch ( $type ) {
21+
case 'alnum':
22+
$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
23+
break;
24+
case 'alpha':
25+
$pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
26+
break;
27+
case 'hexdec':
28+
$pool = '0123456789abcdef';
29+
break;
30+
case 'numeric':
31+
$pool = '0123456789';
32+
break;
33+
case 'nozero':
34+
$pool = '123456789';
35+
break;
36+
case 'distinct':
37+
$pool = '2345679ACDEFHJKLMNPRSTUVWXYZ';
38+
break;
39+
default:
40+
$pool = (string) $type;
41+
break;
42+
}
43+
44+
return $pool;
45+
}
46+
47+
48+
public static function secure_crypt($min, $max) {
49+
$range = $max - $min;
50+
51+
if ($range < 0) {
52+
return $min; // not so random...
53+
}
54+
55+
$log = log($range, 2);
56+
$bytes = (int) ($log / 8) + 1; // length in bytes
57+
$bits = (int) $log + 1; // length in bits
58+
$filter = (int) (1 << $bits) - 1; // set all lower bits to 1
59+
do {
60+
$rnd = hexdec( bin2hex( openssl_random_pseudo_bytes( $bytes ) ) );
61+
$rnd = $rnd & $filter; // discard irrelevant bits
62+
} while ($rnd >= $range);
63+
64+
return $min + $rnd;
65+
}
66+
67+
public static function getHashedToken($length = 25)
68+
{
69+
$token = "";
70+
$max = strlen(static::getPool());
71+
for ($i = 0; $i < $length; $i++) {
72+
$token .= static::getPool()[static::secure_crypt(0, $max)];
73+
}
74+
75+
return $token;
76+
}
77+
78+
}

0 commit comments

Comments
 (0)