|
@@ -26,6 +26,7 @@ use App\Models\Schedule;
|
|
|
use App\Services\Pagarme\Concerns\FormatsPagarmeData;
|
|
use App\Services\Pagarme\Concerns\FormatsPagarmeData;
|
|
|
use App\Services\Pagarme\Concerns\MocksPagarmeRequests;
|
|
use App\Services\Pagarme\Concerns\MocksPagarmeRequests;
|
|
|
use App\Services\Pagarme\Concerns\SendsPagarmeRequests;
|
|
use App\Services\Pagarme\Concerns\SendsPagarmeRequests;
|
|
|
|
|
+use Illuminate\Support\Collection;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
|
@@ -155,6 +156,85 @@ class PagarmePaymentService
|
|
|
);
|
|
);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public function processCartPayment(
|
|
|
|
|
+ Payment $payment,
|
|
|
|
|
+ Collection $schedules,
|
|
|
|
|
+ string $paymentMethod,
|
|
|
|
|
+ ?string $cardId = null,
|
|
|
|
|
+ array $options = [],
|
|
|
|
|
+ ): array {
|
|
|
|
|
+ $firstSchedule = $schedules->first();
|
|
|
|
|
+
|
|
|
|
|
+ if (! $firstSchedule) {
|
|
|
|
|
+ throw new \InvalidArgumentException('Carrinho precisa ter ao menos um agendamento.');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $items = $schedules
|
|
|
|
|
+ ->map(fn (Schedule $schedule) => $this->buildOrderItem(
|
|
|
|
|
+ schedule: $schedule,
|
|
|
|
|
+ grossAmount: (float) data_get($schedule, 'payment_gross_amount', $schedule->total_amount),
|
|
|
|
|
+ ))
|
|
|
|
|
+ ->values()
|
|
|
|
|
+ ->all();
|
|
|
|
|
+
|
|
|
|
|
+ $customer = $this->buildCustomer($firstSchedule, $options);
|
|
|
|
|
+ $split = $this->buildSplit($payment, $options);
|
|
|
|
|
+
|
|
|
|
|
+ $metadataOptions = [
|
|
|
|
|
+ 'metadata' => [
|
|
|
|
|
+ 'schedule_ids' => $schedules->pluck('id')->implode(','),
|
|
|
|
|
+ ],
|
|
|
|
|
+ ];
|
|
|
|
|
+
|
|
|
|
|
+ $splitOptions = ['split' => $split];
|
|
|
|
|
+
|
|
|
|
|
+ $pixOptions = config('services.pagarme.pix_disable_split')
|
|
|
|
|
+ ? $metadataOptions
|
|
|
|
|
+ : array_merge($metadataOptions, $splitOptions);
|
|
|
|
|
+
|
|
|
|
|
+ $creditCardOptions = array_merge($metadataOptions, $splitOptions);
|
|
|
|
|
+
|
|
|
|
|
+ if ($paymentMethod === 'credit_card') {
|
|
|
|
|
+ $creditCard = new CreditCardData(
|
|
|
|
|
+ cardId: $cardId,
|
|
|
|
|
+ installments: $payment->installments,
|
|
|
|
|
+ statementDescriptor: Str::limit((string) config('app.name', 'SOFTPAR'), 13, ''),
|
|
|
|
|
+ operationType: 'auth_and_capture',
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ $result = $this->createOrderWithCreditCard(
|
|
|
|
|
+ payment: $payment,
|
|
|
|
|
+ items: $items,
|
|
|
|
|
+ customer: $customer,
|
|
|
|
|
+ creditCard: $creditCard,
|
|
|
|
|
+ options: $creditCardOptions,
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ $this->logCreditCardOrderResult($payment, $result, 'create_cart_order');
|
|
|
|
|
+
|
|
|
|
|
+ return $result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $pixData = new PixData(
|
|
|
|
|
+ expiresIn: 1800,
|
|
|
|
|
+
|
|
|
|
|
+ additionalInformation: [
|
|
|
|
|
+ new PixAdditionalInformationData(
|
|
|
|
|
+ name: 'Agendamentos',
|
|
|
|
|
+ value: $schedules->pluck('id')->implode(','),
|
|
|
|
|
+ ),
|
|
|
|
|
+ ],
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ return $this->createOrderWithPix(
|
|
|
|
|
+ payment: $payment,
|
|
|
|
|
+ items: $items,
|
|
|
|
|
+ customer: $customer,
|
|
|
|
|
+ pix: $pixData,
|
|
|
|
|
+ options: $pixOptions,
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public function createOrderWithCreditCard(
|
|
public function createOrderWithCreditCard(
|
|
|
Payment $payment,
|
|
Payment $payment,
|
|
|
array $items,
|
|
array $items,
|
|
@@ -204,12 +284,14 @@ class PagarmePaymentService
|
|
|
PaymentData $paymentMethod,
|
|
PaymentData $paymentMethod,
|
|
|
array $options = []
|
|
array $options = []
|
|
|
): array {
|
|
): array {
|
|
|
- $metadata = array_merge([
|
|
|
|
|
|
|
+ $metadata = array_filter([
|
|
|
'payment_id' => (string) $payment->id,
|
|
'payment_id' => (string) $payment->id,
|
|
|
- 'schedule_id' => (string) $payment->schedule_id,
|
|
|
|
|
|
|
+ 'schedule_id' => $payment->schedule_id ? (string) $payment->schedule_id : null,
|
|
|
'client_id' => (string) $payment->client_id,
|
|
'client_id' => (string) $payment->client_id,
|
|
|
- 'provider_id' => (string) $payment->provider_id,
|
|
|
|
|
- ], $options['metadata'] ?? []);
|
|
|
|
|
|
|
+ 'provider_id' => $payment->provider_id ? (string) $payment->provider_id : null,
|
|
|
|
|
+ ], fn ($value) => $value !== null);
|
|
|
|
|
+
|
|
|
|
|
+ $metadata = array_merge($metadata, $options['metadata'] ?? []);
|
|
|
|
|
|
|
|
$requestData = new OrderRequestData(
|
|
$requestData = new OrderRequestData(
|
|
|
code: $payment->ensureGatewayCode(),
|
|
code: $payment->ensureGatewayCode(),
|
|
@@ -474,16 +556,21 @@ class PagarmePaymentService
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private function buildOrderItems(Schedule $schedule, float $grossAmount): array
|
|
private function buildOrderItems(Schedule $schedule, float $grossAmount): array
|
|
|
|
|
+ {
|
|
|
|
|
+ return [$this->buildOrderItem($schedule, $grossAmount)];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private function buildOrderItem(Schedule $schedule, float $grossAmount): ItemData
|
|
|
{
|
|
{
|
|
|
$description = $schedule->customSchedule?->serviceType?->description
|
|
$description = $schedule->customSchedule?->serviceType?->description
|
|
|
?? "Servico {$schedule->id}";
|
|
?? "Servico {$schedule->id}";
|
|
|
|
|
|
|
|
- return [new ItemData(
|
|
|
|
|
|
|
+ return new ItemData(
|
|
|
code: "schedule-{$schedule->id}",
|
|
code: "schedule-{$schedule->id}",
|
|
|
amount: OrderRequestData::amountInCents($grossAmount),
|
|
amount: OrderRequestData::amountInCents($grossAmount),
|
|
|
quantity: 1,
|
|
quantity: 1,
|
|
|
description: $description,
|
|
description: $description,
|
|
|
- )];
|
|
|
|
|
|
|
+ );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private function buildPhonePayload(?string $phone): ?array
|
|
private function buildPhonePayload(?string $phone): ?array
|