nikitakls/yii2-unitpay-gate

UnitPay billing gate

Installs: 1 166

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:yii2-extension

v0.0.3 2018-01-23 14:58 UTC

This package is not auto-updated.

Last update: 2020-04-07 06:52:59 UTC


README

Unitpay gate for Yii2

Installation

The preferred way to install this extension is through composer.

Either run

composer require --prefer-dist nikitakls/yii2-unitpay-gate "*"

or add

"nikitakls/yii2-unitpay-gate": "*"

to the require section of your composer.json file.

Configuration

'components' => [
    'unitpay' => [
        'class' => '\nikitakls\unitpay\Merchant',
        'secretKey' => '',
        'publicKey' => '',
        'orderCurrency' => 'RUB', # 'EUR', 'UAH', 'BYR', 'USD', 'RUB'
        'locale' => 'ru',
    ]
    ...
]

Usage

Once the extension is installed, simply use it in your code by :

You can use result action in you controller:

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');
    }
}