Skip to content

Commit 318b605

Browse files
authored
fix incorrect data being generated (fixes #43) (#45)
* fix incorrect data being generated (fixes #43) * fix test * fix return type
1 parent 7ef4fbb commit 318b605

File tree

4 files changed

+129
-25
lines changed

4 files changed

+129
-25
lines changed

README.md

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ Currently used in Germany, Austria, Netherlands, Finland and Belgium.
99

1010
> See also QR code payment generator for [Czech](https://github.com/RikudouSage/QrPaymentCZ) or [Slovak](https://github.com/RikudouSage/QrPaymentSK) accounts
1111
12-
> Using Symfony? See the [QR Payment Bundle](https://github.com/RikudouSage/QrPaymentBundle).
13-
1412
## Installation
1513

1614
Via composer: `composer require rikudou/euqrpayment`
@@ -77,6 +75,7 @@ $payment
7775
->setAmount(100)
7876
->setPurpose(Purpose::ACCOUNT_MANAGEMENT)
7977
->setRemittanceText("Invoice ID: XXX")
78+
->setCreditorReference('RF123456') // setting both creditor reference and remittance text will actually result in exception
8079
->setInformation("This is some note")
8180
->setCurrency("EUR");
8281

@@ -100,12 +99,14 @@ than 70 characters
10099
- `setPurpose()` - `InvalidArgumentException` - if the purpose is longer than 4 characters
101100
- `setRemittanceText()` - `InvalidArgumentException` - if the remittance text is longer
102101
than 140 characters
102+
- `setCreditorReference()` - `InvalidArgumentException` - if the creditor reference is longer
103+
than 35 characters
103104
- `setInformation()` and `setComment()` - `InvalidArgumentException` - if the comment is longer
104105
than 70 characters
105106
- `setCurrency()` - `InvalidArgumentException` - if the currency is not exactly 3 characters
106107
long
107108
- `getQrString()` - `LogicException` - if the beneficiary name is missing or if the
108-
resulting string is bigger than 331 bytes
109+
resulting string is bigger than 331 bytes or if both remittance text and creditor reference are set
109110
- `getQrImage()` - `LogicException` - if the `endroid/qr-code` library is not installed
110111

111112
## List of public methods
@@ -312,6 +313,27 @@ $payment = new QrPayment("CZ5530300000001325090010");
312313
$payment->getRemittanceText();
313314
```
314315

316+
### getCreditorReference()
317+
318+
Returns the structured creditor reference. Defaults to empty string.
319+
320+
**Returns**
321+
322+
`string`
323+
324+
**Example**
325+
326+
```php
327+
<?php
328+
329+
use rikudou\EuQrPayment\QrPayment;
330+
331+
$payment = new QrPayment("CZ5530300000001325090010");
332+
$payment->setCreditorReference('RF123456');
333+
// do other stuff
334+
echo $payment->getCreditorReference();
335+
```
336+
315337
### getInformation() or getComment()
316338

317339
Returns the information (comment) of the payment.
@@ -479,6 +501,8 @@ $payment->setPurpose(Purpose::TRUST_FUND);
479501

480502
The payment reference, up to 140 characters.
481503

504+
> Note: You cannot set both remittance text and creditor reference
505+
482506
**Params**
483507

484508
- `string $remittanceText` - the remittance text
@@ -498,6 +522,31 @@ $payment = new QrPayment("CZ5530300000001325090010");
498522
$payment->setRemittanceText("Invoice ID: ###");
499523
```
500524

525+
### setCreditorReference()
526+
527+
The structured creditor reference (ISO 11649), up to 35 characters.
528+
529+
> Note: You cannot set both remittance text and creditor reference
530+
531+
**Params**
532+
533+
- `string $creditorReference` - the remittance text
534+
535+
**Returns**
536+
537+
`$this`
538+
539+
**Example**
540+
541+
```php
542+
<?php
543+
544+
use rikudou\EuQrPayment\QrPayment;
545+
546+
$payment = new QrPayment("CZ5530300000001325090010");
547+
$payment->setCreditorReference("RF123456");
548+
```
549+
501550
### setInformation() or setComment()
502551

503552
Comment for the payment, up to 70 characters. `setComment()` is alias to `setInformation()`.
@@ -588,6 +637,7 @@ My Cool Company
588637
CZ5530300000001325090010
589638
EUR100
590639
ACCT
640+
591641
Invoice ID: XXX
592642
This is some note"
593643
*/

src/QrPayment.php

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ final class QrPayment implements QrPaymentInterface
4747
*/
4848
private $purpose = '';
4949

50+
/**
51+
* @var string
52+
*/
53+
private $creditorReference = '';
54+
5055
/**
5156
* @var string
5257
*/
@@ -236,20 +241,20 @@ public function setPurpose(string $purpose): QrPayment
236241
/**
237242
* @return string
238243
*/
239-
public function getRemittanceText(): string
244+
public function getCreditorReference(): string
240245
{
241-
return $this->remittanceText;
246+
return $this->creditorReference;
242247
}
243248

244249
/**
245-
* @param string $remittanceText
250+
* @param string $creditorReference
246251
*
247252
* @return QrPayment
248253
*/
249-
public function setRemittanceText(string $remittanceText): QrPayment
254+
public function setCreditorReference(string $creditorReference): QrPayment
250255
{
251-
$this->checkLength($remittanceText, 140);
252-
$this->remittanceText = $remittanceText;
256+
$this->checkLength($creditorReference, 35);
257+
$this->creditorReference = $creditorReference;
253258

254259
return $this;
255260
}
@@ -320,7 +325,7 @@ public function setCurrency(string $currency): QrPayment
320325

321326
public function getQrString(): string
322327
{
323-
$this->checkRequiredParameters();
328+
$this->checkParameterValidity();
324329

325330
$result = [];
326331
if ($validator = $this->getIban()->getValidator()) {
@@ -346,6 +351,7 @@ public function getQrString(): string
346351
$result[] = $this->getIban()->asString();
347352
$result[] = $amount ? $this->getCurrency() . $amount : '';
348353
$result[] = $this->getPurpose();
354+
$result[] = $this->getCreditorReference();
349355
$result[] = $this->getRemittanceText();
350356
$result[] = $this->getInformation();
351357
foreach ($this->configuration->getCustomData() as $customDatum) {
@@ -416,6 +422,19 @@ public function getDueDate(): DateTimeInterface
416422
throw new UnsupportedMethodException('The European standard does not support setting due date');
417423
}
418424

425+
public function getRemittanceText(): string
426+
{
427+
return $this->remittanceText;
428+
}
429+
430+
public function setRemittanceText(string $remittanceText): QrPayment
431+
{
432+
$this->checkLength($remittanceText, 140);
433+
$this->remittanceText = $remittanceText;
434+
435+
return $this;
436+
}
437+
419438
/**
420439
* @param string $string
421440
* @param int $max
@@ -429,10 +448,13 @@ private function checkLength(string $string, int $max, int $min = 1): void
429448
}
430449
}
431450

432-
private function checkRequiredParameters(): void
451+
private function checkParameterValidity(): void
433452
{
434453
if (!$this->getBeneficiaryName()) {
435454
throw new \LogicException('The beneficiary name is a mandatory parameter');
436455
}
456+
if ($this->getCreditorReference() && $this->getRemittanceText()) {
457+
throw new \LogicException('Only creditor reference or remittance text or neither can be set but not both');
458+
}
437459
}
438460
}

tests/Config/AbstractConfigurationTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function testGetCustomData()
5151
{
5252
// test default
5353
$instance = $this->getInstance();
54-
self::assertCount(11, $this->getLines($instance));
54+
self::assertCount(12, $this->getLines($instance));
5555

5656
$config = new class extends AbstractConfiguration {
5757
public function getCustomData(): iterable
@@ -61,9 +61,9 @@ public function getCustomData(): iterable
6161
}
6262
};
6363
$instance = $this->getInstance($config);
64-
self::assertCount(13, $this->getLines($instance));
65-
self::assertEquals('A', $this->getLine($instance, 11));
66-
self::assertEquals('B', $this->getLine($instance, 12));
64+
self::assertCount(14, $this->getLines($instance));
65+
self::assertEquals('A', $this->getLine($instance, 12));
66+
self::assertEquals('B', $this->getLine($instance, 13));
6767
}
6868

6969
public function testGetAmountPrecision()
@@ -164,7 +164,7 @@ public function getCustomData(): iterable
164164
$instance = $this->getInstance($config);
165165
$instance->setDueDate(new DateTimeImmutable('2025-01-01 12:00:00'));
166166
self::assertEquals('2025-01-01 12:00:00', $instance->getDueDate()->format('Y-m-d H:i:s'));
167-
self::assertEquals('20250101', $this->getLine($instance, 11));
167+
self::assertEquals('20250101', $this->getLine($instance, 12));
168168
}
169169

170170
private function getInstance(AbstractConfiguration $configuration = null): QrPayment

tests/QrPaymentTest.php

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
<?php
2-
/**
3-
* Created by PhpStorm.
4-
* User: root
5-
* Date: 15.12.18
6-
* Time: 1:11.
7-
*/
82

93
namespace rikudou\EuQrPayment\Tests;
104

@@ -91,13 +85,34 @@ public function testGetQrString()
9185
->setRemittanceText('Invoice ID: 1')
9286
->setCharacterSet(CharacterSet::UTF_8);
9387

94-
$this->assertEquals("BCD\n002\n1\nSCT\nAIRACZPP\nMy Company\nCZ5530300000001325090010\nEUR10\nACCT\nInvoice ID: 1\nRandom comment", $payment->getQrString());
88+
$this->assertEquals("BCD\n002\n1\nSCT\nAIRACZPP\nMy Company\nCZ5530300000001325090010\nEUR10\nACCT\n\nInvoice ID: 1\nRandom comment", $payment->getQrString());
9589

9690
$payment = $this->getDefaultPayment();
97-
$payment// no unnecessary parameters
91+
$payment
92+
->setPurpose(Purpose::ACCOUNT_MANAGEMENT)
93+
->setBeneficiaryName('My Company')
94+
->setSwift('AIRACZPP')
95+
->setAmount(10)
96+
->setCurrency('EUR')
97+
->setComment('Random comment')
98+
->setCreditorReference('RF0000000')
99+
->setCharacterSet(CharacterSet::UTF_8);
100+
101+
$this->assertEquals("BCD\n002\n1\nSCT\nAIRACZPP\nMy Company\nCZ5530300000001325090010\nEUR10\nACCT\nRF0000000\n\nRandom comment", $payment->getQrString());
102+
103+
$payment = $this->getDefaultPayment();
104+
$payment // no unnecessary parameters
98105
->setBeneficiaryName('My Company');
99106

100-
$this->assertEquals("BCD\n002\n1\nSCT\n\nMy Company\nCZ5530300000001325090010\n\n\n\n", $payment->getQrString());
107+
$this->assertEquals("BCD\n002\n1\nSCT\n\nMy Company\nCZ5530300000001325090010\n\n\n\n\n", $payment->getQrString());
108+
109+
$this->expectException(LogicException::class);
110+
$payment = $this->getDefaultPayment();
111+
$payment
112+
->setBeneficiaryName('My Company')
113+
->setRemittanceText('test')
114+
->setCreditorReference('RF123');
115+
$payment->getQrString();
101116
}
102117

103118
public function testGetQrStringNoBeneficiary()
@@ -366,6 +381,7 @@ public function testSetOptions()
366381
'amount' => 150.34,
367382
'purpose' => Purpose::ACCOUNT_OVERDRAFT_REPAYMENT,
368383
'remittanceText' => 'Some string',
384+
'creditorReference' => 'Some creditor',
369385
'information' => 'Another string',
370386
'currency' => 'USD',
371387
];
@@ -378,6 +394,7 @@ public function testSetOptions()
378394
self::assertEquals($options['amount'], $instance->getAmount());
379395
self::assertEquals($options['purpose'], $instance->getPurpose());
380396
self::assertEquals($options['remittanceText'], $instance->getRemittanceText());
397+
self::assertEquals($options['creditorReference'], $instance->getCreditorReference());
381398
self::assertEquals($options['information'], $instance->getInformation());
382399
self::assertEquals($options['currency'], $instance->getCurrency());
383400
}
@@ -398,6 +415,21 @@ public function testSetOptionsNonexistentProperty()
398415
]);
399416
}
400417

418+
public function testSetCreditorReference()
419+
{
420+
$strings = [
421+
$this->getRandomString(10),
422+
$this->getRandomString(35),
423+
$this->getRandomString(36),
424+
];
425+
426+
$this->assertEquals($strings[0], $this->getDefaultPayment()->setCreditorReference($strings[0])->getCreditorReference());
427+
$this->assertEquals($strings[1], $this->getDefaultPayment()->setCreditorReference($strings[1])->getCreditorReference());
428+
429+
$this->expectException(InvalidArgumentException::class);
430+
$this->getDefaultPayment()->setCreditorReference($strings[2]);
431+
}
432+
401433
private function getDefaultPayment(): QrPayment
402434
{
403435
return new QrPayment('CZ5530300000001325090010');

0 commit comments

Comments
 (0)