-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcharge-authorization.php
More file actions
57 lines (49 loc) · 2.72 KB
/
Copy pathcharge-authorization.php
File metadata and controls
57 lines (49 loc) · 2.72 KB
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
<?php
/* handles the call from your Android App,
* Call thus > https://url.domain.tld/to/sample/charge-authorization.php
* since your auth code should only be store don the server, we are assuming that calling charge authorization is
* only meant to be started by your server and not directly by the android app; you must have already saved the
* authentication code against the user's email and both will be used together to make the charge call
*/
require __DIR__ . '/paystack-class/Paystack.php';
require './functions.php';
// you may simply add the secret key you wish to use as the parameter when creating a paystack object,
// that's what we do here, as against the config array we used in charge-token.php
$paystack = new Paystack('sk_test_xxxx');
$auth_code = 'AUTH_code generated by calling charge-token.php with an email and a paystack token generated from android app';
$email = 'Email which must match authorisation code!';
// make sure we have a valid email and that our auth code starst with AUTH_
if ($email && is_string($auth_code) && (substr_compare($auth_code, 'AUTH_', 0, 5, true)===0)) {
// get a code to use for this request
$reference = getUnusedCode();
$request['params'] = [
'reference'=>$reference,
'authorization_code'=>$auth_code,
'email'=>$email,
'amount'=>10000 // in kobo
];
// add the time of the request to the array
$requset['time'] = gmdate("Y-m-d\TH:i:s\Z");
// add the user's ip to the array
$request['ip'] = getIp();
// log the request to our file-based storage
// save the array to a file named by the code chosen
file_put_contents('results/' . $reference . '-auth-request.json', json_encode($request));
// make the paystack call - charge on the request params
$body = $paystack->transaction->charge($request['params']);
// header('Content-Type: application/json');
// echo json_encode([$headers, $body, $code]);
// test that the status code was 200 and the body gave status true, and there's data in body
$response_data = $body->data;
// save the response data for customer in database
// we are using a file-based storage in results folder
file_put_contents('results/' . $reference . '-response.json', json_encode($body));
// in this sample we simply echo the status, rather you should go ahead and check
echo $response_data->status;
// data should also have status of success if the 100 naira charge was successful
if ($response_data->status==='success') {
// do what you need to do to serve your customer. payment went through
}
} else {
throw new \Exception('A valid email and a valid Paystack authorization code is required.');
}