Overview
This guide will walk you through creating your first subscription with Eufaturo Billing. We’ll cover:
Creating a currency
Creating a product and plan
Setting up features
Creating a subscription
Make sure you’ve completed the installation before proceeding.
Step 1: Create a Currency
First, create a currency using the Billing facade:
use Eufaturo\Billing\ Billing ;
$currency = Billing :: currencies () -> create (
isoCode : 'EUR' ,
name : 'Euro' ,
symbol : '€' ,
rate : 100 , // Exchange rate (100 = 1:1)
);
The rate represents the exchange rate in basis points (100 = 1:1 ratio).
Step 2: Create a Product
Create a product:
$product = Billing :: products () -> create (
name : 'Premium Plan' ,
slug : 'premium' ,
description : 'Access to all premium features' ,
isVisible : true ,
isPopular : false ,
isDefault : false ,
);
Step 3: Create a Product Plan
Create a pricing plan:
use Eufaturo\Billing\ProductPlans\Enums\ Interval ;
use Eufaturo\Billing\ProductPlans\Enums\ ProductPlanType ;
$plan = Billing :: plans () -> create (
product : $product ,
currency : $currency ,
type : ProductPlanType :: Recurring ,
name : 'Premium Monthly' ,
slug : 'premium-monthly' ,
isActive : true ,
price : 2999 , // $29.99 in cents
interval : Interval :: Month ,
intervalCount : 1 ,
description : 'Monthly premium subscription' ,
);
All amounts are stored in cents (minor units) for precision. The intervalCount determines billing frequency (1 = every interval, 3 = every 3 intervals).
Step 4: Add Features (Optional)
Define features and attach them to products:
use Eufaturo\Billing\Features\Enums\ FeatureType ;
use Eufaturo\Billing\Features\Enums\ ResetPeriod ;
// Create API Access feature (Boolean)
$apiFeature = Billing :: features () -> create (
name : 'API Access' ,
slug : 'api-access' ,
type : FeatureType :: Boolean ,
description : 'Access to REST API' ,
isActive : true ,
);
// Attach feature to product
$product -> features () -> attach ( $apiFeature , [
'value' => 'true' , // Enable API access
]);
// Create API Rate Limit feature (Quota)
$rateLimitFeature = Billing :: features () -> create (
name : 'API Rate Limit' ,
slug : 'api-rate-limit' ,
type : FeatureType :: Quota ,
description : 'API requests per month' ,
unit : 'requests' ,
resetPeriod : ResetPeriod :: Monthly ,
isActive : true ,
);
// Attach quota feature to product
$product -> features () -> attach ( $rateLimitFeature , [
'value' => '10000' , // 10,000 requests/month
]);
You can attach the same feature to multiple products with different values!
Install and configure a payment gateway:
composer require eufaturo/billing-stripe
php artisan billing:gateway:install stripe
Follow the interactive prompts to enter your Stripe credentials.
Step 6: Create a Subscription
Now create a subscription for a user:
use Eufaturo\Billing\ Gateway ;
use Eufaturo\Billing\Subscriptions\Enums\ SubscriptionType ;
// Get the billable entity (user/company)
$billable = Billing :: getBillable ();
// Get the gateway
$gateway = Gateway :: create ( 'stripe' );
// Create subscription
$subscription = Billing :: subscriptions () -> create (
billable : $billable ,
plan : $plan ,
type : SubscriptionType :: PaymentGatewayManaged ,
paymentGateway : $gateway -> getPaymentGateway (),
trialEndsAt : now () -> addDays ( 14 ), // 14-day trial (optional)
);
Step 7: Check Subscription Status
Verify the subscription was created:
// Check if user is subscribed
if ( $billable -> isSubscribed ()) {
echo "User has an active subscription!" ;
}
// Get the subscription
$activeSubscription = $billable -> subscription ();
// Check feature access
if ( $activeSubscription -> canUseFeature ( 'api-access' )) {
// Grant API access
}
// Check quota
$remaining = $activeSubscription -> getRemainingQuota ( 'api-rate-limit' );
echo "API calls remaining: { $remaining }" ;
Complete Example
Here’s a complete example putting it all together using the clean Billing facade API:
use Eufaturo\Billing\ Billing ;
use Eufaturo\Billing\ Gateway ;
use Eufaturo\Billing\ProductPlans\Enums\ { Interval , ProductPlanType };
use Eufaturo\Billing\Features\Enums\ FeatureType ;
use Eufaturo\Billing\Subscriptions\Enums\ SubscriptionType ;
// 1. Create currency
$currency = Billing :: currencies () -> create (
isoCode : 'EUR' ,
name : 'Euro' ,
symbol : '€' ,
rate : 100 ,
);
// 2. Create product
$product = Billing :: products () -> create (
name : 'Premium Plan' ,
slug : 'premium' ,
description : 'Full access to all features' ,
);
// 3. Create product plan
$plan = Billing :: plans () -> create (
product : $product ,
currency : $currency ,
type : ProductPlanType :: Recurring ,
name : 'Premium Monthly' ,
slug : 'premium-monthly' ,
isActive : true ,
price : 2999 ,
interval : Interval :: Month ,
intervalCount : 1 ,
);
// 4. Add features
$apiAccess = Billing :: features () -> create (
name : 'API Access' ,
slug : 'api-access' ,
type : FeatureType :: Boolean ,
);
$product -> features () -> attach ( $apiAccess , [ 'value' => 'true' ]);
// 5. Create subscription
$billable = Billing :: getBillable ();
$gateway = Gateway :: create ( 'stripe' );
$subscription = Billing :: subscriptions () -> create (
billable : $billable ,
plan : $plan ,
type : SubscriptionType :: PaymentGatewayManaged ,
paymentGateway : $gateway -> getPaymentGateway (),
);
// 6. Verify
if ( $billable -> isSubscribed ()) {
echo "✅ Subscription created successfully!" ;
}
Next Steps
Common Patterns
$subscription = app ( CreateSubscription :: class ) -> execute (
new CreateSubscriptionDto (
billable : $billable ,
plan : $plan ,
type : SubscriptionType :: PaymentGatewayManaged ,
paymentGateway : $gateway -> getPaymentGateway (),
trialEnds : now () -> addDays ( 14 ),
)
);
$subscription = $user -> subscription ();
$features = [
'api-access' => $subscription -> canUseFeature ( 'api-access' ),
'export-data' => $subscription -> canUseFeature ( 'export-data' ),
'priority-support' => $subscription -> canUseFeature ( 'priority-support' ),
];
// Consume a quota
$subscription -> consumeFeature ( 'api-rate-limit' , 1 );
// Get remaining quota
$remaining = $subscription -> getRemainingQuota ( 'api-rate-limit' );
// Check if quota available
if ( $subscription -> canUseFeature ( 'api-rate-limit' )) {
// Process API request
$subscription -> consumeFeature ( 'api-rate-limit' );
}