From b59dff7c6257b4a3f16dbbac83d3bd6f80e9841b Mon Sep 17 00:00:00 2001 From: anas-srikou Date: Tue, 1 Aug 2023 14:22:44 +0900 Subject: [PATCH 1/2] Get customer key when they sign up for DF for first time --- src/Models/User.php | 50 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/src/Models/User.php b/src/Models/User.php index 701706b8..f7e8f4c4 100644 --- a/src/Models/User.php +++ b/src/Models/User.php @@ -13,14 +13,18 @@ use DreamFactory\Core\Utility\DataFormatter; use DreamFactory\Core\Utility\JWTUtilities; use DreamFactory\Core\Utility\Session; +use GuzzleHttp\Exception\GuzzleException; use Illuminate\Auth\Authenticatable; use Illuminate\Auth\Passwords\CanResetPassword; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Hash; +use Illuminate\Support\Facades\Log; use Validator; - +use Illuminate\Support\Facades\Http; +use Carbon\Carbon; /** * User * @@ -628,6 +632,21 @@ public static function createFirstAdmin(array &$data) return false; } else { + // Send request to create the customer in updates.dreamfactory.com and get the customer key + $res = self::createCustomer($data); + + // Extract the key from the response and save it in the config file and .env file + $customerKey = $res['data']; + $path = base_path('.env'); + $app = app(); + $key = $app['config']['app.license_key']; + if (file_exists($path)) { + file_put_contents($path, str_replace( + 'DF_LICENSE_KEY='.$key, 'DF_LICENSE_KEY='.$customerKey, file_get_contents($path) + )); + } + $app['config']['app.license_key'] = $customerKey; + /** @type User $user */ $attributes = array_only($data, ['name', 'first_name', 'last_name', 'email', 'username', 'phone']); $attributes['is_active'] = 1; @@ -648,6 +667,35 @@ public static function createFirstAdmin(array &$data) } } + /** + * @param array $data + * @return array + * @throws \Exception + */ + public static function createCustomer(array $data) + { + try { + $token = hash_hmac('sha256', $data['email'], config('services.marketplace.secret')); + $response = Http::withHeaders([ + 'X-Marketplace-Token' => $token, + ])->post(config('services.df_updates.endpoint'), [ + 'name' => $data['name'], + 'email' => $data['email'], + 'phone' => $data['phone'], + ]); + + if ($response->status() !== 200) { + throw new \Exception(); + } + + return $response->json(); + } catch (\Throwable $exception) { + Log::error($exception->getMessage()); + Log::error($exception->getTraceAsString()); + throw new \Exception('Error creating customer in DF updates'); + } + } + public function save(array $options = []) { if ($this->exists) { From 404127c94b96d4cc6d62355fea9c426d02035af4 Mon Sep 17 00:00:00 2001 From: anas-srikou Date: Wed, 2 Aug 2023 16:06:28 +0900 Subject: [PATCH 2/2] Check for 200 and 201 status code --- src/Models/User.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Models/User.php b/src/Models/User.php index f7e8f4c4..4dc3bcc5 100644 --- a/src/Models/User.php +++ b/src/Models/User.php @@ -684,7 +684,8 @@ public static function createCustomer(array $data) 'phone' => $data['phone'], ]); - if ($response->status() !== 200) { + if ($response->status() !== 200 || $response->status() !== 201) { + Log::error("Error creating customer in DF updates: " . $response->body()); throw new \Exception(); }