Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.66 KB | None | 0 0
  1. <?php
  2.  
  3. namespace common\models;
  4.  
  5. use Yii;
  6. use yii\base\Model;
  7.  
  8.  
  9. /**
  10. * This is the model class for table "YandexKassa".
  11. *
  12. * @property string $action
  13. * @property string $orderSumAmount
  14. * @property string $orderSumCurrencyPaycash
  15. * @property string $orderSumBankPaycash
  16. * @property string $shopId
  17. * @property string $invoiceId
  18. * @property string $customerNumber
  19. * @property string $shopPassword
  20. * @property string $requestDatetime
  21. * @property string $md5
  22. */
  23. class YandexKassa extends Model
  24. {
  25. const SHOP_ID = 142263;
  26. const CURRENCY_RUB = 643;
  27. const SC_ID = 103506;
  28. const SHOP_PASSWORD = "I7BKJ713dvLWDH91dgkq";
  29.  
  30. public $requestDatetime;
  31.  
  32. public $action;
  33. public $orderSumAmount;
  34. public $orderSumCurrencyPaycash;
  35. public $orderSumBankPaycash;
  36. public $shopId;
  37. public $scid;
  38. public $invoiceId;
  39. public $customerNumber;
  40.  
  41. public $md5;
  42. public $code;
  43.  
  44. public function rules()
  45. {
  46. return [
  47. [['requestDatetime','action','orderSumAmount','orderSumCurrencyPaycash','orderSumBankPaycash','shopId'
  48. ,'scid','invoiceId','customerNumber','md5','code',], 'safe'],
  49. ];
  50. }
  51.  
  52. public function check()
  53. {
  54. if ($this->orderSumCurrencyPaycash != static::CURRENCY_RUB ||
  55. $this->shopId != static::SHOP_ID ||
  56. $this->scid != static::SC_ID
  57. ) {
  58. $this->code = 100;
  59. return;
  60. }
  61.  
  62.  
  63.  
  64.  
  65. if ($this->checkMD5()) {
  66. if ($user = User::findOne($this->customerNumber)) {
  67. $transaction = new Transaction();
  68. $transaction->attributes = [
  69. 'status' => Transaction::STATUS_PENDING,
  70. 'user_id' => $user->id,
  71. 'invoice_id' => $this->invoiceId,
  72. ];
  73. $transaction->save();
  74. $this->code = 0;
  75. return;
  76. }
  77. }
  78. $this->code = 1;
  79. }
  80.  
  81. public function aviso()
  82. {
  83. file_put_contents(__DIR__ . '/resp.txt', print_r($this->attributes, true), FILE_APPEND);
  84. if ($this->orderSumCurrencyPaycash != static::CURRENCY_RUB ||
  85. $this->shopId != static::SHOP_ID ||
  86. $this->scid != static::SC_ID
  87. ) {
  88. $this->code = 200;
  89. return;
  90. }
  91. if (!$this->checkMD5()) {
  92. $this->code = 1;
  93. return;
  94. }
  95.  
  96. if ($user = User::findOne($this->customerNumber)) {
  97. if ($transation = Transaction::findOne(['invoice_id' => $this->invoiceId])) {
  98. if ($transation->status = Transaction::STATUS_PENDING) {
  99. $user->addMoney(intval($this->orderSumAmount));
  100. $transation->status = Transaction::STATUS_DONE;
  101. $transation->save();
  102. }
  103. $this->code = 0;
  104. return;
  105. }
  106.  
  107. }
  108. $this->code = 200;
  109. }
  110.  
  111. private function checkMD5()
  112. {
  113. return strtoupper(md5(implode(';', [
  114. $this->action,
  115. $this->orderSumAmount,
  116. $this->orderSumCurrencyPaycash,
  117. $this->orderSumBankPaycash,
  118. $this->shopId,
  119. $this->invoiceId,
  120. $this->customerNumber,
  121. static::SHOP_PASSWORD
  122. ]))) == $this->md5;
  123. }
  124.  
  125. }
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135. <?php
  136.  
  137. namespace common\models;
  138.  
  139. use Yii;
  140.  
  141. /**
  142. * This is the model class for table "transaction".
  143. *
  144. * @property int $id
  145. * @property string $created_at
  146. * @property int $invoice_id
  147. * @property int $status
  148. * @property int $user_id
  149. */
  150. class Transaction extends \yii\db\ActiveRecord
  151. {
  152. const STATUS_PENDING = 1;
  153. const STATUS_DONE = 2;
  154.  
  155. /**
  156. * @inheritdoc
  157. */
  158. public static function tableName()
  159. {
  160. return 'transaction';
  161. }
  162.  
  163. /**
  164. * @inheritdoc
  165. */
  166. public function rules()
  167. {
  168. return [
  169. [['created_at'], 'safe'],
  170. [['invoice_id', 'user_id'], 'required'],
  171. [['invoice_id', 'status', 'user_id'], 'default', 'value' => null],
  172. [['invoice_id', 'status', 'user_id'], 'integer'],
  173. [['invoice_id'], 'unique'],
  174. ];
  175. }
  176.  
  177. /**
  178. * @inheritdoc
  179. */
  180. public function attributeLabels()
  181. {
  182. return [
  183. 'id' => 'ID',
  184. 'created_at' => 'Created At',
  185. 'invoice_id' => 'Invoice ID',
  186. 'status' => 'Status',
  187. 'user_id' => 'User ID',
  188. ];
  189. }
  190. }
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203. <?php
  204.  
  205. namespace frontend\controllers;
  206.  
  207. use common\models\User;
  208. use common\models\YandexKassa;
  209. use Yii;
  210. use common\models\Payment;
  211. use common\models\PaymentSearch;
  212. use yii\filters\AccessControl;
  213. use yii\web\Controller;
  214. use yii\web\NotFoundHttpException;
  215. use yii\filters\VerbFilter;
  216. use yii\web\Response;
  217.  
  218. /**
  219. * PaymentController implements the CRUD actions for Payment model.
  220. */
  221. class PaymentController extends Controller
  222. {
  223. /**
  224. * @inheritdoc
  225. */
  226. public function behaviors()
  227. {
  228. return [
  229. 'access' => [
  230. 'class' => AccessControl::className(),
  231. 'rules' => [
  232. [
  233. 'allow' => true,
  234. 'actions' => ['index'],
  235. 'roles' => ['@'],
  236. ],
  237. [
  238. 'allow' => true,
  239. 'actions' => ['accept', 'validate'],
  240. 'roles' => ['?', '@'],
  241. ],
  242. ],
  243. ],
  244. ];
  245. }
  246.  
  247. public function beforeAction($action)
  248. {
  249. $this->enableCsrfValidation = false;
  250. return parent::beforeAction($action);
  251. }
  252.  
  253. /**
  254. * Lists all Payment models.
  255. * @return mixed
  256. */
  257. public function actionIndex()
  258. {
  259. // return $this->redirect(['realty/index']);
  260. $searchModel = new PaymentSearch();
  261. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  262.  
  263. return $this->render('index', [
  264. 'user' =>Yii::$app->user->identity,
  265. 'searchModel' => $searchModel,
  266. 'dataProvider' => $dataProvider,
  267. ]);
  268. }
  269.  
  270. /**
  271. * Displays a single Payment model.
  272. * @param integer $id
  273. * @return mixed
  274. */
  275. // public function actionView($id)
  276. // {
  277. // return $this->render('view', [
  278. // 'model' => $this->findModel($id),
  279. // ]);
  280. // }
  281.  
  282.  
  283. public function actionValidate()
  284. {
  285. Yii::$app->response->format = Response::FORMAT_RAW;
  286.  
  287. $kassa = new YandexKassa();
  288. $kassa->attributes = Yii::$app->request->post();
  289. file_put_contents(__DIR__ . '/resp.txt', print_r($kassa->attributes, true), FILE_APPEND);
  290. $kassa->check();
  291.  
  292. file_put_contents(__DIR__ . "/resp.txt", '<?xml version="1.0" encoding="UTF-8"?><checkOrderResponse performedDatetime="' . $kassa->requestDatetime
  293. . '" code="' . $kassa->code . '" invoiceId="' . $kassa->invoiceId . '" shopId="' . YandexKassa::SHOP_ID . '"/>', FILE_APPEND);
  294.  
  295. return '<?xml version="1.0" encoding="UTF-8"?><checkOrderResponse performedDatetime="' . $kassa->requestDatetime
  296. . '" code="' . $kassa->code . '" invoiceId="' . $kassa->invoiceId . '" shopId="' . YandexKassa::SHOP_ID . '"/>';
  297. }
  298.  
  299. public function actionAccept()
  300. {
  301. Yii::$app->response->format = Response::FORMAT_RAW;
  302.  
  303. $kassa = new YandexKassa();
  304. $kassa->attributes = Yii::$app->request->post();
  305. $kassa->aviso();
  306.  
  307. file_put_contents(__DIR__ . "/resp.txt", '<?xml version="1.0" encoding="UTF-8"?><paymentAvisoResponse performedDatetime="' . $kassa->requestDatetime
  308. . '" code="' . $kassa->code . '" invoiceId="' . $kassa->invoiceId . '" shopId="' . YandexKassa::SHOP_ID . '"/>', FILE_APPEND);
  309.  
  310. return '<?xml version="1.0" encoding="UTF-8"?><paymentAvisoResponse performedDatetime="' . $kassa->requestDatetime
  311. . '" code="' . $kassa->code . '" invoiceId="' . $kassa->invoiceId . '" shopId="' . YandexKassa::SHOP_ID . '"/>';
  312. }
  313.  
  314. /**
  315. * Updates an existing Payment model.
  316. * If update is successful, the browser will be redirected to the 'view' page.
  317. * @param integer $id
  318. * @return mixed
  319. */
  320. // public function actionUpdate($id)
  321. // {
  322. // $model = $this->findModel($id);
  323. //
  324. // if ($model->load(Yii::$app->request->post()) && $model->save()) {
  325. // return $this->redirect(['view', 'id' => $model->id]);
  326. // } else {
  327. // return $this->render('update', [
  328. // 'model' => $model,
  329. // ]);
  330. // }
  331. // }
  332.  
  333. /**
  334. * Deletes an existing Payment model.
  335. * If deletion is successful, the browser will be redirected to the 'index' page.
  336. * @param integer $id
  337. * @return mixed
  338. */
  339. // public function actionDelete($id)
  340. // {
  341. // $this->findModel($id)->delete();
  342. //
  343. // return $this->redirect(['index']);
  344. // }
  345.  
  346. /**
  347. * Finds the Payment model based on its primary key value.
  348. * If the model is not found, a 404 HTTP exception will be thrown.
  349. * @param integer $id
  350. * @return Payment the loaded model
  351. * @throws NotFoundHttpException if the model cannot be found
  352. */
  353. // protected function findModel($id)
  354. // {
  355. // if (($model = Payment::findOne($id)) !== null) {
  356. // return $model;
  357. // } else {
  358. // throw new NotFoundHttpException('The requested page does not exist.');
  359. // }
  360. // }
  361. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement