1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+ require_once dirname (__DIR__ , 2 ) . '/vendor/autoload.php ' ;
5
+
6
+ use \Monolog \Logger ;
7
+ use \B24io \Loyalty \SDK ;
8
+ use \B24io \Loyalty \SDK \OperationsJournal \DTO \OperationType ;
9
+ use Ramsey \Uuid \Uuid ;
10
+
11
+ use GuzzleHttp \HandlerStack ;
12
+ use GuzzleHttp \Middleware ;
13
+ use GuzzleHttp \MessageFormatter ;
14
+
15
+ $ argv = getopt ('' , ['clientApiKey:: ' , 'authApiKey:: ' , 'apiEndpoint:: ' , 'dateFrom:: ' , 'dateTo:: ' ]);
16
+ $ fileName = basename (__FILE__ );
17
+
18
+
19
+ $ clientApiKey = $ argv ['clientApiKey ' ];
20
+ if ($ clientApiKey === null ) {
21
+ throw new \InvalidArgumentException (sprintf ('error: argument «clientApiKey» not found ' ) . PHP_EOL );
22
+ }
23
+ $ authApiKey = $ argv ['authApiKey ' ];
24
+ if ($ authApiKey === null ) {
25
+ throw new \InvalidArgumentException (sprintf ('error: argument «authApiKey» not found ' ) . PHP_EOL );
26
+ }
27
+ $ apiEndpoint = $ argv ['apiEndpoint ' ];
28
+ if ($ apiEndpoint === null ) {
29
+ throw new \InvalidArgumentException (sprintf ('error: argument «apiEndpoint» not found ' ) . PHP_EOL );
30
+ }
31
+ $ dateFrom = new \DateTime ($ argv ['dateFrom ' ]);
32
+ $ dateTo = new \DateTime ($ argv ['dateTo ' ]);
33
+
34
+ // check connection to API
35
+ $ log = new Logger ('loyalty-php-sdk ' );
36
+ $ log ->pushHandler (new \Monolog \Handler \StreamHandler ('loyalty-php-sdk-example.log ' , Logger::DEBUG ));
37
+ $ guzzleHandlerStack = HandlerStack::create ();
38
+ $ guzzleHandlerStack ->push (Middleware::log ($ log , new MessageFormatter (MessageFormatter::SHORT )));
39
+ $ httpClient = new \GuzzleHttp \Client ();
40
+ $ log ->info ('loyalty.apiClient.start ' );
41
+
42
+ $ token = new SDK \Auth \DTO \Token (
43
+ SDK \Transport \DTO \Role::initializeByCode ('user ' ),
44
+ Uuid::fromString ($ clientApiKey ),
45
+ Uuid::fromString ($ authApiKey )
46
+ );
47
+ $ apiClient = new SDK \ApiClient ($ apiEndpoint , $ token , $ httpClient , $ log );
48
+ $ apiClient ->setGuzzleHandlerStack ($ guzzleHandlerStack );
49
+
50
+ $ operationsTransport = SDK \OperationsJournal \Transport \User \Fabric::getOperationsJournalTransport ($ apiClient , $ log );
51
+ $ decimalMoneyFormatter = new \Money \Formatter \DecimalMoneyFormatter (new \Money \Currencies \ISOCurrencies ());
52
+ try {
53
+ $ operationsResponse = $ operationsTransport ->getOperationsByPeriod ($ dateFrom , $ dateTo );
54
+
55
+ print (sprintf ('operations journal response: ' . PHP_EOL ));
56
+ print (sprintf ('- date from: %s ' . PHP_EOL , $ operationsResponse ->getOperationsJournal ()->getDateFrom ()->format (\DATE_ATOM )));
57
+ print (sprintf ('- date to: %s ' . PHP_EOL , $ operationsResponse ->getOperationsJournal ()->getDateTo ()->format (\DATE_ATOM )));
58
+ print (sprintf ('- count: %s ' . PHP_EOL , $ operationsResponse ->getOperationsJournal ()->getOperations ()->count ()));
59
+
60
+ $ decimalMoneyFormatter = new \Money \Formatter \DecimalMoneyFormatter (new \Money \Currencies \ISOCurrencies ());
61
+ foreach ($ operationsResponse ->getOperationsJournal ()->getOperations () as $ cnt => $ op ) {
62
+ switch ($ op ->getOperationType ()) {
63
+ case OperationType::BITRIX24_DEAL_PERCENTAGE_DISCOUNT_PAYMENT_TRANSACTION ():
64
+ /**
65
+ * @var SDK\OperationsJournal\DTO\Bitrix24\DealPercentageDiscount $op
66
+ */
67
+ print (sprintf (
68
+ '%s | %s | user - %s |b24Deal - %s |card uuid - %s | percent %s --- %s ' ,
69
+ $ cnt ,
70
+ $ op ->getOperationType ()->key (),
71
+ $ op ->getUserId ()->getId (),
72
+ $ op ->getDealId (),
73
+ $ op ->getCardUuid ()->toString (),
74
+ $ op ->getPercentage ()->format (),
75
+ $ op ->getReason ()->getComment ()
76
+ ) . PHP_EOL );
77
+ break ;
78
+ case OperationType::BITRIX24_DEAL_MONETARY_DISCOUNT_PAYMENT_TRANSACTION ():
79
+ /**
80
+ * @var SDK\OperationsJournal\DTO\Bitrix24\DealMonetaryDiscount $op
81
+ */
82
+ print (sprintf (
83
+ '%s | %s | user - %s |b24Deal - %s |card uuid - %s | %s %s --- %s ' ,
84
+ $ cnt ,
85
+ $ op ->getOperationType ()->key (),
86
+ $ op ->getUserId ()->getId (),
87
+ $ op ->getDealId (),
88
+ $ op ->getCardUuid ()->toString (),
89
+ $ decimalMoneyFormatter ->format ($ op ->getValue ()),
90
+ $ op ->getValue ()->getCurrency ()->getCode (),
91
+ $ op ->getReason ()->getComment ()
92
+ ) . PHP_EOL );
93
+ break ;
94
+ case OperationType::ACCRUAL_TRANSACTION ():
95
+ case OperationType::PAYMENT_TRANSACTION ():
96
+ /**
97
+ * @var SDK\Transactions\DTO\TransactionInterface $op
98
+ */
99
+ print (sprintf (
100
+ '%s | %s | user - %s |card uuid - %s | %s %s --- %s ' ,
101
+ $ cnt ,
102
+ $ op ->getOperationType ()->key (),
103
+ $ op ->getUserId ()->getId (),
104
+ $ op ->getCardUuid ()->toString (),
105
+ $ decimalMoneyFormatter ->format ($ op ->getValue ()),
106
+ $ op ->getValue ()->getCurrency ()->getCode (),
107
+ $ op ->getReason ()->getComment ()
108
+ ) . PHP_EOL );
109
+ break ;
110
+ case OperationType::PURCHASE ():
111
+ /**
112
+ * @var SDK\OperationsJournal\DTO\Purchases\Purchase $op
113
+ */
114
+ print (sprintf (
115
+ '%s | %s | user - %s |card uuid - %s | purchase id - %s | %s ' ,
116
+ $ cnt ,
117
+ $ op ->getOperationType ()->key (),
118
+ $ op ->getUserId ()->getId (),
119
+ $ op ->getCardUuid ()->toString (),
120
+ $ op ->getPurchaseId ()->getId (),
121
+ $ op ->getReason ()->getComment ()
122
+ ) . PHP_EOL );
123
+ break ;
124
+ default :
125
+ print (sprintf (
126
+ '%s | %s | user - %s |card uuid - %s | %s ' ,
127
+ $ cnt ,
128
+ $ op ->getOperationType ()->key (),
129
+ $ op ->getUserId ()->getId (),
130
+ $ op ->getCardUuid ()->toString (),
131
+ $ op ->getReason ()->getComment ()
132
+ ) . PHP_EOL );
133
+ break ;
134
+ }
135
+ }
136
+ } catch (\Throwable $ exception ) {
137
+ var_dump ($ exception ->getMessage ());
138
+ }
0 commit comments