|
@@ -2,15 +2,16 @@
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
|
|
+use App\Enums\PaymentSplitStatusEnum;
|
|
|
|
|
+use App\Enums\PaymentStatusEnum;
|
|
|
use App\Models\Address;
|
|
use App\Models\Address;
|
|
|
use App\Models\ClientPaymentMethod;
|
|
use App\Models\ClientPaymentMethod;
|
|
|
use App\Models\Payment;
|
|
use App\Models\Payment;
|
|
|
-use App\Models\PaymentTransfer;
|
|
|
|
|
|
|
+use App\Models\PaymentSplit;
|
|
|
use App\Models\Schedule;
|
|
use App\Models\Schedule;
|
|
|
use App\Services\Pagarme\PagarmePaymentService;
|
|
use App\Services\Pagarme\PagarmePaymentService;
|
|
|
use Carbon\Carbon;
|
|
use Carbon\Carbon;
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
-use Illuminate\Support\Facades\Log;
|
|
|
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
|
|
class PaymentService
|
|
class PaymentService
|
|
@@ -92,19 +93,24 @@ class PaymentService
|
|
|
|
|
|
|
|
$existingPayment = Payment::query()
|
|
$existingPayment = Payment::query()
|
|
|
->where('schedule_id', $schedule->id)
|
|
->where('schedule_id', $schedule->id)
|
|
|
- ->whereIn('status', ['pending', 'processing', 'authorized', 'paid'])
|
|
|
|
|
|
|
+ ->whereIn('status', [
|
|
|
|
|
+ PaymentStatusEnum::PENDING->value,
|
|
|
|
|
+ PaymentStatusEnum::PROCESSING->value,
|
|
|
|
|
+ PaymentStatusEnum::AUTHORIZED->value,
|
|
|
|
|
+ PaymentStatusEnum::PAID->value,
|
|
|
|
|
+ ])
|
|
|
->latest('id')
|
|
->latest('id')
|
|
|
->first();
|
|
->first();
|
|
|
|
|
|
|
|
if ($existingPayment) {
|
|
if ($existingPayment) {
|
|
|
if ($this->isIncompleteGatewayPayment($existingPayment)) {
|
|
if ($this->isIncompleteGatewayPayment($existingPayment)) {
|
|
|
$existingPayment->forceFill([
|
|
$existingPayment->forceFill([
|
|
|
- 'status' => 'failed',
|
|
|
|
|
|
|
+ 'status' => PaymentStatusEnum::FAILED,
|
|
|
'failed_at' => now(),
|
|
'failed_at' => now(),
|
|
|
'failure_message' => 'Pagamento pendente sem retorno do gateway.',
|
|
'failure_message' => 'Pagamento pendente sem retorno do gateway.',
|
|
|
])->save();
|
|
])->save();
|
|
|
} else {
|
|
} else {
|
|
|
- if ($existingPayment->payment_method !== $paymentMethod && $existingPayment->status !== 'paid') {
|
|
|
|
|
|
|
+ if ($existingPayment->payment_method !== $paymentMethod && $existingPayment->status !== PaymentStatusEnum::PAID) {
|
|
|
throw new \InvalidArgumentException('Ja existe um pagamento em andamento para este agendamento.');
|
|
throw new \InvalidArgumentException('Ja existe um pagamento em andamento para este agendamento.');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -142,9 +148,9 @@ class PaymentService
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$serviceAmount = (float) $schedule->total_amount;
|
|
$serviceAmount = (float) $schedule->total_amount;
|
|
|
- $platformFee = round($serviceAmount * 0.11, 2);
|
|
|
|
|
- $grossAmount = round($serviceAmount + $platformFee, 2);
|
|
|
|
|
- $items = $this->buildOrderItems($schedule, $grossAmount);
|
|
|
|
|
|
|
+ $platformFee = round($serviceAmount * 0.11, 2);
|
|
|
|
|
+ $grossAmount = round($serviceAmount + $platformFee, 2);
|
|
|
|
|
+ $items = $this->buildOrderItems($schedule, $grossAmount);
|
|
|
|
|
|
|
|
$this->ensureCustomerPhoneForPayment($schedule, $options);
|
|
$this->ensureCustomerPhoneForPayment($schedule, $options);
|
|
|
|
|
|
|
@@ -164,7 +170,7 @@ class PaymentService
|
|
|
'gateway_provider' => 'pagarme',
|
|
'gateway_provider' => 'pagarme',
|
|
|
'gateway_code' => 'payment-'.(string) Str::uuid(),
|
|
'gateway_code' => 'payment-'.(string) Str::uuid(),
|
|
|
'payment_method' => $paymentMethod,
|
|
'payment_method' => $paymentMethod,
|
|
|
- 'status' => 'pending',
|
|
|
|
|
|
|
+ 'status' => PaymentStatusEnum::PENDING,
|
|
|
'gross_amount' => $grossAmount,
|
|
'gross_amount' => $grossAmount,
|
|
|
'gateway_fee_amount' => 0,
|
|
'gateway_fee_amount' => 0,
|
|
|
'platform_fee_amount' => $platformFee,
|
|
'platform_fee_amount' => $platformFee,
|
|
@@ -179,13 +185,13 @@ class PaymentService
|
|
|
],
|
|
],
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
|
- $transfer = PaymentTransfer::create([
|
|
|
|
|
|
|
+ $transfer = PaymentSplit::create([
|
|
|
'payment_id' => $payment->id,
|
|
'payment_id' => $payment->id,
|
|
|
'provider_id' => $schedule->provider_id,
|
|
'provider_id' => $schedule->provider_id,
|
|
|
'gateway_provider' => 'pagarme',
|
|
'gateway_provider' => 'pagarme',
|
|
|
'gateway_transfer_target_reference' => $schedule->provider->recipient_id,
|
|
'gateway_transfer_target_reference' => $schedule->provider->recipient_id,
|
|
|
'gateway_transfer_target_label' => 'recipient',
|
|
'gateway_transfer_target_label' => 'recipient',
|
|
|
- 'status' => 'pending',
|
|
|
|
|
|
|
+ 'status' => PaymentSplitStatusEnum::PENDING,
|
|
|
'gross_amount' => $serviceAmount,
|
|
'gross_amount' => $serviceAmount,
|
|
|
'gateway_fee_amount' => 0,
|
|
'gateway_fee_amount' => 0,
|
|
|
'net_amount' => $serviceAmount,
|
|
'net_amount' => $serviceAmount,
|
|
@@ -251,12 +257,12 @@ class PaymentService
|
|
|
);
|
|
);
|
|
|
} catch (\Throwable $e) {
|
|
} catch (\Throwable $e) {
|
|
|
$payment->forceFill([
|
|
$payment->forceFill([
|
|
|
- 'status' => 'failed',
|
|
|
|
|
|
|
+ 'status' => PaymentStatusEnum::FAILED,
|
|
|
'failed_at' => now(),
|
|
'failed_at' => now(),
|
|
|
'failure_message' => $e->getMessage(),
|
|
'failure_message' => $e->getMessage(),
|
|
|
])->save();
|
|
])->save();
|
|
|
|
|
|
|
|
- $transfer->update(['status' => 'failed']);
|
|
|
|
|
|
|
+ $transfer->update(['status' => PaymentSplitStatusEnum::FAILED]);
|
|
|
|
|
|
|
|
throw $e;
|
|
throw $e;
|
|
|
}
|
|
}
|
|
@@ -288,8 +294,8 @@ class PaymentService
|
|
|
array $options = [],
|
|
array $options = [],
|
|
|
bool $requirePhone = true
|
|
bool $requirePhone = true
|
|
|
): array {
|
|
): array {
|
|
|
- $client = $schedule->client;
|
|
|
|
|
- $user = $client->user()->first(['id', 'name', 'email', 'phone']);
|
|
|
|
|
|
|
+ $client = $schedule->client;
|
|
|
|
|
+ $user = $client->user()->first(['id', 'name', 'email', 'phone']);
|
|
|
$address = Address::with(['city.state', 'state'])->find($schedule->address_id);
|
|
$address = Address::with(['city.state', 'state'])->find($schedule->address_id);
|
|
|
|
|
|
|
|
foreach ([
|
|
foreach ([
|
|
@@ -311,8 +317,8 @@ class PaymentService
|
|
|
$phone = $this->buildPhonePayload($user->phone)
|
|
$phone = $this->buildPhonePayload($user->phone)
|
|
|
?: $this->buildPhonePayload($options['phone'] ?? null);
|
|
?: $this->buildPhonePayload($options['phone'] ?? null);
|
|
|
|
|
|
|
|
- $state = $address->state?->code ?? $address->city?->state?->code;
|
|
|
|
|
- $city = $address->city?->name;
|
|
|
|
|
|
|
+ $state = $address->state?->code ?? $address->city?->state?->code;
|
|
|
|
|
+ $city = $address->city?->name;
|
|
|
$zipCode = $this->digits($address->zip_code);
|
|
$zipCode = $this->digits($address->zip_code);
|
|
|
|
|
|
|
|
$line1 = implode(', ', array_filter([
|
|
$line1 = implode(', ', array_filter([
|
|
@@ -382,7 +388,7 @@ class PaymentService
|
|
|
private function ensureCustomerPhoneForPayment(Schedule $schedule, array $options = []): void
|
|
private function ensureCustomerPhoneForPayment(Schedule $schedule, array $options = []): void
|
|
|
{
|
|
{
|
|
|
$userPhone = $schedule->client?->user?->phone;
|
|
$userPhone = $schedule->client?->user?->phone;
|
|
|
- $phone = $this->buildPhonePayload($userPhone)
|
|
|
|
|
|
|
+ $phone = $this->buildPhonePayload($userPhone)
|
|
|
?: $this->buildPhonePayload($options['phone'] ?? null);
|
|
?: $this->buildPhonePayload($options['phone'] ?? null);
|
|
|
|
|
|
|
|
if ($phone) {
|
|
if ($phone) {
|
|
@@ -401,7 +407,7 @@ class PaymentService
|
|
|
|
|
|
|
|
private function isIncompleteGatewayPayment(Payment $payment): bool
|
|
private function isIncompleteGatewayPayment(Payment $payment): bool
|
|
|
{
|
|
{
|
|
|
- return $payment->status === 'pending'
|
|
|
|
|
|
|
+ return $payment->status === PaymentStatusEnum::PENDING
|
|
|
&& empty($payment->gateway_entity_reference)
|
|
&& empty($payment->gateway_entity_reference)
|
|
|
&& empty($payment->gateway_operation_reference)
|
|
&& empty($payment->gateway_operation_reference)
|
|
|
&& empty($payment->gateway_payload);
|
|
&& empty($payment->gateway_payload);
|
|
@@ -422,7 +428,7 @@ class PaymentService
|
|
|
|
|
|
|
|
public function syncScheduleStatusAfterPayment(Schedule $schedule, Payment $payment): void
|
|
public function syncScheduleStatusAfterPayment(Schedule $schedule, Payment $payment): void
|
|
|
{
|
|
{
|
|
|
- if ($payment->status !== 'paid' || $schedule->status === 'paid') {
|
|
|
|
|
|
|
+ if ($payment->status !== PaymentStatusEnum::PAID || $schedule->status === 'paid') {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|