Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
registerservice.php
1<?php
2
4
7
8final class RegisterService
9{
10 private const SIGN_PART = 'bitrix';
11 private const CREATE_ACTION = 'rk_reg_created';
12
13 public function processRequest(Main\Request $request): Sale\PaySystem\ServiceResult
14 {
15 $result = new Sale\PaySystem\ServiceResult();
16
17 $action = $request->get('act');
18 if ($action === self::CREATE_ACTION)
19 {
20 $checkRequiredFieldsResult = $this->checkRequiredFields($request);
21 if (!$checkRequiredFieldsResult->isSuccess())
22 {
23 $result->addErrors($checkRequiredFieldsResult->getErrors());
24 return $result;
25 }
26
27 $signedDomain = $request->get('signed_domain') ?? '';
28 if (!$this->isValidDomain($signedDomain))
29 {
30 $result->addError(new Main\Error('Signed domain not valid'));
31 return $result;
32 }
33
34 $shopId = $request->get('shopId');
35 $sign = $request->get('sign');
36 $key1 = $request->get('key_1');
37 $key2 = $request->get('key_2');
38
39 if (!$this->isSignValid($shopId, $sign))
40 {
41 $result->addError(new Main\Error('Bad sign'));
42 return $result;
43 }
44
45 try
46 {
47 $saveResult = self::save($shopId, $key1, $key2);
48 }
49 catch (\Exception $exception)
50 {
51 $saveResult = new Main\Result();
52 $saveResult->addError(new Main\Error($exception->getMessage()));
53 }
54
55 if (!$saveResult->isSuccess())
56 {
57 $result->addErrors($saveResult->getErrors());
58 }
59 }
60
61 return $result;
62 }
63
72 private static function save(string $shopId, string $key1, string $key2): Main\Result
73 {
74 $settingsFields = self::prepareSettings($shopId, $key1, $key2);
75
76 $shopSettings = new Sale\PaySystem\Robokassa\ShopSettings();
77
78 $currentSettings = $shopSettings->get();
79 if ($currentSettings)
80 {
81 return $shopSettings->update($settingsFields);
82 }
83
84 return $shopSettings->add($settingsFields);
85 }
86
87 private static function prepareSettings(string $shopId, string $key1, string $key2): array
88 {
89 return [
90 'ROBOXCHANGE_SHOPLOGIN' => $shopId,
91 'ROBOXCHANGE_SHOPPASSWORD' => $key1,
92 'ROBOXCHANGE_SHOPPASSWORD2' => $key2,
93 ];
94 }
95
96 private function isSignValid(string $shopId, string $sign): bool
97 {
98 $calculatedSign = md5(sprintf('%s.%s', self::SIGN_PART, $shopId));
99 return strcasecmp($calculatedSign, $sign) === 0;
100 }
101
102 private function checkRequiredFields(Main\Request $request): Main\Result
103 {
104 $result = new Main\Result();
105
106 $requireFields = [
107 'shopId',
108 'sign',
109 'key_1',
110 'key_2',
111 'signed_domain',
112 ];
113
114 foreach ($requireFields as $field)
115 {
116 if (!$request->get($field))
117 {
118 $result->addError(new Main\Error("{$field} not found"));
119 }
120 }
121
122 return $result;
123 }
124
125 public function isValidDomain(string $signedDomain): bool
126 {
127 $request = Main\Application::getInstance()->getContext()->getRequest();
128 $protocol = $request->isHttps() ? 'https' : 'http';
129 $domain = "{$protocol}://{$request->getHttpHost()}";
130
131 return (new Sale\PaySystem\Robokassa\DomainSigner($domain))->isValidDomain($signedDomain);
132 }
133}