|
1 | 1 | # loyalty-php-sdk
|
2 |
| -B24io.Loyalty application PHP-SDK |
| 2 | +Loyalty PHP SDK is a tool for work with REST-API Bitrix24 Application [Loyalty Program and bonus cards for Bitrix24 CRM](https://www.bitrix24.ru/apps/?app=b24io.loyalty) |
3 | 3 |
|
4 |
| -## Domain items |
| 4 | +* Loyalty app adds bonus card for Bitrix24 client profile in CRM |
| 5 | +* Loyalty app support transactions for payment and accrual operations |
| 6 | +* store percentage of discount |
| 7 | +* operations with cards: create, read, delete, block |
| 8 | + |
| 9 | +## Who uses loyalty PHP SDK |
| 10 | +* B2C companies who works with customers and grow they LTV |
| 11 | +* HoReCa companies such us fast food or restaurants |
| 12 | + |
| 13 | +## How it works |
| 14 | +<p align="center"> |
| 15 | + <img src="./docs/img/loyalty-php-sdk-base-schema.jpg" alt="Loyalty Program and bonus cards for Bitrix24 CRM" width="1333"> |
| 16 | +</p> |
| 17 | + |
| 18 | +## Domain entities |
| 19 | +In loyalty-php-sdk available domain entities from application with DTO (data transfer objects). |
| 20 | + |
| 21 | +### Cards |
| 22 | +Loyalty card object: |
| 23 | +* `number` - card number |
| 24 | +* `barcode` - card barcode |
| 25 | +* `status` - card status enumeration (active, blocked or deleted) |
| 26 | +* `user` - card owner user id |
| 27 | +* `balance` - card balance with php-money object |
| 28 | +* `percentage` - card percentage |
| 29 | +* `created` - card date create |
| 30 | +* `modified` - card date modified |
| 31 | +* `uuid` - card [universally unique identifier](https://en.wikipedia.org/wiki/Universally_unique_identifier) |
| 32 | + |
| 33 | +### Transactions |
| 34 | +Transactions - accrual or payment operation with card balance |
| 35 | + |
| 36 | +Transaction object: |
| 37 | + |
| 38 | +* `value` - transaction amount with php-money object |
| 39 | +* `operationId` - internal operation id, read only |
| 40 | +* `type` - payment or accrual transaction |
| 41 | +* `cardNumber` - card number |
| 42 | +* `created` - transaction date create |
| 43 | +* `reason` - transaction reason with comment |
| 44 | + |
| 45 | +### Turnovers |
| 46 | +Turnover object: |
| 47 | + |
| 48 | +* `modified` - last operation date |
| 49 | +* `totalPurchasesSum` - total purchases sum for card |
| 50 | +* `totalPurchasesCount` - total purchases count for card |
| 51 | + |
| 52 | +### OperationsJournal |
| 53 | +Operations journal with card - it's read only log with card state history |
| 54 | + |
| 55 | +Operation objects implements operation interface: |
| 56 | + |
| 57 | +* `Uuid` - operation uuid |
| 58 | +* `OperationType` - enumeration of operation types () |
| 59 | +* `CardUuid` - card uuid |
| 60 | +* `UserId` - user id |
| 61 | +* `Timestamp` - operation timestamp |
| 62 | +* `Reason` - operation reason |
| 63 | + |
| 64 | + #### Operation types |
| 65 | +* `accrual_transaction` - accrual transaction |
| 66 | +* `payment_transaction` - payment transaction |
| 67 | +* `create_card` - card create |
| 68 | +* `block_card` - block card |
| 69 | +* `unblock_card` - unblock card |
| 70 | +* `delete_card` - delete card |
| 71 | +* `decrement_percent` - decrement percent on card |
| 72 | +* `increment_percent` - increment percent on card |
| 73 | +* `purchase` - purchase registration |
| 74 | +* `b24_deal_monetary_discount_payment_trx` - Bitrix24 deal partial payment with monetary discount |
| 75 | +* `b24_deal_percentage_discount_payment_trx` - Bitrix24 deal partial payment with percentage discount |
| 76 | + |
| 77 | +### Metrics |
| 78 | +Metrics describe operational parameters loyalty application |
| 79 | + |
| 80 | +Metric object: |
| 81 | +* `uuid` - metric uuid |
| 82 | +* `name` - metric name |
| 83 | +* `description` - metric description |
| 84 | +* `code` - metric code |
| 85 | +* `type` - metric type enumeration: INTEGER, FLOAT, MONEY, PERCENTAGE |
| 86 | +* `created` - metric created date time |
| 87 | + |
| 88 | +### Bitrix24 Contacts |
| 89 | +Bitrix24 Loyalty application do not store contacts, app fetch Bitrix24 contacts in realtime with each API-request. |
| 90 | + |
| 91 | +Contact object: |
| 92 | +* `contactId` - Bitrix24 contact id |
| 93 | +* `name` - name |
| 94 | +* `secondName` - second name |
| 95 | +* `lastName` - last name |
| 96 | +* `birthday` - contact birthday |
| 97 | +* `comments` - comments about contact |
| 98 | +* `created` - contact date create |
| 99 | +* `modified` - contact date modified |
| 100 | +* `mobilePhone` - contact mobile phone |
| 101 | +* `email` - contact personal email |
| 102 | +* `address` - contact address |
| 103 | +* `originId` - origin identifier |
| 104 | +* `originatorId` - originator identifier |
| 105 | +* `utm` - utm labels for contact ads channel tracking |
| 106 | +* `sourceDescription` - source description |
| 107 | + |
| 108 | +## Installation |
| 109 | +Via Composer |
| 110 | + |
| 111 | +```bash |
| 112 | +$ composer require b24io/loyalty-php-sdk |
| 113 | +``` |
| 114 | + |
| 115 | +### Requirements |
| 116 | +Loyalty PHP SDK works with PHP 7.1 or above, need `ext-json` and `ext-curl` support |
| 117 | + |
| 118 | +## Authentication with admin and user roles |
| 119 | +SDK can work with two roles: |
| 120 | +* `admin` - can work with all cards in his Bitrix24 account and loyalty application instance |
| 121 | +* `user` - can work only with his card |
| 122 | + |
| 123 | +Bitrix24 Application Loyalty Program and bonus cards work with many Bitrix24 accounts, each account has a `CLIENT_API_KEY` |
| 124 | +If you want work in admin role you must use `ADMIN_API_KEY` to sign queries. |
| 125 | + |
| 126 | +```php |
| 127 | +$token = new SDK\Auth\DTO\Token( |
| 128 | + SDK\Transport\DTO\Role::initializeByCode('admin'), |
| 129 | + Uuid::fromString('CLIENT_API_KEY'), |
| 130 | + Uuid::fromString('ADMIN_API_KEY') |
| 131 | +); |
| 132 | +``` |
| 133 | +If you want work with client role in JS you must use `CLIENT_API_KEY` and `CARD_UUID` as user API key. |
| 134 | + |
| 135 | +## Basic Usage |
| 136 | +```php |
| 137 | +use \Monolog\Logger; |
| 138 | +use \B24io\Loyalty\SDK; |
| 139 | +use Ramsey\Uuid\Uuid; |
| 140 | + |
| 141 | +use GuzzleHttp\HandlerStack; |
| 142 | +use GuzzleHttp\Middleware; |
| 143 | +use GuzzleHttp\MessageFormatter; |
| 144 | + |
| 145 | +$log = new Logger('loyalty-php-sdk'); |
| 146 | +$log->pushHandler(new \Monolog\Handler\StreamHandler('loyalty-php-sdk-example.log', Logger::DEBUG)); |
| 147 | +$guzzleHandlerStack = HandlerStack::create(); |
| 148 | +$guzzleHandlerStack->push( |
| 149 | + Middleware::log( |
| 150 | + $log, |
| 151 | + new MessageFormatter(MessageFormatter::SHORT) |
| 152 | + ) |
| 153 | +); |
| 154 | +$httpClient = new \GuzzleHttp\Client(); |
| 155 | + |
| 156 | +$log->info('loyalty.apiClient.start'); |
| 157 | +$token = new SDK\Auth\DTO\Token( |
| 158 | + SDK\Transport\DTO\Role::initializeByCode('admin'), |
| 159 | + Uuid::fromString('CLIENT_API_KEY'), |
| 160 | + Uuid::fromString('ADMIN_API_KEY') |
| 161 | +); |
| 162 | +$apiClient = new SDK\ApiClient($apiEndpoint, $token, $httpClient, $log); |
| 163 | +$apiClient->setGuzzleHandlerStack($guzzleHandlerStack); |
| 164 | + |
| 165 | +$cardsTransport = SDK\Cards\Transport\Admin\Fabric::getCardTransport($apiClient, $log); |
| 166 | + |
| 167 | +$cardResponse = $cardsTransport->getCardByNumber(22222); |
| 168 | + |
| 169 | +$decimalMoneyFormatter = new \Money\Formatter\DecimalMoneyFormatter(new \Money\Currencies\ISOCurrencies()); |
| 170 | +var_dump($cardResponse->getCard()->getNumber()); |
| 171 | +var_dump($cardResponse->getCard()->getStatus()->getCode()); |
| 172 | +var_dump($decimalMoneyFormatter->format($cardResponse->getCard()->getBalance())); |
| 173 | +var_dump($cardResponse->getCard()->getPercentage()->format()); |
| 174 | +``` |
| 175 | +## Documentation and examples |
| 176 | +More complex examples and use cases you can see in folder [examples](examples) |
| 177 | + |
| 178 | +## Submitting bugs and feature requests |
| 179 | +Bugs and feature request are tracked on [GitHub](https://github.com/b24io/loyalty-php-sdk/issues) |
| 180 | + |
| 181 | +## Support |
| 182 | +* [Telegram chat](https://t.me/joinchat/PhcdgxWKHu7gOGRmowqCpA) with developers |
| 183 | +* [app@b24.io](mailto:app@b24.io) |
| 184 | + |
| 185 | +## Security |
| 186 | +If you discover any security related issues, please contact us at [app@b24.io](mailto:app@b24.io) |
| 187 | + |
| 188 | +## License |
| 189 | +The MIT License (MIT). Please see [License File](LICENSE) for more information. |
0 commit comments