feat(payment): integrate Polar.sh payment provider with checkout flow
- Build PolarProvider from scratch with proper HTTP API integration - Add encrypted configuration loading from payment_providers table via model - Implement sandbox/live environment switching with proper credential handling - Fix product creation API structure for Polar.sh requirements - Add comprehensive error handling and logging throughout checkout flow - Fix PaymentController checkout URL handling to support Polar's checkout_url response - Add debug logging for troubleshooting checkout session creation - Support both regular and trial checkout flows for Polar payments
This commit is contained in:
@@ -4,14 +4,17 @@ namespace App\Services\Payments;
|
||||
|
||||
use App\Contracts\Payments\PaymentProviderContract;
|
||||
use App\Models\Coupon;
|
||||
use App\Models\CouponUsage;
|
||||
use App\Models\Plan;
|
||||
use App\Models\Subscription;
|
||||
use App\Models\SubscriptionChange;
|
||||
use App\Models\TrialExtension;
|
||||
use App\Models\User;
|
||||
use Exception;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class PaymentOrchestrator
|
||||
{
|
||||
@@ -588,16 +591,41 @@ class PaymentOrchestrator
|
||||
*/
|
||||
protected function getProviderForPlan(Plan $plan, ?string $providerName = null): PaymentProviderContract
|
||||
{
|
||||
Log::info('PaymentOrchestrator: Getting provider for plan', [
|
||||
'plan_id' => $plan->id,
|
||||
'requested_provider' => $providerName,
|
||||
]);
|
||||
|
||||
if ($providerName) {
|
||||
$provider = $this->providerRegistry->get($providerName);
|
||||
Log::info('PaymentOrchestrator: Checking specific provider', [
|
||||
'provider_name' => $providerName,
|
||||
'provider_exists' => $provider ? true : false,
|
||||
'provider_active' => $provider?->isActive(),
|
||||
'provider_supported' => $provider ? $this->isProviderSupportedForPlan($provider, $plan) : false,
|
||||
]);
|
||||
|
||||
if ($provider && $provider->isActive() && $this->isProviderSupportedForPlan($provider, $plan)) {
|
||||
Log::info('PaymentOrchestrator: Using requested provider', [
|
||||
'provider' => $providerName,
|
||||
]);
|
||||
|
||||
return $provider;
|
||||
}
|
||||
}
|
||||
|
||||
// Find the first active provider that supports this plan
|
||||
foreach ($this->providerRegistry->getActiveProviders() as $provider) {
|
||||
Log::info('PaymentOrchestrator: Checking fallback provider', [
|
||||
'provider' => $provider->getName(),
|
||||
'supported' => $this->isProviderSupportedForPlan($provider, $plan),
|
||||
]);
|
||||
|
||||
if ($this->isProviderSupportedForPlan($provider, $plan)) {
|
||||
Log::info('PaymentOrchestrator: Using fallback provider', [
|
||||
'provider' => $provider->getName(),
|
||||
]);
|
||||
|
||||
return $provider;
|
||||
}
|
||||
}
|
||||
@@ -625,10 +653,13 @@ class PaymentOrchestrator
|
||||
*/
|
||||
protected function isProviderSupportedForPlan(PaymentProviderContract $provider, Plan $plan): bool
|
||||
{
|
||||
// Check if plan has provider-specific configuration
|
||||
$providerConfig = $plan->details['providers'][$provider->getName()] ?? null;
|
||||
// Use the same approach as Plan::supportsProvider() - check database relationship
|
||||
$isSupported = $plan->planProviders()
|
||||
->where('provider', $provider->getName())
|
||||
->where('is_enabled', true)
|
||||
->exists();
|
||||
|
||||
if (! $providerConfig || ! ($providerConfig['enabled'] ?? false)) {
|
||||
if (! $isSupported) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Services\Payments;
|
||||
|
||||
use App\Contracts\Payments\PaymentProviderContract;
|
||||
use App\Models\PaymentProvider as PaymentProviderModel;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
@@ -15,8 +16,8 @@ class ProviderRegistry
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->loadConfigurations();
|
||||
$this->registerDefaultProviders();
|
||||
$this->loadConfigurationsFromDatabase();
|
||||
$this->registerProvidersFromDatabase();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -93,14 +94,30 @@ class ProviderRegistry
|
||||
*/
|
||||
public function updateConfiguration(string $providerName, array $config): void
|
||||
{
|
||||
$this->configurations[$providerName] = $config;
|
||||
try {
|
||||
// Update database - encode as JSON string since the model expects to cast it
|
||||
PaymentProviderModel::where('name', $providerName)
|
||||
->update(['configuration' => $config]); // Laravel will automatically cast array to JSON
|
||||
|
||||
Cache::put("payment_config_{$providerName}", $config);
|
||||
// Update local cache
|
||||
$this->configurations[$providerName] = array_merge(
|
||||
$this->configurations[$providerName] ?? [],
|
||||
['config' => $config]
|
||||
);
|
||||
|
||||
Log::info('Payment provider configuration updated', [
|
||||
'provider' => $providerName,
|
||||
'config_keys' => array_keys($config),
|
||||
]);
|
||||
// Clear main cache
|
||||
Cache::forget('payment_providers_config');
|
||||
|
||||
Log::info('Payment provider configuration updated', [
|
||||
'provider' => $providerName,
|
||||
'config_keys' => array_keys($config),
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Failed to update payment provider configuration', [
|
||||
'provider' => $providerName,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -237,47 +254,80 @@ class ProviderRegistry
|
||||
}
|
||||
|
||||
/**
|
||||
* Load provider configurations from cache/database
|
||||
* Load provider configurations from database
|
||||
*/
|
||||
protected function loadConfigurations(): void
|
||||
protected function loadConfigurationsFromDatabase(): void
|
||||
{
|
||||
// Load from cache first
|
||||
$cachedConfigs = Cache::get('payment_providers_config', []);
|
||||
try {
|
||||
// Load from cache first
|
||||
$cachedConfigs = Cache::get('payment_providers_config', []);
|
||||
|
||||
if (empty($cachedConfigs)) {
|
||||
// Load from database or config files
|
||||
$this->configurations = config('payment.providers', []);
|
||||
if (empty($cachedConfigs)) {
|
||||
// Load from database
|
||||
$providers = PaymentProviderModel::where('is_active', true)->get();
|
||||
|
||||
// Cache for 1 hour
|
||||
Cache::put('payment_providers_config', $this->configurations, 3600);
|
||||
} else {
|
||||
$this->configurations = $cachedConfigs;
|
||||
$this->configurations = [];
|
||||
foreach ($providers as $provider) {
|
||||
// Configuration is already cast to array by the model
|
||||
$configData = $provider->configuration ?? [];
|
||||
|
||||
$this->configurations[$provider->name] = [
|
||||
'enabled' => $provider->is_active,
|
||||
'display_name' => $provider->display_name,
|
||||
'class' => $configData['class'] ?? null,
|
||||
'config' => $configData,
|
||||
'supports_recurring' => $provider->supports_recurring,
|
||||
'supports_one_time' => $provider->supports_one_time,
|
||||
'supported_currencies' => $provider->supported_currencies ?? [],
|
||||
'fee_structure' => $provider->fee_structure ?? [],
|
||||
'priority' => $provider->priority,
|
||||
'is_fallback' => $provider->is_fallback,
|
||||
];
|
||||
}
|
||||
|
||||
// Cache for 1 hour
|
||||
Cache::put('payment_providers_config', $this->configurations, 3600);
|
||||
} else {
|
||||
$this->configurations = $cachedConfigs;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Failed to load payment provider configurations from database', [
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
$this->configurations = [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register default providers
|
||||
* Register providers from database
|
||||
*/
|
||||
protected function registerDefaultProviders(): void
|
||||
protected function registerProvidersFromDatabase(): void
|
||||
{
|
||||
// Auto-register providers based on configuration
|
||||
$enabledProviders = config('payment.enabled_providers', []);
|
||||
try {
|
||||
$activeProviders = PaymentProviderModel::where('is_active', true)->get();
|
||||
|
||||
foreach ($enabledProviders as $providerName) {
|
||||
$this->registerProviderByName($providerName);
|
||||
foreach ($activeProviders as $providerModel) {
|
||||
$this->registerProviderFromModel($providerModel);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Failed to register payment providers from database', [
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register provider by name using configuration
|
||||
* Register provider from database model
|
||||
*/
|
||||
protected function registerProviderByName(string $providerName): void
|
||||
protected function registerProviderFromModel(PaymentProviderModel $providerModel): void
|
||||
{
|
||||
$providerClass = config("payment.providers.{$providerName}.class");
|
||||
// Configuration is already cast to array by the model
|
||||
$configData = $providerModel->configuration ?? [];
|
||||
$providerClass = $configData['class'] ?? null;
|
||||
|
||||
if (! $providerClass || ! class_exists($providerClass)) {
|
||||
Log::error('Payment provider class not found', [
|
||||
'provider' => $providerName,
|
||||
'provider' => $providerModel->name,
|
||||
'class' => $providerClass,
|
||||
]);
|
||||
|
||||
@@ -285,42 +335,120 @@ class ProviderRegistry
|
||||
}
|
||||
|
||||
try {
|
||||
$config = $this->getConfiguration($providerName);
|
||||
// Use the full configuration data for the provider
|
||||
$config = $this->getConfiguration($providerModel->name);
|
||||
$provider = new $providerClass($config);
|
||||
|
||||
if ($provider instanceof PaymentProviderContract) {
|
||||
$this->register($providerName, $provider);
|
||||
$this->register($providerModel->name, $provider);
|
||||
} else {
|
||||
Log::error('Payment provider does not implement contract', [
|
||||
'provider' => $providerName,
|
||||
'provider' => $providerModel->name,
|
||||
'class' => $providerClass,
|
||||
]);
|
||||
}
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Failed to register payment provider', [
|
||||
'provider' => $providerName,
|
||||
'provider' => $providerModel->name,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get provider class name based on provider name from database
|
||||
*/
|
||||
protected function getProviderClass(string $providerName): ?string
|
||||
{
|
||||
$config = $this->getConfiguration($providerName);
|
||||
|
||||
return $config['class'] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get API key from database configuration
|
||||
*/
|
||||
protected function getApiKeyFromConfig(string $providerName): ?string
|
||||
{
|
||||
$config = $this->getConfiguration($providerName);
|
||||
$configData = $config['config'] ?? [];
|
||||
|
||||
// Try different possible API key field names
|
||||
$apiKeyFields = [
|
||||
'secret_key',
|
||||
'api_key',
|
||||
'merchant_api_key',
|
||||
'private_key',
|
||||
'key',
|
||||
];
|
||||
|
||||
foreach ($apiKeyFields as $field) {
|
||||
if (isset($configData[$field]) && ! empty($configData[$field])) {
|
||||
return $configData[$field];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get webhook secret from database configuration
|
||||
*/
|
||||
protected function getWebhookSecretFromConfig(string $providerName): ?string
|
||||
{
|
||||
$config = $this->getConfiguration($providerName);
|
||||
$configData = $config['config'] ?? [];
|
||||
|
||||
// Try different possible webhook secret field names
|
||||
$webhookFields = [
|
||||
'webhook_secret',
|
||||
'secret',
|
||||
'signing_secret',
|
||||
];
|
||||
|
||||
foreach ($webhookFields as $field) {
|
||||
if (isset($configData[$field]) && ! empty($configData[$field])) {
|
||||
return $configData[$field];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh provider (useful for configuration changes)
|
||||
*/
|
||||
public function refreshProvider(string $name): bool
|
||||
{
|
||||
if (! isset($this->providers[$name])) {
|
||||
try {
|
||||
// Clear cache to force reload from database
|
||||
Cache::forget('payment_providers_config');
|
||||
|
||||
// Unregister current instance
|
||||
unset($this->providers[$name]);
|
||||
|
||||
// Reload configurations from database
|
||||
$this->loadConfigurationsFromDatabase();
|
||||
|
||||
// Re-register from database
|
||||
$providerModel = PaymentProviderModel::where('name', $name)
|
||||
->where('is_active', true)
|
||||
->first();
|
||||
|
||||
if ($providerModel) {
|
||||
$this->registerProviderFromModel($providerModel);
|
||||
}
|
||||
|
||||
return isset($this->providers[$name]);
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Failed to refresh payment provider', [
|
||||
'provider' => $name,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Unregister current instance
|
||||
unset($this->providers[$name]);
|
||||
|
||||
// Re-register with fresh configuration
|
||||
$this->registerProviderByName($name);
|
||||
|
||||
return isset($this->providers[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -328,17 +456,29 @@ class ProviderRegistry
|
||||
*/
|
||||
public function toggleProvider(string $name, bool $enabled): bool
|
||||
{
|
||||
$config = $this->getConfiguration($name);
|
||||
try {
|
||||
// Update database
|
||||
$updated = PaymentProviderModel::where('name', $name)
|
||||
->update(['is_active' => $enabled]);
|
||||
|
||||
if (! $updated) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Clear cache to force reload
|
||||
Cache::forget('payment_providers_config');
|
||||
|
||||
// Refresh the provider to apply changes
|
||||
return $this->refreshProvider($name);
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Failed to toggle payment provider', [
|
||||
'provider' => $name,
|
||||
'enabled' => $enabled,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
|
||||
if (empty($config)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$config['enabled'] = $enabled;
|
||||
$this->updateConfiguration($name, $config);
|
||||
|
||||
// Refresh the provider to apply changes
|
||||
return $this->refreshProvider($name);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -346,17 +486,36 @@ class ProviderRegistry
|
||||
*/
|
||||
public function getFallbackProvider(): ?PaymentProviderContract
|
||||
{
|
||||
$fallbackProvider = config('payment.fallback_provider');
|
||||
try {
|
||||
// First try to get the provider marked as fallback in database
|
||||
$fallbackProvider = PaymentProviderModel::where('is_fallback', true)
|
||||
->where('is_active', true)
|
||||
->first();
|
||||
|
||||
if ($fallbackProvider && $this->has($fallbackProvider)) {
|
||||
$provider = $this->get($fallbackProvider);
|
||||
|
||||
if ($provider && $provider->isActive()) {
|
||||
return $provider;
|
||||
if ($fallbackProvider && $this->has($fallbackProvider->name)) {
|
||||
$provider = $this->get($fallbackProvider->name);
|
||||
if ($provider && $provider->isActive()) {
|
||||
return $provider;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Return first active provider as fallback
|
||||
return $this->getActiveProviders()->first();
|
||||
// Fallback to environment variable
|
||||
$fallbackProviderName = env('PAYMENT_FALLBACK_PROVIDER', 'stripe');
|
||||
if ($this->has($fallbackProviderName)) {
|
||||
$provider = $this->get($fallbackProviderName);
|
||||
if ($provider && $provider->isActive()) {
|
||||
return $provider;
|
||||
}
|
||||
}
|
||||
|
||||
// Return first active provider as final fallback
|
||||
return $this->getActiveProviders()->first();
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Failed to get fallback provider', [
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
|
||||
return $this->getActiveProviders()->first();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user