external_customer_id)) { return $client->external_customer_id; } $client->loadMissing('user'); $name = $client->user?->name ?? $data['name'] ?? 'Cliente'; $email = $client->user?->email ?? $data['email'] ?? null; $code = $this->ensureCustomerCode($client); $document = $this->onlyDigits($client->document ?? $data['document'] ?? null); $documentLen = strlen($document); $address = $this->buildAddressData($data); $phones = $this->buildPhones($client->user?->phone ?? $data['phone'] ?? null); $customerRequest = new PagarmeCustomerRequestData( name: $name, email: (string) $email, document: $document, type: $documentLen === 14 ? 'company' : 'individual', documentType: $documentLen === 14 ? 'CNPJ' : 'CPF', code: $code, address: $address, phones: $phones, ); $raw = $this->pagarmeRequest( method: 'POST', path: '/customers', payload: $customerRequest, idempotencyKey: $this->idempotencyKey($client->id), errorMessage: 'Erro ao criar cliente no Pagar.me.', ); $customerData = $this->buildCustomerResponse($raw); $customerId = $customerData->requireId(); $client->forceFill([ 'external_customer_id' => $customerId, 'external_customer_code' => $code, ])->save(); return $customerId; } // private function buildAddressData(array $data): PagarmeCustomerAddressRequestData { $line1Parts = array_filter([ (string) ($data['number'] ?? '0'), (string) ($data['address'] ?? ''), (string) ($data['district'] ?? ''), ], static fn ($value) => $value !== ''); return new PagarmeCustomerAddressRequestData( line1: implode(', ', $line1Parts), line2: (string) ($data['complement'] ?? $data['instructions'] ?? ''), zipCode: $this->onlyDigits($data['zip_code'] ?? null), city: (string) ($data['city'] ?? ''), state: (string) ($data['state'] ?? ''), country: (string) ($data['country'] ?? 'BR'), ); } private function buildPhones(?string $phone): PagarmeCustomerPhonesRequestData { $digits = $this->onlyDigits($phone); if ($digits === '') { return new PagarmeCustomerPhonesRequestData; } $areaCode = substr($digits, 0, 2); $number = substr($digits, 2); if (strlen($digits) <= 2) { $areaCode = ''; $number = $digits; } return new PagarmeCustomerPhonesRequestData( mobilePhone: new PagarmeCustomerPhoneData( countryCode: '55', areaCode: $areaCode, number: $number, ), ); } private function buildCustomerResponse(array $raw): PagarmeCustomerResponseData { return new PagarmeCustomerResponseData( id: $raw['id'] ?? null, name: $raw['name'] ?? null, email: $raw['email'] ?? null, code: $raw['code'] ?? null, document: $raw['document'] ?? null, documentType: $raw['document_type'] ?? null, type: $raw['type'] ?? null, delinquent: $raw['delinquent'] ?? null, address: ! empty($raw['address']) ? new PagarmeCustomerAddressResponseData( id: $raw['address']['id'] ?? null, line1: $raw['address']['line_1'] ?? null, line2: $raw['address']['line_2'] ?? null, zipCode: $raw['address']['zip_code'] ?? null, city: $raw['address']['city'] ?? null, state: $raw['address']['state'] ?? null, country: $raw['address']['country'] ?? null, status: $raw['address']['status'] ?? null, createdAt: $raw['address']['created_at'] ?? null, updatedAt: $raw['address']['updated_at'] ?? null, ) : null, phones: new PagarmeCustomerPhonesResponseData( homePhone: ! empty($raw['phones']['home_phone']) ? new PagarmePhoneResponseData( countryCode: $raw['phones']['home_phone']['country_code'] ?? null, areaCode: $raw['phones']['home_phone']['area_code'] ?? null, number: $raw['phones']['home_phone']['number'] ?? null, ) : null, mobilePhone: ! empty($raw['phones']['mobile_phone']) ? new PagarmePhoneResponseData( countryCode: $raw['phones']['mobile_phone']['country_code'] ?? null, areaCode: $raw['phones']['mobile_phone']['area_code'] ?? null, number: $raw['phones']['mobile_phone']['number'] ?? null, ) : null, ), createdAt: $raw['created_at'] ?? null, updatedAt: $raw['updated_at'] ?? null, ); } private function onlyDigits(?string $value): string { return preg_replace('/\D+/', '', (string) $value) ?? ''; } // evita criacao duplicada de customer private function idempotencyKey(int $clientId, string $suffix = 'customer'): string { return "client-{$clientId}-{$suffix}"; } private function ensureCustomerCode(Client $client): string { if (! empty($client->external_customer_code)) { return $client->external_customer_code; } $code = 'client-'.(string) Str::uuid(); $client->forceFill(['external_customer_code' => $code])->save(); return $code; } }