DEV Community

mmunir007
mmunir007

Posted on

how can i add success page with customer details in php slim stripe

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);

use Slim\Http\Request;
use Slim\Http\Response;
use Stripe\Stripe;




require 'vendor/autoload.php';

$dotenv = Dotenv\Dotenv::create(__DIR__);
$dotenv->load();

require './config.php';

$app = new \Slim\App;

$app->add(function ($request, $response, $next) {
    Stripe::setApiKey(getenv('STRIPE_SECRET_KEY'));
    return $next($request, $response);
});

$app->get('/', function (Request $request, Response $response, array $args) {
  return $response->write(file_get_contents(getenv('STATIC_DIR') . '/index.html'));
});

$app->post('/checkout_sessions', function(Request $request, Response $response) use ($app)  {
  $params = json_decode($request->getBody());
  $payment_method_types = [
    'usd' => ['card'],
    'eur' => ['card'],
    'cad' => ['card']
  ];
  $products = [
    'cause-a' => 'prod_KP3YP2a3IGYqsb',
    'cause-b' => 'prod_KP3iZRGcEjn5W8',
  ];

  $session = \Stripe\Checkout\Session::create([
    'success_url' => 'http://localhost:4242/?success=true',
    'cancel_url' => 'http://localhost:4242/?cancel=true',
    'mode' => 'payment',
    'payment_method_types' => $payment_method_types[$params->currency],
    'metadata' => [
      'cause' => $params->cause,
      'currency' => $params->currency,
    ],
    'submit_type' => 'donate',
    'line_items' => [[
      'price_data' => [
        'currency' => $params->currency,
        'product' => $products[$params->cause],
        'unit_amount' => $params->amount,
      ],
      'quantity' => 1,
    ]]
  ]);

  return $response->withJson([
    'id' => $session->id
  ]);
});

$app->post('/webhook', function(Request $request, Response $response) {
    $params = json_decode($request->getBody(), true);
    $event = \Stripe\Event::constructFrom($params);
    switch($event->type) {
      case 'checkout.session.completed':
        $session = $event->data->object;
        ob_start();
        var_dump('Checkout session completed!' . $session->id);
        error_log(ob_get_clean(), 4);
        break;
    }

    return $response->withJson([ 'status' => 'success' ])->withStatus(200);
});



$app->run();
Enter fullscreen mode Exit fullscreen mode

https://www.youtube.com/watch?v=FOLRATK4pVA

i was trying this to learn stripe and i was wondering how can i show cause amount and customer details on success page so it can be used for verification what cause he paid for?

Top comments (0)