57 lines
1.9 KiB
PHP
57 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
use App\Models\EventPurchase;
|
|
use App\Models\Tenant;
|
|
|
|
class PayPalWebhookController extends Controller
|
|
{
|
|
public function handle(Request $request)
|
|
{
|
|
$input = $request->all();
|
|
$ipnMessage = $input['ipn_track_id'] ?? null;
|
|
$payerEmail = $input['payer_email'] ?? null;
|
|
$paymentStatus = $input['payment_status'] ?? null;
|
|
$mcGross = $input['mc_gross'] ?? 0;
|
|
$packageId = $input['custom'] ?? null;
|
|
|
|
if ($paymentStatus === 'Completed' && $mcGross > 0) {
|
|
// Verify IPN with PayPal (simplified; use SDK for full verification)
|
|
// $verified = $this->verifyIPN($input);
|
|
|
|
// Find or create tenant (for public checkout, perhaps create new or use session)
|
|
// For now, assume tenant_id from custom or session
|
|
$tenantId = $packageId ? Tenant::where('slug', $packageId)->first()->id ?? 1 : 1;
|
|
|
|
// Create purchase and increment credits
|
|
$purchase = EventPurchase::create([
|
|
'tenant_id' => $tenantId, // Implement tenant resolution
|
|
'events_purchased' => $mcGross / 49, // Example: 49€ per event credit
|
|
'amount' => $mcGross,
|
|
'currency' => $input['mc_currency'] ?? 'EUR',
|
|
'provider' => 'paypal',
|
|
'external_receipt_id' => $ipnMessage,
|
|
'status' => 'completed',
|
|
'purchased_at' => now(),
|
|
]);
|
|
|
|
$tenant = Tenant::find($tenantId);
|
|
$tenant->incrementCredits($purchase->events_purchased, 'paypal_purchase', 'PayPal IPN', $purchase->id);
|
|
|
|
Log::info('PayPal IPN processed', $input);
|
|
}
|
|
|
|
return response('OK', 200);
|
|
}
|
|
|
|
private function verifyIPN($input)
|
|
{
|
|
// Use PayPal SDK to verify
|
|
// Return true/false
|
|
return true; // Placeholder
|
|
}
|
|
}
|