Skip to content

Commit d614207

Browse files
add getPaymentParametersAsJson method for robokassa_iframe.js
1 parent ec5179c commit d614207

3 files changed

Lines changed: 73 additions & 32 deletions

File tree

src/RobokassaApi.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,14 @@ public function getPaymentParameters(InvoiceOptions $invoiceOptions): array
172172
];
173173
}
174174

175+
public function getPaymentParametersAsJson(InvoiceOptions $invoiceOptions): string
176+
{
177+
return json_encode(
178+
$this->getPaymentParameters($invoiceOptions),
179+
JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
180+
);
181+
}
182+
175183
public function smsRequest(int $phone, string $message): RequestInterface
176184
{
177185
$requestData = $this->getSendSmsData($phone, $message);

src/RobokassaApiInterface.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,23 @@ public function getBase64SignedPostData(SecondReceiptOptions|ReceiptStatusOption
5959
*/
6060
public function getPaymentParameters(InvoiceOptions $invoiceOptions): array;
6161

62+
/**
63+
* Получает параметры платежа для передачи в Робокассу в формате JSON строки для использования
64+
* с PopUp iFrame виджетом {@link https://docs.robokassa.ru/iframe/}
65+
*
66+
* ##Пример:
67+
* ```
68+
* <script type="text/javascript" src="https://auth.robokassa.ru/Merchant/bundle/robokassa_iframe.js"></script>
69+
* <input type="submit"
70+
* onclick="Robokassa.StartPayment(<?= $robokassa->getPaymentParametersAsJson($invoiceOptions) ?>)"
71+
* value="Оплатить">
72+
* ```
73+
* @param InvoiceOptions $invoiceOptions
74+
* @return string
75+
* @throws JsonException
76+
*/
77+
public function getPaymentParametersAsJson(InvoiceOptions $invoiceOptions): string;
78+
6279
/**
6380
* Получение URL для оплаты счета с указанными параметрами
6481
* (GET запрос длиной более 2083 символов может не работать,

tests/RobokassaApiTest.php

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -86,44 +86,45 @@ public function testGetPaymentParameters(): void
8686
password2: 'password#2',
8787
);
8888

89-
$paymentParameters = $robokassa->getPaymentParameters(
90-
new InvoiceOptions(
91-
outSum: 100,
92-
invId: 999,
93-
description: 'Description',
94-
receipt: new Receipt(
95-
items: [
96-
new Item(
97-
name: 'Название товара 1 (50%)',
98-
quantity: 1,
99-
sum: 100,
100-
tax: Tax::vat10,
101-
payment_method: PaymentMethod::full_payment,
102-
payment_object: PaymentObject::commodity
103-
),
104-
new Item(
105-
name: 'Название товара 2',
106-
quantity: 3,
107-
sum: 450,
108-
tax: Tax::vat120,
109-
payment_method: PaymentMethod::full_payment,
110-
payment_object: PaymentObject::excise,
111-
nomenclature_code: '04620034587217'
112-
),
113-
],
114-
sno: Sno::osn,
115-
),
116-
culture: Culture::en,
89+
$invoiceOptions = new InvoiceOptions(
90+
outSum: 100,
91+
invId: 999,
92+
description: "Description\"'><b>",
93+
receipt: new Receipt(
94+
items: [
95+
new Item(
96+
name: 'Название товара 1 (50%)',
97+
quantity: 1,
98+
sum: 100,
99+
tax: Tax::vat10,
100+
payment_method: PaymentMethod::full_payment,
101+
payment_object: PaymentObject::commodity
102+
),
103+
new Item(
104+
name: "Название товара 2\"'><b>",
105+
quantity: 3,
106+
sum: 450,
107+
tax: Tax::vat120,
108+
payment_method: PaymentMethod::full_payment,
109+
payment_object: PaymentObject::excise,
110+
nomenclature_code: '04620034587217'
111+
),
112+
],
113+
sno: Sno::osn,
117114
),
115+
culture: Culture::en,
116+
);
117+
$paymentParameters = $robokassa->getPaymentParameters(
118+
$invoiceOptions,
118119
);
119120

120121
$expectedPaymentParameters = [
121122
'MerchantLogin' => 'robo-demo',
122123
'OutSum' => '100.00',
123-
'Description' => 'Description',
124-
'SignatureValue' => '9da55279722ba167a1384b05a2c5e330',
124+
'Description' => "Description\"'><b>",
125+
'SignatureValue' => '7c6a95094fa5c3e5e2baf83c8817e8c0',
125126
'IncCurrLabel' => null,
126-
'InvId' => 999,
127+
'InvId' => '999',
127128
'Culture' => 'en',
128129
'Encoding' => 'utf-8',
129130
'Email' => null,
@@ -133,12 +134,27 @@ public function testGetPaymentParameters(): void
133134
'Receipt' => '{"items":['
134135
. '{"sum":"100.00","name":"Название товара 1 (50%)","quantity":1,"tax":"vat10",'
135136
. '"payment_method":"full_payment","payment_object":"commodity"},'
136-
. '{"sum":"450.00","name":"Название товара 2","quantity":3,"tax":"vat120",'
137+
. '{"sum":"450.00","name":"Название товара 2\"\'><b>","quantity":3,"tax":"vat120",'
137138
. '"payment_method":"full_payment","payment_object":"excise","nomenclature_code":"04620034587217"}'
138139
. '],"sno":"osn"}',
139140
'IsTest' => null,
140141
];
141142
$this->assertEquals($expectedPaymentParameters, $paymentParameters);
143+
144+
$paymentParametersAsJson = $robokassa->getPaymentParametersAsJson($invoiceOptions);
145+
$expectedPaymentParametersAsJson = '{"MerchantLogin":"robo-demo","OutSum":"100.00","Description":"Description\"\'><b>",'
146+
. '"SignatureValue":"7c6a95094fa5c3e5e2baf83c8817e8c0","IncCurrLabel":null,"InvId":"999","Culture":"en",'
147+
. '"Encoding":"utf-8","Email":null,"ExpirationDate":null,"OutSumCurrency":null,"UserIp":null,'
148+
. '"Receipt":"{\"items\":['
149+
. '{\"sum\":\"100.00\",\"name\":\"Название товара 1 (50%)\",\"quantity\":1,\"tax\":\"vat10\",'
150+
. '\"payment_method\":\"full_payment\",\"payment_object\":\"commodity\"},'
151+
. '{\\"sum\\":\\"450.00\\",\\"name\\":\\"Название товара 2\\\\\\"\'><b>\\",\\"quantity\\":3,\\"tax\\":\\"vat120\\",'
152+
. '\"payment_method\":\"full_payment\",\"payment_object\":\"excise\",\"nomenclature_code\":\"04620034587217\"}],'
153+
. '\"sno\":\"osn\"}","IsTest":null}';
154+
$this->assertEquals($expectedPaymentParametersAsJson, $paymentParametersAsJson);
155+
156+
$decodedJson = json_decode($paymentParametersAsJson, true, 512, JSON_THROW_ON_ERROR);
157+
$this->assertEquals($expectedPaymentParameters, $decodedJson);
142158
}
143159

144160
public function testPaymentReceipt(): void

0 commit comments

Comments
 (0)