3namespace Sale\Handlers\PaySystem;
5use Bitrix\Main\Application;
6use Bitrix\Main\ArgumentException;
8use Bitrix\Main\Localization\Loc;
9use Bitrix\Main\Request;
10use Bitrix\Main\Type\DateTime;
12use Bitrix\Sale\BusinessValue;
13use Bitrix\Sale\Internals\YandexSettingsTable;
14use Bitrix\Sale\Payment;
15use Bitrix\Sale\PaySystem;
16use Bitrix\Sale\PriceMaths;
18Loc::loadMessages(__FILE__);
38 $serviceResult =
new PaySystem\ServiceResult();
50 'customerId' =>
$request->get(
'phone'),
51 'amount' => $this->getBusinessValue(
$payment,
'PAYMENT_SHOULD_PAY'),
52 'currency' =>
$payment->getField(
'CURRENCY'),
53 'parameters' =>
array(
54 'pay_system_id' => $this->service->getField(
'ID')
57 'schemes' =>
array($this->service->getField(
'PS_MODE'))
62 $http =
new Web\HttpClient();
63 $http->setCharset(
"utf-8");
72 catch (ArgumentException $e)
79 if (in_array(
$result[
'status'],
array(
'Created',
'Approved')))
81 $scheme = current(
$result[
'schemes']);
87 'scheme' => $scheme[
'scheme'],
88 'orderId' =>
$result[
'orderId'],
91 $http =
new Web\HttpClient();
92 $http->setCharset(
"utf-8");
100 catch (ArgumentException $e)
105 if (in_array(
$result[
'status'],
array(
'Authorized',
'Processing')))
106 $serviceResult->setPsData(
array(
'PS_INVOICE_ID' =>
$result[
'orderId']));
112 $errors .= implode(
"\n", $http->getError());
116 else if (
$result[
'status'] ==
'Refused')
124 $errors .= implode(
"\n", $http->getError());
129 $templateName =
'success';
135 $templateName =
'failure';
139 $serviceResult->setTemplate(
$template->getTemplate());
151 'ORDER_ID' =>
$order->getId(),
152 'ACCOUNT_NUMBER' =>
$order->getField(
'ACCOUNT_NUMBER'),
153 'PAYSYSTEM_ID' => $this->service->getField(
'ID')
160 return $serviceResult;
178 $serviceResult =
new PaySystem\ServiceResult();
180 if (
$request->get(
'request') ===
null)
181 return $serviceResult;
183 list($header, $payload,
$sign) = explode(
'.',
$request->get(
'request'));
184 if (!$this->isSignCorrect(
$payment, $header.
'.'.$payload,
$sign))
186 $payload = Web\Json::decode(self::base64Decode($payload));
189 'notificationType' =>
'PaymentAviso',
190 'orderId' => $payload[
'orderId'],
193 'status' =>
'Refused',
194 'error' =>
'IllegalSignature'
199 return $serviceResult;
202 $header = Web\Json::decode(self::base64Decode($header));
203 $payload = Web\Json::decode(self::base64Decode($payload));
205 if ($payload[
'notificationType'] ===
'PaymentAviso' && $header[
'iss'] ===
'Yandex.Money')
207 return $this->processNoticeAction(
$payment, $payload);
211 'notificationType' =>
'PaymentAviso',
212 'orderId' => $payload[
'orderId'],
215 'status' =>
'Refused',
216 'error' =>
'SyntaxError'
221 return $serviceResult;
229 private function processNoticeAction(Payment
$payment,
array $payload)
231 $serviceResult =
new PaySystem\ServiceResult();
234 'notificationType' =>
'PaymentAviso',
235 'orderId' => $payload[
'orderId'],
241 $yandexPrice = PriceMaths::roundPrecision($payload[
'order'][
'amount']);
242 if ($yandexPrice === $paymentPrice)
244 $serviceResult->setOperationType($serviceResult::MONEY_COMING);
246 'PS_INVOICE_ID' => $payload[
'orderId'],
247 'PS_STATUS' => ($payload[
'status'] ==
'Authorized') ?
'Y' :
'N',
248 'PS_SUM' => $payload[
'order'][
'amount'],
249 'PS_CURRENCY' => mb_substr($payload[
'order'][
'currency'], 0, 3),
250 'PS_RESPONSE_DATE' =>
new DateTime(),
252 $serviceResult->setPsData($psData);
254 $data[
'status'] =
'Delivered';
258 $data[
'status'] =
'Refused';
259 $data[
'error'] =
'SyntaxError';
260 $serviceResult->addError(
new Error(Loc::getMessage(
'SALE_HPS_YANDEX_INVOICE_ERROR_SUM')));
264 return $serviceResult;
271 public function getPaymentIdFromRequest(Request
$request)
273 list($header, $payload,
$sign) = explode(
'.',
$request->get(
'request'));
276 $payload = Web\Json::decode(self::base64Decode($payload));
277 return $payload[
'order'][
'clientOrderId'];
291 self::TEST_URL =>
'http://angius.yandex.ru:8082/payment-api/json-api/api/version',
292 self::ACTIVE_URL =>
'https://money.yandex.ru/api/v2/version'
295 self::TEST_URL =>
'http://angius.yandex.ru:8082/payment-api/json-api/api/payments',
296 self::ACTIVE_URL =>
'https://money.yandex.ru/api/v2/payments'
299 self::TEST_URL =>
'http://angius.yandex.ru:8082/payment-api/json-api/api/mobileInvoice',
300 self::ACTIVE_URL =>
'https://money.yandex.ru/api/v2/payments/mobileInvoice'
311 'Sberbank' => Loc::getMessage(
'SALE_HPS_YANDEX_INVOICE_SBERBANK'),
312 'Wallet' => Loc::getMessage(
'SALE_HPS_YANDEX_INVOICE_WALLET')
322 private function prepareRequest(Payment
$payment,
array $payload)
326 "iat" => round(microtime(
true) * 1000),
328 "aud" => $this->isTestMode(
$payment) ?
'test' :
'production'
331 $jsonHeader = Web\Json::encode((
object)$header);
332 $jsonPayload = Web\Json::encode((
object)$payload);
334 $data = self::base64Encode($jsonHeader).
'.'.self::base64Encode($jsonPayload);
351 static private function base64Encode(
$data)
353 return rtrim(strtr(base64_encode(
$data),
'+/',
'-_'),
'=');
360 static private function base64Decode(
$data)
362 return base64_decode(strtr(
$data,
'-_',
'+/'));
370 public function sendResponse(PaySystem\ServiceResult
$result, Request
$request)
376 echo
$data[
'response'];
390 $binary = self::base64Decode(
$sign);
391 list($r, $s) = str_split($binary, (
int) (mb_strlen($binary) / 2));
393 $r = ltrim($r,
"\x00");
394 $s = ltrim($s,
"\x00");
396 if (ord($r[0]) > 0x7f) $r =
"\x00" . $r;
397 if (ord($s[0]) > 0x7f) $s =
"\x00" . $s;
399 $binary = PaySystem\ASN1::encodeDER(
409 $verify = openssl_verify(
$data, $binary,
$settings[
'PUB_KEY'],
'SHA256');
410 return $verify === 1;
421 private function sign($input, $keyResource)
424 $signAlgo = OPENSSL_ALGO_SHA256;
426 $r = openssl_sign($input, $signature, $keyResource, $signAlgo);
431 $offset += PaySystem\ASN1::readDER($signature, $offset, $value);
432 $offset += PaySystem\ASN1::readDER($signature, $offset, $r);
433 $offset += PaySystem\ASN1::readDER($signature, $offset, $s);
435 $r = ltrim($r,
"\x00");
436 $s = ltrim($s,
"\x00");
441 $signature = $r . $s;
451 private function getKeyResource(
$key)
453 if (is_resource(
$key))
456 return openssl_pkey_get_public(
$key) ?: openssl_pkey_get_private(
$key);
463 protected function isTestMode(Payment
$payment =
null)
473 return array(
'request');
481 protected static function isMyResponseExtended(Request
$request, $paySystemId)
483 list($header, $payload,
$sign) = explode(
'.',
$request->get(
'request'));
486 $payload = Web\Json::decode(self::base64Decode($payload));
488 if (!array_key_exists(
'parameters', $payload[
'order']))
491 if (!array_key_exists(
'pay_system_id', $payload[
'order'][
'parameters']))
494 return $paySystemId == $payload[
'order'][
'parameters'][
'pay_system_id'];
505 $personTypeList = PaySystem\Manager::getPersonTypeIdList($this->service->getField(
'ID'));
506 $personTypeId = array_shift($personTypeList);
507 $shopId = BusinessValue::get(
'YANDEX_INVOICE_SHOP_ID', $this->service->getConsumerName(), $personTypeId);
if(!Loader::includeModule('catalog')) if(!AccessController::getCurrent() ->check(ActionDictionary::ACTION_PRICE_EDIT)) if(!check_bitrix_sessid()) $request
static encodeDER($type, $value='', $primitive=true, $class=0)
static getHandlerModeList()
getUrl(Payment $payment=null, $action)
initiatePay(Payment $payment, Request $request=null)
showTemplate(Payment $payment=null, $template='')
setExtraParams(array $values)
getBusinessValue(Payment $payment=null, $code)
static getIndicativeFields()
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
if(empty($signedUserToken)) $key
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']