|
|
@@ -111,6 +111,16 @@ class PaymentService
|
|
|
'failed_at' => now(),
|
|
|
'failure_message' => 'Pagamento pendente sem retorno do gateway.',
|
|
|
])->save();
|
|
|
+ } elseif ($this->isExpiredPixPayment($existingPayment)) {
|
|
|
+ $existingPayment->forceFill([
|
|
|
+ 'status' => PaymentStatusEnum::FAILED,
|
|
|
+ 'failed_at' => now(),
|
|
|
+ 'failure_message' => 'Pagamento Pix expirado.',
|
|
|
+ ])->save();
|
|
|
+
|
|
|
+ PaymentSplit::query()
|
|
|
+ ->where('payment_id', $existingPayment->id)
|
|
|
+ ->update(['status' => PaymentSplitStatusEnum::FAILED]);
|
|
|
} else {
|
|
|
if ($existingPayment->payment_method !== $paymentMethod && $existingPayment->status !== PaymentStatusEnum::PAID) {
|
|
|
throw new \InvalidArgumentException('Ja existe um pagamento em andamento para este agendamento.');
|
|
|
@@ -229,8 +239,70 @@ class PaymentService
|
|
|
return $payment;
|
|
|
}
|
|
|
|
|
|
+ public function getOrCreatePixPayment(Schedule $schedule): Payment
|
|
|
+ {
|
|
|
+ $existingPayment = Payment::query()
|
|
|
+ ->where('schedule_id', $schedule->id)
|
|
|
+ ->where('payment_method', 'pix')
|
|
|
+ ->whereIn('status', [
|
|
|
+ PaymentStatusEnum::PENDING->value,
|
|
|
+ PaymentStatusEnum::PROCESSING->value,
|
|
|
+ PaymentStatusEnum::AUTHORIZED->value,
|
|
|
+ PaymentStatusEnum::PAID->value,
|
|
|
+ ])
|
|
|
+ ->latest('id')
|
|
|
+ ->first();
|
|
|
+
|
|
|
+ if ($existingPayment && $this->isExpiredPixPayment($existingPayment)) {
|
|
|
+ $existingPayment->forceFill([
|
|
|
+ 'status' => PaymentStatusEnum::FAILED,
|
|
|
+ 'failed_at' => Carbon::now(),
|
|
|
+ 'failure_message' => 'Pagamento Pix expirado.',
|
|
|
+ ])->save();
|
|
|
+
|
|
|
+ PaymentSplit::query()
|
|
|
+ ->where('payment_id', $existingPayment->id)
|
|
|
+ ->update(['status' => PaymentSplitStatusEnum::FAILED]);
|
|
|
+
|
|
|
+ $existingPayment = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($existingPayment) {
|
|
|
+ if ($this->isIncompleteGatewayPayment($existingPayment)) {
|
|
|
+ $existingPayment->forceFill([
|
|
|
+ 'status' => PaymentStatusEnum::FAILED,
|
|
|
+ 'failed_at' => Carbon::now(),
|
|
|
+ 'failure_message' => 'Pagamento pendente sem retorno do gateway.',
|
|
|
+ ])->save();
|
|
|
+
|
|
|
+ PaymentSplit::query()
|
|
|
+ ->where('payment_id', $existingPayment->id)
|
|
|
+ ->update(['status' => PaymentSplitStatusEnum::FAILED]);
|
|
|
+ } else {
|
|
|
+ $this->syncScheduleStatusAfterPayment($schedule, $existingPayment);
|
|
|
+
|
|
|
+ return $existingPayment;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->payAcceptedSchedule(
|
|
|
+ schedule: $schedule,
|
|
|
+ paymentMethod: 'pix',
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
//
|
|
|
|
|
|
+ private function isExpiredPixPayment(Payment $payment): bool
|
|
|
+ {
|
|
|
+ if ($payment->payment_method !== 'pix') {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return $payment->expires_at !== null
|
|
|
+ && $payment->expires_at->isPast();
|
|
|
+ }
|
|
|
+
|
|
|
private function isIncompleteGatewayPayment(Payment $payment): bool
|
|
|
{
|
|
|
return $payment->status === PaymentStatusEnum::PENDING
|