crmcarecloud / sdk-php
此包最新版本(3.1.0)没有提供许可证信息。
3.1.0
2024-09-05 14:44 UTC
Requires
- php: ^8.1
- crmcarecloud/api-php-client: ^2.1
- kevinrob/guzzle-cache-middleware: ^5.1
- psr/simple-cache: ^3.0
- symfony/cache: ^6.4
Requires (Dev)
- phpstan/phpstan: ^1.10
- rector/rector: ^1.0
README
此包为CareCloud PHP客户端提供了一个便捷的包装。
安装
您可以使用composer安装此包
composer require crmcarecloud/sdk-php:^3.0
客户端设置
您需要首先创建客户端
use CrmCareCloud\Webservice\RestApi\Client\SDK\CareCloud; use CrmCareCloud\Webservice\RestApi\Client\SDK\Config; use CrmCareCloud\Webservice\RestApi\Client\SDK\Data\AuthTypes; use CrmCareCloud\Webservice\RestApi\Client\SDK\Data\Interfaces; require_once 'vendor/autoload.php'; $projectUri = 'https://yourapiurl.com/webservice/rest-api/enterprise-interface/v1.0'; $login = 'login'; $password = 'pass'; $externalAppId = 'appId'; $authType = AuthTypes::BEARER_AUTH; $interface = Interfaces::ENTERPRISE; // Or if using basic auth, just change the AuthType to Basic Auth // $authType = AuthTypes::BASIC_AUTH; $config = new Config( $projectUri, $login, $password, $externalAppId, $authType, $interface ); $careCloud = new CareCloud( $config );
现在您可以通过$client
对象访问所有API端点
/** * Get information about a specific agreement */ // Set Header parameter Accept-Language $accept_language = 'cs'; // string | The unique id of the language code by ISO 639-1 Default: cs, en-gb;q=0.8 // Set path parameter $agreement_id = "81eaeea13b8984a169c490a325"; // string | The unique id of an agreement in CareCloud try { $agreement = $careCloud->agreementsApi()->getAgreement($agreement_id, $accept_language); $name = $agreement->getData()->getName(); $text = $agreement->getData()->getText(); } catch (CrmCareCloud\Webservice\RestApi\Client\ApiException $exception) { die(var_dump($exception->getResponseBody() ?: $exception->getMessage())); }
示例
获取所有客户
/** * Get a list of all customers */ // Set Header parameter Accept-Language $accept_language = 'cs'; // string | The unique id of the language code by ISO 639-1 Default: cs, en-gb;q=0.8 // Set query parameters $count = 100; // integer >= 1 | The number of records to return. $offset = 0; // integer | The number of records from a collection to skip. $sort_field = 'last_change'; // string | One of the query string parameters for sorting. Response is sorted by the specified field. $sort_direction = 'DESC'; // string | Direction of sorting the response list. $email = 'happy_customer@crmcarecloud.com'; // string | Search by email $phone = '420523828931'; // string | Phone number with international prefix (420000000) $customer_source_id = null; // string | The unique id of the customer source. It identifies the system where the customer belongs or the customer account was created $first_name = 'John'; // string | Search by first name $last_name = 'Smith'; // string | Search by last name $birthdate = '1985-02-12'; // string | Customer's date of birth. Possible values are: YYYY-MM-DD / DD.MM.YYYY // Call endpoint and get data try { $getCustomers = $careCloud->customersApi()->getCustomers($accept_language, $count, $offset, $sort_field, $sort_direction, $email, $phone, $customer_source_id, $first_name, $last_name, $birthdate); $customers = $getCustomers->getData()->getCustomers(); $totalItems = $getCustomers->getData()->getTotalItems(); } catch (CrmCareCloud\Webservice\RestApi\Client\ApiException $exception) { die(var_dump($exception->getResponseBody() ?: $exception->getMessage())); }
获取客户信息
/** * Get information about a specific customer account */ // Set Header parameter Accept-Language $accept_language = 'cs'; // string | The unique id of the language code by ISO 639-1 Default: cs, en-gb;q=0.8 // Set path parameter $customer_id = '87af991126405bf8e7dfb36045'; // string | The unique id for the customer // Call endpoint and get data try { $getCustomer = $careCloud->customersApi()->getCustomer($customer_id, $accept_language); $customer = $getCustomer->getData(); } catch (CrmCareCloud\Webservice\RestApi\Client\ApiException $exception) { die(var_dump($exception->getResponseBody() ?: $exception->getMessage())); }
更新客户
/** * Update the information on a specific customer account */ // Set Header parameter Accept-Language $accept_language = 'cs'; // string | The unique id of the language code by ISO 639-1 Default: cs, en-gb;q=0.8 // Set path parameter $customer_id = '87af991126405bf8e7dfb36045'; // string | The unique id for the customer // Set address of customer $address = new Address(); $address->setAddress1('Old Town Square') // string | Street name of the address ->setAddress2('34') // string | Street number (Land registry number) ->setAddress3('12') // string | House number ->setZip('11000') // string | ZIP code ->setCity('Prague 1') // string | City ->setCountryCode('cz'); // string | ISO code of the country Possible values de / gb / us / it / cz / etc. // Set custom agreements of customer $custom_agreement1 = new CustomAgreements(); $custom_agreement1->setAgreementId('custom_agreement_id') // string | The unique id of the agreement in CareCloud ->setAgreementValue(1); // integer | Value of the specific agreement Possible values: 0 - no, consent canceled / 1 - yes, consent given / 2 - consent not set $custom_agreement2 = new CustomAgreements(); $custom_agreement2->setAgreementId('second_custom_agreement_id') // string | The unique id of the agreement in CareCloud ->setAgreementValue(1); // integer | Value of the specific agreement Possible values: 0 - no, consent canceled / 1 - yes, consent given / 2 - consent not set $custom_agreements = [ $custom_agreement1, $custom_agreement2 ]; // Set agreement of customer $agreement = new Agreement(); $agreement->setAgreementGtc(1) // integer | Consent to General Terms & Conditions Possible values: 0 - no, consent canceled / 1 - yes, consent given / 2 - consent not set ->setAgreementProfiling(1) // integer | Consent to profiling Possible values: 0 - no, consent canceled / 1 - yes, consent given / 2 - consent not set ->setAgreementMarketingCommunication(0) // integer | Consent to marketing communication Possible values: 0 - no, consent canceled / 1 - yes, consent given / 2 - consent not set ->setCustomAgreements($custom_agreements); // Set personal information of customer $personal_information = new PersonalInformation(); $personal_information->setGender(1) // integer | Gender of the customer Possible values: 1 - male, 2 - female ->setFirstName('John') // string | First name of the customer ->setLastName('Smith') // string | Last name of the customer ->setBirthdate('1985-02-12') // string <date> | Customer's date of birth (YYYY-MM-DD) ->setEmail('happy_customer@crmcarecloud.com') // string | Email of the customer ->setPhone('420523828931') // string | Phone number of the customer with international prefix (420000000000) ->setLanguageId('cs') // string | The unique id for the language by ISO 639 code ->setStoreId('8bed991c68a4') // string | The unique id for the original customer account store of registration ->setPhotoUrl(null) // string | URL address of the customer photo. If customer has no photo, this parameter is not send ->setAddress($address) ->setAgreement($agreement); $customer = new Customer(); $customer->setPersonalInformation($personal_information); // Set customer Social network credentials $social_network_credentials = new SocialNetworkCredentials(); $social_network_credentials->setSocialNetworkId('twitter') // string | The unique id of the social network ->setSocialNetworkToken('38e123j1jedu12d1jnjqwd'); // string | Social network customer's token // Set basic information about customer $body = new CustomersCustomerIdBody(); $body->setCustomer($customer) ->setPassword('fO7mrC7spZjr') // string | Password of the customer. ->setSocialNetworkCredentials($social_network_credentials); // Call endpoint and get data try { $putCustomer = $careCloud->customersApi()->putCustomer( $body, $customer_id, $accept_language ); } catch (CrmCareCloud\Webservice\RestApi\Client\ApiException $exception) { die(var_dump($exception->getResponseBody() ?: $exception->getMessage())); }
更多示例,请查看Examples
文件夹。
扩展方法
将卡片分配给客户
此方法检查卡片是否空闲且未被锁定。如果一切正常,则将其分配给指定的客户。可以在该方法中添加附加信息到卡片。
/** * Assigning a free card to a customer */ // Set Header parameter Accept-Language $accept_language = 'cs'; // string | The unique id of the language code by ISO 639-1 Default: cs, en-gb;q=0.8 // Set method parameters $card_number = '1000000000030'; // string | number of the searched card $customer_id = '82ab3d112cba4cb26c9b6eafbd'; // string | customer id for assignment to a free card $valid_from = '2021-08-01'; // string | set the card validity from (YYYY-MM-DD) $valid_to = '2025-08-01'; // string | set the card validity to (YYYY-MM-DD) $store_id = null; // string | assign a store id to the card // Call endpoint and get data try { $assignCard = $careCloud->cardsApi()->putUnassignedCard($card_number, $customer_id, $valid_from, $valid_to, $store_id, $accept_language); } catch (CrmCareCloud\Webservice\RestApi\Client\ApiException $exception) { die(var_dump($exception->getResponseBody() ?: $exception->getMessage())); }
响应:204 无内容
添加新客户
使用此扩展方法,您可以直接在创建客户时分配卡片、设置客户的属性记录和兴趣记录。
如果我们知道卡片号,则会查找并检查其可用性。如果可用,则将其分配给创建的客户。如果我们不知道号码,则分配任何可用的卡片。
/** * Create a new customer with extended method */ // Set Header parameter Accept-Language $accept_language = 'cs'; // string | The unique id of the language code by ISO 639-1 Default: cs, en-gb;q=0.8 // Set address of new customer $address = new Address(); $address->setAddress1('Old Town Square') // string | Street name of the address ->setAddress2('34') // string | Street number (Land registry number) ->setAddress3('12') // string | House number ->setZip('11000') // string | ZIP code ->setCity('Prague 1') // string | City ->setCountryCode('cz'); // string | ISO code of the country Possible values de / gb / us / it / cz / etc. // Set custom agreements of new customer $custom_agreement1 = new CustomAgreements(); $custom_agreement1->setAgreementId('custom_agreement_id') // string | The unique id of the agreement in CareCloud ->setAgreementValue(1); // integer | Value of the specific agreement Possible values: 0 - no, consent canceled / 1 - yes, consent given / 2 - consent not set $custom_agreement2 = new CustomAgreements(); $custom_agreement2->setAgreementId('second_custom_agreement_id') // string | The unique id of the agreement in CareCloud ->setAgreementValue(1); // integer | Value of the specific agreement Possible values: 0 - no, consent canceled / 1 - yes, consent given / 2 - consent not set $custom_agreements = [ $custom_agreement1, $custom_agreement2 ]; // Set agreement of new customer $agreement = new Agreement(); $agreement->setAgreementGtc(1) // integer | Consent to General Terms & Conditions Possible values: 0 - no, consent canceled / 1 - yes, consent given / 2 - consent not set ->setAgreementProfiling(1) // integer | Consent to profiling Possible values: 0 - no, consent canceled / 1 - yes, consent given / 2 - consent not set ->setAgreementMarketingCommunication(0) // integer | Consent to marketing communication Possible values: 0 - no, consent canceled / 1 - yes, consent given / 2 - consent not set ->setCustomAgreements($custom_agreements); // Set personal information of new customer $personal_information = new PersonalInformation(); $personal_information->setGender(1) // integer | Gender of the customer Possible values: 1 - male, 2 - female ->setFirstName('John') // string | First name of the customer ->setLastName('Smith') // string | Last name of the customer ->setBirthdate('1985-02-12') // string <date> | Customer's date of birth (YYYY-MM-DD) ->setEmail('happy_customer@crmcarecloud.com') // string | Email of the customer ->setPhone('420523828931') // string | Phone number of the customer with international prefix (420000000000) ->setLanguageId('cs') // string | The unique id for the language by ISO 639 code ->setStoreId('8bed991c68a4') // string | The unique id for the original customer account store of registration ->setPhotoUrl(null) // string | URL address of the customer photo. If customer has no photo, this parameter is not send ->setAddress($address) ->setAgreement($agreement); $customer = new Customer(); $customer->setPersonalInformation($personal_information); // Set source record of new customer from object (CustomerSourceRecord) $customer_source = new CustomerSourceRecord(); $customer_source->setCustomerSourceId('8fd73167342d06899c4c015320') // string | The unique id of the customer source. It identifies the system where the customer belongs or the customer account was created ->setExternalId('external-id'); // string | The unique external id of the customer. It may be id from the other system // Set customer Social network credentials $social_network_credentials = new SocialNetworkCredentials(); $social_network_credentials->setSocialNetworkId('twitter') // string | The unique id of the social network ->setSocialNetworkToken('38e123j1jedu12d1jnjqwd'); // string | Social network customer's token // Set basic information about new customer $customerBody = new CustomersBody(); $customerBody->setCustomer($customer) ->setCustomerSource($customer_source) ->setPassword('fO7mrC7spZjr') // string | Password of the customer. ->setAutologin(false) // boolean | If true, password is required and customer is logged in. Otherwise password is optional ->setSocialNetworkCredentials($social_network_credentials); // Set the card information that will be assigned to the customer $card = new Card(); $card->setCardNumber('1000000000') // string | set card_number if you know it else set card_type_id ->setCardTypeId('8bed991c68a470e7aaeffbf048') // string | The unique id for the card type. (Required if we don't know the card number) ->setValidFrom('2021-11-01') // string | set the card validity from (YYYY-MM-DD) ->setValidTo('2022-11-02') // string | set the card validity to (YYYY-MM-DD) ->setStoreId(null); // string | assign a store id to the card // Set property record $property_record = new PropertyRecord(); $property_record->setPropertyId('custom_id') // string ->setPropertyName('Custom property name') // string ->setPropertyValue( 'Some value'); // string or integer or number or object or boolean or (Array of strings or integers or numbers or objects) $propertyBody = new CustomerIdPropertyrecordsBody(); $propertyBody->setPropertyRecord($property_record); // Set interest record $interest_record = new InterestRecord(); $interest_record->setInterestId('81eaeea13b8984a169c490a325') // string | The unique id of the interest ->setCustomerId('89ac83ca207a820c62c79bf03a'); // string | The unique id of the customer $interestBody = new CustomerIdInterestrecordsBody(); $interestBody->setInterestRecord($interest_record); // Call endpoint and get data try { $newCustomer = $careCloud->customersApi()->postCustomerExtended($customerBody, $card, $propertyBody, $interestBody, $accept_language ); } catch (CrmCareCloud\Webservice\RestApi\Client\ApiException $exception) { die(var_dump($exception->getResponseBody() ?: $exception->getMessage())); }
响应
Array ( [customer_id] => ... [card_id] => ... [property_record_id] => ... [interest_record_id] => ... )
获取特定客户的全部奖励
该方法返回特定客户的全部奖励(奖励、优惠券、活动产品)。
/** * Get all rewards for a specific customer */ // Set Header parameter Accept-Language $accept_language = 'cs'; // string | The unique id of the language code by ISO 639-1 Default: cs, en-gb;q=0.8 // Set method parameters $customer_id = '8bed991c68a470e7aaeffbf048'; // string | Id of customer $rewards = true; // boolean | false - we don't want to get rewards / true - get rewards $reward_group = null; // integer | null - all groups / 0 - cash desk reward (party time reward) / 1 - catalog reward $vouchers = true; // boolean | false - we don't want to get vouchers / true - get vouchers $campaign_products = true; // boolean | false - we don't want to get campaign products / true - get campaign products $is_valid = null; // boolean | true / false / null - all $customer_type_id = null; // By resource customer-types // Call endpoints and get data try { $allRewards = $careCloud->customersApi()->getAllRewards( $customer_id, $rewards, $reward_group, $vouchers, $campaign_products, $is_valid, $customer_type_id, $accept_language ); die(print_r($allRewards)); } catch (CrmCareCloud\Webservice\RestApi\Client\ApiException $exception) { die(var_dump($exception->getResponseBody() ?: $exception->getMessage())); }
响应
Array ( [rewards] => Array ( [items] => Array ( ... ) [total_items] => 0 ) )
缓存
SDK提供了一个机制,可以轻松地使用任何PSR-6兼容的缓存适配器来缓存对特定端点的请求。
缓存示例
我们将使用Symfony缓存
composer require symfony/cache
设置规则
use CrmCareCloud\Webservice\RestApi\Client\SDK\Cache\Rule; $cache_rules = [ new Rule( Rule::REQUEST_TYPE_GET, // Request type 'agreements', // Path 400 // TTL ), ];
设置缓存并将其传递给客户端
use Symfony\Component\Cache\Adapter\FilesystemAdapter; use CrmCareCloud\Webservice\RestApi\Client\SDK\Cache\Cache; $cache = new Cache( new FilesystemAdapter( 'testCache', 0, __DIR__ ), // PSR-6 compatible Cache Pool, in our case Symfony FileAdapter $cache_rules ); $careCloud = new CareCloud( $config, $cache );
现在所有对agreements
端点的GET
请求都缓存了400秒
。对于完整的示例,请查看Examples/Caching.php
。