PHP code example of akhur0286 / yii2-sberpay
1. Go to this page and download the library: Download akhur0286/yii2-sberpay 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/ */
akhur0286 / yii2-sberpay example snippets
[
'components' => [
'sberpay' => [
'class' => 'akhur0286\sberpay\Merchant',
'sessionTimeoutSecs' => 60 * 60 * 24 * 7,
'merchantLogin' => '',
'merchantPassword' => '',
'orderModel' => '', //модель таблицы заказов
'isTest' => false,
'registerPreAuth' => false,
'returnUrl' => '/payment/result-payment',
'failUrl' => '/payment/error-payment',
],
//..
],
];
php composer.phar
class PaymentController extends \yii\web\Controller
{
/**
* @inheritdoc
*/
public function actions()
{
return [
'result-payment' => [
'class' => '\akhur0286\sberpay\actions\BaseAction',
'callback' => [$this, 'resultCallback'],
],
'error-payment' => [
'class' => '\akhur0286\sberpay\actions\BaseAction',
'callback' => [$this, 'failCallback'],
],
];
}
public function resultCallback($orderId)
{
/* @var $model SberpayInvoice */
$model = SberpayInvoice::findOne(['orderId' => $orderId]);
if (is_null($model)) {
throw new NotFoundHttpException();
}
$merchant = \Yii::$app->get('sberpay');
$result = $merchant->checkStatus($orderId);
//Проверяем статус оплаты если всё хорошо обновим инвойс и редерекним
if (isset($result['OrderStatus']) && ($result['OrderStatus'] != $merchant->successStatus)) {
//обработка при успешной оплате $model->related_id номер заказа
echo 'ok';
} else {
$this->redirect($merchant->failUrl.'?orderId=' . $orderId);
}
}
public function failCallback($orderId)
{
/* @var $model SberpayInvoice */
$model = SberpayInvoice::findOne(['orderId' => $orderId]);
if (is_null($model)) {
throw new NotFoundHttpException();
}
//вывод страницы ошибки $model->related_id номер заказа
echo 'error payment';
}
}