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+ }
0 commit comments