PagarmePaymentService.php 14 KB

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