This repository has been archived by the owner on Jul 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.php
94 lines (79 loc) · 2.67 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
require_once 'vendor/autoload.php';
// documented at https://github.com/yabacon/paystack-php
use Yabacon\Paystack;
use Yabacon\Paystack\MetadataBuilder;
define('PAYSTACK_SECRET', 'sk_test_badb30a68e7aa6913d8c6d4d7dda2b765f4c785c');
$path = filter_input(INPUT_GET, 'path');
// sample code to start a transaction
// more about thr transaction/initialize enpoint
// here: https://developers.paystack.co/v1.0/reference#initialize-a-transaction
if($path === 'new-access-code'){
if(!(($email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL))
&& ($amount = filter_input(INPUT_POST, 'amount', FILTER_VALIDATE_FLOAT)))){
http_response_code(400);
die('Invalid Email or amount sent');
}
$amountinkobo = 100 * $amount;
$builder = new MetadataBuilder();
$builder->withCustomField('Started From', 'sample charge card backend');
time()%2 && $builder->withCustomFilters(['recurring'=>true]);
$metadata = $builder->build();
try{
$paystack = new Paystack(PAYSTACK_SECRET);
$paystack->disableFileGetContentsFallback();
$trx = $paystack->transaction->initialize([
'amount'=>$amountinkobo,
'email'=>$email,
'metadata'=>$metadata,
]);
} catch(Exception $e){
http_response_code(400);
die($e->getMessage());
}
die($trx->data->access_code);
}
// sample code to verify a transaction reference
// more about the transaction/verify enpoint
// here: https://developers.paystack.co/v1.0/reference#verifying-transactions
if(strpos($path, 'verify/') === 0){
// whatever is after verify is our refernce
$reference = substr($path, 7);
try{
$paystack = new Paystack(PAYSTACK_SECRET);
$paystack->disableFileGetContentsFallback();
$trx = $paystack->transaction->verify([
'reference'=>$reference,
]);
} catch(Exception $e){
http_response_code(400);
die($e->getMessage());
}
if($trx->data->status === 'success'){
// give value
}
// dump gateway response for display to user
die($trx->data->gateway_response);
}
// our payment form
if($path === 'payment'){
include_once 'pay.html';
die();
}
// our payment form
if(($path === '/') || ($path === '')){
include_once 'pay.html';
die();
}
// log a client-side error
if($path === 'report'){
file_put_contents('client-errors.log', "\n".json_encode($_POST), FILE_APPEND);
die();
}
// if it got here, it was neither of the recognized paths
// show the welcome message
http_response_code(404);
?><p>Your server is set up.
<br>/<?php echo $path; ?> does not exist<br>
Open <i>/payment</i> to test the form.
</p>