As mobile and online payments continue to grow in popularity, integrating payment features has become essential for many websites and applications. Yii, a high-performance PHP framework, provides developers with a flexible structure ideal for building extensible features like payment processing. This article walks you through the process of adding an online payment feature using Yii.
Before starting, ensure that Yii is installed and you're ready to create a new application. Use the following CLI command to generate a new project structure:
yii startapp payment
Once the project is created, add a controller to manage payment logic:
yii generate/controller PaymentController
Open your application's config/main.php file and add the following to the components section to configure the payment gateway:
'paymentGateway' => [
'class' => 'app\components\PaymentGateway',
'apiKey' => 'YOUR_API_KEY',
'apiUrl' => 'https://api.paymentgateway.com/pay',
],
Replace the placeholders with the actual API credentials and endpoint provided by your payment provider.
Create a new PHP class PaymentGateway.php inside the components directory and define the following code:
<?php
namespace app\components;
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
class PaymentGateway extends Component
{
public $apiKey;
public $apiUrl;
public function init()
{
parent::init();
if ($this->apiKey === null) {
throw new InvalidConfigException('The "apiKey" property must be set.');
}
if ($this->apiUrl === null) {
throw new InvalidConfigException('The "apiUrl" property must be set.');
}
}
public function processPayment($amount, $cardNumber, $cardExpiry)
{
// Logic to send the payment request
// Actual implementation depends on your payment gateway
}
}
This component handles API interaction with the payment provider, validating initialization and exposing a method to process payments.
Edit PaymentController.php to add a method that handles payment requests from clients:
<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use app\components\PaymentGateway;
class PaymentController extends Controller
{
public function actionProcess($amount, $cardNumber, $cardExpiry)
{
$paymentGateway = Yii::$app->paymentGateway;
try {
$response = $paymentGateway->processPayment($amount, $cardNumber, $cardExpiry);
// Handle successful payment
} catch (\Exception $e) {
// Handle failed payment
Yii::error($e->getMessage());
}
}
}
The controller retrieves the payment component instance and invokes the processPayment method, handling both success and error cases.
To allow front-end access to the controller's method via a clean URL, configure a routing rule in main.php:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'payment/process/<amount:\d+>/<cardNumber:\d+>/<cardExpiry:\d+>' => 'payment/process',
],
],
This setup maps URLs like /payment/process/100/1234567890123456/1225 to the correct action method with properly validated numeric parameters.
With these steps, you've now successfully integrated basic online payment functionality into a Yii application. Depending on your project, you may want to add support for multiple payment methods, implement security checks, or handle payment notifications.
Always ensure that sensitive data is transmitted securely using HTTPS, and consider additional encryption and error-handling strategies to protect your users and their transactions.
We hope this tutorial helps you implement payment features efficiently in your Yii-based projects.