Intégration fonctionnalites V1
This commit is contained in:
+152
-34
@@ -6,12 +6,14 @@ use App\Models\PaymentTransaction;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
use Stripe\Exception\ApiErrorException;
|
||||
|
||||
class PaymentService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly WalletService $walletService,
|
||||
private readonly AuditService $auditService,
|
||||
private readonly StripePaymentService $stripePaymentService,
|
||||
) {}
|
||||
|
||||
public function initiateTopUp(
|
||||
@@ -20,8 +22,13 @@ class PaymentService
|
||||
string $idempotencyKey,
|
||||
?string $returnUrl = null,
|
||||
): PaymentTransaction {
|
||||
if ($amount <= 0) {
|
||||
throw new \InvalidArgumentException('Le montant doit être strictement positif.');
|
||||
$minAmount = (float) config('laverie.payment.top_up_min', 5);
|
||||
$maxAmount = (float) config('laverie.payment.top_up_max', 150);
|
||||
|
||||
if ($amount < $minAmount || $amount > $maxAmount) {
|
||||
throw new \InvalidArgumentException(
|
||||
"Le montant doit être compris entre {$minAmount} et {$maxAmount} EUR.",
|
||||
);
|
||||
}
|
||||
|
||||
$existing = PaymentTransaction::query()
|
||||
@@ -32,21 +39,49 @@ class PaymentService
|
||||
return $existing;
|
||||
}
|
||||
|
||||
$payment = PaymentTransaction::query()->create([
|
||||
'uuid' => (string) Str::uuid(),
|
||||
'user_id' => $user->id,
|
||||
'provider' => config('laverie.payment.default_provider', 'simulated'),
|
||||
'provider_payment_id' => 'sim-'.Str::random(16),
|
||||
'amount' => $amount,
|
||||
'currency' => 'EUR',
|
||||
'status' => 'initiated',
|
||||
'idempotency_key' => $idempotencyKey,
|
||||
'return_url' => $returnUrl,
|
||||
]);
|
||||
$provider = config('laverie.payment.default_provider', 'simulated');
|
||||
|
||||
$this->auditService->log($user, 'payment.initiated', $payment, null, $payment->toArray());
|
||||
if ($provider === 'stripe' && ! $this->stripePaymentService->isEnabled()) {
|
||||
throw new \RuntimeException('Stripe n\'est pas configuré.');
|
||||
}
|
||||
|
||||
return $payment;
|
||||
return DB::transaction(function () use ($user, $amount, $idempotencyKey, $returnUrl, $provider) {
|
||||
$payment = PaymentTransaction::query()->create([
|
||||
'uuid' => (string) Str::uuid(),
|
||||
'user_id' => $user->id,
|
||||
'provider' => $provider,
|
||||
'provider_payment_id' => null,
|
||||
'amount' => $amount,
|
||||
'currency' => 'EUR',
|
||||
'status' => 'initiated',
|
||||
'idempotency_key' => $idempotencyKey,
|
||||
'return_url' => $returnUrl,
|
||||
]);
|
||||
|
||||
if ($provider === 'stripe') {
|
||||
try {
|
||||
$intent = $this->stripePaymentService->createPaymentIntent($user, $payment, $amount);
|
||||
} catch (ApiErrorException $e) {
|
||||
throw new \RuntimeException('Impossible de créer le paiement Stripe.', 0, $e);
|
||||
}
|
||||
|
||||
$payment->update([
|
||||
'provider_payment_id' => $intent->id,
|
||||
'status' => 'pending',
|
||||
'raw_payload' => [
|
||||
'client_secret' => $intent->client_secret,
|
||||
],
|
||||
]);
|
||||
} else {
|
||||
$payment->update([
|
||||
'provider_payment_id' => 'sim-'.Str::random(16),
|
||||
]);
|
||||
}
|
||||
|
||||
$this->auditService->log($user, 'payment.initiated', $payment, null, $payment->fresh()->toArray());
|
||||
|
||||
return $payment->fresh();
|
||||
});
|
||||
}
|
||||
|
||||
public function confirmTopUp(string $paymentUuid): PaymentTransaction
|
||||
@@ -65,26 +100,29 @@ class PaymentService
|
||||
throw new \RuntimeException("Paiement non confirmable (statut : {$payment->status}).");
|
||||
}
|
||||
|
||||
$payment->update(['status' => 'succeeded']);
|
||||
if ($payment->provider === 'stripe') {
|
||||
if (blank($payment->provider_payment_id)) {
|
||||
throw new \RuntimeException('Paiement Stripe introuvable.');
|
||||
}
|
||||
|
||||
$this->walletService->credit(
|
||||
$payment->user,
|
||||
(float) $payment->amount,
|
||||
PaymentTransaction::class,
|
||||
$payment->id,
|
||||
"payment-credit:{$payment->uuid}",
|
||||
['provider' => $payment->provider],
|
||||
);
|
||||
try {
|
||||
$intent = $this->stripePaymentService->retrievePaymentIntent($payment->provider_payment_id);
|
||||
} catch (ApiErrorException $e) {
|
||||
throw new \RuntimeException('Impossible de vérifier le paiement Stripe.', 0, $e);
|
||||
}
|
||||
|
||||
$this->auditService->log(
|
||||
$payment->user,
|
||||
'payment.confirmed',
|
||||
$payment,
|
||||
null,
|
||||
$payment->fresh()->toArray(),
|
||||
);
|
||||
if (! $this->stripePaymentService->isPaymentSucceeded($intent)) {
|
||||
throw new \RuntimeException('Le paiement Stripe n\'est pas encore confirmé.');
|
||||
}
|
||||
|
||||
return $payment->fresh();
|
||||
$payment->update([
|
||||
'raw_payload' => array_merge($payment->raw_payload ?? [], [
|
||||
'stripe' => $this->stripePaymentService->summarizePaymentIntent($intent),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->markPaymentSucceeded($payment->fresh());
|
||||
});
|
||||
}
|
||||
|
||||
@@ -98,6 +136,10 @@ class PaymentService
|
||||
throw new \InvalidArgumentException('Webhook paiement invalide.');
|
||||
}
|
||||
|
||||
if ($status === 'ignored') {
|
||||
return;
|
||||
}
|
||||
|
||||
DB::transaction(function () use ($provider, $payload, $providerPaymentId, $idempotencyKey, $status) {
|
||||
$query = PaymentTransaction::query()->lockForUpdate();
|
||||
|
||||
@@ -119,14 +161,90 @@ class PaymentService
|
||||
}
|
||||
|
||||
$payment->update([
|
||||
'raw_payload' => $payload,
|
||||
'raw_payload' => array_merge($payment->raw_payload ?? [], [
|
||||
'webhook' => $payload,
|
||||
]),
|
||||
]);
|
||||
|
||||
if ($status === 'succeeded') {
|
||||
$this->confirmTopUp($payment->uuid);
|
||||
$this->markPaymentSucceeded($payment);
|
||||
} elseif (in_array($status, ['failed', 'cancelled', 'refunded'], true)) {
|
||||
$payment->update(['status' => $status]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function handleStripeWebhook(string $payload, string $signature): void
|
||||
{
|
||||
$parsed = $this->stripePaymentService->parseWebhook($payload, $signature);
|
||||
|
||||
if ($parsed['status'] === 'ignored') {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->handleWebhook('stripe', [
|
||||
'provider_payment_id' => $parsed['provider_payment_id'],
|
||||
'status' => $parsed['status'],
|
||||
'payload' => $parsed['payload'],
|
||||
]);
|
||||
}
|
||||
|
||||
private function markPaymentSucceeded(PaymentTransaction $payment): PaymentTransaction
|
||||
{
|
||||
if ($payment->status === 'succeeded') {
|
||||
return $payment;
|
||||
}
|
||||
|
||||
$payment->update(['status' => 'succeeded']);
|
||||
|
||||
$this->walletService->credit(
|
||||
$payment->user,
|
||||
(float) $payment->amount,
|
||||
PaymentTransaction::class,
|
||||
$payment->id,
|
||||
"payment-credit:{$payment->uuid}",
|
||||
$this->buildWalletCreditMetadata($payment),
|
||||
);
|
||||
|
||||
$this->auditService->log(
|
||||
$payment->user,
|
||||
'payment.confirmed',
|
||||
$payment,
|
||||
null,
|
||||
$payment->fresh()->toArray(),
|
||||
);
|
||||
|
||||
return $payment->fresh();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function buildWalletCreditMetadata(PaymentTransaction $payment): array
|
||||
{
|
||||
$metadata = [
|
||||
'operation' => 'top_up',
|
||||
'provider' => $payment->provider,
|
||||
'label' => match ($payment->provider) {
|
||||
'stripe' => 'Rechargement par carte',
|
||||
'simulated' => 'Rechargement (simulation)',
|
||||
default => 'Rechargement portefeuille',
|
||||
},
|
||||
'payment_uuid' => $payment->uuid,
|
||||
'provider_payment_id' => $payment->provider_payment_id,
|
||||
'currency' => $payment->currency,
|
||||
'amount' => (float) $payment->amount,
|
||||
];
|
||||
|
||||
$raw = $payment->raw_payload ?? [];
|
||||
|
||||
if ($payment->provider === 'stripe') {
|
||||
$metadata['stripe'] = $raw['stripe'] ?? [
|
||||
'payment_intent_id' => $payment->provider_payment_id,
|
||||
'status' => 'succeeded',
|
||||
];
|
||||
}
|
||||
|
||||
return $metadata;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user