1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+ require_once '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:: ' ]);
16
+ $ fileName = basename (__FILE__ );
17
+
18
+ $ clientApiKey = $ argv ['clientApiKey ' ];
19
+ if ($ clientApiKey === null ) {
20
+ throw new \InvalidArgumentException (sprintf ('error: argument «clientApiKey» not found ' ) . PHP_EOL );
21
+ }
22
+ $ authApiKey = $ argv ['authApiKey ' ];
23
+ if ($ authApiKey === null ) {
24
+ throw new \InvalidArgumentException (sprintf ('error: argument «authApiKey» not found ' ) . PHP_EOL );
25
+ }
26
+ $ apiEndpoint = $ argv ['apiEndpoint ' ];
27
+ if ($ apiEndpoint === null ) {
28
+ throw new \InvalidArgumentException (sprintf ('error: argument «apiEndpoint» not found ' ) . PHP_EOL );
29
+ }
30
+
31
+ // check connection to API
32
+ $ log = new Logger ('loyalty-php-sdk ' );
33
+ $ log ->pushHandler (new \Monolog \Handler \StreamHandler ('loyalty-php-sdk-example.log ' , Logger::DEBUG ));
34
+ $ guzzleHandlerStack = HandlerStack::create ();
35
+ $ guzzleHandlerStack ->push (
36
+ Middleware::log (
37
+ $ log ,
38
+ new MessageFormatter (MessageFormatter::SHORT )
39
+ )
40
+ );
41
+ $ httpClient = new \GuzzleHttp \Client ();
42
+
43
+ $ log ->info ('loyalty.apiClient.start ' );
44
+ $ token = new SDK \Auth \DTO \Token (
45
+ SDK \Transport \DTO \Role::initializeByCode ('admin ' ),
46
+ Uuid::fromString ($ clientApiKey ),
47
+ Uuid::fromString ($ authApiKey )
48
+ );
49
+ $ apiClient = new SDK \ApiClient ($ apiEndpoint , $ token , $ httpClient , $ log );
50
+ $ apiClient ->setGuzzleHandlerStack ($ guzzleHandlerStack );
51
+
52
+ $ bitrix24Transport = SDK \Bitrix24 \Contacts \Transport \Admin \Fabric::getBitrix24ContactsTransport ($ apiClient , $ log );
53
+
54
+ try {
55
+ $ phoneUtil = \libphonenumber \PhoneNumberUtil::getInstance ();
56
+ $ randomMobileNumber = $ phoneUtil ->parse (
57
+ sprintf (
58
+ '+7%s ' ,
59
+ substr (str_replace ('. ' , '' , (string )microtime (true )), 0 , 10 )
60
+ )
61
+ );
62
+ $ randomCardNumber = (int )str_replace ('. ' , '' , (string )microtime (true ));
63
+
64
+ $ contactDto = new SDK \Bitrix24 \Contacts \DTO \Contact (
65
+ new \DateTime (),
66
+ new \DateTime (),
67
+ 'John ' ,
68
+ 'Doe ' ,
69
+ null ,
70
+ $ randomMobileNumber
71
+ );
72
+
73
+ $ result = $ bitrix24Transport ->addWithCardNumber ($ contactDto , $ randomCardNumber , new SDK \Transport \DTO \Reason ('examples ' ));
74
+ print (sprintf ('query result: ' ) . PHP_EOL );
75
+ print (sprintf (' - message operation: %s ' , $ result ->getMeta ()->getMessage ()) . PHP_EOL );
76
+ print (sprintf (' - role: %s ' , $ result ->getMeta ()->getRole ()->key ()) . PHP_EOL );
77
+ print (sprintf (' - duration: %s ' , $ result ->getMeta ()->getDuration ()) . PHP_EOL );
78
+
79
+ $ phoneNumberFormatter = \libphonenumber \PhoneNumberUtil::getInstance ();
80
+ print (sprintf ('- contact: ' ) . PHP_EOL );
81
+ print (sprintf (' id: %s ' , $ result ->getContact ()->getContactId ()->getId ()) . PHP_EOL );
82
+ print (sprintf (' email: %s ' , $ result ->getContact ()->getEmail ()) . PHP_EOL );
83
+ print (sprintf (
84
+ ' phone: %s ' ,
85
+ $ result ->getContact ()->getMobilePhone () !== null ?
86
+ $ phoneNumberFormatter ->format (
87
+ $ result ->getContact ()->getMobilePhone (),
88
+ \libphonenumber \PhoneNumberFormat::INTERNATIONAL
89
+ ) : null
90
+ ) . PHP_EOL );
91
+
92
+ $ decimalMoneyFormatter = new \Money \Formatter \DecimalMoneyFormatter (new \Money \Currencies \ISOCurrencies ());
93
+ print (sprintf ('- card: ' ) . PHP_EOL );
94
+ print (sprintf (' number: %s ' , $ result ->getCard ()->getNumber ()) . PHP_EOL );
95
+ print (sprintf (' uuid: %s ' , $ result ->getCard ()->getUuid ()->toString ()) . PHP_EOL );
96
+ print (sprintf (' status: %s ' , $ result ->getCard ()->getStatus ()->getCode ()) . PHP_EOL );
97
+ print (sprintf (
98
+ ' balance: %s %s ' ,
99
+ $ decimalMoneyFormatter ->format ($ result ->getCard ()->getBalance ()),
100
+ $ result ->getCard ()->getBalance ()->getCurrency ()->getCode () . PHP_EOL
101
+ ));
102
+ print (sprintf (' percentage: %s ' , $ result ->getCard ()->getPercentage ()->format ()) . PHP_EOL );
103
+ } catch (SDK \Exceptions \ApiClientException $ exception ) {
104
+ var_dump ($ exception ->getApiProblem ()->asArray ());
105
+ } catch (\Throwable $ exception ) {
106
+ var_dump ($ exception ->getMessage ());
107
+ }
0 commit comments