|
@@ -32,10 +32,6 @@ class PagarmePaymentService
|
|
|
use FormatsPagarmeData;
|
|
use FormatsPagarmeData;
|
|
|
use SendsPagarmeRequests;
|
|
use SendsPagarmeRequests;
|
|
|
|
|
|
|
|
- public function __construct(
|
|
|
|
|
- private readonly PagarmeAnticipationService $anticipationService,
|
|
|
|
|
- ) {}
|
|
|
|
|
-
|
|
|
|
|
public function calculatePaymentAmounts(float $serviceAmount, string $paymentMethod): array
|
|
public function calculatePaymentAmounts(float $serviceAmount, string $paymentMethod): array
|
|
|
{
|
|
{
|
|
|
if ($serviceAmount <= 0) {
|
|
if ($serviceAmount <= 0) {
|
|
@@ -115,10 +111,6 @@ class PagarmePaymentService
|
|
|
return $result;
|
|
return $result;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $anticipationLimits = $this->anticipationService->fetchAnticipationLimitsForPayment($payment);
|
|
|
|
|
-
|
|
|
|
|
- $this->anticipationService->createBulkAnticipation($payment, $anticipationLimits);
|
|
|
|
|
-
|
|
|
|
|
return $result;
|
|
return $result;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -246,6 +238,10 @@ class PagarmePaymentService
|
|
|
|
|
|
|
|
$gatewayFee = $gatewayFeeCents > 0 ? round($gatewayFeeCents / 100, 2) : 0;
|
|
$gatewayFee = $gatewayFeeCents > 0 ? round($gatewayFeeCents / 100, 2) : 0;
|
|
|
|
|
|
|
|
|
|
+ $gatewayPayload = $newStatus === PaymentStatusEnum::FAILED
|
|
|
|
|
+ ? $this->normalizeFailedGatewayPayload($orderResponse, $failureCode, $failureMessage)
|
|
|
|
|
+ : $orderResponse;
|
|
|
|
|
+
|
|
|
$payment->forceFill([
|
|
$payment->forceFill([
|
|
|
'gateway_provider' => 'pagarme',
|
|
'gateway_provider' => 'pagarme',
|
|
|
'gateway_entity_reference' => $order->gatewayEntityReference(),
|
|
'gateway_entity_reference' => $order->gatewayEntityReference(),
|
|
@@ -255,7 +251,7 @@ class PagarmePaymentService
|
|
|
'status' => $newStatus,
|
|
'status' => $newStatus,
|
|
|
'paid_at' => $order->paidAt(),
|
|
'paid_at' => $order->paidAt(),
|
|
|
'authorized_at' => $order->authorizedAt(),
|
|
'authorized_at' => $order->authorizedAt(),
|
|
|
- 'gateway_payload' => $orderResponse,
|
|
|
|
|
|
|
+ 'gateway_payload' => $gatewayPayload,
|
|
|
'gateway_fee_amount' => $gatewayFee,
|
|
'gateway_fee_amount' => $gatewayFee,
|
|
|
'failure_code' => $failureCode,
|
|
'failure_code' => $failureCode,
|
|
|
'failure_message' => $failureMessage,
|
|
'failure_message' => $failureMessage,
|
|
@@ -377,6 +373,7 @@ class PagarmePaymentService
|
|
|
$order = OrderResponseData::fromArray($orderResponse);
|
|
$order = OrderResponseData::fromArray($orderResponse);
|
|
|
$charge = $order->firstCharge();
|
|
$charge = $order->firstCharge();
|
|
|
$transaction = $order->lastTransaction();
|
|
$transaction = $order->lastTransaction();
|
|
|
|
|
+ $failureCode = $order->failureCode();
|
|
|
|
|
|
|
|
Log::channel('pagarme')->info('Pagar.me credit card order result', [
|
|
Log::channel('pagarme')->info('Pagar.me credit card order result', [
|
|
|
'source' => $source,
|
|
'source' => $source,
|
|
@@ -388,13 +385,59 @@ class PagarmePaymentService
|
|
|
'charge_status' => $charge?->status,
|
|
'charge_status' => $charge?->status,
|
|
|
'transaction_id' => $transaction?->id,
|
|
'transaction_id' => $transaction?->id,
|
|
|
'transaction_status' => $transaction?->status,
|
|
'transaction_status' => $transaction?->status,
|
|
|
- 'failure_code' => $order->failureCode(),
|
|
|
|
|
|
|
+ 'failure_code' => $failureCode,
|
|
|
'failure_message' => $order->failureMessage(),
|
|
'failure_message' => $order->failureMessage(),
|
|
|
'acquirer_message' => $transaction?->acquirerMessage,
|
|
'acquirer_message' => $transaction?->acquirerMessage,
|
|
|
- 'gateway_response' => $transaction?->gatewayResponse,
|
|
|
|
|
|
|
+
|
|
|
|
|
+ 'gateway_response' => $this->normalizeGatewayResponseForFailure(
|
|
|
|
|
+ $transaction?->gatewayResponse ?? [],
|
|
|
|
|
+ $failureCode,
|
|
|
|
|
+ ),
|
|
|
]);
|
|
]);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private function normalizeFailedGatewayPayload(array $payload, ?string $failureCode, ?string $failureMessage): array
|
|
|
|
|
+ {
|
|
|
|
|
+ $payload['failure_code'] = $failureCode;
|
|
|
|
|
+ $payload['failure_message'] = $failureMessage;
|
|
|
|
|
+
|
|
|
|
|
+ if (isset($payload['charges'][0]['last_transaction']['gateway_response'])
|
|
|
|
|
+ && is_array($payload['charges'][0]['last_transaction']['gateway_response'])) {
|
|
|
|
|
+ $payload['charges'][0]['last_transaction']['gateway_response'] = $this->normalizeGatewayResponseForFailure(
|
|
|
|
|
+ $payload['charges'][0]['last_transaction']['gateway_response'],
|
|
|
|
|
+ $failureCode,
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return $payload;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private function normalizeGatewayResponseForFailure(array $gatewayResponse, ?string $failureCode): array
|
|
|
|
|
+ {
|
|
|
|
|
+ $code = $gatewayResponse['code'] ?? null;
|
|
|
|
|
+
|
|
|
|
|
+ if (! $failureCode || ! $this->isMisleadingGatewayCode($code)) {
|
|
|
|
|
+ return $gatewayResponse;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $gatewayResponse['raw_code'] = $code;
|
|
|
|
|
+ $gatewayResponse['code'] = $failureCode;
|
|
|
|
|
+
|
|
|
|
|
+ return $gatewayResponse;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private function isMisleadingGatewayCode(mixed $code): bool
|
|
|
|
|
+ {
|
|
|
|
|
+ if ($code === null || $code === '') {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $code = mb_strtolower((string) $code);
|
|
|
|
|
+
|
|
|
|
|
+ return preg_match('/^[1-2]\d{2}$/', $code) === 1
|
|
|
|
|
+ || in_array($code, ['00', '0', 'approved', 'success'], true);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private function buildPhonePayload(?string $phone): ?array
|
|
private function buildPhonePayload(?string $phone): ?array
|
|
|
{
|
|
{
|
|
|
$digits = $this->digits($phone);
|
|
$digits = $this->digits($phone);
|