1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
handler.php
См. документацию.
1<?php
2
3namespace Sale\Handlers\PaySystem;
4
5use Bitrix\Main\Entity\EntityError;
6use Bitrix\Main\Localization\Loc;
7use Bitrix\Main\Request;
8use Bitrix\Main\Error;
9use Bitrix\Main\Type\DateTime;
10use Bitrix\Main\Web\HttpClient;
11use Bitrix\Sale\PaySystem;
12use Bitrix\Sale\Payment;
13use Bitrix\Sale\PriceMaths;
14
15Loc::loadMessages(__FILE__);
16
17class AssistHandler extends PaySystem\ServiceHandler implements PaySystem\IRefund, PaySystem\ICheckable
18{
24 public function initiatePay(Payment $payment, Request $request = null)
25 {
27 'URL' => $this->getUrl($payment, 'pay'),
28 'ASSIST_SUCCESS_URL' => $this->getSuccessUrl($payment),
29 'ASSIST_FAIL_URL' => $this->getFailUrl($payment),
30 );
32
33 return $this->showTemplate($payment, "template");
34 }
35
39 public static function getIndicativeFields()
40 {
41 return array('ordernumber', 'billnumber', 'orderamount', 'amount', 'meantypename', 'meantype_id', 'approvalcode', 'operationtype');
42 }
43
49 public function refund(Payment $payment, $refundableSum)
50 {
51 $result = new PaySystem\ServiceResult();
52
54 $refundUrl = $this->getUrl($payment, 'return');
55
56 $data = array(
57 'Billnumber' => $payment->getField('PS_INVOICE_ID'),
58 'Merchant_ID' => $params['ASSIST_SHOP_IDP'],
59 'Login' => $params['ASSIST_SHOP_LOGIN'],
60 'Password' => $params['ASSIST_SHOP_PASSWORD'],
61 'Amount' => $refundableSum,
62 'Currency' => $params['PAYMENT_CURRENCY'],
63 'Format' => 3
64 );
65
66 $clientHttp = new HttpClient();
67 $response = $clientHttp->post($refundUrl, $data);
68
69 if ($response)
70 {
71 $xml = new \CDataXML();
72 $xml->LoadString($response);
73 $data = $xml->GetArray();
74 if ($data && $data['result']['@']['firstcode'] == '0' && $data['result']['@']['secondcode'] == '0')
75 {
76 $result->setOperationType(PaySystem\ServiceResult::MONEY_LEAVING);
77 }
78 else
79 {
80 $error = 'assist error refund: firstcode='.$data['result']['@']['firstcode'].' secondcode='.$data['result']['@']['secondcode'];
81 PaySystem\Logger::addError('Assist: return: '.$error);
82 $result->addError(new EntityError(Loc::getMessage('SALE_PS_MESSAGE_ERROR_CONNECT_PAY_SYS')));
83 }
84 }
85 else
86 {
87 $message = 'Incorrect server response';
88 $result->addError(new Error($message));
89
90 PaySystem\Logger::addError('Assist: return: '.$message);
91 }
92
93 return $result;
94 }
95
101 private function isCorrectHash(Payment $payment, Request $request)
102 {
103 $hash = md5(
104 mb_strtoupper(md5($this->getBusinessValue($payment, 'ASSIST_SHOP_SECRET_WORLD')).md5(
105 $this->getBusinessValue($payment, 'ASSIST_SHOP_IDP').$request->get('ordernumber').$request->get('amount').$this->getBusinessValue($payment, 'PAYMENT_CURRENCY').$request->get('orderstate')
106 ))
107 );
108
109 return (mb_strtoupper($hash) == mb_strtoupper($request->get('checkvalue')));
110 }
111
117 private function isCorrectSum(Payment $payment, Request $request)
118 {
119 $sum = $request->get('orderamount');
120 $paymentSum = $this->getBusinessValue($payment, 'PAYMENT_SHOULD_PAY');
121
122 return PriceMaths::roundPrecision($paymentSum) === PriceMaths::roundPrecision($sum);
123 }
124
130 public function sendResponse(PaySystem\ServiceResult $result, Request $request)
131 {
132 global $APPLICATION;
133 $APPLICATION->RestartBuffer();
134
135 header('Content-Type: text/xml');
136 header('Pragma: no-cache');
137 $text = '<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n';
138
139 if ($result->isResultApplied())
140 {
141 $text .= '<pushpaymentresult firstcode=\'0\' secondcode=\'0\'>';
142 $text .= '<order>';
143 $text .= '<billnumber>'.$request->get('billnumber').'</billnumber>';
144 $text .= '<packetdate>'.$request->get('packetdate').'</packetdate>';
145 $text .= '</order>';
146 }
147 else
148 {
149 $text .= '<pushpaymentresult firstcode=\'9\' secondcode=\'7\'>';
150 }
151
152 $text .= '</pushpaymentresult>';
153
154 echo $text;
155 die();
156 }
157
162 public function getPaymentIdFromRequest(Request $request)
163 {
164 return $request->get('ordernumber');
165 }
166
172 public function processRequest(Payment $payment, Request $request)
173 {
174 $result = new PaySystem\ServiceResult();
175
176 if ($this->isCorrectHash($payment, $request))
177 {
178 $status = str_replace(' ', '', $request->get('orderstate'));
179 $psStatus = ($status == "Approved") ? "Y" : "N";
180
181 $result->setPSData(
182 array(
183 "PS_STATUS" => $psStatus,
184 "PS_STATUS_CODE" => mb_substr($status, 0, 5),
185 "PS_STATUS_DESCRIPTION" => Loc::getMessage('SALE_PS_DESCRIPTION_'.mb_strtoupper($status)),
186 "PS_STATUS_MESSAGE" => Loc::getMessage('SALE_PS_MESSAGE_'.mb_strtoupper($status)),
187 "PS_SUM" => $request->get('orderamount'),
188 "PS_CURRENCY" => $request->get('ordercurrency'),
189 "PS_INVOICE_ID" => $request->get('billnumber'),
190 "PS_RESPONSE_DATE" => new DateTime()
191 )
192 );
193
194 if ($this->isCorrectSum($payment, $request) &&
195 $this->getBusinessValue($payment, 'PS_CHANGE_STATUS_PAY') == 'Y' &&
196 $psStatus == 'Y' &&
197 !$payment->isPaid()
198 )
199 {
200 $result->setOperationType(PaySystem\ServiceResult::MONEY_COMING);
201 }
202 else
203 {
204 $result->addError(new Error('Incorrect sum or payment flag'));
205 }
206 }
207 else
208 {
209 $result->addError(new Error('Incorrect hash sum'));
210 }
211
212 if (!$result->isSuccess())
213 {
214 PaySystem\Logger::addError('Assist: '.$request->get('orderstate').': '.join('\n', $result->getErrorMessages()));
215 }
216
217 return $result;
218 }
219
224 protected function isTestMode(Payment $payment = null)
225 {
226 return ($this->getBusinessValue($payment, 'PS_IS_TEST') == 'Y');
227 }
228
232 protected function getUrlList()
233 {
234 return array(
235 'confirm' => 'https://#SERVER_NAME#/charge/charge.cfm',
236 'return' => 'https://#SERVER_NAME#/cancel/wscancel.cfm',
237 'pay' => 'https://#SERVER_NAME#/pay/order.cfm',
238 'check' => 'https://#SERVER_NAME#/orderstate/orderstate.cfm',
239 );
240 }
241
247 protected function getUrl(Payment $payment = null, $action)
248 {
249 $url = parent::getUrl($payment, $action);
250 if ($this->isTestMode($payment))
251 $domain = 'payments.demo.paysecure.ru';
252 else
253 $domain = $this->getBusinessValue($payment, 'ASSIST_SERVER_URL');
254
255 return str_replace('#SERVER_NAME#', $domain, $url);
256 }
257
261 public function getCurrencyList()
262 {
263 return array('RUB', 'USD', 'EUR');
264 }
265
270 public function check(Payment $payment)
271 {
272 $serviceResult = new PaySystem\ServiceResult();
273
274 $dtm = AddToTimeStamp(array("MM" => -1), false);
275
277 'Ordernumber' => $this->getBusinessValue($payment, 'PAYMENT_ID'),
278 'Merchant_ID' => $this->getBusinessValue($payment, 'ASSIST_SHOP_IDP'),
279 'login' => $this->getBusinessValue($payment, 'ASSIST_SHOP_LOGIN'),
280 'password' => $this->getBusinessValue($payment, 'ASSIST_SHOP_PASSWORD'),
281 'FORMAT' => 3,
282 'StartYear' => date('Y', $dtm),
283 'StartMonth' => date('n', $dtm),
284 'StartYDay' => date('j', $dtm)
285 );
286
287 $httpClient = new HttpClient();
288 $queryRes = $httpClient->query('POST', $this->getUrl($payment, 'check'), $postData);
289
290 if ($queryRes)
291 {
292 $httpResult = $httpClient->getResult();
293
294 $objXML = new \CDataXML();
295 $objXML->LoadString($httpResult);
296 $data = $objXML->GetArray();
297
298 if ($data && $data['result']['@']['firstcode'] == '0')
299 {
300 $orderData = $data['result']['#']['order'][0]['#'];
301 if ((int)$orderData['ordernumber'][0]['#'] == $this->getBusinessValue($payment, 'PAYMENT_ID'))
302 {
303 $check = mb_strtoupper(md5(ToUpper(md5($this->getBusinessValue($payment, 'ASSIST_SHOP_SECRET_WORLD')).md5($this->getBusinessValue($payment, 'ASSIST_SHOP_IDP').$orderData['ordernumber'][0]['#'].$orderData['orderamount'][0]['#'].$orderData['ordercurrency'][0]['#'].$orderData['orderstate'][0]['#']))));
304
305 if (mb_strtoupper($orderData['checkvalue'][0]['#']) == $check)
306 {
307 $status = str_replace(' ', '', $orderData['orderstate'][0]['#']);
308
309 $psData = array(
310 'PS_STATUS' => ($orderData['orderstate'][0]['#'] == 'Approved' ? 'Y' : 'N'),
311 'PS_STATUS_CODE' => mb_substr($orderData['orderstate'][0]['#'], 0, 5),
312 'PS_STATUS_DESCRIPTION' => Loc::getMessage('SALE_PS_DESCRIPTION_'.mb_strtoupper($status)),
313 'PS_STATUS_MESSAGE' => Loc::getMessage('SALE_PS_MESSAGE_'.mb_strtoupper($status)),
314 'PS_SUM' => DoubleVal($orderData['orderamount'][0]['#']),
315 'PS_CURRENCY' => $orderData['ordercurrency'][0]['#'],
316 'PS_RESPONSE_DATE' => new DateTime(),
317 );
318 $serviceResult->setPsData($psData);
319
320 if (
321 !$payment->isPaid() &&
322 $this->getBusinessValue($payment, 'PS_CHANGE_STATUS_PAY') == 'Y' &&
323 $psData["PS_STATUS"] == "Y" &&
324 $payment->getSum() == floatval($psData["PS_SUM"])
325 )
326 {
327 $serviceResult->setOperationType(PaySystem\ServiceResult::MONEY_COMING);
328 }
329 }
330 }
331 }
332 else
333 {
334 $serviceResult->addError(new EntityError(Loc::getMessage('SALE_PS_MESSAGE_ERROR_CONNECT_PAY_SYS')));
335 }
336 }
337
338 return $serviceResult;
339 }
340
345 private function getSuccessUrl(Payment $payment)
346 {
347 return $this->getBusinessValue($payment, 'ASSIST_SUCCESS_URL') ?: $this->service->getContext()->getUrl();
348 }
349
354 private function getFailUrl(Payment $payment)
355 {
356 return $this->getBusinessValue($payment, 'ASSIST_FAIL_URL') ?: $this->service->getContext()->getUrl();
357 }
358}
$hash
Определения ajax_redirector.php:8
$sum
Определения checkout.php:6
global $APPLICATION
Определения include.php:80
if(!Loader::includeModule('catalog')) if(!AccessController::getCurrent() ->check(ActionDictionary::ACTION_PRICE_EDIT)) if(!check_bitrix_sessid()) $request
Определения catalog_reindex.php:36
showTemplate(Payment $payment=null, $template='')
Определения baseservicehandler.php:59
getParamsBusValue(Payment $payment=null)
Определения baseservicehandler.php:157
getBusinessValue(Payment $payment=null, $code)
Определения baseservicehandler.php:184
$data['IS_AVAILABLE']
Определения .description.php:13
</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
$status
Определения session.php:10
ToUpper($str)
Определения tools.php:2247
AddToTimeStamp($arrAdd, $stmp=false)
Определения tools.php:687
trait Error
Определения error.php:11
$payment
Определения payment.php:14
$message
Определения payment.php:8
die
Определения quickway.php:367
$text
Определения template_pdf.php:79
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$dtm
Определения result.php:15
$response
Определения result.php:21
$postData
Определения index.php:29
$error
Определения subscription_card_product.php:20
$action
Определения file_dialog.php:21
$url
Определения iframe.php:7