/* handles the call from your Android App, 
 *   Call thus > https://url.domain.tld/to/sample/charge-token.php?token=PSTK_xxx&email=xxx@xxx.xxx
 */
require __DIR__ . '/paystack-class/Paystack.php';
require './functions.php';
// Configuration options
$config['paystack_key_test_secret'] = PAYSTACK_TEST_SECRET_KEY;
$config['paystack_key_live_secret'] = PAYSTACK_LIVE_SECRET_KEY;
$config['paystack_test_mode'] = true;
// set to false when you are ready to go live
$paystack = new Paystack($config);
$token = filter_input(INPUT_GET, 'token');
$email = filter_input(INPUT_GET, 'email', FILTER_VALIDATE_EMAIL);
if ($email && is_string($token) && substr_compare($token, 'PSTK_', 0, 5, true) === 0) {
    // get a code to use for this request
    $reference = getUnusedCode();
    $request['params'] = ['reference' => $reference, 'token' => $token, 'email' => $email, 'amount' => 100];
    // add the time of the request to the array
    $request['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 . '-request.json', json_encode($request));
    // make the paystack call on the request params
    $body = $paystack->transaction->chargeToken($request['params']);
    // You should get - and save - the authorization code so you may charge the customer in future
    $response_data = $body->data;
    // data should also have status success if the 1 naira charge was successful
    if ($response_data->status === 'success') {
        // save the authorization code for customer in database
require './functions.php';
$paystack = new \Yabacon\Paystack(PAYSTACK_SECRET_KEY);
// an array object to store the request to file
$req = [];
// add the time of the request to the array
$req['time'] = gmdate("Y-m-d\\TH:i:s\\Z");
// add the user's ip to the array
$req['ip'] = getIp();
// get the formdata submitted
$req['form'] = json_encode($_POST);
// add the user email to the array
$req['email'] = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
// add the amount to the array
$req['amountngn'] = floatval(filter_input(INPUT_POST, 'amountngn'));
// get a code to use for this transaction
$newcode = getUnusedCode();
// save the array to a file named by the code chosen
file_put_contents('results/' . $newcode . '-request.json', json_encode($req));
// confirm that we got a valid email
if (!$req['email']) {
    die('An invalid email was sent');
}
// confirm that we got a valid amount
if (!$req['amountngn']) {
    die('An invalid amount was sent');
}
// initiate transaction (Remember to change this if you are using Guzzle)
$virdir = new VirtualDirectory();
// Check the README here > https://github.com/yabacon/paystack-php/
$response = $paystack->transaction->initialize(['reference' => $newcode, 'amount' => $req['amountngn'] * 100, 'email' => $req['email'], 'callback_url' => rtrim($virdir->thisURL, '/') . '/donate-conclude.php']);
// check if transaction url was generated