Skip to content

Commit

Permalink
added the ability to transfer nomenclatures data (#17)
Browse files Browse the repository at this point in the history
* chane old baseURL on new

* add addReceiptData method for sending nomenclatures data

* Update README.md
  • Loading branch information
Artorios authored Apr 15, 2024
1 parent a265a69 commit c4ae625
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 16 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions src/Exception/ReceiptDataException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* This file is part of Robokassa package.
*
* (c) 2014 IDM Agency (http://idma.ru)
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Idma\Robokassa\Exception;

/**
* Class EmptyDescriptionException
*
* @author JhaoDa <[email protected]>
*
* @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);
}
}
62 changes: 46 additions & 16 deletions src/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,6 +34,7 @@ class Payment
private $data;
private $isTestMode;
private $customParams = [];
private $receiptData = null;

private $login;
private $paymentPassword;
Expand Down Expand Up @@ -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,
];
}

Expand All @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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
*/
Expand Down Expand Up @@ -256,7 +286,7 @@ public function getInvoiceId()
*
* @return Payment
*/
public function setInvoiceId($id)
public function setInvoiceId($id): self
{
$this->data['InvId'] = (int)$id;

Expand All @@ -278,7 +308,7 @@ public function getSum()
* @throws InvalidSumException
*
*/
public function setSum($summ)
public function setSum($summ): self
{
$summ = number_format($summ, 2, '.', '');

Expand All @@ -304,7 +334,7 @@ public function getDescription()
*
* @return Payment
*/
public function setDescription($description)
public function setDescription($description): self
{
$this->data['Desc'] = (string)$description;

Expand All @@ -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;

Expand All @@ -344,7 +374,7 @@ public function getCurrencyLabel()
*
* @return Payment
*/
public function setCurrencyLabel($currLabel)
public function setCurrencyLabel($currLabel): self
{
$this->data['IncCurrLabel'] = (string)$currLabel;

Expand All @@ -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;

Expand Down

0 comments on commit c4ae625

Please sign in to comment.