PayPal provides an easy way to integrate a standard payment gateway in PHP. If your web application is built with CodeIgniter 4, the PayPal PHP library and API need to be integrated into the CodeIgniter 4 application.
This tutorial will guide you through integrating PayPal payment processing into a CodeIgniter 4 application. We’ll create a simple example to demonstrate the process.
Prerequisites
- Basic knowledge of PHP and CodeIgniter 4.
- A CodeIgniter 4 project set up.
- A PayPal Developer account.
Set Up Your CodeIgniter 4 Project & Install PayPal SDK via Composer
Ensure you have CodeIgniter 4 installed.
composer require paypal/paypal-checkout-sdk
Create a PayPal Developer Account
- Visit the PayPal Developer Portal.
- Log in or create an account.
- Create a new app under the “My Apps & Credentials” section to get your Client ID and Secret.
Configure CodeIgniter for PayPal
$clientId = 'ARF2P04R0r_x_fQ-fATDzXXXXXXX7hCy1eMxymIYYQQ5zR-0QzKPpFGhTH6vDVMG';
$clientSecret = 'EB-Gy0qyz1JbQJoAM1QNXXXXXXXXXXXXXq2fNCI0Ir92UsRO3TlSJ5Wmne9aM-cJqGopgIHK0Ec1';
Create a Controller for PayPal Integration
Create a new controller app/Controllers/payment/Paypal.php
:
<?php
namespace App\Controllers\payment;
use App\Controllers\BaseController;
use PayPalCheckoutSdk\Core\PayPalHttpClient;
use PayPalCheckoutSdk\Core\SandboxEnvironment;
use PayPalCheckoutSdk\Core\ProductionEnvironment;
use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;
class Paypal extends BaseController {
public function __construct() {
$db = db_connect();
$this->ip_address = $_SERVER['REMOTE_ADDR'];
$this->datetime = date("Y-m-d H:i:s");
}
public function index() {
$data = [];
$data ['content_title'] = 'Pay with Paypal using CodeIgniter 4';
echo view('home', $data);
}
public function create() {
$amount = $this->request->getGet('amount');
$currency = $this->request->getGet('currency');
$clientId = 'ARF2P04R0r_x_fQ-fATDz7AqlyFY1Vpqk9Gu2uXCzk9R7hCy1eMxymIYYQQ5zR-0QzKPpFGhTH6vDVMG';
$clientSecret = 'EB-Gy0qyz1JbQJoAM1QNmX_ld0G8HNA8BIkkVq2fNCI0Ir92UsRO3TlSJ5Wmne9aM-cJqGopgIHK0Ec1';
$paypal_sandbox = true;
if ($paypal_sandbox) {
$environment = new SandboxEnvironment($clientId, $clientSecret);
} else {
$environment = new ProductionEnvironment($clientId, $clientSecret);
}
$client = new PayPalHttpClient($environment);
$request = new OrdersCreateRequest();
$request->prefer('return=representation');
$request->body = [
"intent" => "CAPTURE",
"purchase_units" => [[
"reference_id" => rand(000000,999999),
"amount" => [
"value" => number_format($amount, 2, '.', ''),
"currency_code" => $currency,
]
]],
"application_context" => [
"cancel_url" => base_url('payment/paypal/cancel'),
"return_url" => base_url('payment/paypal/complete')
]
];
try {
// Call API with your client and get a response for your call
$response = $client->execute($request);
// If call returns body in response, you can get the deserialized version from the result attribute of the response
return redirect()->to($response->result->links[1]->href);
} catch (HttpException $ex) {
echo $ex->statusCode;
print_r($ex->getMessage());
}
}
public function cancel() {
$token = $this->request->getGet('token');
echo 'Payment has been failed';
}
public function complete() {
$order_id = $this->request->getGet('order_id');
$token = $this->request->getGet('token');
$clientId = 'ARF2P04R0r_x_fQ-fATDz7AqlyFY1Vpqk9Gu2uXCzk9R7hCy1eMxymIYYQQ5zR-0QzKPpFGhTH6vDVMG';
$clientSecret = 'EB-Gy0qyz1JbQJoAM1QNmX_ld0G8HNA8BIkkVq2fNCI0Ir92UsRO3TlSJ5Wmne9aM-cJqGopgIHK0Ec1';
$paypal_sandbox = true;
if ($paypal_sandbox) {
$environment = new SandboxEnvironment($clientId, $clientSecret);
} else {
$environment = new ProductionEnvironment($clientId, $clientSecret);
}
$client = new PayPalHttpClient($environment);
$request = new OrdersCaptureRequest($token);
$request->prefer('return=representation');
try {
// Call API with your client and get a response for your call
$response = $client->execute($request);
// If call returns body in response, you can get the deserialized version from the result attribute of the response
print_r($response);
echo 'Paid successfully';
} catch (HttpException $ex) {
// echo $ex->statusCode;
print_r($ex->getMessage());
}
}
}
Define Routes
Add routes for the PayPal controller in app/Config/Routes.php
:
$routes->get('/', 'Home::index');
$routes->group('payment', function($routes) {
$routes->group('paypal', function($routes) {
$routes->get('/', 'payment\Paypal::index');
$routes->get('create', 'payment\Paypal::create');
$routes->post('create', 'payment\Paypal::create');
$routes->get('cancel', 'payment\Paypal::cancel');
$routes->get('complete', 'payment\Paypal::complete');
});
});
Create a View for Payment
Create a simple view app/Views/home.php
:
<!DOCTYPE html>
<html lang="en">
<head>
<title><?php echo $content_title; ?></title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/jquery@3.7.1/dist/jquery.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-5">
<div class="jumbotron text-center">
<h1>Pay with <img src="<?php echo base_url('images/paypal.png'); ?>" class="img img-fluid" width="150px"> using CodeIgniter 4</h1>
<p>By infovistar.in</p>
</div>
<div class="row">
<div class="col-sm-3">
</div>
<div class="col-sm-6">
<form method="get" action="<?php echo base_url('payment/paypal/create'); ?>" autocomplete="off">
<div class="form-group">
<label>Amount</label>
<input type="number" name="amount" min="1" value="1" class="form-control">
</div>
<div class="form-group">
<label>Currency</label>
<input type="text" name="currency" value="USD" class="form-control" readonly>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Pay</button>
</div>
</form>
</div>
<div class="col-sm-3">
</div>
</div>
</div>
</body>
</html>
Test the Integration
Run your CodeIgniter application:
php spark serve
Navigate to http://localhost:8080
and click the “Pay Now” button to start the PayPal payment process.
This simple tutorial demonstrates how to integrate PayPal payments into a CodeIgniter 4 application. You can further enhance the example by adding error handling, logging, and other necessary features for a production environment.