165 lines
5.2 KiB
PHP
165 lines
5.2 KiB
PHP
<?php
|
|
|
|
class PaymentParams
|
|
{
|
|
private $order_id;
|
|
private $merchant_id;
|
|
private $merchant_key;
|
|
private $request_url;
|
|
private $success_url;
|
|
private $fail_url;
|
|
private $showOrderId;
|
|
private $hold;
|
|
private $nonce;
|
|
private $items_receipt;
|
|
private $send_receipt;
|
|
private $message;
|
|
private $messageText;
|
|
private $params;
|
|
private $config;
|
|
|
|
public function __construct($config)
|
|
{
|
|
$this->config = $config ?? null;
|
|
|
|
$this->order_id = $this->config['orderId'] ?? null;
|
|
|
|
$this->merchant_id = $this->config['merchant_id'] ?? null;
|
|
$this->merchant_key = $this->config['merchant_key'] ?? null;
|
|
$this->request_url = $this->config['domain'] ?? null;
|
|
$this->success_url = $this->config['success_url'] ?? null;
|
|
$this->fail_url = $this->config['fail_url'] ?? null;
|
|
$this->nonce = sha1(time() . "order-123");
|
|
$this->items_receipt = [];
|
|
$this->send_receipt = true; // Добавляем инициализацию свойства
|
|
$this->message = '';
|
|
$this->params = [];
|
|
|
|
$optionalFields =
|
|
[
|
|
"showOrderId","details","paymentMethod","customerAccount","customerComment",
|
|
"customerEmail","customerPhone","displayLanguage","recurrentable","debug",
|
|
"fdReceipt","hold","merchantData","templates","ac","merchantData",
|
|
"sbpSubscriptionPurpose","sbpRedirectUrl"
|
|
];
|
|
|
|
foreach($optionalFields as $field)
|
|
{
|
|
$this->{$field} = $this->config[$field] ?? null;
|
|
}
|
|
}
|
|
|
|
public function addItemToReceipt(string $text, int $quantity, float $price, float $amount)
|
|
{
|
|
$this->items_receipt[] = [
|
|
"text" => $text,
|
|
"quantity" => $quantity,
|
|
"price" => $price,
|
|
"amount" => $amount,
|
|
"tax" => "vat110",
|
|
"type" => "full_prepayment",
|
|
"object" => "commodity",
|
|
"unit" => "шт."
|
|
];
|
|
}
|
|
|
|
public function generateReceipt()
|
|
{
|
|
$sum = 0;
|
|
foreach ($this->items_receipt as $item)
|
|
{
|
|
$sum += $item['amount'] ?? null;
|
|
}
|
|
|
|
// Формирование чека по 54-ФЗ
|
|
if($this->send_receipt)
|
|
{
|
|
|
|
$optionalFields =
|
|
[
|
|
"showOrderId","details","paymentMethod","customerAccount","customerComment",
|
|
"customerEmail","customerPhone","fail_url","success_url","displayLanguage",
|
|
"recurrentable","debug","fdReceipt"
|
|
];
|
|
|
|
$this->message = [
|
|
"nonce" => $this->nonce,
|
|
"amount" => $sum ?? null,
|
|
"orderId" => $this->order_id ?? null,
|
|
// "showOrderId" => $this->showOrderId,
|
|
];
|
|
|
|
foreach ($optionalFields as $field) {
|
|
if ($this->{$field} !== null) {
|
|
$this->message = array_merge($this->message, [$field => $this->{$field}]);
|
|
}
|
|
}
|
|
|
|
$dataFields = ["debug","fdReceipt","hold","merchantData","templates","ac","merchantData","sbpSubscriptionPurpose","sbpRedirectUrl"];
|
|
|
|
foreach ($dataFields as $field)
|
|
{
|
|
if ($this->{$field} !== null)
|
|
{
|
|
|
|
$this->message = array_merge($this->message,
|
|
[
|
|
"data" =>
|
|
[
|
|
$field => $this->{$field},
|
|
]
|
|
]);
|
|
}
|
|
}
|
|
|
|
$receipt_info = [];
|
|
$receipt_info['fdReceipt'] = ["taxSystem" => "", "items" => $this->items_receipt];
|
|
$this->message['data'] += $receipt_info;
|
|
}
|
|
$this->messageText = json_encode($this->message);
|
|
|
|
$this->params = [
|
|
"url" => "https://" .$this->request_url. "/pay",
|
|
"marketPlace" => $this->merchant_id,
|
|
"message" => base64_encode($this->messageText),
|
|
"signature" => hash('sha256', $this->messageText . $this->merchant_key)
|
|
];
|
|
}
|
|
|
|
public function renderPaymentForm(array $params): string
|
|
{
|
|
if (isset($params))
|
|
{
|
|
?>
|
|
<style>
|
|
#paymentForm
|
|
{
|
|
display: none; /* Скрыть форму */
|
|
}
|
|
</style>
|
|
<script type="text/javascript">
|
|
document.addEventListener("DOMContentLoaded", function() {
|
|
document.getElementById('paymentForm').submit();
|
|
});
|
|
</script>
|
|
<?php
|
|
}
|
|
|
|
$html = '<form id="paymentForm" method="post" action="'. $params['url'] .'">';
|
|
$html .= '<input type="hidden" name="marketPlace" value="'. $params['marketPlace'] .'">';
|
|
$html .= '<input type="hidden" name="message" value="'. $params['message'] .'">';
|
|
$html .= '<input type="hidden" name="signature" value="'. $params['signature'] .'">';
|
|
$html .= '<input type="submit" value="Оплатить">';
|
|
$html .= '</form>';
|
|
|
|
return $html;
|
|
}
|
|
|
|
|
|
public function getParams(): array
|
|
{
|
|
return $this->params;
|
|
}
|
|
}
|
|
?>
|