PagarmePaymentService.php 13 KB

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