serenity_technologies / gumroad
Laravel package for Gumroad API integration with DTOs and QueryBuilders
Package info
gitlab.com/serenity-technologies-library/gumroad
pkg:composer/serenity_technologies/gumroad
Requires
- php: ^8.1
- guzzlehttp/guzzle: ^7.0
- illuminate/http: ^9.0|^10.0|^11.0
- illuminate/support: ^9.0|^10.0|^11.0
- spatie/laravel-data: ^4.19
Requires (Dev)
- mockery/mockery: ^1.4
- orchestra/testbench: ^7.0|^8.0|^9.0
- phpunit/phpunit: ^9.0|^10.0
README
A comprehensive Laravel package for interacting with the Gumroad API. This package provides full support for all Gumroad API endpoints with constructor-based Laravel Data DTOs, QueryBuilders, and Laravel integration.
Built with Spatie Laravel Data featuring constructor-based instantiation for improved type safety and immutability.
Features
- ✅ Full API coverage for all Gumroad endpoints
- ✅ Constructor-based Laravel Data DTOs with type safety and immutability
- ✅ Fluent QueryBuilder classes for complex queries
- ✅ Laravel Service Provider and Facade integration
- ✅ Proper exception handling
- ✅ PSR-4 autoloading
- ✅ Comprehensive documentation
- ✅ Automatic array-to-DTO conversion utilities
- ✅ Backward compatibility with factory methods
Installation
composer require serenity_technologies/gumroad
Configuration
Publish the configuration file:
php artisan vendor:publish --provider="Gumroad\GumroadServiceProvider" --tag=config
Add your Gumroad access token to your .env file:
GUMROAD_ACCESS_TOKEN=your_gumroad_access_token_here
Usage
Using the Facade
use Gumroad\Facades\Gumroad;
// Get all products
$products = Gumroad::getAllProducts();
// Get a specific product
$product = Gumroad::getProduct('product-id-here');
// Create a new offer code using constructor
$offerCodeData = new CreateOfferCodeDTO(
name: 'SPRING20',
amount_off: null,
percent_off: 20,
offer_type: 'percent',
max_purchase_count: null,
universal: null
);
$offerCode = Gumroad::createOfferCode('product-id', $offerCodeData);
Using Dependency Injection
use Gumroad\Clients\GumroadClient;
class ProductService
{
public function __construct(private GumroadClient $gumroad) {}
public function getAllProducts()
{
return $this->gumroad->getAllProducts();
}
}
API Coverage
Products
getAllProducts()- Get all productsgetProduct($productId)- Get specific product detailsenableProduct($productId)- Enable a productdisableProduct($productId)- Disable a productdeleteProduct($productId)- Delete a product
Variant Categories
getVariantCategories($productId)- Get all variant categoriesgetVariantCategory($productId, $categoryId)- Get specific variant categorycreateVariantCategory($productId, $data)- Create new variant categoryupdateVariantCategory($productId, $categoryId, $data)- Update variant categorydeleteVariantCategory($productId, $categoryId)- Delete variant category
Variants
getVariants($productId, $categoryId)- Get all variants in a categorygetVariant($productId, $categoryId, $variantId)- Get specific variantcreateVariant($productId, $categoryId, $data)- Create new variantupdateVariant($productId, $categoryId, $variantId, $data)- Update variantdeleteVariant($productId, $categoryId, $variantId)- Delete variant
Offer Codes
getOfferCodes($productId)- Get all offer codesgetOfferCode($productId, $offerCodeId)- Get specific offer codecreateOfferCode($productId, $offerCodeData)- Create new offer codeupdateOfferCode($productId, $offerCodeId, $offerCodeData)- Update offer codedeleteOfferCode($productId, $offerCodeId)- Delete offer code
Sales
getSales($queryParams)- Get sales with filteringgetSale($saleId)- Get specific sale detailsmarkSaleAsShipped($saleId, $trackingData)- Mark sale as shippedrefundSale($saleId)- Refund a sale
Licenses
verifyLicense($licenseData)- Verify a license keyenableLicense($licenseData)- Enable a licensedisableLicense($licenseData)- Disable a licensedecrementLicenseUses($licenseData)- Decrement license usage count
Users
getUser()- Get current user information
Custom Fields
getCustomFields($productId)- Get product custom fieldsaddCustomField($productId, $data)- Add custom field to productupdateCustomField($productId, $fieldName, $data)- Update custom fielddeleteCustomField($productId, $fieldName)- Delete custom field
Subscribers
getActiveSubscribers($productId, $queryParams)- Get active subscribersgetSubscriberDetails($subscriberId)- Get subscriber details
Query Builders
Sales Query Builder
use Gumroad\QueryBuilders\SalesQueryBuilder;
$query = (new SalesQueryBuilder())
->after('2023-01-01')
->before('2023-12-31')
->productId('product-id')
->email('customer@example.com')
->build();
$sales = Gumroad::getSales($query);
Subscribers Query Builder
use Gumroad\QueryBuilders\SubscribersQueryBuilder;
$query = (new SubscribersQueryBuilder())
->email('subscriber@example.com')
->paginated(true)
->pageKey('next-page-key')
->build();
$subscribers = Gumroad::getActiveSubscribers('product-id', $query);
DTO Usage
Constructor-Based Approach (Recommended)
use Gumroad\DTOs\CreateOfferCodeDTO;
use Gumroad\DTOs\VerifyLicenseDTO;
// Create offer code with named parameters
$offerCode = new CreateOfferCodeDTO(
name: 'WELCOME10',
amount_off: null,
percent_off: 10,
offer_type: 'percent',
max_purchase_count: 100,
universal: null
);
// Verify license
$license = new VerifyLicenseDTO(
product_id: 'product-123',
license_key: 'ABC123-DEF456',
increment_uses_count: true
);
Factory Method Approach (Backward Compatible)
use Gumroad\DTOs\CreateOfferCodeDTO;
// Create from array data
$offerCode = CreateOfferCodeDTO::fromArray([
'name' => 'BACKWARD20',
'amount_off' => null,
'percent_off' => 20,
'offer_type' => 'percent',
'max_purchase_count' => 50,
'universal' => null
]);
DTO to Array Conversion
use Gumroad\DTOs\ProductDTO;
// Convert DTO to array
$productDto = new ProductDTO(/* ... */);
$arrayData = $productDto->toArray();
Error Handling
All API errors are wrapped in GumroadException:
use Gumroad\Exceptions\GumroadException;
try {
$product = Gumroad::getProduct('invalid-id');
} catch (GumroadException $e) {
echo "API Error: " . $e->getMessage();
}
Testing
composer test
DTO Conversion Tools
This package includes automated tools for converting DTOs to constructor-based definitions:
# Batch convert all DTOs
php batch_convert_dtos.php
# Full-featured converter with logging
php convert_dtos_to_constructors.php
# Convert single DTO file
php simple_dto_converter.php ProductDTO.php
See DTO_CONVERSION_SCRIPTS.md for detailed documentation.
Requirements
- PHP ^8.1
- Laravel ^9.0|^10.0|^11.0
- GuzzleHttp ^7.0
- Spatie Laravel Data ^4.19
License
The MIT License (MIT). Please see License File for more information.
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for details.
Security
If you discover any security related issues, please email kyerematics@gmail.com instead of using the issue tracker.