|
|
@@ -3,21 +3,11 @@
|
|
|
namespace App\Services\Pagarme;
|
|
|
|
|
|
use App\Models\Client;
|
|
|
+use Illuminate\Support\Facades\Http;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
-use PagarmeApiSDKLib\Authentication\BasicAuthCredentialsBuilder;
|
|
|
-use PagarmeApiSDKLib\Models\CreateAddressRequest;
|
|
|
-use PagarmeApiSDKLib\Models\CreatePhonesRequest;
|
|
|
-use PagarmeApiSDKLib\Models\Builders\CreateAddressRequestBuilder;
|
|
|
-use PagarmeApiSDKLib\Models\Builders\CreateCustomerRequestBuilder;
|
|
|
-use PagarmeApiSDKLib\Models\Builders\CreatePhoneRequestBuilder;
|
|
|
-use PagarmeApiSDKLib\Models\Builders\CreatePhonesRequestBuilder;
|
|
|
-use PagarmeApiSDKLib\PagarmeApiSDKClient;
|
|
|
-use PagarmeApiSDKLib\PagarmeApiSDKClientBuilder;
|
|
|
|
|
|
class PagarmeCustomerService
|
|
|
{
|
|
|
- private ?PagarmeApiSDKClient $client = null;
|
|
|
-
|
|
|
public function createCustomerForClient(Client $client, array $data): ?string
|
|
|
{
|
|
|
if (!empty($client->external_customer_id)) {
|
|
|
@@ -26,120 +16,158 @@ class PagarmeCustomerService
|
|
|
|
|
|
$client->loadMissing('user');
|
|
|
|
|
|
- $name = $client->user?->name ?? $data['name'] ?? 'Cliente';
|
|
|
- $email = $client->user?->email ?? $data['email'] ?? null;
|
|
|
+ $name = $client->user?->name ?? $data['name'] ?? 'Cliente';
|
|
|
+ $email = $client->user?->email ?? $data['email'] ?? null;
|
|
|
$document = $this->sanitizeDigits($client->document ?? $data['document'] ?? null);
|
|
|
|
|
|
if (empty($email) || empty($document)) {
|
|
|
- Log::warning('Skipping customer creation because the client is missing email or document.', [
|
|
|
- 'client_id' => $client->id,
|
|
|
- 'user_id' => $client->user_id,
|
|
|
- ]);
|
|
|
+ Log::channel('pagarme')->warning(
|
|
|
+ 'Skipping customer creation because the client is missing email or document.',
|
|
|
+ [
|
|
|
+ 'client_id' => $client->id,
|
|
|
+ 'user_id' => $client->user_id,
|
|
|
+ ]
|
|
|
+ );
|
|
|
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
$address = $this->buildAddress($data);
|
|
|
- $phones = $this->buildPhones($client->user?->phone ?? $data['phone'] ?? null);
|
|
|
- $code = $client->external_customer_code ?? "client-{$client->id}";
|
|
|
-
|
|
|
- $customerRequest = CreateCustomerRequestBuilder::init(
|
|
|
- $name,
|
|
|
- $email,
|
|
|
- $document,
|
|
|
- $this->personType($document),
|
|
|
- $address,
|
|
|
- [
|
|
|
+ $phones = $this->buildPhones($client->user?->phone ?? $data['phone'] ?? null);
|
|
|
+ $code = $client->external_customer_code ?? "client-{$client->id}";
|
|
|
+
|
|
|
+ $payload = [
|
|
|
+ '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 ?? ''),
|
|
|
+ 'user_id' => (string) ($client->user_id ?? ''),
|
|
|
],
|
|
|
- $phones,
|
|
|
- $code
|
|
|
- )
|
|
|
- ->documentType($this->documentType($document))
|
|
|
- ->build();
|
|
|
-
|
|
|
- $customer = $this->client()->getCustomersController()->createCustomer(
|
|
|
- $customerRequest,
|
|
|
- $this->idempotencyKey($client->id)
|
|
|
- );
|
|
|
-
|
|
|
- $customerId = $customer->getId();
|
|
|
+ ];
|
|
|
+
|
|
|
+ $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,
|
|
|
+ ],
|
|
|
+ ]);
|
|
|
+
|
|
|
+ throw new \RuntimeException('Erro ao criar cliente no Pagar.me.');
|
|
|
+ }
|
|
|
+
|
|
|
+ $customerData = $response->json();
|
|
|
+ $customerId = $customerData['id'] ?? null;
|
|
|
+
|
|
|
if (!$customerId) {
|
|
|
+ Log::channel('pagarme')->error('Customer creation returned an empty id.', [
|
|
|
+ 'client_id' => $client->id,
|
|
|
+ 'response' => $customerData,
|
|
|
+ ]);
|
|
|
+
|
|
|
throw new \RuntimeException('Customer creation returned an empty id.');
|
|
|
}
|
|
|
|
|
|
$client->forceFill([
|
|
|
- 'external_customer_id' => $customerId,
|
|
|
+ 'external_customer_id' => $customerId,
|
|
|
'external_customer_code' => $code,
|
|
|
])->save();
|
|
|
|
|
|
return $customerId;
|
|
|
}
|
|
|
|
|
|
- private function buildAddress(array $data): CreateAddressRequest
|
|
|
+ //
|
|
|
+
|
|
|
+ private function pagarmeUrl(string $path): string
|
|
|
{
|
|
|
- $zipCode = $this->sanitizeDigits($data['zip_code'] ?? null);
|
|
|
- $street = (string) ($data['address'] ?? '');
|
|
|
- $number = (string) ($data['number'] ?? '0');
|
|
|
+ return rtrim(config('services.pagarme.base_url'), '/') . '/' . ltrim($path, '/');
|
|
|
+ }
|
|
|
+
|
|
|
+ private function idempotencyKey(int $clientId): string
|
|
|
+ {
|
|
|
+ return "client-{$clientId}-customer";
|
|
|
+ }
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
+ private function buildAddress(array $data): array
|
|
|
+ {
|
|
|
+ $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 !== '');
|
|
|
+
|
|
|
$line1 = implode(', ', $line1Parts);
|
|
|
$line2 = $complement ?: (string) ($data['instructions'] ?? '');
|
|
|
|
|
|
- return CreateAddressRequestBuilder::init(
|
|
|
- $street,
|
|
|
- $number,
|
|
|
- $zipCode,
|
|
|
- $neighborhood,
|
|
|
- $city,
|
|
|
- $state,
|
|
|
- $country,
|
|
|
- $complement,
|
|
|
- $line1,
|
|
|
- $line2
|
|
|
- )
|
|
|
- ->metadata([
|
|
|
+ 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',
|
|
|
- ])
|
|
|
- ->build();
|
|
|
+ ],
|
|
|
+ ];
|
|
|
}
|
|
|
|
|
|
- private function buildPhones(?string $phone): CreatePhonesRequest
|
|
|
+ private function buildPhones(?string $phone): array
|
|
|
{
|
|
|
$digits = $this->sanitizeDigits($phone);
|
|
|
- $phonesBuilder = CreatePhonesRequestBuilder::init();
|
|
|
|
|
|
if (empty($digits)) {
|
|
|
- return $phonesBuilder->build();
|
|
|
+ return [];
|
|
|
}
|
|
|
|
|
|
$countryCode = '55';
|
|
|
- $areaCode = substr($digits, 0, 2);
|
|
|
- $number = substr($digits, 2);
|
|
|
+ $areaCode = substr($digits, 0, 2);
|
|
|
+ $number = substr($digits, 2);
|
|
|
|
|
|
if (strlen($digits) <= 2) {
|
|
|
$areaCode = '';
|
|
|
- $number = $digits;
|
|
|
+ $number = $digits;
|
|
|
}
|
|
|
|
|
|
- $mobilePhone = CreatePhoneRequestBuilder::init()
|
|
|
- ->countryCode($countryCode)
|
|
|
- ->areaCode($areaCode)
|
|
|
- ->number($number)
|
|
|
- ->type('mobile')
|
|
|
- ->build();
|
|
|
-
|
|
|
- return $phonesBuilder
|
|
|
- ->mobilePhone($mobilePhone)
|
|
|
- ->build();
|
|
|
+ return [
|
|
|
+ 'mobile_phone' => [
|
|
|
+ 'country_code' => $countryCode,
|
|
|
+ 'area_code' => $areaCode,
|
|
|
+ 'number' => $number,
|
|
|
+ 'type' => 'mobile',
|
|
|
+ ],
|
|
|
+ ];
|
|
|
}
|
|
|
|
|
|
+ //
|
|
|
+
|
|
|
private function documentType(string $document): string
|
|
|
{
|
|
|
return strlen($document) === 14 ? 'CNPJ' : 'CPF';
|
|
|
@@ -155,31 +183,23 @@ class PagarmeCustomerService
|
|
|
return preg_replace('/\D+/', '', (string) $value) ?? '';
|
|
|
}
|
|
|
|
|
|
- private function client(): PagarmeApiSDKClient
|
|
|
- {
|
|
|
- if ($this->client) {
|
|
|
- return $this->client;
|
|
|
- }
|
|
|
+ //
|
|
|
|
|
|
+ private function pagarmeRequest(int $clientId)
|
|
|
+ {
|
|
|
$secretKey = config('services.pagarme.secret_key');
|
|
|
+
|
|
|
if (empty($secretKey)) {
|
|
|
+ Log::channel('pagarme')->error('PAGARME_SECRET_KEY is not configured.');
|
|
|
+
|
|
|
throw new \RuntimeException('PAGARME_SECRET_KEY is not configured.');
|
|
|
}
|
|
|
|
|
|
- $serviceRefererName = (string) config('services.pagarme.service_referer_name', config('app.name'));
|
|
|
-
|
|
|
- $this->client = PagarmeApiSDKClientBuilder::init()
|
|
|
- ->basicAuthCredentials(
|
|
|
- BasicAuthCredentialsBuilder::init($secretKey, '')
|
|
|
- )
|
|
|
- ->serviceRefererName($serviceRefererName)
|
|
|
- ->build();
|
|
|
-
|
|
|
- return $this->client;
|
|
|
- }
|
|
|
-
|
|
|
- private function idempotencyKey(int $clientId): string
|
|
|
- {
|
|
|
- return "client-{$clientId}-customer";
|
|
|
+ return Http::withBasicAuth($secretKey, '')
|
|
|
+ ->withHeaders([
|
|
|
+ 'Idempotency-Key' => $this->idempotencyKey($clientId),
|
|
|
+ 'Content-Type' => 'application/json',
|
|
|
+ 'Accept' => 'application/json',
|
|
|
+ ]);
|
|
|
}
|
|
|
-}
|
|
|
+}
|