|
|
@@ -9,10 +9,10 @@ use App\Data\Pagarme\Request\CustomerRequestData\CustomerRequestData;
|
|
|
use App\Data\Pagarme\Request\OrderRequestData\OrderItemData;
|
|
|
use App\Data\Pagarme\Request\OrderRequestData\OrderPaymentData\OrderCreditCardData;
|
|
|
use App\Data\Pagarme\Request\OrderRequestData\OrderPaymentData\OrderPaymentData;
|
|
|
-use App\Data\Pagarme\Request\OrderRequestData\OrderPaymentData\OrderPixAdditionalInformationData;
|
|
|
-use App\Data\Pagarme\Request\OrderRequestData\OrderPaymentData\OrderPixData;
|
|
|
-use App\Data\Pagarme\Request\OrderRequestData\OrderPaymentData\OrderSplitData;
|
|
|
-use App\Data\Pagarme\Request\OrderRequestData\OrderPaymentData\OrderSplitOptionsData;
|
|
|
+use App\Data\Pagarme\Request\OrderRequestData\OrderPaymentData\OrderPixData\OrderPixAdditionalInformationData;
|
|
|
+use App\Data\Pagarme\Request\OrderRequestData\OrderPaymentData\OrderPixData\OrderPixData;
|
|
|
+use App\Data\Pagarme\Request\OrderRequestData\OrderPaymentData\OrderSplitData\OrderSplitData;
|
|
|
+use App\Data\Pagarme\Request\OrderRequestData\OrderPaymentData\OrderSplitData\OrderSplitOptionsData;
|
|
|
use App\Data\Pagarme\Request\OrderRequestData\OrderRequestData;
|
|
|
use App\Data\Pagarme\Response\OrderResponseData\OrderResponseData;
|
|
|
use App\Enums\PaymentSplitStatusEnum;
|
|
|
@@ -31,17 +31,17 @@ class PagarmePaymentService
|
|
|
use SendsPagarmeRequests;
|
|
|
|
|
|
public function processPayment(
|
|
|
- Payment $payment,
|
|
|
+ Payment $payment,
|
|
|
Schedule $schedule,
|
|
|
- string $paymentMethod,
|
|
|
- ?string $cardId = null,
|
|
|
- array $options = [],
|
|
|
+ string $paymentMethod,
|
|
|
+ ?string $cardId = null,
|
|
|
+ array $options = [],
|
|
|
): array {
|
|
|
$grossAmount = (float) $payment->gross_amount;
|
|
|
|
|
|
- $items = $this->buildOrderItems($schedule, $grossAmount);
|
|
|
+ $items = $this->buildOrderItems($schedule, $grossAmount);
|
|
|
$customer = $this->buildCustomer($schedule, $options);
|
|
|
- $split = $this->buildSplit($payment, $options);
|
|
|
+ $split = $this->buildSplit($payment, $options);
|
|
|
|
|
|
$pixOptions = config('services.pagarme.pix_disable_split')
|
|
|
? []
|
|
|
@@ -51,84 +51,89 @@ class PagarmePaymentService
|
|
|
|
|
|
if ($paymentMethod === 'credit_card') {
|
|
|
$creditCard = new OrderCreditCardData(
|
|
|
- cardId: $cardId,
|
|
|
- installments: 1,
|
|
|
+ cardId: $cardId,
|
|
|
+ installments: 1,
|
|
|
statementDescriptor: Str::limit((string) config('app.name', 'SOFTPAR'), 13, ''),
|
|
|
- operationType: 'auth_and_capture',
|
|
|
+ operationType: 'auth_and_capture',
|
|
|
);
|
|
|
|
|
|
return $this->createOrderWithCreditCard(
|
|
|
- payment: $payment,
|
|
|
- items: $items,
|
|
|
- customer: $customer,
|
|
|
+ payment: $payment,
|
|
|
+ items: $items,
|
|
|
+ customer: $customer,
|
|
|
creditCard: $creditCard,
|
|
|
- options: $orderOptions,
|
|
|
+ options: $orderOptions,
|
|
|
);
|
|
|
}
|
|
|
|
|
|
$pixData = new OrderPixData(
|
|
|
expiresIn: 1800,
|
|
|
+
|
|
|
additionalInformation: [
|
|
|
new OrderPixAdditionalInformationData(
|
|
|
- name: 'Agendamento',
|
|
|
+ name: 'Agendamento',
|
|
|
value: (string) $schedule->id,
|
|
|
),
|
|
|
],
|
|
|
);
|
|
|
|
|
|
return $this->createOrderWithPix(
|
|
|
- payment: $payment,
|
|
|
- items: $items,
|
|
|
+ payment: $payment,
|
|
|
+ items: $items,
|
|
|
customer: $customer,
|
|
|
- pix: $pixData,
|
|
|
- options: $pixOptions,
|
|
|
+ pix: $pixData,
|
|
|
+ options: $pixOptions,
|
|
|
);
|
|
|
}
|
|
|
|
|
|
public function createOrderWithCreditCard(
|
|
|
- Payment $payment,
|
|
|
- array $items,
|
|
|
+ Payment $payment,
|
|
|
+ array $items,
|
|
|
CustomerRequestData $customer,
|
|
|
OrderCreditCardData $creditCard,
|
|
|
- array $options = []
|
|
|
+ array $options = []
|
|
|
): array {
|
|
|
return $this->createOrder(
|
|
|
- payment: $payment,
|
|
|
- items: $items,
|
|
|
+ payment: $payment,
|
|
|
+ items: $items,
|
|
|
customer: $customer,
|
|
|
+
|
|
|
paymentMethod: OrderRequestData::creditCardPaymentMethod(
|
|
|
creditCard: $creditCard,
|
|
|
- split: is_array($options['split'] ?? null) ? $options['split'] : null,
|
|
|
+ split: is_array($options['split'] ?? null) ? $options['split'] : null,
|
|
|
),
|
|
|
+
|
|
|
options: $options,
|
|
|
);
|
|
|
}
|
|
|
|
|
|
public function createOrderWithPix(
|
|
|
- Payment $payment,
|
|
|
- array $items,
|
|
|
+ Payment $payment,
|
|
|
+ array $items,
|
|
|
CustomerRequestData $customer,
|
|
|
- OrderPixData $pix,
|
|
|
- array $options = []
|
|
|
+ OrderPixData $pix,
|
|
|
+ array $options = []
|
|
|
): array {
|
|
|
return $this->createOrder(
|
|
|
- payment: $payment,
|
|
|
- items: $items,
|
|
|
+ payment: $payment,
|
|
|
+ items: $items,
|
|
|
customer: $customer,
|
|
|
+
|
|
|
paymentMethod: OrderRequestData::pixPaymentMethod(
|
|
|
- pix: $pix,
|
|
|
+ pix: $pix,
|
|
|
split: is_array($options['split'] ?? null) ? $options['split'] : null,
|
|
|
),
|
|
|
+
|
|
|
options: $options,
|
|
|
);
|
|
|
}
|
|
|
|
|
|
public function createOrder(
|
|
|
- Payment $payment,
|
|
|
- array $items,
|
|
|
+ Payment $payment,
|
|
|
+ array $items,
|
|
|
CustomerRequestData $customer,
|
|
|
- OrderPaymentData $paymentMethod,
|
|
|
- array $options = []
|
|
|
+ OrderPaymentData $paymentMethod,
|
|
|
+ array $options = []
|
|
|
): array {
|
|
|
$metadata = array_merge([
|
|
|
'payment_id' => (string) $payment->id,
|
|
|
@@ -138,22 +143,22 @@ class PagarmePaymentService
|
|
|
], $options['metadata'] ?? []);
|
|
|
|
|
|
$requestData = new OrderRequestData(
|
|
|
- code: $payment->ensureGatewayCode(),
|
|
|
- items: $items,
|
|
|
- payments: [$paymentMethod],
|
|
|
- metadata: $metadata,
|
|
|
- customer: $customer,
|
|
|
+ code: $payment->ensureGatewayCode(),
|
|
|
+ items: $items,
|
|
|
+ payments: [$paymentMethod],
|
|
|
+ metadata: $metadata,
|
|
|
+ customer: $customer,
|
|
|
customerId: $options['customer_id'] ?? null,
|
|
|
- closed: $options['closed'] ?? true,
|
|
|
- channel: $options['channel'] ?? null,
|
|
|
+ closed: $options['closed'] ?? true,
|
|
|
+ channel: $options['channel'] ?? null,
|
|
|
);
|
|
|
|
|
|
$order = OrderResponseData::fromArray($this->pagarmeRequest(
|
|
|
- method: 'POST',
|
|
|
- path: '/orders',
|
|
|
- payload: $requestData,
|
|
|
+ method: 'POST',
|
|
|
+ path: '/orders',
|
|
|
+ payload: $requestData,
|
|
|
idempotencyKey: $this->idempotencyKey($payment),
|
|
|
- errorMessage: 'Erro ao criar pedido de pagamento no Pagar.me.',
|
|
|
+ errorMessage: 'Erro ao criar pedido de pagamento no Pagar.me.',
|
|
|
));
|
|
|
|
|
|
$order->requireId();
|
|
|
@@ -166,12 +171,13 @@ class PagarmePaymentService
|
|
|
public function applyGatewayResponseToPayment(Payment $payment, array $orderResponse): Payment
|
|
|
{
|
|
|
$order = OrderResponseData::fromArray($orderResponse);
|
|
|
- $newStatus = $order->paymentStatus();
|
|
|
- $failureCode = null;
|
|
|
+
|
|
|
+ $newStatus = $order->paymentStatus();
|
|
|
+ $failureCode = null;
|
|
|
$failureMessage = null;
|
|
|
|
|
|
if ($newStatus === PaymentStatusEnum::FAILED) {
|
|
|
- $failureCode = $order->failureCode();
|
|
|
+ $failureCode = $order->failureCode();
|
|
|
$failureMessage = $order->failureMessage();
|
|
|
}
|
|
|
|
|
|
@@ -208,8 +214,8 @@ class PagarmePaymentService
|
|
|
|
|
|
private function buildCustomer(Schedule $schedule, array $options = []): CustomerRequestData
|
|
|
{
|
|
|
- $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);
|
|
|
|
|
|
foreach ([
|
|
|
@@ -231,8 +237,8 @@ class PagarmePaymentService
|
|
|
$phone = $this->buildPhonePayload($user->phone)
|
|
|
?: $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);
|
|
|
|
|
|
$line1 = implode(', ', array_filter([
|
|
|
@@ -255,11 +261,11 @@ class PagarmePaymentService
|
|
|
}
|
|
|
|
|
|
$customerAddress = new CustomerAddressRequestData(
|
|
|
- line1: $line1,
|
|
|
- line2: $address->complement ?: $address->instructions,
|
|
|
+ line1: $line1,
|
|
|
+ line2: $address->complement ?: $address->instructions,
|
|
|
zipCode: $zipCode,
|
|
|
- city: $city,
|
|
|
- state: $state,
|
|
|
+ city: $city,
|
|
|
+ state: $state,
|
|
|
country: 'BR',
|
|
|
);
|
|
|
|
|
|
@@ -269,21 +275,21 @@ class PagarmePaymentService
|
|
|
$customerPhones = new CustomerPhonesRequestData(
|
|
|
mobilePhone: new CustomerPhoneData(
|
|
|
countryCode: $phone['country_code'],
|
|
|
- areaCode: $phone['area_code'],
|
|
|
- number: $phone['number'],
|
|
|
+ areaCode: $phone['area_code'],
|
|
|
+ number: $phone['number'],
|
|
|
),
|
|
|
);
|
|
|
}
|
|
|
|
|
|
return new CustomerRequestData(
|
|
|
- name: $user->name,
|
|
|
- email: $user->email,
|
|
|
- document: $document,
|
|
|
- type: strlen($document) === 14 ? 'company' : 'individual',
|
|
|
+ name: $user->name,
|
|
|
+ email: $user->email,
|
|
|
+ document: $document,
|
|
|
+ type: strlen($document) === 14 ? 'company' : 'individual',
|
|
|
documentType: strlen($document) === 14 ? 'CNPJ' : 'CPF',
|
|
|
- code: "client-{$client->id}",
|
|
|
- address: $customerAddress,
|
|
|
- phones: $customerPhones,
|
|
|
+ code: "client-{$client->id}",
|
|
|
+ address: $customerAddress,
|
|
|
+ phones: $customerPhones,
|
|
|
);
|
|
|
}
|
|
|
|
|
|
@@ -293,9 +299,9 @@ class PagarmePaymentService
|
|
|
?? "Servico {$schedule->id}";
|
|
|
|
|
|
return [new OrderItemData(
|
|
|
- code: "schedule-{$schedule->id}",
|
|
|
- amount: OrderRequestData::amountInCents($grossAmount),
|
|
|
- quantity: 1,
|
|
|
+ code: "schedule-{$schedule->id}",
|
|
|
+ amount: OrderRequestData::amountInCents($grossAmount),
|
|
|
+ quantity: 1,
|
|
|
description: $description,
|
|
|
)];
|
|
|
}
|
|
|
@@ -333,13 +339,14 @@ class PagarmePaymentService
|
|
|
$platformRecipientId = config('services.pagarme.platform_recipient_id');
|
|
|
|
|
|
$split[] = new OrderSplitData(
|
|
|
- amount: OrderRequestData::amountInCents($platformFee),
|
|
|
+ amount: OrderRequestData::amountInCents($platformFee),
|
|
|
recipientId: $platformRecipientId,
|
|
|
- type: 'flat',
|
|
|
+ type: 'flat',
|
|
|
+
|
|
|
options: new OrderSplitOptionsData(
|
|
|
chargeProcessingFee: true,
|
|
|
- chargeRemainderFee: true,
|
|
|
- liable: true,
|
|
|
+ chargeRemainderFee: true,
|
|
|
+ liable: true,
|
|
|
),
|
|
|
);
|
|
|
}
|