PagarmePaymentService.php 16 KB

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