|
|
@@ -2,19 +2,19 @@
|
|
|
|
|
|
namespace App\Services\Pagarme;
|
|
|
|
|
|
-use App\Data\Pagarme\Request\CustomerRequestData\CustomerAddressRequestData;
|
|
|
-use App\Data\Pagarme\Request\CustomerRequestData\CustomerPhonesRequestData\CustomerPhoneData;
|
|
|
-use App\Data\Pagarme\Request\CustomerRequestData\CustomerPhonesRequestData\CustomerPhonesRequestData;
|
|
|
-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\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\Data\Pagarme\Customer\CustomerRequestData;
|
|
|
+use App\Data\Pagarme\Customer\Parts\Request\AddressData;
|
|
|
+use App\Data\Pagarme\Customer\Parts\Request\PhoneData;
|
|
|
+use App\Data\Pagarme\Customer\Parts\Request\PhonesData;
|
|
|
+use App\Data\Pagarme\Order\OrderRequestData;
|
|
|
+use App\Data\Pagarme\Order\OrderResponseData;
|
|
|
+use App\Data\Pagarme\Order\Parts\Request\CreditCardData;
|
|
|
+use App\Data\Pagarme\Order\Parts\Request\ItemData;
|
|
|
+use App\Data\Pagarme\Order\Parts\Request\PaymentData;
|
|
|
+use App\Data\Pagarme\Order\Parts\Request\PixAdditionalInformationData;
|
|
|
+use App\Data\Pagarme\Order\Parts\Request\PixData;
|
|
|
+use App\Data\Pagarme\Order\Parts\Request\SplitData;
|
|
|
+use App\Data\Pagarme\Order\Parts\Request\SplitOptionsData;
|
|
|
use App\Enums\PaymentSplitStatusEnum;
|
|
|
use App\Enums\PaymentStatusEnum;
|
|
|
use App\Models\Address;
|
|
|
@@ -35,6 +35,50 @@ class PagarmePaymentService
|
|
|
private readonly PagarmeAnticipationService $anticipationService,
|
|
|
) {}
|
|
|
|
|
|
+ public function calculatePaymentAmounts(float $serviceAmount, string $paymentMethod): array
|
|
|
+ {
|
|
|
+ $installments = 1;
|
|
|
+
|
|
|
+ if ($serviceAmount <= 0) {
|
|
|
+ throw new \InvalidArgumentException('Valor do servico precisa ser maior que zero.');
|
|
|
+ }
|
|
|
+
|
|
|
+ if (! in_array($paymentMethod, ['credit_card', 'pix'], true)) {
|
|
|
+ throw new \InvalidArgumentException('Forma de pagamento invalida.');
|
|
|
+ }
|
|
|
+
|
|
|
+ $platformFeeRate = (float) config('services.pagarme.platform_fee_rate', 0.11);
|
|
|
+
|
|
|
+ $anticipationFeeRate = $paymentMethod === 'credit_card'
|
|
|
+ ? (float) config('services.pagarme.anticipation_fee_rate', 0.0311)
|
|
|
+ : 0.0;
|
|
|
+
|
|
|
+ $anticipationFeeRate = $anticipationFeeRate * $installments;
|
|
|
+
|
|
|
+ if ($anticipationFeeRate >= 1) {
|
|
|
+ throw new \InvalidArgumentException('Taxa de antecipacao invalida para calcular valor do pagamento.');
|
|
|
+ }
|
|
|
+
|
|
|
+ $platformFee = round($serviceAmount * $platformFeeRate, 2);
|
|
|
+ $desiredSplitAmount = round($serviceAmount + $platformFee, 2);
|
|
|
+
|
|
|
+ $grossAmount = $anticipationFeeRate > 0
|
|
|
+ ? $this->roundMoneyUp($desiredSplitAmount / (1 - $anticipationFeeRate))
|
|
|
+ : $desiredSplitAmount;
|
|
|
+
|
|
|
+ if ($platformFee > 0 && empty(config('services.pagarme.platform_recipient_id'))) {
|
|
|
+ throw new \InvalidArgumentException('PAGARME_PLATFORM_RECIPIENT_ID precisa estar configurado para receber a taxa da plataforma no split.');
|
|
|
+ }
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'service_amount' => round($serviceAmount, 2),
|
|
|
+ 'platform_fee_amount' => $platformFee,
|
|
|
+ 'desired_split_amount' => $desiredSplitAmount,
|
|
|
+ 'anticipation_fee_amount' => round($grossAmount - $desiredSplitAmount, 2),
|
|
|
+ 'gross_amount' => $grossAmount,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
public function processPayment(
|
|
|
Payment $payment,
|
|
|
Schedule $schedule,
|
|
|
@@ -55,9 +99,9 @@ class PagarmePaymentService
|
|
|
$orderOptions = array_merge(['split' => $split], $pixOptions);
|
|
|
|
|
|
if ($paymentMethod === 'credit_card') {
|
|
|
- $creditCard = new OrderCreditCardData(
|
|
|
+ $creditCard = new CreditCardData(
|
|
|
cardId: $cardId,
|
|
|
- installments: 1,
|
|
|
+ installments: $payment->installments,
|
|
|
statementDescriptor: Str::limit((string) config('app.name', 'SOFTPAR'), 13, ''),
|
|
|
operationType: 'auth_and_capture',
|
|
|
);
|
|
|
@@ -75,11 +119,11 @@ class PagarmePaymentService
|
|
|
return $result;
|
|
|
}
|
|
|
|
|
|
- $pixData = new OrderPixData(
|
|
|
+ $pixData = new PixData(
|
|
|
expiresIn: 1800,
|
|
|
|
|
|
additionalInformation: [
|
|
|
- new OrderPixAdditionalInformationData(
|
|
|
+ new PixAdditionalInformationData(
|
|
|
name: 'Agendamento',
|
|
|
value: (string) $schedule->id,
|
|
|
),
|
|
|
@@ -99,7 +143,7 @@ class PagarmePaymentService
|
|
|
Payment $payment,
|
|
|
array $items,
|
|
|
CustomerRequestData $customer,
|
|
|
- OrderCreditCardData $creditCard,
|
|
|
+ CreditCardData $creditCard,
|
|
|
array $options = []
|
|
|
): array {
|
|
|
return $this->createOrder(
|
|
|
@@ -120,7 +164,7 @@ class PagarmePaymentService
|
|
|
Payment $payment,
|
|
|
array $items,
|
|
|
CustomerRequestData $customer,
|
|
|
- OrderPixData $pix,
|
|
|
+ PixData $pix,
|
|
|
array $options = []
|
|
|
): array {
|
|
|
return $this->createOrder(
|
|
|
@@ -141,7 +185,7 @@ class PagarmePaymentService
|
|
|
Payment $payment,
|
|
|
array $items,
|
|
|
CustomerRequestData $customer,
|
|
|
- OrderPaymentData $paymentMethod,
|
|
|
+ PaymentData $paymentMethod,
|
|
|
array $options = []
|
|
|
): array {
|
|
|
$metadata = array_merge([
|
|
|
@@ -275,7 +319,7 @@ class PagarmePaymentService
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- $customerAddress = new CustomerAddressRequestData(
|
|
|
+ $customerAddress = new AddressData(
|
|
|
line1: $line1,
|
|
|
line2: $address->complement ?: $address->instructions,
|
|
|
zipCode: $zipCode,
|
|
|
@@ -287,8 +331,8 @@ class PagarmePaymentService
|
|
|
$customerPhones = null;
|
|
|
|
|
|
if ($phone) {
|
|
|
- $customerPhones = new CustomerPhonesRequestData(
|
|
|
- mobilePhone: new CustomerPhoneData(
|
|
|
+ $customerPhones = new PhonesData(
|
|
|
+ mobilePhone: new PhoneData(
|
|
|
countryCode: $phone['country_code'],
|
|
|
areaCode: $phone['area_code'],
|
|
|
number: $phone['number'],
|
|
|
@@ -313,7 +357,7 @@ class PagarmePaymentService
|
|
|
$description = $schedule->customSchedule?->serviceType?->description
|
|
|
?? "Servico {$schedule->id}";
|
|
|
|
|
|
- return [new OrderItemData(
|
|
|
+ return [new ItemData(
|
|
|
code: "schedule-{$schedule->id}",
|
|
|
amount: OrderRequestData::amountInCents($grossAmount),
|
|
|
quantity: 1,
|
|
|
@@ -340,6 +384,11 @@ class PagarmePaymentService
|
|
|
];
|
|
|
}
|
|
|
|
|
|
+ private function roundMoneyUp(float $amount): float
|
|
|
+ {
|
|
|
+ return ceil($amount * 100) / 100;
|
|
|
+ }
|
|
|
+
|
|
|
private function buildSplit(Payment $payment, array $options): array
|
|
|
{
|
|
|
$transfers = PaymentSplit::query()
|
|
|
@@ -357,19 +406,19 @@ class PagarmePaymentService
|
|
|
$orderAmountCents = OrderRequestData::amountInCents((float) $payment->gross_amount);
|
|
|
|
|
|
$providerTotalCents = array_sum(array_map(
|
|
|
- static fn (OrderSplitData $s) => $s->amount,
|
|
|
+ static fn (SplitData $s) => $s->amount,
|
|
|
$split,
|
|
|
));
|
|
|
|
|
|
$platformAmountCents = $orderAmountCents - $providerTotalCents;
|
|
|
|
|
|
if ($platformAmountCents > 0) {
|
|
|
- $split[] = new OrderSplitData(
|
|
|
+ $split[] = new SplitData(
|
|
|
amount: $platformAmountCents,
|
|
|
recipientId: $platformRecipientId,
|
|
|
type: 'flat',
|
|
|
|
|
|
- options: new OrderSplitOptionsData(
|
|
|
+ options: new SplitOptionsData(
|
|
|
chargeProcessingFee: true,
|
|
|
chargeRemainderFee: true,
|
|
|
liable: true,
|