| <?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; |
| |
| |
| /** |
| /** |
| * 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); |
| } |
| } |
| |
| |
| /** |
| /** |
| * 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.password'); |
| $shopDomain = Config::get('shopify.shopDomain'); |
| $shopDomain = Config::get('shopify.shopDomain'); |
| |
| |
| $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(); |
| $metafield = 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($metafield, $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('Token updated with creating the user in DB : '.$email.' DB id:'.$userId); |
| 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); |
| Log::info('Token updated in DB : '.$email.' DB id:'.$userId); |
| |
| |
| |
| |
| } |
| } |
| // update user |
| // update user |
| $params = array( |
| $params = array( |
| 'customer' => array( |
| 'customer' => array( |
| 'id' => $customerId, |
| 'id' => $customerId, |
| 'metafields' => $metafield |
| 'metafields' => $metafield |
| ) |
| ) |
| ); |
| ); |
| |
| |
| $method = 'PUT'; |
| $method = 'PUT'; |
. | $url = '/admin/customers/'.$customerId.'.json'; |
| $url = '/admin/api/'.$this->shopify_api_version.'/customers/'.$customerId.'.json'; |
| |
| |
| $result = $api->rest($method, $url, $params); |
| $result = $api->rest($method, $url, $params); |
| |
| |
| if(empty($result->errors)) //success |
| if(empty($result->errors)) //success |
| { |
| { |
| // TO DO |
| // TO DO |
| Log::info('Token saved in shopify metafield for user: '.$email); |
| Log::info('Token saved in shopify metafield for user: '.$email); |
| } |
| } |
| else //error |
| else //error |
| { |
| { |
| // TO DO |
| // TO DO |
| Log::info('Token saved in shopify metafield for user: '.$email); |
| Log::info('Token saved in shopify metafield for user: '.$email); |
| Log::info(json_encode($result->errors)); |
| Log::info(json_encode($result->errors)); |
| } |
| } |
| |
| |
| } |
| } |
| } |
| } |
| |
| |