| <?php |
| <?php |
| |
| |
| namespace App\Jobs; |
| namespace App\Jobs; |
| |
| |
| use Illuminate\Bus\Queueable; |
| use Illuminate\Bus\Queueable; |
| use Illuminate\Queue\SerializesModels; |
| use Illuminate\Queue\SerializesModels; |
| use Illuminate\Queue\InteractsWithQueue; |
| use Illuminate\Queue\InteractsWithQueue; |
| use Illuminate\Contracts\Queue\ShouldQueue; |
| use Illuminate\Contracts\Queue\ShouldQueue; |
| use Illuminate\Foundation\Bus\Dispatchable; |
| use Illuminate\Foundation\Bus\Dispatchable; |
| use Config; |
| use Config; |
| use OhMyBrew\BasicShopifyAPI; |
| use OhMyBrew\BasicShopifyAPI; |
| use DB; |
| use DB; |
| use Log; |
| use Log; |
| |
| |
| |
| |
| class GenerateUserTokenJob implements ShouldQueue |
| class GenerateUserTokenJob implements ShouldQueue |
| { |
| { |
| use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; |
| use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; |
| |
| |
| |
| |
| public $webhook; |
| public $webhook; |
| public $randString; |
| public $randString; |
| public $shopify_api_version; |
| public $shopify_api_version; |
| |
| |
| /** |
| /** |
| * Create a new job instance. |
| * Create a new job instance. |
| * |
| * |
| * @return void |
| * @return void |
| */ |
| */ |
| public function __construct($webhook, $randString) |
| public function __construct($webhook, $randString) |
| { |
| { |
| $this->webhook = $webhook; |
| $this->webhook = $webhook; |
| $this->randString = $randString; |
| $this->randString = $randString; |
| $this->shopify_api_version = env('SHOPIFY_API_VERSION', true); |
| $this->shopify_api_version = env('SHOPIFY_API_VERSION', true); |
| } |
| } |
| |
| |
| /** |
| /** |
| * Execute the job. |
| * Execute the job. |
| * |
| * |
| * @return void |
| * @return void |
| */ |
| */ |
| public function handle() |
| public function handle() |
| { |
| { |
| $webhookDetails = json_decode($this->webhook); |
| $webhookDetails = json_decode($this->webhook); |
. | |
| |
| $api = new BasicShopifyAPI(); |
| $api = new BasicShopifyAPI(); |
| |
| |
. | $password = Config::get('shopify.password'); |
| $password = Config::get('shopify.passwordWebhook'); |
| $shopDomain = Config::get('shopify.shopDomain'); |
| $shopDomain = Config::get('shopify.shopDomainWebhook'); |
| |
| |
| $api->setShop($shopDomain); |
| $api->setShop($shopDomain); |
| $api->setAccessToken($password); |
| $api->setAccessToken($password); |
| |
| |
| $email = $webhookDetails->email; |
| $email = $webhookDetails->email; |
| $customerId = $webhookDetails->id; |
| $customerId = $webhookDetails->id; |
| |
| |
| $authToken = $this->randString; |
| $authToken = $this->randString; |
. | $metafield = array(); |
| |
| |
| Log::info('AuthToken 1 = '.$authToken. " Email = ".$email); |
| |
| $metafieldArray = array(); |
| |
| |
| $temp['key'] = 'authToken'; |
| $temp['key'] = 'authToken'; |
| $temp['value'] = $authToken; |
| $temp['value'] = $authToken; |
| $temp['value_type'] = 'string'; |
| $temp['value_type'] = 'string'; |
| $temp['namespace'] = 'global'; |
| $temp['namespace'] = 'global'; |
| |
| |
. | array_push($metafield, $temp); |
| array_push($metafieldArray, $temp); |
| |
| |
| $user = DB::table('users')->where('user_email', '=', $email)->get(); |
| $user = DB::table('users')->where('user_email', '=', $email)->get(); |
| |
| |
| if(!isset($user[0]->user_email)){ |
| if(!isset($user[0]->user_email)){ |
| // create user in DB |
| // create user in DB |
. | |
| |
| $userId = DB::table('users')->insertGetId([ |
| $userId = DB::table('users')->insertGetId([ |
| 'user_email' => $email, |
| 'user_email' => $email, |
| 'status' => 1, |
| 'status' => 1, |
| 'auth_token' => $authToken |
| 'auth_token' => $authToken |
| ]); |
| ]); |
. | |
| Log::info('AuthToken 2 = '.$authToken. " Email = ".$email); |
| |
| |
. | Log::info('Token updated with creating the user in DB : '.$email.' DB id:'.$userId); |
| |
| } |
| } |
| else |
| else |
| { |
| { |
| $userId = DB::table('users')->where('user_email', $email)->update(['auth_token' => $authToken]); |
| $userId = DB::table('users')->where('user_email', $email)->update(['auth_token' => $authToken]); |
. | |
| |
| |
| } |
| |
| |
. | Log::info('Token updated in DB : '.$email.' DB id:'.$userId); |
| $method = 'GET'; |
| |
| $url = '/admin/api/'.$this->shopify_api_version.'/customers/'.$customerId.'/metafields.json'; |
| |
| $params = null; |
| |
| $retry = false; |
| |
| $try = 1; |
| |
| do{ |
| |
| Log::info($try.' try-get customer metafields '); |
| |
| $result = $api->rest($method, $url, $params); |
| |
| Log::info(json_encode($result)); |
| |
| if(!isset($result->body->metafields)){ |
| |
| $retry = true; |
| |
| $try++; |
| |
| } |
| |
| } while($retry && $try <=3); |
| |
| if(empty($result->errors)) |
| |
| { |
| |
| $metafields = $result->body->metafields; |
| |
| |
| |
| if(!empty($metafields)){ |
| |
| $updated = false; |
| |
| foreach($metafields as $metafield){ |
| |
| |
| |
| $metaId = $metafield->id; |
| |
| $metaKey = $metafield->key; |
| |
| $metaNameSpace = $metafield->namespace; |
| |
| |
| |
| if($metaKey == 'authToken' && $metaNameSpace == 'global'){ |
| |
| |
| |
| // update Metafield |
| |
| $updateRequest = []; |
| |
| $updateRequest['metafield']['id'] = $metaId; |
| |
| $updateRequest['metafield']['value'] = $authToken; |
| |
| $updateRequest['metafield']['value_type'] = "string"; |
| |
| |
| |
| $params = $updateRequest; |
| |
| |
| |
| $method = 'PUT'; |
| |
| $url = '/admin/api/'.$this->shopify_api_version.'/metafields/'.$metaId.'.json'; |
| |
| // sleep(1); |
| |
| $retry = false; |
| |
| $try = 1; |
| |
| do{ |
| |
| Log::info($try.' try-update metafields '); |
| |
| $result = $api->rest($method, $url, $params); |
| |
| if(!isset($result->body->metafield)){ |
| |
| $retry = true; |
| |
| $try++; |
| |
| } |
| |
| } while($retry && $try <=3); |
| |
| if(!empty($result->body->metafield)){ |
| |
| // Updated succesfully |
| |
| Log::info('AuthToken 6 = '.$authToken. " Email = ".$email); |
| |
| |
| |
| $updated = true; |
| |
| Log::info("Updated metafield successgully for the user ".$customerId." with value ".$authToken); |
| |
| |
| |
| } |
| |
| else{ |
| |
| // not updated |
| |
| $updated = false; |
| |
| Log::info("Failed to update metafield for the user ".$customerId." with value ".$authToken); |
| |
| |
| |
| } |
| |
| |
| |
| } |
| |
| |
| |
| } |
| |
| |
| |
| if($updated == false){ |
| |
| Log::info('AuthToken 4 = '.$authToken. " Email = ".$email); |
| |
| |
| |
| $this->createCustomerMetafield($metafieldArray, $customerId, $authToken, $api, $email); |
| |
| } |
| |
| }else{ |
| |
| Log::info('AuthToken 3 = '.$authToken. " Email = ".$email); |
| |
| $this->createCustomerMetafield($metafieldArray, $customerId, $authToken, $api, $email); |
| |
| |
| |
| } |
| |
| |
. | |
| |
| } |
| } |
. | // update user |
| } |
| |
| |
| |
| function createCustomerMetafield($metafield, $customerId, $authToken, $api, $email){ |
| |
| Log::info('AuthToken 5 = '.$authToken. " Email = ".$email); |
| |
| |
| $params = array( |
| $params = array( |
| 'customer' => array( |
| 'customer' => array( |
| 'id' => $customerId, |
| 'id' => $customerId, |
| 'metafields' => $metafield |
| 'metafields' => $metafield |
| ) |
| ) |
| ); |
| ); |
| |
| |
| $method = 'PUT'; |
| $method = 'PUT'; |
| $url = '/admin/api/'.$this->shopify_api_version.'/customers/'.$customerId.'.json'; |
| $url = '/admin/api/'.$this->shopify_api_version.'/customers/'.$customerId.'.json'; |
. | |
| //sleep(1); |
| $result = $api->rest($method, $url, $params); |
| $retry = false; |
| |
| $try = 1; |
| if(empty($result->errors)) //success |
| do{ |
| { |
| Log::info($try.' try-update customer metafields '); |
| // TO DO |
| $result = $api->rest($method, $url, $params); |
| Log::info('Token saved in shopify metafield for user: '.$email); |
| if(!isset($result->body->customer->id)){ |
| |
| $retry = true; |
| |
| $try++; |
| |
| } |
| |
| } while($retry && $try <=3); |
| |
| if(!empty($result->body->customer)){ |
| |
| // Updated customer succssfully by adding new metafield |
| |
| $updated = true; |
| |
| Log::info("Updated customer succssfully by adding new metafield ".$customerId." with value ".$authToken); |
| |
| |
| } |
| } |
. | else //error |
| else{ |
| { |
| // not updated |
| // TO DO |
| $updated = false; |
| Log::info('Token saved in shopify metafield for user: '.$email); |
| Log::info("Failed to update customer metafield for the user ".$customerId." with value ".$authToken); |
| Log::info(json_encode($result->errors)); |
| |
| } |
| } |
. | |
| |
| } |
| } |
. | } |
| } |
| |
| |