47 lines
1.9 KiB
PHP
Executable File
47 lines
1.9 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Services\Licensing;
|
|
|
|
use App\Models\License;
|
|
use App\Models\LicenseDeliveryOutbox;
|
|
use App\Models\LicenseProduct;
|
|
use Illuminate\Support\Facades\Crypt;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
final class LicenseIssuer
|
|
{
|
|
public function issue(LicenseProduct $product, ?string $name, ?string $email, ?string $provider = null, ?string $orderID = null, ?string $customerID = null, bool $queueDelivery = false): array
|
|
{
|
|
return DB::transaction(function () use ($product, $name, $email, $provider, $orderID, $customerID, $queueDelivery) {
|
|
if ($provider && $orderID) {
|
|
$existing = License::where('payment_provider', $provider)->where('external_order_id', $orderID)->first();
|
|
if ($existing) {
|
|
return [$existing, null];
|
|
}
|
|
}
|
|
$plain = LicenseKey::generate();
|
|
$license = License::create([
|
|
'license_product_id' => $product->id,
|
|
'license_key_hash' => LicenseKey::hash($plain),
|
|
'license_key_last4' => substr(LicenseKey::normalize($plain), -4),
|
|
'customer_name' => $name,
|
|
'customer_email' => $email ? strtolower(trim($email)) : null,
|
|
'edition' => $product->edition,
|
|
'activation_limit' => $product->activation_limit,
|
|
'max_major_version' => $product->max_major_version,
|
|
'payment_provider' => $provider,
|
|
'external_order_id' => $orderID,
|
|
'external_customer_id' => $customerID,
|
|
]);
|
|
if ($queueDelivery) {
|
|
LicenseDeliveryOutbox::updateOrCreate(
|
|
['license_id' => $license->id],
|
|
['encrypted_license_key' => Crypt::encryptString($plain), 'recipient_email' => $license->customer_email, 'delivered_at' => null]
|
|
);
|
|
}
|
|
|
|
return [$license, $plain];
|
|
});
|
|
}
|
|
}
|