|
|
@@ -3,7 +3,6 @@
|
|
|
namespace App\Services;
|
|
|
|
|
|
use App\Models\Address;
|
|
|
-use App\Models\Client;
|
|
|
use App\Models\ClientPaymentMethod;
|
|
|
use App\Models\Payment;
|
|
|
use App\Models\PaymentTransfer;
|
|
|
@@ -142,13 +141,12 @@ class PaymentService
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- $serviceAmount = (float) $schedule->total_amount;
|
|
|
- $platformFee = round($serviceAmount * 0.11, 2);
|
|
|
- $grossAmount = round($serviceAmount + $platformFee, 2);
|
|
|
- $items = $this->buildOrderItems($schedule, $grossAmount);
|
|
|
- $client = Client::find($schedule->client_id);
|
|
|
- $customerId = $client?->external_customer_id;
|
|
|
- $customer = empty($customerId) ? $this->buildCustomerPayload($schedule, $options) : [];
|
|
|
+ $serviceAmount = (float) $schedule->total_amount;
|
|
|
+ $platformFee = round($serviceAmount * 0.11, 2);
|
|
|
+ $grossAmount = round($serviceAmount + $platformFee, 2);
|
|
|
+ $items = $this->buildOrderItems($schedule, $grossAmount);
|
|
|
+ $this->ensureCustomerPhoneForPayment($schedule, $options);
|
|
|
+ $customer = $this->buildCustomerPayload(schedule: $schedule, options: $options, requirePhone: true);
|
|
|
$platformRecipientId = config('services.pagarme.platform_recipient_id');
|
|
|
|
|
|
if ($platformFee > 0 && empty($platformRecipientId)) {
|
|
|
@@ -234,8 +232,7 @@ class PaymentService
|
|
|
'operation_type' => 'auth_and_capture',
|
|
|
] + $creditCardReference,
|
|
|
options: [
|
|
|
- 'split' => $split,
|
|
|
- 'customer_id' => $customerId,
|
|
|
+ 'split' => $split,
|
|
|
],
|
|
|
)
|
|
|
: $this->pagarmePaymentService->createOrderWithPix(
|
|
|
@@ -246,8 +243,7 @@ class PaymentService
|
|
|
'expires_at' => $payment->expires_at?->toISOString(),
|
|
|
],
|
|
|
options: [
|
|
|
- 'split' => $split,
|
|
|
- 'customer_id' => $customerId,
|
|
|
+ 'split' => $split,
|
|
|
],
|
|
|
);
|
|
|
} catch (\Throwable $e) {
|
|
|
@@ -284,8 +280,11 @@ class PaymentService
|
|
|
]];
|
|
|
}
|
|
|
|
|
|
- private function buildCustomerPayload(Schedule $schedule, array $options = []): array
|
|
|
- {
|
|
|
+ private function buildCustomerPayload(
|
|
|
+ Schedule $schedule,
|
|
|
+ array $options = [],
|
|
|
+ bool $requirePhone = true
|
|
|
+ ): array {
|
|
|
$client = $schedule->client;
|
|
|
$user = $client->user()->first(['id', 'name', 'email', 'phone']);
|
|
|
$address = Address::with(['city.state', 'state'])->find($schedule->address_id);
|
|
|
@@ -319,14 +318,19 @@ class PaymentService
|
|
|
$address->district,
|
|
|
]));
|
|
|
|
|
|
- foreach ([
|
|
|
+ $requiredFields = [
|
|
|
'documento' => $document,
|
|
|
- 'telefone' => $phone,
|
|
|
'estado' => $state,
|
|
|
'cidade' => $city,
|
|
|
'cep' => $zipCode,
|
|
|
'endereco' => $line1,
|
|
|
- ] as $field => $value) {
|
|
|
+ ];
|
|
|
+
|
|
|
+ if ($requirePhone) {
|
|
|
+ $requiredFields['telefone'] = $phone;
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach ($requiredFields as $field => $value) {
|
|
|
if ($value === null || $value === '' || $value === []) {
|
|
|
throw new \InvalidArgumentException("Cliente precisa ter {$field} valido para criar pedido no Pagar.me.");
|
|
|
}
|
|
|
@@ -349,7 +353,7 @@ class PaymentService
|
|
|
'line_2' => $address->complement ?: $address->instructions,
|
|
|
],
|
|
|
|
|
|
- 'phones' => ['mobile_phone' => $phone],
|
|
|
+ 'phones' => $phone ? ['mobile_phone' => $phone] : null,
|
|
|
];
|
|
|
}
|
|
|
|
|
|
@@ -372,6 +376,21 @@ class PaymentService
|
|
|
];
|
|
|
}
|
|
|
|
|
|
+ private function ensureCustomerPhoneForPayment(Schedule $schedule, array $options = []): void
|
|
|
+ {
|
|
|
+ $userPhone = $schedule->client?->user?->phone;
|
|
|
+ $phone = $this->buildPhonePayload($userPhone)
|
|
|
+ ?: $this->buildPhonePayload($options['phone'] ?? null);
|
|
|
+
|
|
|
+ if ($phone) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ throw new \InvalidArgumentException(
|
|
|
+ 'Voce precisa cadastrar um numero de celular valido no seu perfil para concluir o pagamento.'
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
private function digits(?string $value): string
|
|
|
{
|
|
|
return preg_replace('/\D+/', '', (string) $value) ?? '';
|