PHP code example of emeroid / laravel-billing-core
1. Go to this page and download the library: Download emeroid/laravel-billing-core library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
emeroid / laravel-billing-core example snippets
// app/Models/User.php
use Emeroid\Billing\Traits\Billable;
use Illuminate{...};
class User extends Authenticatable
{
use Billable, HasFactory, Notifiable;
// ...
}
public static function findByEmail(string $email): ?Model
namespace App\Lookups;
use App\Models\User;
use App\Models\Team;
use Illuminate\Database\Eloquent\Model;
class BillableLookup
{
/**
* Custom strategy to find the billable model instance (Team) from an email.
*
* @param string $email The email address received from the payment gateway.
* @return Model|null The Billable Model instance (Team) or null if not found.
*/
public static function findByEmail(string $email): ?Model
{
// 1. Find the User by email.
$user = User::where('email', $email)->first();
if (!$user) {
return null;
}
// 2. Return the Billable model (Team) associated with this User.
// Example: Team is linked to User via the 'owner_id' foreign key.
return Team::where('owner_id', $user->id)->first();
}
}
// File: config/billing.php
'email_lookup' => [
\App\Lookups\BillableLookup::class,
'findByEmail'
],
// ... rest of your config
// Excerpt from Emeroid\Billing\Drivers\AbstractDriver.php
protected function findBillableByEmail(string $email): ?Model
{
// 1. Check for a custom lookup strategy defined in the billing config.
$customLookup = config('billing.email_lookup');
if (is_callable($customLookup)) {
// Use the custom callable if defined.
return call_user_func($customLookup, $email);
}
// 2. Fallback: Check the billable model directly.
$modelClass = $this->billableModelClass();
return $modelClass::where('email', $email)->first();
}