Skip to content

Commit

Permalink
Add instant payments (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
RikudouSage committed Jan 21, 2022
1 parent 496b234 commit 9f22c88
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
8 changes: 6 additions & 2 deletions README.md
Expand Up @@ -87,7 +87,8 @@ $payment->setOptions([
QrPaymentOptions::VARIABLE_SYMBOL => 123456,
QrPaymentOptions::AMOUNT => 100,
QrPaymentOptions::CURRENCY => "CZK",
QrPaymentOptions::DUE_DATE => date("Y-m-d", strtotime("+14 days"))
QrPaymentOptions::DUE_DATE => date("Y-m-d", strtotime("+14 days")),
QrPaymentOptions::INSTANT_PAYMENT => true,
]);
```

Expand All @@ -104,7 +105,8 @@ $payment
->setVariableSymbol(123456)
->setAmount(100)
->setCurrency("CZK")
->setDueDate(new DateTimeImmutable('+14 days'));
->setDueDate(new DateTimeImmutable('+14 days'))
->setInstantPayment(true);
```

## Exceptions
Expand Down Expand Up @@ -325,6 +327,8 @@ This is a list of options you can set.
- `float $amount` - the amount for the payment, shouldn't have more than 2 decimal places, has no default
- `string $country` - [ISO 3166-1 alpha-2 code](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_assigned_code_elements)
for country, defaults to `CZ`
- `bool $instantPayment` - whether the payment should be made as instant instead of standard payment
(depends on bank support)

All of these options can be set using the `QrPaymentOptions` helper class as constants for the constructor or
`setOptions()` or as methods.
Expand Down
1 change: 1 addition & 0 deletions src/Options/QrPaymentOptions.php
Expand Up @@ -14,4 +14,5 @@ final class QrPaymentOptions
public const DUE_DATE = 'dueDate';
public const AMOUNT = 'amount';
public const PAYEE_NAME = 'payeeName';
public const INSTANT_PAYMENT = 'instantPayment';
}
20 changes: 20 additions & 0 deletions src/QrPayment.php
Expand Up @@ -77,6 +77,11 @@ final class QrPayment implements QrPaymentInterface
*/
private $payeeName = null;

/**
* @var bool
*/
private $instantPayment = false;

/**
* @param IbanInterface $iban
* @param array<string,mixed>|null $options
Expand Down Expand Up @@ -153,6 +158,9 @@ public function getQrString(): string
if ($this->dueDate !== null) {
$qrString .= sprintf('DT:%s*', $this->dueDate->format('Ymd'));
}
if ($this->instantPayment) {
$qrString .= 'PT:IP*';
}

return substr($qrString, 0, -1);
}
Expand Down Expand Up @@ -351,6 +359,18 @@ public function setPayeeName(?string $payeeName): self
return $this;
}

public function isInstantPayment(): bool
{
return $this->instantPayment;
}

public function setInstantPayment(bool $isInstant): self
{
$this->instantPayment = $isInstant;

return $this;
}

/**
* Checks all properties for asterisk and throws exception if asterisk
* is found
Expand Down
12 changes: 12 additions & 0 deletions tests/QrPaymentTest.php
Expand Up @@ -315,6 +315,18 @@ public function testPayeeName()
$this->instance->getQrString();
}

public function testInstantPayment()
{
self::assertFalse(strpos($this->instance->getQrString(), 'PT:IP'));
self::assertFalse($this->instance->isInstantPayment());
$this->instance->setInstantPayment(true);
self::assertTrue($this->instance->isInstantPayment());
self::assertEquals($this->instance->getQrString(), "SPD*1.0*ACC:{$this->getIban()}*AM:0.00*CC:CZK*X-PER:7*PT:IP");
$this->instance->setInstantPayment(false);
self::assertFalse($this->instance->isInstantPayment());
self::assertFalse(strpos($this->instance->getQrString(), 'PT:IP'));
}

/**
* @see https://github.com/RikudouSage/QrPaymentCZ/issues/35
*/
Expand Down

0 comments on commit 9f22c88

Please sign in to comment.