Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace common\models;
- use Yii;
- use yii\base\Model;
- /**
- * This is the model class for table "YandexKassa".
- *
- * @property string $action
- * @property string $orderSumAmount
- * @property string $orderSumCurrencyPaycash
- * @property string $orderSumBankPaycash
- * @property string $shopId
- * @property string $invoiceId
- * @property string $customerNumber
- * @property string $shopPassword
- * @property string $requestDatetime
- * @property string $md5
- */
- class YandexKassa extends Model
- {
- const SHOP_ID = 142263;
- const CURRENCY_RUB = 643;
- const SC_ID = 103506;
- const SHOP_PASSWORD = "I7BKJ713dvLWDH91dgkq";
- public $requestDatetime;
- public $action;
- public $orderSumAmount;
- public $orderSumCurrencyPaycash;
- public $orderSumBankPaycash;
- public $shopId;
- public $scid;
- public $invoiceId;
- public $customerNumber;
- public $md5;
- public $code;
- public function rules()
- {
- return [
- [['requestDatetime','action','orderSumAmount','orderSumCurrencyPaycash','orderSumBankPaycash','shopId'
- ,'scid','invoiceId','customerNumber','md5','code',], 'safe'],
- ];
- }
- public function check()
- {
- if ($this->orderSumCurrencyPaycash != static::CURRENCY_RUB ||
- $this->shopId != static::SHOP_ID ||
- $this->scid != static::SC_ID
- ) {
- $this->code = 100;
- return;
- }
- if ($this->checkMD5()) {
- if ($user = User::findOne($this->customerNumber)) {
- $transaction = new Transaction();
- $transaction->attributes = [
- 'status' => Transaction::STATUS_PENDING,
- 'user_id' => $user->id,
- 'invoice_id' => $this->invoiceId,
- ];
- $transaction->save();
- $this->code = 0;
- return;
- }
- }
- $this->code = 1;
- }
- public function aviso()
- {
- file_put_contents(__DIR__ . '/resp.txt', print_r($this->attributes, true), FILE_APPEND);
- if ($this->orderSumCurrencyPaycash != static::CURRENCY_RUB ||
- $this->shopId != static::SHOP_ID ||
- $this->scid != static::SC_ID
- ) {
- $this->code = 200;
- return;
- }
- if (!$this->checkMD5()) {
- $this->code = 1;
- return;
- }
- if ($user = User::findOne($this->customerNumber)) {
- if ($transation = Transaction::findOne(['invoice_id' => $this->invoiceId])) {
- if ($transation->status = Transaction::STATUS_PENDING) {
- $user->addMoney(intval($this->orderSumAmount));
- $transation->status = Transaction::STATUS_DONE;
- $transation->save();
- }
- $this->code = 0;
- return;
- }
- }
- $this->code = 200;
- }
- private function checkMD5()
- {
- return strtoupper(md5(implode(';', [
- $this->action,
- $this->orderSumAmount,
- $this->orderSumCurrencyPaycash,
- $this->orderSumBankPaycash,
- $this->shopId,
- $this->invoiceId,
- $this->customerNumber,
- static::SHOP_PASSWORD
- ]))) == $this->md5;
- }
- }
- <?php
- namespace common\models;
- use Yii;
- /**
- * This is the model class for table "transaction".
- *
- * @property int $id
- * @property string $created_at
- * @property int $invoice_id
- * @property int $status
- * @property int $user_id
- */
- class Transaction extends \yii\db\ActiveRecord
- {
- const STATUS_PENDING = 1;
- const STATUS_DONE = 2;
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return 'transaction';
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['created_at'], 'safe'],
- [['invoice_id', 'user_id'], 'required'],
- [['invoice_id', 'status', 'user_id'], 'default', 'value' => null],
- [['invoice_id', 'status', 'user_id'], 'integer'],
- [['invoice_id'], 'unique'],
- ];
- }
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'created_at' => 'Created At',
- 'invoice_id' => 'Invoice ID',
- 'status' => 'Status',
- 'user_id' => 'User ID',
- ];
- }
- }
- <?php
- namespace frontend\controllers;
- use common\models\User;
- use common\models\YandexKassa;
- use Yii;
- use common\models\Payment;
- use common\models\PaymentSearch;
- use yii\filters\AccessControl;
- use yii\web\Controller;
- use yii\web\NotFoundHttpException;
- use yii\filters\VerbFilter;
- use yii\web\Response;
- /**
- * PaymentController implements the CRUD actions for Payment model.
- */
- class PaymentController extends Controller
- {
- /**
- * @inheritdoc
- */
- public function behaviors()
- {
- return [
- 'access' => [
- 'class' => AccessControl::className(),
- 'rules' => [
- [
- 'allow' => true,
- 'actions' => ['index'],
- 'roles' => ['@'],
- ],
- [
- 'allow' => true,
- 'actions' => ['accept', 'validate'],
- 'roles' => ['?', '@'],
- ],
- ],
- ],
- ];
- }
- public function beforeAction($action)
- {
- $this->enableCsrfValidation = false;
- return parent::beforeAction($action);
- }
- /**
- * Lists all Payment models.
- * @return mixed
- */
- public function actionIndex()
- {
- // return $this->redirect(['realty/index']);
- $searchModel = new PaymentSearch();
- $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
- return $this->render('index', [
- 'user' =>Yii::$app->user->identity,
- 'searchModel' => $searchModel,
- 'dataProvider' => $dataProvider,
- ]);
- }
- /**
- * Displays a single Payment model.
- * @param integer $id
- * @return mixed
- */
- // public function actionView($id)
- // {
- // return $this->render('view', [
- // 'model' => $this->findModel($id),
- // ]);
- // }
- public function actionValidate()
- {
- Yii::$app->response->format = Response::FORMAT_RAW;
- $kassa = new YandexKassa();
- $kassa->attributes = Yii::$app->request->post();
- file_put_contents(__DIR__ . '/resp.txt', print_r($kassa->attributes, true), FILE_APPEND);
- $kassa->check();
- file_put_contents(__DIR__ . "/resp.txt", '<?xml version="1.0" encoding="UTF-8"?><checkOrderResponse performedDatetime="' . $kassa->requestDatetime
- . '" code="' . $kassa->code . '" invoiceId="' . $kassa->invoiceId . '" shopId="' . YandexKassa::SHOP_ID . '"/>', FILE_APPEND);
- return '<?xml version="1.0" encoding="UTF-8"?><checkOrderResponse performedDatetime="' . $kassa->requestDatetime
- . '" code="' . $kassa->code . '" invoiceId="' . $kassa->invoiceId . '" shopId="' . YandexKassa::SHOP_ID . '"/>';
- }
- public function actionAccept()
- {
- Yii::$app->response->format = Response::FORMAT_RAW;
- $kassa = new YandexKassa();
- $kassa->attributes = Yii::$app->request->post();
- $kassa->aviso();
- file_put_contents(__DIR__ . "/resp.txt", '<?xml version="1.0" encoding="UTF-8"?><paymentAvisoResponse performedDatetime="' . $kassa->requestDatetime
- . '" code="' . $kassa->code . '" invoiceId="' . $kassa->invoiceId . '" shopId="' . YandexKassa::SHOP_ID . '"/>', FILE_APPEND);
- return '<?xml version="1.0" encoding="UTF-8"?><paymentAvisoResponse performedDatetime="' . $kassa->requestDatetime
- . '" code="' . $kassa->code . '" invoiceId="' . $kassa->invoiceId . '" shopId="' . YandexKassa::SHOP_ID . '"/>';
- }
- /**
- * Updates an existing Payment model.
- * If update is successful, the browser will be redirected to the 'view' page.
- * @param integer $id
- * @return mixed
- */
- // public function actionUpdate($id)
- // {
- // $model = $this->findModel($id);
- //
- // if ($model->load(Yii::$app->request->post()) && $model->save()) {
- // return $this->redirect(['view', 'id' => $model->id]);
- // } else {
- // return $this->render('update', [
- // 'model' => $model,
- // ]);
- // }
- // }
- /**
- * Deletes an existing Payment model.
- * If deletion is successful, the browser will be redirected to the 'index' page.
- * @param integer $id
- * @return mixed
- */
- // public function actionDelete($id)
- // {
- // $this->findModel($id)->delete();
- //
- // return $this->redirect(['index']);
- // }
- /**
- * Finds the Payment model based on its primary key value.
- * If the model is not found, a 404 HTTP exception will be thrown.
- * @param integer $id
- * @return Payment the loaded model
- * @throws NotFoundHttpException if the model cannot be found
- */
- // protected function findModel($id)
- // {
- // if (($model = Payment::findOne($id)) !== null) {
- // return $model;
- // } else {
- // throw new NotFoundHttpException('The requested page does not exist.');
- // }
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement