From c4ae6251ada06172065b1363abe8a733bd29e9b4 Mon Sep 17 00:00:00 2001 From: Artorios Date: Mon, 15 Apr 2024 11:14:39 +0300 Subject: [PATCH] added the ability to transfer nomenclatures data (#17) * chane old baseURL on new * add addReceiptData method for sending nomenclatures data * Update README.md --- README.md | 18 ++++++++ src/Exception/ReceiptDataException.php | 27 +++++++++++ src/Payment.php | 62 +++++++++++++++++++------- 3 files changed, 91 insertions(+), 16 deletions(-) create mode 100644 src/Exception/ReceiptDataException.php diff --git a/README.md b/README.md index 81b7287..270bd45 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,24 @@ $payment // redirect to payment url $user->redirect($payment->getPaymentUrl()); ``` +For pointining nomenclatures data: +```php +// for details - https://docs.robokassa.ru/fiscalization/ +$receiptData = array( + 'items' => array([ + 'sum' => $sum, + 'name' => 'name of order', + 'quantity' => 1, + 'tax' => 'none', + ]) +); +$payment + ->setInvoiceId($order->id) + ->setSum($order->amount) + ->setDescription('Payment for some goods') + ->addReceiptData($receiptData); + +... Check payment result: ```php diff --git a/src/Exception/ReceiptDataException.php b/src/Exception/ReceiptDataException.php new file mode 100644 index 0000000..4b6751e --- /dev/null +++ b/src/Exception/ReceiptDataException.php @@ -0,0 +1,27 @@ + + * + * @package Idma\Robokassa\Exception + */ +class ReceiptDataException extends PaymentException +{ + public function __construct($message = '', $code = 0, \Exception $previous = null) + { + parent::__construct('This receipt data: sum, name, quantity, tax are required and cannot be empty.', $code, $previous); + } +} diff --git a/src/Payment.php b/src/Payment.php index 1995237..d4b1a21 100644 --- a/src/Payment.php +++ b/src/Payment.php @@ -15,6 +15,7 @@ use Idma\Robokassa\Exception\InvalidParamException; use Idma\Robokassa\Exception\InvalidInvoiceIdException; use Idma\Robokassa\Exception\EmptyDescriptionException; +use Idma\Robokassa\Exception\ReceiptDataException; /** * Class Payment @@ -33,6 +34,7 @@ class Payment private $data; private $isTestMode; private $customParams = []; + private $receiptData = null; private $login; private $paymentPassword; @@ -62,7 +64,7 @@ public function __construct($login, $paymentPassword, $validationPassword, $test 'Encoding' => 'utf-8', 'Culture' => self::CULTURE_RU, 'IncCurrLabel' => '', - 'IsTest' => $testMode ? 1 : 0 + 'IsTest' => $testMode ? 1 : 0, ]; } @@ -88,14 +90,23 @@ public function getPaymentUrl() if ($this->data['InvId'] <= 0) { throw new InvalidInvoiceIdException(); } - - $signature = vsprintf('%s:%01.2F:%u:%s', [ - // '$login:$OutSum:$InvId:$passwordPayment' - $this->login, - $this->data['OutSum'], - $this->data['InvId'], - $this->paymentPassword - ]); + $signature = isset($this->data['Receipt']) ? + vsprintf('%s:%01.2F:%u:%s:%s', [ + // '$login:$OutSum:$InvId:Receipt:$passwordPayment' + $this->login, + $this->data['OutSum'], + $this->data['InvId'], + urlencode(json_encode($this->data['Receipt'])), + $this->paymentPassword + ]): + + vsprintf('%s:%01.2F:%u:%s', [ + // '$login:$OutSum:$InvId:$passwordPayment' + $this->login, + $this->data['OutSum'], + $this->data['InvId'], + $this->paymentPassword + ]); if ($this->customParams) { // sort params alphabetically @@ -185,7 +196,7 @@ public function isValid() * @throws InvalidParamException if params is not an array * */ - public function addCustomParameters($params) + public function addCustomParameters($params): self { if (!is_array($params)) { throw new InvalidParamException(); @@ -198,6 +209,25 @@ public function addCustomParameters($params) return $this; } + public function addReceiptData(array $receiptData): self + { + if (!isset($receiptData['items'])) + throw new ReceiptDataException(); + + foreach ($receiptData['items'] as $items => $item) + + if ( + !isset($item['sum']) || + !isset($item['name']) || + !isset($item['quantity']) || + !isset($item['tax'])) + throw new ReceiptDataException(); + + $this->receiptData = $receiptData; + $this->data['Recipt'] = $this->receiptData; + return $this; + } + /** * @return string */ @@ -256,7 +286,7 @@ public function getInvoiceId() * * @return Payment */ - public function setInvoiceId($id) + public function setInvoiceId($id): self { $this->data['InvId'] = (int)$id; @@ -278,7 +308,7 @@ public function getSum() * @throws InvalidSumException * */ - public function setSum($summ) + public function setSum($summ): self { $summ = number_format($summ, 2, '.', ''); @@ -304,7 +334,7 @@ public function getDescription() * * @return Payment */ - public function setDescription($description) + public function setDescription($description): self { $this->data['Desc'] = (string)$description; @@ -324,7 +354,7 @@ public function getCulture() * * @return Payment */ - public function setCulture($culture = self::CULTURE_RU) + public function setCulture($culture = self::CULTURE_RU): self { $this->data['Culture'] = (string)$culture; @@ -344,7 +374,7 @@ public function getCurrencyLabel() * * @return Payment */ - public function setCurrencyLabel($currLabel) + public function setCurrencyLabel($currLabel): self { $this->data['IncCurrLabel'] = (string)$currLabel; @@ -355,7 +385,7 @@ public function setCurrencyLabel($currLabel) * @param $email * @return $this */ - public function setEmail($email) + public function setEmail($email): self { $this->data['Email'] = $email;