Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added the ability to transfer nomenclatures data #17

Merged
merged 3 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
64 changes: 47 additions & 17 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 @@ -28,11 +29,12 @@ class Payment
const CULTURE_EN = 'en';
const CULTURE_RU = 'ru';

private $baseUrl = 'https://merchant.roboxchange.com/Index.aspx?';
private $baseUrl = 'https://auth.robokassa.ru/Merchant/Index.aspx?';
private $valid = false;
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