PagarmePaymentService.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. <?php
  2. namespace App\Services\Pagarme;
  3. use App\Data\Pagarme\Customer\CustomerRequestData;
  4. use App\Data\Pagarme\Customer\Parts\Request\AddressData;
  5. use App\Data\Pagarme\Customer\Parts\Request\PhoneData;
  6. use App\Data\Pagarme\Customer\Parts\Request\PhonesData;
  7. use App\Data\Pagarme\Order\OrderRequestData;
  8. use App\Data\Pagarme\Order\OrderResponseData;
  9. use App\Data\Pagarme\Order\Parts\Request\CreditCardData;
  10. use App\Data\Pagarme\Order\Parts\Request\ItemData;
  11. use App\Data\Pagarme\Order\Parts\Request\PaymentData;
  12. use App\Data\Pagarme\Order\Parts\Request\PixAdditionalInformationData;
  13. use App\Data\Pagarme\Order\Parts\Request\PixData;
  14. use App\Data\Pagarme\Order\Parts\Request\SplitData;
  15. use App\Data\Pagarme\Order\Parts\Request\SplitOptionsData;
  16. use App\Enums\PaymentSplitStatusEnum;
  17. use App\Enums\PaymentStatusEnum;
  18. use App\Models\Address;
  19. use App\Models\Cart;
  20. use App\Models\Client;
  21. use App\Models\Payment;
  22. use App\Models\PaymentSplit;
  23. use App\Models\Schedule;
  24. use App\Services\Pagarme\Concerns\FormatsPagarmeData;
  25. use App\Services\Pagarme\Concerns\SendsPagarmeRequests;
  26. use Illuminate\Support\Facades\Log;
  27. use Illuminate\Support\Str;
  28. class PagarmePaymentService
  29. {
  30. use FormatsPagarmeData;
  31. use SendsPagarmeRequests;
  32. public function calculatePaymentAmounts(float $serviceAmount, string $paymentMethod, ?Schedule $schedule = null): array
  33. {
  34. if ($serviceAmount <= 0) {
  35. throw new \InvalidArgumentException('Valor do servico precisa ser maior que zero.');
  36. }
  37. if (! in_array($paymentMethod, ['credit_card', 'pix'], true)) {
  38. throw new \InvalidArgumentException('Forma de pagamento invalida.');
  39. }
  40. $platformFeeRate = $this->platformFeeRate($paymentMethod, $schedule);
  41. $platformFee = round($serviceAmount * $platformFeeRate, 2);
  42. $grossAmount = round($serviceAmount + $platformFee, 2);
  43. if ($platformFee > 0 && empty(config('services.pagarme.platform_recipient_id'))) {
  44. throw new \InvalidArgumentException('PAGARME_PLATFORM_RECIPIENT_ID precisa estar configurado para receber a taxa da plataforma no split.');
  45. }
  46. return [
  47. 'service_amount' => round($serviceAmount, 2),
  48. 'platform_fee_amount' => $platformFee,
  49. 'gross_amount' => $grossAmount,
  50. ];
  51. }
  52. public function platformFeeRates(): array
  53. {
  54. return [
  55. 'pix' => (float) config('services.pagarme.platform_pix_fee_rate'),
  56. 'credit_card' => (float) config('services.pagarme.platform_credit_card_fee_rate'),
  57. 'cart_min_3_schedules' => (float) config('services.pagarme.platform_cart_min_3_schedules_fee_rate'),
  58. ];
  59. }
  60. private function platformFeeRate(string $paymentMethod, ?Schedule $schedule = null): float
  61. {
  62. if ($schedule && $this->scheduleBelongsToCartWithAtLeastThreeItems($schedule)) {
  63. return (float) config('services.pagarme.platform_cart_min_3_schedules_fee_rate');
  64. }
  65. return $paymentMethod === 'credit_card'
  66. ? (float) config('services.pagarme.platform_credit_card_fee_rate')
  67. : (float) config('services.pagarme.platform_pix_fee_rate');
  68. }
  69. private function scheduleBelongsToCartWithAtLeastThreeItems(Schedule $schedule): bool
  70. {
  71. return Cart::query()
  72. ->whereHas('items', fn ($query) => $query->where('schedule_id', $schedule->id))
  73. ->whereHas('items', null, '>=', 3)
  74. ->exists();
  75. }
  76. public function processPayment(
  77. Payment $payment,
  78. Schedule $schedule,
  79. string $paymentMethod,
  80. ?string $cardId = null,
  81. array $options = [],
  82. ): array {
  83. $grossAmount = (float) $payment->gross_amount;
  84. $items = $this->buildOrderItems($schedule, $grossAmount);
  85. $customer = $this->buildCustomer($schedule, $options);
  86. $split = $this->buildSplit($payment, $options);
  87. $pixOptions = config('services.pagarme.pix_disable_split')
  88. ? []
  89. : ['split' => $split];
  90. $orderOptions = array_merge(['split' => $split], $pixOptions);
  91. if ($paymentMethod === 'credit_card') {
  92. $creditCard = new CreditCardData(
  93. cardId: $cardId,
  94. installments: $payment->installments,
  95. statementDescriptor: Str::limit((string) config('app.name', 'SOFTPAR'), 13, ''),
  96. operationType: 'auth_and_capture',
  97. );
  98. $result = $this->createOrderWithCreditCard(
  99. payment: $payment,
  100. items: $items,
  101. customer: $customer,
  102. creditCard: $creditCard,
  103. options: $orderOptions,
  104. );
  105. $this->logCreditCardOrderResult($payment, $result, 'create_order');
  106. $orderStatus = OrderResponseData::fromArray($result)->paymentStatus();
  107. if (! in_array($orderStatus, [PaymentStatusEnum::PAID, PaymentStatusEnum::AUTHORIZED], true)) {
  108. return $result;
  109. }
  110. return $result;
  111. }
  112. $pixData = new PixData(
  113. expiresIn: 1800,
  114. additionalInformation: [
  115. new PixAdditionalInformationData(
  116. name: 'Agendamento',
  117. value: (string) $schedule->id,
  118. ),
  119. ],
  120. );
  121. return $this->createOrderWithPix(
  122. payment: $payment,
  123. items: $items,
  124. customer: $customer,
  125. pix: $pixData,
  126. options: $pixOptions,
  127. );
  128. }
  129. public function createOrderWithCreditCard(
  130. Payment $payment,
  131. array $items,
  132. CustomerRequestData $customer,
  133. CreditCardData $creditCard,
  134. array $options = []
  135. ): array {
  136. return $this->createOrder(
  137. payment: $payment,
  138. items: $items,
  139. customer: $customer,
  140. paymentMethod: OrderRequestData::creditCardPaymentMethod(
  141. creditCard: $creditCard,
  142. split: is_array($options['split'] ?? null) ? $options['split'] : null,
  143. ),
  144. options: $options,
  145. );
  146. }
  147. public function createOrderWithPix(
  148. Payment $payment,
  149. array $items,
  150. CustomerRequestData $customer,
  151. PixData $pix,
  152. array $options = []
  153. ): array {
  154. return $this->createOrder(
  155. payment: $payment,
  156. items: $items,
  157. customer: $customer,
  158. paymentMethod: OrderRequestData::pixPaymentMethod(
  159. pix: $pix,
  160. split: is_array($options['split'] ?? null) ? $options['split'] : null,
  161. ),
  162. options: $options,
  163. );
  164. }
  165. public function createOrder(
  166. Payment $payment,
  167. array $items,
  168. CustomerRequestData $customer,
  169. PaymentData $paymentMethod,
  170. array $options = []
  171. ): array {
  172. $metadata = array_merge([
  173. 'payment_id' => (string) $payment->id,
  174. 'schedule_id' => (string) $payment->schedule_id,
  175. 'client_id' => (string) $payment->client_id,
  176. 'provider_id' => (string) $payment->provider_id,
  177. ], $options['metadata'] ?? []);
  178. $requestData = new OrderRequestData(
  179. code: $payment->ensureGatewayCode(),
  180. items: $items,
  181. payments: [$paymentMethod],
  182. metadata: $metadata,
  183. customer: $customer,
  184. customerId: $options['customer_id'] ?? null,
  185. closed: $options['closed'] ?? true,
  186. channel: $options['channel'] ?? null,
  187. );
  188. $order = OrderResponseData::fromArray($this->pagarmeRequest(
  189. method: 'POST',
  190. path: '/orders',
  191. payload: $requestData,
  192. idempotencyKey: $this->idempotencyKey($payment),
  193. errorMessage: 'Erro ao criar pedido de pagamento no Pagar.me.',
  194. ));
  195. $order->requireId();
  196. $this->saveExternalCustomerId($payment, $order);
  197. return $order->toArray();
  198. }
  199. //
  200. public function applyGatewayResponseToPayment(Payment $payment, array $orderResponse): Payment
  201. {
  202. $order = OrderResponseData::fromArray($orderResponse);
  203. $newStatus = $order->paymentStatus();
  204. $failureCode = null;
  205. $failureMessage = null;
  206. if ($newStatus === PaymentStatusEnum::FAILED) {
  207. $failureCode = $order->failureCode();
  208. $failureMessage = $order->failureMessage();
  209. $this->logCreditCardOrderResult($payment, $orderResponse, 'webhook_failed');
  210. }
  211. $gatewayFeeCents = $order->lastTransaction()?->cost ?? 0;
  212. $gatewayFee = $gatewayFeeCents > 0 ? round($gatewayFeeCents / 100, 2) : 0;
  213. $gatewayPayload = $newStatus === PaymentStatusEnum::FAILED
  214. ? $this->normalizeFailedGatewayPayload($orderResponse, $failureCode, $failureMessage)
  215. : $orderResponse;
  216. $payment->forceFill([
  217. 'gateway_provider' => 'pagarme',
  218. 'gateway_entity_reference' => $order->gatewayEntityReference(),
  219. 'gateway_entity_label' => $order->gatewayEntityLabel(),
  220. 'gateway_operation_reference' => $order->gatewayOperationReference(),
  221. 'gateway_operation_label' => $order->gatewayOperationLabel(),
  222. 'status' => $newStatus,
  223. 'paid_at' => $order->paidAt(),
  224. 'authorized_at' => $order->authorizedAt(),
  225. 'gateway_payload' => $gatewayPayload,
  226. 'gateway_fee_amount' => $gatewayFee,
  227. 'failure_code' => $failureCode,
  228. 'failure_message' => $failureMessage,
  229. ])->save();
  230. $splitStatus = match ($newStatus) {
  231. PaymentStatusEnum::PAID => PaymentSplitStatusEnum::TRANSFERRED,
  232. PaymentStatusEnum::FAILED => PaymentSplitStatusEnum::FAILED,
  233. PaymentStatusEnum::CANCELLED => PaymentSplitStatusEnum::CANCELLED,
  234. PaymentStatusEnum::AUTHORIZED => PaymentSplitStatusEnum::PROCESSING,
  235. default => PaymentSplitStatusEnum::PENDING,
  236. };
  237. PaymentSplit::query()
  238. ->where('payment_id', $payment->id)
  239. ->update(['status' => $splitStatus]);
  240. return $payment->fresh();
  241. }
  242. //
  243. private function buildCustomer(Schedule $schedule, array $options = []): CustomerRequestData
  244. {
  245. $client = $schedule->client;
  246. $user = $client->user()->first(['id', 'name', 'email', 'phone']);
  247. $address = Address::with(['city.state', 'state'])->find($schedule->address_id);
  248. foreach ([
  249. 'nome' => $user?->name,
  250. 'email' => $user?->email,
  251. 'documento' => $client->document,
  252. ] as $field => $value) {
  253. if ($value === null || $value === '') {
  254. throw new \InvalidArgumentException("Cliente precisa ter {$field} para criar pedido no Pagar.me.");
  255. }
  256. }
  257. if (! $address) {
  258. throw new \InvalidArgumentException('Endereco do agendamento nao encontrado para criar pedido no Pagar.me.');
  259. }
  260. $document = $this->customerDocument($client->document);
  261. $documentType = $this->customerDocumentType($document);
  262. $phone = $this->buildPhonePayload($user->phone)
  263. ?: $this->buildPhonePayload($options['phone'] ?? null);
  264. $state = $address->state?->code ?? $address->city?->state?->code;
  265. $city = $address->city?->name;
  266. $zipCode = $this->digits($address->zip_code);
  267. $line1 = implode(', ', array_filter([
  268. $address->number ?: 'S/N',
  269. $address->address,
  270. $address->district,
  271. ]));
  272. foreach ([
  273. 'documento' => $document,
  274. 'estado' => $state,
  275. 'cidade' => $city,
  276. 'cep' => $zipCode,
  277. 'endereco' => $line1,
  278. 'telefone' => $phone,
  279. ] as $field => $value) {
  280. if ($value === null || $value === '' || $value === []) {
  281. throw new \InvalidArgumentException("Cliente precisa ter {$field} valido para criar pedido no Pagar.me.");
  282. }
  283. }
  284. $customerAddress = new AddressData(
  285. line1: $line1,
  286. line2: $address->complement ?: $address->instructions,
  287. zipCode: $zipCode,
  288. city: $city,
  289. state: $state,
  290. country: 'BR',
  291. );
  292. $customerPhones = null;
  293. if ($phone) {
  294. $customerPhones = new PhonesData(
  295. mobilePhone: new PhoneData(
  296. countryCode: $phone['country_code'],
  297. areaCode: $phone['area_code'],
  298. number: $phone['number'],
  299. ),
  300. );
  301. }
  302. return new CustomerRequestData(
  303. name: $user->name,
  304. email: $user->email,
  305. document: $document,
  306. type: $documentType === 'CNPJ' ? 'company' : 'individual',
  307. documentType: $documentType,
  308. code: $client->ensureGatewayCode(),
  309. address: $customerAddress,
  310. phones: $customerPhones,
  311. );
  312. }
  313. private function buildOrderItems(Schedule $schedule, float $grossAmount): array
  314. {
  315. $description = $schedule->customSchedule?->serviceType?->description
  316. ?? "Servico {$schedule->id}";
  317. return [new ItemData(
  318. code: "schedule-{$schedule->id}",
  319. amount: OrderRequestData::amountInCents($grossAmount),
  320. quantity: 1,
  321. description: $description,
  322. )];
  323. }
  324. private function logCreditCardOrderResult(Payment $payment, array $orderResponse, string $source): void
  325. {
  326. $order = OrderResponseData::fromArray($orderResponse);
  327. $charge = $order->firstCharge();
  328. $transaction = $order->lastTransaction();
  329. $failureCode = $order->failureCode();
  330. Log::channel('pagarme')->info('Pagar.me credit card order result', [
  331. 'source' => $source,
  332. 'payment_id' => $payment->id,
  333. 'provider_id' => $payment->provider_id,
  334. 'order_id' => $order->id,
  335. 'order_status' => $order->status,
  336. 'charge_id' => $charge?->id,
  337. 'charge_status' => $charge?->status,
  338. 'transaction_id' => $transaction?->id,
  339. 'transaction_status' => $transaction?->status,
  340. 'failure_code' => $failureCode,
  341. 'failure_message' => $order->failureMessage(),
  342. 'acquirer_message' => $transaction?->acquirerMessage,
  343. 'gateway_response' => $this->normalizeGatewayResponseForFailure(
  344. $transaction?->gatewayResponse ?? [],
  345. $failureCode,
  346. ),
  347. ]);
  348. }
  349. private function normalizeFailedGatewayPayload(array $payload, ?string $failureCode, ?string $failureMessage): array
  350. {
  351. $payload['failure_code'] = $failureCode;
  352. $payload['failure_message'] = $failureMessage;
  353. if (isset($payload['charges'][0]['last_transaction']['gateway_response'])
  354. && is_array($payload['charges'][0]['last_transaction']['gateway_response'])) {
  355. $payload['charges'][0]['last_transaction']['gateway_response'] = $this->normalizeGatewayResponseForFailure(
  356. $payload['charges'][0]['last_transaction']['gateway_response'],
  357. $failureCode,
  358. );
  359. }
  360. return $payload;
  361. }
  362. private function normalizeGatewayResponseForFailure(array $gatewayResponse, ?string $failureCode): array
  363. {
  364. $code = $gatewayResponse['code'] ?? null;
  365. if (! $failureCode || ! $this->isMisleadingGatewayCode($code)) {
  366. return $gatewayResponse;
  367. }
  368. $gatewayResponse['raw_code'] = $code;
  369. $gatewayResponse['code'] = $failureCode;
  370. return $gatewayResponse;
  371. }
  372. private function isMisleadingGatewayCode(mixed $code): bool
  373. {
  374. if ($code === null || $code === '') {
  375. return false;
  376. }
  377. $code = mb_strtolower((string) $code);
  378. return preg_match('/^[1-2]\d{2}$/', $code) === 1
  379. || in_array($code, ['00', '0', 'approved', 'success'], true);
  380. }
  381. private function buildPhonePayload(?string $phone): ?array
  382. {
  383. $digits = $this->digits($phone);
  384. if (strlen($digits) < 10) {
  385. return null;
  386. }
  387. if (str_starts_with($digits, '55')) {
  388. $digits = substr($digits, 2);
  389. }
  390. return [
  391. 'country_code' => '55',
  392. 'area_code' => substr($digits, 0, 2),
  393. 'number' => substr($digits, 2),
  394. ];
  395. }
  396. private function roundMoneyUp(float $amount): float
  397. {
  398. return ceil($amount * 100) / 100;
  399. }
  400. private function buildSplit(Payment $payment, array $options): array
  401. {
  402. $transfers = PaymentSplit::query()
  403. ->where('payment_id', $payment->id)
  404. ->get();
  405. $split = OrderRequestData::splitFromTransfers($transfers);
  406. $platformRecipientId = config('services.pagarme.platform_recipient_id');
  407. if (empty($platformRecipientId)) {
  408. return $split;
  409. }
  410. $orderAmountCents = OrderRequestData::amountInCents((float) $payment->gross_amount);
  411. $providerTotalCents = array_sum(array_map(
  412. static fn (SplitData $s) => $s->amount,
  413. $split,
  414. ));
  415. $platformAmountCents = $orderAmountCents - $providerTotalCents;
  416. if ($platformAmountCents > 0) {
  417. $split[] = new SplitData(
  418. amount: $platformAmountCents,
  419. recipientId: $platformRecipientId,
  420. type: 'flat',
  421. options: new SplitOptionsData(
  422. chargeProcessingFee: true,
  423. chargeRemainderFee: true,
  424. liable: true,
  425. ),
  426. );
  427. }
  428. return $split;
  429. }
  430. // evita criacao duplicada de payment
  431. private function idempotencyKey(Payment $payment): string
  432. {
  433. if (! empty($payment->idempotency_key)) {
  434. return $payment->idempotency_key;
  435. }
  436. $key = 'order-'.(string) \Illuminate\Support\Str::uuid();
  437. $payment->forceFill(['idempotency_key' => $key])->save();
  438. return $key;
  439. }
  440. // salva o gateway_customer_id do Pagar.me no Client apos criacao de ordem
  441. private function saveExternalCustomerId(Payment $payment, OrderResponseData $order): void
  442. {
  443. $customerId = $order->customer?->id;
  444. $customerCode = $order->customer?->code;
  445. if (! $customerId && ! $customerCode) {
  446. return;
  447. }
  448. $client = Client::find($payment->client_id);
  449. if (! $client) {
  450. return;
  451. }
  452. $updated = false;
  453. if (! $client->gateway_customer_id && $customerId) {
  454. $client->gateway_customer_id = $customerId;
  455. $updated = true;
  456. }
  457. if (! $client->gateway_customer_code && $customerCode) {
  458. $client->gateway_customer_code = $customerCode;
  459. $updated = true;
  460. }
  461. if ($updated) {
  462. $client->save();
  463. }
  464. }
  465. }