121 lines
3.8 KiB
PHP
121 lines
3.8 KiB
PHP
<?php
|
|
|
|
class CreatingAndTransmittingParametersForInvoicing
|
|
{
|
|
private int $marketPlace;
|
|
private string $secretKey;
|
|
private $message;
|
|
private $items_receipt;
|
|
private $jsonString;
|
|
private $request_url;
|
|
private $sbpRedirectUrl;
|
|
|
|
public function __construct($jsonString)
|
|
{
|
|
$this->jsonString = $jsonString;
|
|
$this->marketPlace = $this->jsonString['marketPlace'] ?? null;
|
|
$this->secretKey = $this->jsonString['secretKey'] ?? null;
|
|
$this->request_url = $this->jsonString['domain'] ?? null;
|
|
$this->sbpRedirectUrl = $this->jsonString['sbpRedirectUrl'] ?? null;
|
|
|
|
$this->items_receipt = [];
|
|
|
|
$optionalFields =
|
|
[
|
|
"marketPlace","amount","orderId","paymentMethod","showOrderId",
|
|
"details","customerAccount","customerComment","customerEmail",
|
|
"customerPhone","expirationFromNow","expirationDateTime",
|
|
"recurrentable","nonce",
|
|
|
|
"user","userPhone","debug","sbpSubscriptionPurpose",
|
|
|
|
];
|
|
foreach($optionalFields as $field)
|
|
{
|
|
$this->{$field} = $this->jsonString[$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 processPayment2()
|
|
{
|
|
|
|
$sum = 0;
|
|
foreach ($this->items_receipt as $item)
|
|
{
|
|
$sum += $item['amount'];
|
|
}
|
|
|
|
$receipt_info = [];
|
|
$receipt_info_fd = [];
|
|
|
|
if ($this->marketPlace != null)
|
|
{
|
|
$this->message = [
|
|
"marketPlace"=> $this->jsonString['marketPlace'],
|
|
"orderId"=> $this->jsonString['orderId'],
|
|
"amount"=> (int)$sum,
|
|
"requestQrCodeImageUrl"=> false,
|
|
];
|
|
}
|
|
|
|
$optionalFields =
|
|
[
|
|
"marketPlace","amount","orderId","paymentMethod","showOrderId",
|
|
"details","customerAccount","customerComment","customerEmail",
|
|
"customerPhone","expirationFromNow","expirationDateTime",
|
|
"recurrentable","nonce"
|
|
];
|
|
|
|
foreach ($optionalFields as $field) {
|
|
if ($this->{$field} !== null) {
|
|
$this->message = array_merge($this->message, [$field => $this->{$field}]);
|
|
}
|
|
}
|
|
$this->message = array_merge($this->message, ["requestQrCodeImageUrl"=> false]);
|
|
|
|
$receipt_info_fd["fdReceipt"] = ["taxSystem" => "", "items" => $this->items_receipt];
|
|
$receipt_info["data"] = ["requestQrCodeImageUrl"=> false,"requestQrCodeImage"=> false,"fdReceipt"=>$receipt_info_fd["fdReceipt"],"sbpRedirectUrl"=>filter_var($this->sbpRedirectUrl, FILTER_VALIDATE_URL) ? $this->sbpRedirectUrl : null];
|
|
|
|
$this->message = array_merge($this->message + $receipt_info);
|
|
|
|
$newJsonString = json_encode($this->message);
|
|
|
|
$values = json_decode($newJsonString, true);
|
|
|
|
$messageTxt = json_encode($values);
|
|
$signature = hash('sha256', $messageTxt.$this->secretKey);
|
|
|
|
$ch = curl_init();
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_URL => "https://".$this->request_url."/merchantApi/pay",
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_POST => true,
|
|
CURLOPT_POSTFIELDS => $messageTxt,
|
|
CURLOPT_HTTPHEADER => [
|
|
"Signature: $signature",
|
|
"Content-Type: application/json"
|
|
]
|
|
]);
|
|
|
|
$res = curl_exec($ch);
|
|
|
|
return $res;
|
|
}
|
|
|
|
}
|
|
|
|
?>
|