Integrate Stripe into Codeigniter 4

Here are some tips on integrating Stripe into your PHP Framework (Codeiginter 4). Simple approach

Controller

 

Public function order(){

 

$data = [

‘amount’ = 100

‘quantity’ = 1

];

\Stripe\Stripe::setApiKey(getenv(‘stripe.secret’));

header(‘Content-Type: application/json’);

 

$YOUR_DOMAIN = base_url();

 

$checkout_session = \Stripe\Checkout\Session::create([

‘line_items’ => [[

# Provide the exact Price ID (e.g. pr_1234) of the product you want to sell

‘price’ => $data[‘amount’],

‘quantity’ => 1,

]],

‘mode’ => ‘payment’,

‘success_url’ => $YOUR_DOMAIN . ‘success’,  // send to success controller

‘cancel_url’ => $YOUR_DOMAIN . ‘cancel’, // send to cancel controller

]);

 

//stripe checkout ends

$data[‘stripe’]= $checkout_session->url;

return view(‘Webpayments’,$data);

}

 

Public function success(){

 

Return view(‘Success’);   // Need to create a View page for Success.php

 

}

 

Public function cancel(){

Return view(‘Cancel’);  //Need to create a view page for Cancel.php

}

 

View

<?= $this->extend(‘/layout/base.php’) ?>

<?= $this->section(‘content’) ?>

<?= $this->include(‘/layout/navigation.php’) ?>

 

<div class=”content-wrapper”>

<section class=”wrapper bg-gradient-primary”>

<div class=”container pt-10 pt-md-14 pb-8 text-center”>

<div class=”row”>

<div class=”col”>

<img src=”./assets/img/photos/stripe.png” alt=”Stripe”>

<p>Make Payment via Credit Card, FPX and Stripe</p>

<a class= ‘btn btn-success’ href=”<?= esc($stripe)?>”>Make Payment Now</a>

</div>

<div class=”col”>

<img src=”./assets/img/photos/Duitnow1.png” alt=”Stripe”>

<p>Scan The Duit Now if you are planning to make payment via Duitnow. You can transfer payment via <br>Touch N Go to 0124594388 <br> Yaw Kean Huat <br> Once you have made payment please message me via the phone number provided </p>

 

</div>

</div>

</div>

</section>

</div>

 

<?= $this->endSection() ?>