pscbonline-php/Src/PayConfirmClass/Confirm/index.php

89 lines
2.4 KiB
PHP

<?php
class CreatingAndTransmittingParametersForConfirm {
// private int $marketPlace;
private $secretKey;
private $items_receipt;
private $message;
private $request_url;
// private $confirmAmount;
private $jsonString;
public function __construct($jsonString)
{
$this->jsonString = $jsonString;
$this->secretKey = $this->jsonString['secretKey'] ?? null;
// $this->confirmAmount = $this->jsonString['confirmAmount'] ?? null;
$this->items_receipt = [];
$this->message = [];
$this->request_url = $this->jsonString['domain'] ?? 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 processPayment($jsonString)
{
$sum = 0;
foreach ($this->items_receipt as $item)
{
$sum += $item['amount'];
}
$receipt_info = [];
$this->message = [
"marketPlace"=> $this->jsonString['marketPlace'],
"orderId"=> $this->jsonString['orderId'],
"confirmAmount"=> $sum,
];
$receipt_info["fdReceipt"] = ["taxSystem" => "", "items" => $this->items_receipt];
$receipt_info["data"] = $receipt_info;
// $array1 = $jsonString;
$this->message = array_merge($this->message + $receipt_info["data"]);
// $resultArray = array_merge($array1, $this->message);
// print_r($resultArray);
$messageTxt = json_encode($this->message);
// print_r($messageTxt);
$signature = hash('sha256', $messageTxt.$this->secretKey);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => "https://" .$this->request_url. "/merchantApi/confirmPayment",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $messageTxt,
CURLOPT_HTTPHEADER => [
"Signature: $signature",
"Content-Type: application/json"
]
]);
$res = curl_exec($ch);
curl_close($ch);
return $res;
}
}
?>