PHP code example of ejen / yii2-onpay

1. Go to this page and download the library: Download ejen/yii2-onpay 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/ */

    

ejen / yii2-onpay example snippets




namespace frontend\controllers;

use ejen\payment\onpay\ApiAction;

class OnpayController extends \yii\web\Controller
{
    public function actions()
    {
        return [
            'api' => [
                'class' => ApiAction::className(),
                'payCallback' => [$this, 'payCallback'],
                'checkCallback' => [$this, 'checkCallback'],
            ],
        ];
    }

    public function payCallback($request, $response)
    {
        $amount = 100;
        $currency = 'RUR';

        if ($request->balance_amount < $amount)
        {
            $response->code = 3;
            $response->comment = 'bad_pay';
            return false;
        }

        if ($request->balance_currency != $currency)
        {
            $response->code = 3;
            $response->comment = 'bad_pay';
            return false;
        }

        return true;
    }

    public function checkCallback($request, $response)
    {
        $amount = 500;
        $currency = 'RUR';

        if ($request->amount < $amount)
        {
            $response->code = 2;
            return false;
        }
        
        if ($request->amount > $amount || $request->order_currency != $currency)
        {
            $response->code = 3;
            return false;
        }

        return true;
    }
}



namespace frontend\controllers;

use Yii;

use frontend\forms\PaymentForm;

class PaymentController extends \yii\web\Controller
{

    public function actionIndex()
    {
        $model = new PaymentForm;
        if ($model->load(Yii::$app->request->post()) && $model->validate())
        {
            $payment = Yii::$app->onpay->createPayment([
                'price' => $model->amount,
                'pay_for' => Yii::$app->user->id,
            ]);

            if ($payment->validate())
            {
                return $this->redirect($payment->getUrl());
            }
        }        

        return $this->render('index', [
            'model' => $model,
        ]);
    }
}
 php
    ...
    'components' => [
        ...
        'onpay' => [
            'secret_key' => 'YOUR_SECRET_KEY',
            'username' => 'YOUR_USERNAME',
        ],
        ...
    ],
    ...