You've already forked wc-licensed-product-client
- Add PHPUnit 11.0 as dev dependency - Add phpunit.xml configuration - Add DTO tests (LicenseInfo, LicenseStatus, ActivationResult) - Add Exception tests (factory method, all exception types) - Add LicenseClient tests with mocked HTTP responses - Update README with testing instructions - Update CHANGELOG 32 tests, 93 assertions Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
138 lines
3.4 KiB
Markdown
138 lines
3.4 KiB
Markdown
# WooCommerce Licensed Product Client
|
|
|
|
A PHP client library for the WooCommerce Licensed Product Plugin REST API. Activate, validate, and check the status of software licenses.
|
|
|
|
## Requirements
|
|
|
|
- PHP 8.3 or higher
|
|
- Composer
|
|
|
|
## Installation
|
|
|
|
```bash
|
|
composer require magdev/wc-licensed-product-client
|
|
```
|
|
|
|
## Features
|
|
|
|
- Object-oriented client library
|
|
- PSR-3 logging support
|
|
- PSR-6 caching support
|
|
- PSR-18 HTTP client compatible
|
|
- License validation against domains
|
|
- License activation on domains
|
|
- License status checking
|
|
- Comprehensive exception handling
|
|
- Built on Symfony HttpClient
|
|
|
|
## Usage
|
|
|
|
### Basic Usage
|
|
|
|
```php
|
|
use Magdev\WcLicensedProductClient\LicenseClient;
|
|
use Symfony\Component\HttpClient\HttpClient;
|
|
|
|
$httpClient = HttpClient::create();
|
|
$client = new LicenseClient(
|
|
httpClient: $httpClient,
|
|
baseUrl: 'https://your-wordpress-site.com',
|
|
);
|
|
|
|
// Validate a license
|
|
$licenseInfo = $client->validate('ABCD-1234-EFGH-5678', 'example.com');
|
|
echo "Product ID: " . $licenseInfo->productId;
|
|
|
|
// Check license status
|
|
$status = $client->status('ABCD-1234-EFGH-5678');
|
|
echo "Status: " . $status->status->value;
|
|
|
|
// Activate a license
|
|
$result = $client->activate('ABCD-1234-EFGH-5678', 'example.com');
|
|
echo "Activated: " . ($result->success ? 'Yes' : 'No');
|
|
```
|
|
|
|
### With Logging
|
|
|
|
```php
|
|
use Magdev\WcLicensedProductClient\LicenseClient;
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Component\HttpClient\HttpClient;
|
|
|
|
$client = new LicenseClient(
|
|
httpClient: HttpClient::create(),
|
|
baseUrl: 'https://your-wordpress-site.com',
|
|
logger: $yourPsrLogger, // Any PSR-3 compatible logger
|
|
);
|
|
```
|
|
|
|
### With Caching
|
|
|
|
```php
|
|
use Magdev\WcLicensedProductClient\LicenseClient;
|
|
use Psr\Cache\CacheItemPoolInterface;
|
|
use Symfony\Component\HttpClient\HttpClient;
|
|
|
|
$client = new LicenseClient(
|
|
httpClient: HttpClient::create(),
|
|
baseUrl: 'https://your-wordpress-site.com',
|
|
cache: $yourPsrCache, // Any PSR-6 compatible cache
|
|
cacheTtl: 600, // Cache TTL in seconds (default: 300)
|
|
);
|
|
```
|
|
|
|
### Exception Handling
|
|
|
|
```php
|
|
use Magdev\WcLicensedProductClient\Exception\LicenseNotFoundException;
|
|
use Magdev\WcLicensedProductClient\Exception\LicenseExpiredException;
|
|
use Magdev\WcLicensedProductClient\Exception\DomainMismatchException;
|
|
use Magdev\WcLicensedProductClient\Exception\RateLimitExceededException;
|
|
use Magdev\WcLicensedProductClient\Exception\LicenseException;
|
|
|
|
try {
|
|
$licenseInfo = $client->validate($licenseKey, $domain);
|
|
} catch (LicenseNotFoundException $e) {
|
|
// License key does not exist
|
|
} catch (LicenseExpiredException $e) {
|
|
// License has expired
|
|
} catch (DomainMismatchException $e) {
|
|
// License is not valid for this domain
|
|
} catch (RateLimitExceededException $e) {
|
|
// Too many requests, retry after $e->retryAfter seconds
|
|
} catch (LicenseException $e) {
|
|
// Other license-related errors
|
|
}
|
|
```
|
|
|
|
## Testing
|
|
|
|
Run the test suite with PHPUnit:
|
|
|
|
```bash
|
|
./vendor/bin/phpunit
|
|
```
|
|
|
|
## API Endpoints
|
|
|
|
This client interacts with the following WooCommerce Licensed Product API endpoints:
|
|
|
|
- **POST /validate** - Validate a license key for a specific domain
|
|
- **POST /status** - Get detailed license status information
|
|
- **POST /activate** - Activate a license on a domain
|
|
|
|
## License
|
|
|
|
GPL-2.0-or-later
|
|
|
|
## Author
|
|
|
|
Marco Graetsch
|
|
|
|
- Website: <https://src.bundespruefstelle.ch/magdev>
|
|
- Email: <magdev3.0@gmail.com>
|
|
|
|
## Contributing
|
|
|
|
Issues and pull requests are welcome at <https://src.bundespruefstelle.ch/magdev/wc-licensed-product-client/issues>
|