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))
{
?>
';
$html .= '';
$html .= '';
$html .= '';
$html .= '';
$html .= '';
return $html;
}
public function getParams(): array
{
return $this->params;
}
}
?>