1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
handler.php
См. документацию.
1<?php
2
3namespace Sale\Handlers\PaySystem;
4
5use Bitrix\Main;
6use Bitrix\Main\Error;
7use Bitrix\Main\Request;
8use Bitrix\Main\Type\DateTime;
9use Bitrix\Main\Localization\Loc;
10use Bitrix\Sale;
11use Bitrix\Sale\PaymentCollection;
12use Bitrix\Sale\PaySystem;
13use Bitrix\Sale\Payment;
14use Bitrix\Sale\PriceMaths;
15
20class LiqPayHandler extends PaySystem\ServiceHandler
21{
30 public function initiatePay(Payment $payment, Request $request = null)
31 {
32 $busValues = $this->getParamsBusValue($payment);
33 $busValues['LIQPAY_PATH_TO_RESULT_URL'] = $this->getPathResultUrl($payment);
34
35 $xml = "<request>
36 <version>1.2</version>
37 <result_url>".$busValues['LIQPAY_PATH_TO_RESULT_URL']."</result_url>
38 <server_url>".$busValues['LIQPAY_PATH_TO_SERVER_URL']."</server_url>
39 <merchant_id>".$busValues['LIQPAY_MERCHANT_ID']."</merchant_id>
40 <order_id>PAYMENT_".$busValues['PAYMENT_ID']."</order_id>
41 <amount>".$busValues["PAYMENT_SHOULD_PAY"]."</amount>
42 <currency>".$busValues['PAYMENT_CURRENCY']."</currency>
43 <description>".$this->getPaymentDescription($payment)."</description>
44 <default_phone>".$busValues['BUYER_PERSON_PHONE']."</default_phone>
45 <pay_way>".$busValues['LIQPAY_PAY_METHOD']."</pay_way>
46 </request>";
47
48 $signature = base64_encode(sha1($busValues['LIQPAY_SIGN'].$xml.$busValues['LIQPAY_SIGN'], 1));
49
50 $params = array(
51 'URL' => $this->getUrl($payment, 'pay'),
52 'OPERATION_XML' => base64_encode($xml),
53 'SIGNATURE' => $signature,
54 );
55
56 if ($busValues['PAYMENT_CURRENCY'] == "RUB")
57 $params['PAYMENT_CURRENCY'] = "RUR";
58
59 $this->setExtraParams($params);
60
61 return $this->showTemplate($payment, "template");
62 }
63
67 public static function getIndicativeFields()
68 {
69 return array('operation_xml', 'signature');
70 }
71
72 protected static function isMyResponseExtended(Request $request, $paySystemId)
73 {
74 $paymentId = self::getValueByTag(self::getOperationXml($request), 'order_id');
75 $paymentId = (int)str_replace("PAYMENT_", "", $paymentId);
76 $payment = Sale\Repository\PaymentRepository::getInstance()->getById($paymentId);
77
78 $merchantId = Sale\BusinessValue::get(
79 'LIQPAY_MERCHANT_ID', PaySystem\Service::PAY_SYSTEM_PREFIX . $paySystemId, $payment
80 );
81 $merchantIdFromRequest = self::getValueByTag(self::getOperationXml($request), 'merchant_id');
82
83 return $merchantId === $merchantIdFromRequest;
84 }
85
91 private function isCorrectHash(Payment $payment, Request $request)
92 {
93 if ($request->get('operation_xml') !== null)
94 {
95 $sign = $this->getBusinessValue($payment, 'LIQPAY_SIGN');
96 if ($sign)
97 {
98 $hash = base64_encode(sha1($sign.self::getOperationXml($request).$sign, 1));
99 return $request->get('signature') == $hash;
100 }
101 }
102
103 return false;
104 }
105
111 private function isCorrectSum(Payment $payment, Request $request)
112 {
113 $sum = self::getValueByTag(self::getOperationXml($request), 'amount');
114 $paymentSum = $this->getBusinessValue($payment, 'PAYMENT_SHOULD_PAY');
115
116 return PriceMaths::roundPrecision($paymentSum) === PriceMaths::roundPrecision($sum);
117 }
118
123 public function getPaymentIdFromRequest(Request $request)
124 {
125 $orderId = self::getValueByTag(self::getOperationXml($request), 'order_id');
126 return str_replace("PAYMENT_", "", $orderId);
127 }
128
132 protected function getUrlList()
133 {
134 return array(
135 'pay' => array(
136 self::ACTIVE_URL => 'https://www.liqpay.ua/?do=clickNbuy'
137 )
138 );
139 }
140
146 public function processRequest(Payment $payment, Request $request)
147 {
148 $result = new PaySystem\ServiceResult();
149
150 if ($request->get('signature') === null || $request->get('operation_xml') === null)
151 {
152 $errorMessage = Loc::getMessage('SALE_HPS_LIQPAY_POST_ERROR');
153 $result->addError(new Error($errorMessage));
154
155 PaySystem\Logger::addError('LiqPay: processRequest: '.$errorMessage);
156 }
157
158 $status = self::getValueByTag(self::getOperationXml($request), 'status');
159
160 if ($this->isCorrectHash($payment, $request))
161 {
162 if ($status === 'success' || $status === 'wait_reserve')
163 {
164 return $this->processNoticeAction($payment, $request);
165 }
166
167 if ($status === 'wait_secure')
168 {
169 return new PaySystem\ServiceResult();
170 }
171 }
172 else
173 {
174 PaySystem\Logger::addError('LiqPay: processRequest: Incorrect hash');
175 $result->addError(new Error('Incorrect hash'));
176 }
177
178 return $result;
179 }
180
186 private function processNoticeAction(Payment $payment, Request $request)
187 {
188 $result = new PaySystem\ServiceResult();
189
190 $response = self::getOperationXml($request);
191
192 $description = 'sender phone: '.self::getValueByTag($response, 'sender_phone').'; ';
193 $description .= 'amount: '.self::getValueByTag($response, 'amount').'; ';
194 $description .= 'currency: '.self::getValueByTag($response, 'currency').'; ';
195
196 $statusMessage = 'status: '.self::getValueByTag($response, 'status').'; ';
197 $statusMessage .= 'transaction_id: '.self::getValueByTag($response, 'transaction_id').'; ';
198 $statusMessage .= 'pay_way: '.self::getValueByTag($response, 'pay_way').'; ';
199 $statusMessage .= 'payment_id: '.self::getValueByTag($response, 'order_id').'; ';
200
201
202 $fields = array(
203 "PS_STATUS" => "Y",
204 "PS_STATUS_CODE" => mb_substr(self::getValueByTag($response, 'status'), 0, 5),
205 "PS_STATUS_DESCRIPTION" => $description,
206 "PS_STATUS_MESSAGE" => $statusMessage,
207 "PS_SUM" => self::getValueByTag($response, 'amount'),
208 "PS_CURRENCY" => self::getValueByTag($response, 'currency'),
209 "PS_RESPONSE_DATE" => new DateTime(),
210 );
211
212 $result->setPsData($fields);
213
214 if ($this->isCorrectSum($payment, $request))
215 {
216 $result->setOperationType(PaySystem\ServiceResult::MONEY_COMING);
217 }
218 else
219 {
220 PaySystem\Logger::addError('LiqPay: processNoticeAction: Incorrect sum');
221 $result->addError(new Error('Incorrect sum'));
222 }
223
224 return $result;
225 }
226
230 public function getCurrencyList()
231 {
232 return ['RUB', 'USD', 'EUR', 'UAH'];
233 }
234
240 private static function getValueByTag($string, $tag)
241 {
242 $string = str_replace("\n", "", str_replace("\r", "", $string));
243 $open = '<'.$tag.'>';
244 $close = '</'.$tag;
245 $start = mb_strpos($string, $open) + mb_strlen($open);
246 $end = mb_strpos($string, $close);
247
248 return mb_substr($string, $start, ($end - $start));
249 }
250
255 private static function getOperationXml(Request $request)
256 {
257 static $operationXml = '';
258
259 if ($operationXml === '')
260 $operationXml = base64_decode($request->get('operation_xml'));
261
262 return $operationXml;
263 }
264
272 private function getPaymentDescription(Payment $payment)
273 {
275 $collection = $payment->getCollection();
276 $order = $collection->getOrder();
277 $userEmail = $order->getPropertyCollection()->getUserEmail();
278
279 return str_replace(
280 [
281 '#PAYMENT_NUMBER#',
282 '#ORDER_NUMBER#',
283 '#PAYMENT_ID#',
284 '#ORDER_ID#',
285 '#USER_EMAIL#'
286 ],
287 [
288 $payment->getField('ACCOUNT_NUMBER'),
289 $order->getField('ACCOUNT_NUMBER'),
290 $payment->getId(),
291 $order->getId(),
292 ($userEmail) ? $userEmail->getValue() : ''
293 ],
294 $this->getBusinessValue($payment, 'LIQPAY_PAYMENT_DESCRIPTION')
295 );
296 }
297
302 private function getPathResultUrl(Payment $payment)
303 {
304 $url = $this->getBusinessValue($payment, 'LIQPAY_PATH_TO_RESULT_URL') ?: $this->service->getContext()->getUrl();
305
306 return str_replace('&', '&amp;', $url);
307 }
308}
$hash
Определения ajax_redirector.php:8
$sum
Определения checkout.php:6
if(!Loader::includeModule('catalog')) if(!AccessController::getCurrent() ->check(ActionDictionary::ACTION_PRICE_EDIT)) if(!check_bitrix_sessid()) $request
Определения catalog_reindex.php:36
getUrl(Payment $payment=null, $action)
Определения baseservicehandler.php:343
showTemplate(Payment $payment=null, $template='')
Определения baseservicehandler.php:59
getParamsBusValue(Payment $payment=null)
Определения baseservicehandler.php:157
getBusinessValue(Payment $payment=null, $code)
Определения baseservicehandler.php:184
$orderId
Определения payment.php:5
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$start
Определения get_search.php:9
if(Loader::includeModule( 'bitrix24')) elseif(Loader::includeModule('intranet') &&CIntranetUtils::getPortalZone() !=='ru') $description
Определения .description.php:24
while($arParentIBlockProperty=$dbParentIBlockProperty->Fetch()) $errorMessage
$status
Определения session.php:10
trait Error
Определения error.php:11
$payment
Определения payment.php:14
$order
Определения payment.php:8
$sign
Определения payment.php:69
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$response
Определения result.php:21
$url
Определения iframe.php:7
$fields
Определения yandex_run.php:501