PHP code example of nikitakls / yii2-unitpay-gate

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

    

nikitakls / yii2-unitpay-gate example snippets


class PaymentController extends Controller
{
    
    public $enableCsrfValidation = false;
    
    public $unitpay = 'unitpay';
    
    
    public function actionInvoice()
    {
        $model = new Invoice();
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            /** @var \nikitakls\unitpay\Merchant $merchant */
            $merchant = Yii::$app->get($this->unitpay);
            return $this->redirect($merchant->payment($model->sum, $model->id, 'Пополнение счета', Yii::$app->user->identity->email, $model->phone));
        } else {
            return $this->render('invoice', [
                'model' => $model,
            ]);
        }
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {

        return [
            'result' => [
                'class' => ResultAction::class,
                'payCallback' => [$this, 'payCallback'],
                'checkCallback' => [$this, 'checkCallback'],
                'failCallback' => [$this, 'failCallback'],
            ],
        ];
    }
    
    public function payCallback(ResultParam $param)
    {
                $this->loadModel($nInvId)->updateAttributes(['status' => Invoice::STATUS_ACCEPTED]);
                return Yii::$app->get('unitpay')->getSuccessResponse('Pay Success');

    }
    
    public function checkCallback(ResultParam $param)
    {
                if($this->loadModel($nInvId)){
                    return Yii::$app->get('unitpay')->getSuccessResponse('Check Success. Ready to pay.');
                };
                return Yii::$app->get('unitpay')->getErrorResponse('Message about error');
    }
    
    public function failCallback(ResultParam $param)
    {
                $this->loadModel($nInvId)->updateAttributes(['status' => Invoice::STATUS_FAIL]);
                Yii::$app->errorHandler->logException($param->getErrorMessage());
                return Yii::$app->get('unitpay')->getSuccessHandlerResponse('Error logged');
    }
}