1. Go to this page and download the library: Download nedarta/yii2-omnipay library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
namespace app\controllers;
use Yii;
use yii\web\Controller;
use yii\web\BadRequestHttpException;
class PaymentController extends Controller
{
/**
* Process a direct credit card charge via Stripe
*/
public function actionStripeCharge()
{
$request = Yii::$app->request;
// Stripe token generated on the frontend by Stripe.js Elements
$token = $request->post('stripeToken');
$amount = 29.99; // Amount in USD
if (!$token) {
throw new BadRequestHttpException('Missing stripe payment token.');
}
try {
// Direct call to Stripe gateway purchase() via magic method delegation
$response = Yii::$app->stripe->purchase([
'amount' => $amount,
'currency' => 'USD',
'token' => $token,
'description' => 'Order #1042 Payment',
])->send();
if ($response->isSuccessful()) {
$transactionReference = $response->getTransactionReference();
Yii::$app->session->setFlash('success', "Payment successful! Ref: {$transactionReference}");
return $this->redirect(['site/success']);
} elseif ($response->isRedirect()) {
// Redirect to 3D Secure / off-site authentication if
namespace app\controllers;
use Yii;
use yii\web\Controller;
use yii\helpers\Url;
class PaypalController extends Controller
{
/**
* Start the PayPal Express Checkout flow
*/
public function actionCheckout()
{
$amount = 99.00;
try {
// Direct call to purchase() via magic method delegation on the paypal component
$response = Yii::$app->paypal->purchase([
'amount' => $amount,
'currency' => 'USD',
'description' => 'Premium Subscription - Annual Plan',
'returnUrl' => Url::to(['paypal/success'], true),
'cancelUrl' => Url::to(['paypal/cancel'], true),
])->send();
if ($response->isRedirect()) {
// Redirect to PayPal checkout page
return $response->redirect();
} else {
Yii::$app->session->setFlash('error', $response->getMessage());
}
} catch (\Exception $e) {
Yii::$app->session->setFlash('error', "Could not initiate payment: " . $e->getMessage());
}
return $this->redirect(['site/checkout']);
}
}
/**
* Complete the PayPal payment after successful return
*/
public function actionSuccess()
{
$request = Yii::$app->request;
$token = $request->get('token');
$payerId = $request->get('PayerID');
try {
// Direct call to completePurchase() via magic method delegation
$response = Yii::$app->paypal->completePurchase([
'amount' => 99.00,
'currency' => 'USD',
'transactionReference' => $token,
'payerId' => $payerId,
])->send();
if ($response->isSuccessful()) {
$data = $response->getData();
$transactionId = $data['PAYMENTINFO_0_TRANSACTIONID'] ?? 'Unknown';
Yii::$app->session->setFlash('success', "PayPal payment completed successfully! Transaction ID: {$transactionId}");
return $this->redirect(['site/success']);
} else {
Yii::$app->session->setFlash('error', "Transaction completion failed: " . $response->getMessage());
}
} catch (\Exception $e) {
Yii::$app->session->setFlash('error', "An error occurred during verification: " . $e->getMessage());
}
return $this->redirect(['site/checkout']);
}
/**
* Handle user cancellation
*/
public function actionCancel()
{
Yii::$app->session->setFlash('info', 'You have cancelled the PayPal payment checkout process.');
return $this->redirect(['site/checkout']);
}
namespace app\controllers;
use Yii;
use yii\web\Controller;
class WebhookController extends Controller
{
/**
* @inheritdoc
*/
public function beforeAction($action)
{
if ($action->id === 'stripe-webhook' || $action->id === 'paypal-ipn') {
$this->enableCsrfValidation = false;
}
return parent::beforeAction($action);
}
public function actionStripeWebhook()
{
// Process Stripe webhook signature and handle notification
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.