فهرست منبع

refactor: ajuste ena service de criar customer do pagarme (lidando com erros de bad request data)

Gustavo Mantovani 3 هفته پیش
والد
کامیت
d42be6abe4
1فایلهای تغییر یافته به همراه31 افزوده شده و 45 حذف شده
  1. 31 45
      app/Services/Pagarme/PagarmeCustomerService.php

+ 31 - 45
app/Services/Pagarme/PagarmeCustomerService.php

@@ -10,7 +10,7 @@ class PagarmeCustomerService
 {
     public function createCustomerForClient(Client $client, array $data): ?string
     {
-        if (!empty($client->external_customer_id)) {
+        if (! empty($client->external_customer_id)) {
             return $client->external_customer_id;
         }
 
@@ -37,35 +37,27 @@ class PagarmeCustomerService
         $code    = $client->external_customer_code ?? "client-{$client->id}";
 
         $payload = [
-            'name'        => $name,
-            'email'       => $email,
-            'document'    => $document,
-            'type'        => $this->personType($document),
+            'name'          => $name,
+            'email'         => $email,
+            'document'      => $document,
+            'type'          => $this->personType($document),
             'document_type' => $this->documentType($document),
-            'code'        => $code,
-
-            'address' => $address,
-
-            'phones' => $phones,
-
-            'metadata' => [
-                'client_id' => (string) $client->id,
-                'user_id'   => (string) ($client->user_id ?? ''),
-            ],
+            'code'          => $code,
+            'address'       => $address,
         ];
 
+        if (! empty($phones)) {
+            $payload['phones'] = $phones;
+        }
+
         $response = $this->pagarmeRequest($client->id)
             ->post($this->pagarmeUrl('/customers'), $payload);
 
         if ($response->failed()) {
             Log::channel('pagarme')->error('Pagar.me customer creation failed', [
-                'status' => $response->status(),
-                'body'   => $response->json() ?? $response->body(),
-                'client' => [
-                    'id'       => $client->id,
-                    'email'    => $email,
-                    'document' => $document,
-                ],
+                'status'  => $response->status(),
+                'body'    => $response->json() ?? $response->body(),
+                'payload' => $payload,
             ]);
 
             throw new \RuntimeException('Erro ao criar cliente no Pagar.me.');
@@ -74,7 +66,7 @@ class PagarmeCustomerService
         $customerData = $response->json();
         $customerId   = $customerData['id'] ?? null;
 
-        if (!$customerId) {
+        if (! $customerId) {
             Log::channel('pagarme')->error('Customer creation returned an empty id.', [
                 'client_id' => $client->id,
                 'response'  => $customerData,
@@ -95,7 +87,7 @@ class PagarmeCustomerService
 
     private function pagarmeUrl(string $path): string
     {
-        return rtrim(config('services.pagarme.base_url'), '/') . '/' . ltrim($path, '/');
+        return rtrim(config('services.pagarme.base_url'), '/').'/'.ltrim($path, '/');
     }
 
     private function idempotencyKey(int $clientId): string
@@ -107,14 +99,17 @@ class PagarmeCustomerService
 
     private function buildAddress(array $data): array
     {
-        $zipCode      = $this->sanitizeDigits($data['zip_code'] ?? null);
+        $zipCode = $this->sanitizeDigits($data['zip_code'] ?? null);
+
         $street       = (string) ($data['address'] ?? '');
         $number       = (string) ($data['number'] ?? '0');
         $neighborhood = (string) ($data['district'] ?? '');
-        $city         = (string) ($data['city'] ?? '');
-        $state        = (string) ($data['state'] ?? '');
-        $country      = (string) ($data['country'] ?? 'BR');
-        $complement   = (string) ($data['complement'] ?? '');
+
+        $city    = (string) ($data['city'] ?? '');
+        $state   = (string) ($data['state'] ?? '');
+        $country = (string) ($data['country'] ?? 'BR');
+
+        $complement = (string) ($data['complement'] ?? '');
 
         $line1Parts = array_filter([$number, $street, $neighborhood], static fn ($value) => $value !== '');
 
@@ -122,20 +117,12 @@ class PagarmeCustomerService
         $line2 = $complement ?: (string) ($data['instructions'] ?? '');
 
         return [
-            'street'       => $street,
-            'number'       => $number,
-            'zip_code'     => $zipCode,
-            'neighborhood' => $neighborhood,
-            'city'         => $city,
-            'state'        => $state,
-            'country'      => $country,
-            'complement'   => $complement,
-            'line_1'       => $line1,
-            'line_2'       => $line2,
-
-            'metadata' => [
-                'source' => 'register-client',
-            ],
+            'line_1'   => $line1,
+            'line_2'   => $line2,
+            'zip_code' => $zipCode,
+            'city'     => $city,
+            'state'    => $state,
+            'country'  => $country,
         ];
     }
 
@@ -161,7 +148,6 @@ class PagarmeCustomerService
                 'country_code' => $countryCode,
                 'area_code'    => $areaCode,
                 'number'       => $number,
-                'type'         => 'mobile',
             ],
         ];
     }
@@ -202,4 +188,4 @@ class PagarmeCustomerService
                 'Accept'          => 'application/json',
             ]);
     }
-}
+}