107 lines
3.4 KiB
PHP
107 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\PackagePurchase;
|
|
use App\Models\Tenant;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Stripe\Stripe;
|
|
use Stripe\PaymentIntent;
|
|
use Stripe\Subscription;
|
|
|
|
class StripeController extends Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
Stripe::setApiKey(config('services.stripe.secret'));
|
|
}
|
|
|
|
public function createPaymentIntent(Request $request)
|
|
{
|
|
$request->validate([
|
|
'tenant_id' => 'required|exists:tenants,id',
|
|
'package_id' => 'required|exists:packages,id',
|
|
'amount' => 'required|numeric|min:0',
|
|
]);
|
|
|
|
$tenant = Tenant::findOrFail($request->tenant_id);
|
|
$package = \App\Models\Package::findOrFail($request->package_id);
|
|
|
|
$paymentIntent = PaymentIntent::create([
|
|
'amount' => $request->amount * 100, // cents
|
|
'currency' => 'eur',
|
|
'metadata' => [
|
|
'tenant_id' => $tenant->id,
|
|
'package_id' => $package->id,
|
|
'type' => 'endcustomer_event', // or reseller
|
|
],
|
|
]);
|
|
|
|
return response()->json([
|
|
'client_secret' => $paymentIntent->client_secret,
|
|
]);
|
|
}
|
|
|
|
public function createSubscription(Request $request)
|
|
{
|
|
$request->validate([
|
|
'tenant_id' => 'required|exists:tenants,id',
|
|
'package_id' => 'required|exists:packages,id',
|
|
'payment_method_id' => 'required',
|
|
]);
|
|
|
|
$tenant = Tenant::findOrFail($request->tenant_id);
|
|
$package = \App\Models\Package::findOrFail($request->package_id);
|
|
|
|
$subscription = Subscription::create([
|
|
'customer' => $tenant->stripe_customer_id ?? $this->createCustomer($tenant),
|
|
'items' => [[
|
|
'price' => $package->stripe_price_id, // Assume package has stripe_price_id
|
|
]],
|
|
'payment_method' => $request->payment_method_id,
|
|
'default_payment_method' => $request->payment_method_id,
|
|
'expand' => ['latest_invoice.payment_intent'],
|
|
'metadata' => [
|
|
'tenant_id' => $tenant->id,
|
|
'package_id' => $package->id,
|
|
'type' => 'reseller_subscription',
|
|
],
|
|
]);
|
|
|
|
// Create TenantPackage and PackagePurchase
|
|
\App\Models\TenantPackage::create([
|
|
'tenant_id' => $tenant->id,
|
|
'package_id' => $package->id,
|
|
'purchased_at' => now(),
|
|
'expires_at' => now()->addYear(),
|
|
'active' => true,
|
|
]);
|
|
|
|
PackagePurchase::create([
|
|
'tenant_id' => $tenant->id,
|
|
'package_id' => $package->id,
|
|
'provider_id' => $subscription->id,
|
|
'price' => $package->price,
|
|
'type' => 'reseller_subscription',
|
|
'purchased_at' => now(),
|
|
]);
|
|
|
|
return response()->json([
|
|
'subscription_id' => $subscription->id,
|
|
'client_secret' => $subscription->latest_invoice->payment_intent->client_secret ?? null,
|
|
]);
|
|
}
|
|
|
|
private function createCustomer(Tenant $tenant)
|
|
{
|
|
$customer = \Stripe\Customer::create([
|
|
'email' => $tenant->email,
|
|
'metadata' => ['tenant_id' => $tenant->id],
|
|
]);
|
|
|
|
$tenant->update(['stripe_customer_id' => $customer->id]);
|
|
|
|
return $customer->id;
|
|
}
|
|
} |