Create a global varible in SLIM framework to use in another route

180 views Asked by At

I need to be create a global varible to use in another route, i'm unfamiliar with the SLIM framework and everything I've searched isn't working.
So basically I grab the price in the route that shows the home screen, then i need to use that price in another route with creates a STRIPE API session. See below;
$app = new Slim\App;

$app->get('/pay/{price}', function (Request $request, Response $response, $args) {
  $response->getBody()->write(file_get_contents("../../client/index.html"));
  $key = "8881727727272";
  $decrypted = openssl_decrypt(hex2bin($args['price']),'AES-128-CBC',$key); <---- CREATED HERE!!!
  return $response;
});
$app->get('/success', function (Request $request, Response $response, $args) {
  $response->getBody()->write(file_get_contents("../../client/success.html"));
  return $response;
});
$app->get('/cancel', function (Request $request, Response $response, $args) {
  $response->getBody()->write(file_get_contents("../../client/cancel.html"));
  return $response;
});

$app->post('/create-session', function(Request $request, Response $response) use ($app)  {
  try {
    $cafe = 'test';
    $session = \Stripe\Checkout\Session::create([
      'payment_method_types' => ['card'],
      'line_items' => [[
        'name' => $cafe,
        'description' => 'ordering',
        'images' => [''],
        'amount' => $decrypted, <--- Needs to be used again here!
        'currency' => 'aud',
        'quantity' => 1,
      ]],
      'success_url' => 'http://localhost/success?session_id={CHECKOUT_SESSION_ID}',
      'cancel_url' => 'http://localhost/cancel',
    ]);
    createSession($session->id);
  } catch (Exception $e) {
    return $response->withJson($e->getJsonBody(), 400);
  }
  return $response->withJson($session);
});

If anyone has any ideas that would be great!! Thank you

0

There are 0 answers