Skip to content

Commit ae973f3

Browse files
authored
Merge pull request #2 from SimPaypl/feat/blik-level-0
feat: add BLIK Level 0 support
2 parents 21a4775 + 750039b commit ae973f3

File tree

6 files changed

+161
-1
lines changed

6 files changed

+161
-1
lines changed

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,53 @@ Public parameters:
264264
- `suffix` (string or null)
265265
- `updatedAt` (Carbon instance or null)
266266

267+
### Make BLIK Level 0 transaction
268+
```php
269+
// at first generate payment
270+
271+
// only required fields are shown
272+
$payment = SimPay::payment()->generate()
273+
->amount(15.00)
274+
->customer(
275+
new \SimPay\Laravel\Dto\Payment\CustomerData(
276+
email: 'Email',
277+
ip: 'IP address',
278+
countryCode: 'PL', // for BLIK it must be PL
279+
)
280+
)
281+
->antifraud(
282+
new \SimPay\Laravel\Dto\Payment\AntiFraudData(
283+
userAgent: 'UserAgent (REQUIRED)',
284+
)
285+
)
286+
->currency('PLN')
287+
->directChannel('blik-level0') // it must be set to blik-level0
288+
->make();
289+
290+
// Now make Level 0 call
291+
try {
292+
$success = SimPay::payment()->blikLevel0()
293+
->ticket('111222') // BLIK code
294+
->ticketType(\SimPay\Laravel\Enums\Payment\BlikLevel0TicketType::T6) // for now, only T6 codes are supported
295+
->transaction($payment) // you may pass full TransactionGenerateResponse or just transactionId
296+
->make();
297+
}
298+
catch(\SimPay\Laravel\Exceptions\BlikLevel0\InvalidBlikTicketException $exception) {
299+
// notify user that BLIK ticket is not valid
300+
}
301+
catch(\SimPay\Laravel\Exceptions\SimPayException $exception) {
302+
// other error
303+
}
304+
```
305+
If ticket has been accepted, $success will be true.
306+
307+
**WARNING**: This does not mean that transaction is finished, you still need to listen to our IPN messages.
308+
309+
If ticket code is not valid, two exceptions may be thrown:
310+
- \SimPay\Laravel\Exceptions\BlikLevel0\InvalidBlikTicketException
311+
- \SimPay\Laravel\Exceptions\SimPayException
312+
313+
267314
## Direct Billing
268315

269316
### Generate transaction
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace SimPay\Laravel\Enums\Payment;
5+
6+
enum BlikLevel0TicketType: string
7+
{
8+
case T6 = 'T6';
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace SimPay\Laravel\Exceptions\BlikLevel0;
5+
6+
use SimPay\Laravel\Exceptions\SimPayException;
7+
8+
class InvalidBlikTicketException extends SimPayException
9+
{
10+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace SimPay\Laravel\Services\Payment;
4+
5+
use SimPay\Laravel\Enums\Payment\BlikLevel0TicketType;
6+
use SimPay\Laravel\Exceptions\BlikLevel0\InvalidBlikTicketException;
7+
use SimPay\Laravel\Exceptions\SimPayException;
8+
use SimPay\Laravel\Responses\TransactionGeneratedResponse;
9+
use SimPay\Laravel\Services\Service;
10+
11+
final class InitiateBlikLevel0 extends Service
12+
{
13+
private BlikLevel0TicketType $ticketType;
14+
private string $ticket;
15+
private string $transactionId;
16+
17+
/**
18+
* @throws SimPayException
19+
*/
20+
public function make(): true
21+
{
22+
$payload = [
23+
'ticket' => [
24+
$this->ticketType->value => $this->ticket,
25+
],
26+
];
27+
28+
$level0 = $this->sendRequest(
29+
sprintf('payment/%s/blik/level0/%s', config('simpay.payment.service_id'), $this->transactionId),
30+
'POST',
31+
['json' => $payload],
32+
);
33+
34+
if ($level0->successful()) {
35+
return true;
36+
}
37+
38+
if (!$level0->badRequest()) {
39+
throw new SimPayException(
40+
sprintf('[%s] %s', $level0->json('errorCode', $level0->status()), $level0->json('message'))
41+
);
42+
}
43+
44+
$errorCode = $level0->json('errorCode');
45+
46+
if (in_array($errorCode, [
47+
'INVALID_BLIK_CODE',
48+
'INVALID_BLIK_CODE_FORMAT',
49+
'BLIK_CODE_EXPIRED',
50+
'BLIK_CODE_CANCELLED',
51+
'BLIK_CODE_USED',
52+
'BLIK_CODE_NOT_SUPPORTED',
53+
])) {
54+
throw new InvalidBlikTicketException(sprintf('[%s] %s', $errorCode, $level0->json('message')));
55+
}
56+
57+
throw new SimPayException(sprintf('[%s] %s', $errorCode, $level0->json('message')));
58+
}
59+
60+
public function ticket(string $ticket): self
61+
{
62+
$this->ticket = $ticket;
63+
64+
return $this;
65+
}
66+
67+
public function ticketType(BlikLevel0TicketType $ticketType): self
68+
{
69+
$this->ticketType = $ticketType;
70+
71+
return $this;
72+
}
73+
74+
public function transaction(TransactionGeneratedResponse|string $transaction): self
75+
{
76+
if ($transaction instanceof TransactionGeneratedResponse) {
77+
$this->transactionId = $transaction->transactionId;
78+
79+
return $this;
80+
}
81+
82+
$this->transactionId = $transaction;
83+
84+
return $this;
85+
}
86+
}

src/Services/Payment/Payment.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,12 @@ public function currencies(): array
5959
{
6060
return (new ServiceCurrencies())->get();
6161
}
62+
63+
/**
64+
* @throws SimPayException
65+
*/
66+
public function blikLevel0(): InitiateBlikLevel0
67+
{
68+
return (new InitiateBlikLevel0());
69+
}
6270
}

src/SimPay.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class SimPay
1010
{
11-
public const string VERSION = '1.2.1';
11+
public const string VERSION = '1.3.0';
1212

1313
public function payment(): Payment
1414
{

0 commit comments

Comments
 (0)