|
@@ -10,7 +10,7 @@ class PagarmeCustomerService
|
|
|
{
|
|
{
|
|
|
public function createCustomerForClient(Client $client, array $data): ?string
|
|
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;
|
|
return $client->external_customer_id;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -37,35 +37,27 @@ class PagarmeCustomerService
|
|
|
$code = $client->external_customer_code ?? "client-{$client->id}";
|
|
$code = $client->external_customer_code ?? "client-{$client->id}";
|
|
|
|
|
|
|
|
$payload = [
|
|
$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),
|
|
'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)
|
|
$response = $this->pagarmeRequest($client->id)
|
|
|
->post($this->pagarmeUrl('/customers'), $payload);
|
|
->post($this->pagarmeUrl('/customers'), $payload);
|
|
|
|
|
|
|
|
if ($response->failed()) {
|
|
if ($response->failed()) {
|
|
|
Log::channel('pagarme')->error('Pagar.me customer creation 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.');
|
|
throw new \RuntimeException('Erro ao criar cliente no Pagar.me.');
|
|
@@ -74,7 +66,7 @@ class PagarmeCustomerService
|
|
|
$customerData = $response->json();
|
|
$customerData = $response->json();
|
|
|
$customerId = $customerData['id'] ?? null;
|
|
$customerId = $customerData['id'] ?? null;
|
|
|
|
|
|
|
|
- if (!$customerId) {
|
|
|
|
|
|
|
+ if (! $customerId) {
|
|
|
Log::channel('pagarme')->error('Customer creation returned an empty id.', [
|
|
Log::channel('pagarme')->error('Customer creation returned an empty id.', [
|
|
|
'client_id' => $client->id,
|
|
'client_id' => $client->id,
|
|
|
'response' => $customerData,
|
|
'response' => $customerData,
|
|
@@ -95,7 +87,7 @@ class PagarmeCustomerService
|
|
|
|
|
|
|
|
private function pagarmeUrl(string $path): string
|
|
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
|
|
private function idempotencyKey(int $clientId): string
|
|
@@ -107,14 +99,17 @@ class PagarmeCustomerService
|
|
|
|
|
|
|
|
private function buildAddress(array $data): array
|
|
private function buildAddress(array $data): array
|
|
|
{
|
|
{
|
|
|
- $zipCode = $this->sanitizeDigits($data['zip_code'] ?? null);
|
|
|
|
|
|
|
+ $zipCode = $this->sanitizeDigits($data['zip_code'] ?? null);
|
|
|
|
|
+
|
|
|
$street = (string) ($data['address'] ?? '');
|
|
$street = (string) ($data['address'] ?? '');
|
|
|
$number = (string) ($data['number'] ?? '0');
|
|
$number = (string) ($data['number'] ?? '0');
|
|
|
$neighborhood = (string) ($data['district'] ?? '');
|
|
$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 !== '');
|
|
$line1Parts = array_filter([$number, $street, $neighborhood], static fn ($value) => $value !== '');
|
|
|
|
|
|
|
@@ -122,20 +117,12 @@ class PagarmeCustomerService
|
|
|
$line2 = $complement ?: (string) ($data['instructions'] ?? '');
|
|
$line2 = $complement ?: (string) ($data['instructions'] ?? '');
|
|
|
|
|
|
|
|
return [
|
|
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,
|
|
'country_code' => $countryCode,
|
|
|
'area_code' => $areaCode,
|
|
'area_code' => $areaCode,
|
|
|
'number' => $number,
|
|
'number' => $number,
|
|
|
- 'type' => 'mobile',
|
|
|
|
|
],
|
|
],
|
|
|
];
|
|
];
|
|
|
}
|
|
}
|
|
@@ -202,4 +188,4 @@ class PagarmeCustomerService
|
|
|
'Accept' => 'application/json',
|
|
'Accept' => 'application/json',
|
|
|
]);
|
|
]);
|
|
|
}
|
|
}
|
|
|
-}
|
|
|
|
|
|
|
+}
|