| <?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; |
| |
| |
| . | |
| use App\Model\IterableApi; |
| |
| |
| class OrderTaggingJob implements ShouldQueue |
| class OrderTaggingJob implements ShouldQueue |
| { |
| { |
| use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; |
| use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; |
| |
| |
| |
| |
| public $webhook; |
| public $webhook; |
| 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) |
| public function __construct($webhook) |
| { |
| { |
| $this->webhook = $webhook; |
| $this->webhook = $webhook; |
| $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() |
| { |
| { |
| $api = new BasicShopifyAPI(); |
| $api = new BasicShopifyAPI(); |
| |
| |
| . | $password = Config::get('shopify.password'); |
| $password = Config::get('shopify.passwordOrderTag'); |
| $shopDomain = Config::get('shopify.shopDomain'); |
| $shopDomain = Config::get('shopify.shopDomainOrderTag'); |
| |
| |
| $api->setShop($shopDomain); |
| $api->setShop($shopDomain); |
| $api->setAccessToken($password); |
| $api->setAccessToken($password); |
| |
| |
| $webhookData = json_decode($this->webhook); |
| $webhookData = json_decode($this->webhook); |
| // $webhookData = json_decode($webhookData); |
| // $webhookData = json_decode($webhookData); |
| $lineItems = $webhookData->line_items; |
| $lineItems = $webhookData->line_items; |
| $orderId = $webhookData->id; |
| $orderId = $webhookData->id; |
| $orderTags = $webhookData->tags; |
| $orderTags = $webhookData->tags; |
| . | |
| $orderEmail = $webhookData->email; |
| $customerId = $webhookData->customer->id; |
| $customerId = $webhookData->customer->id; |
| $customerTags = $webhookData->customer->tags; |
| $customerTags = $webhookData->customer->tags; |
| . | |
| |
| |
| |
| $newOrderTag = []; |
| $newOrderTag = []; |
| $newCustomerTag = []; |
| $newCustomerTag = []; |
| |
| |
| . | |
| |
| if(!empty($lineItems)){ |
| if(!empty($lineItems)){ |
| |
| |
| foreach($lineItems as $lineitem){ |
| foreach($lineItems as $lineitem){ |
| |
| |
| $lineItemProperties = $lineitem->properties; |
| $lineItemProperties = $lineitem->properties; |
| if(!empty($lineItemProperties)){ |
| if(!empty($lineItemProperties)){ |
| |
| |
| foreach($lineItemProperties as $lineItemProperty){ |
| foreach($lineItemProperties as $lineItemProperty){ |
| |
| |
| $name = trim($lineItemProperty->name); |
| $name = trim($lineItemProperty->name); |
| $value = trim($lineItemProperty->value); |
| $value = trim($lineItemProperty->value); |
| |
| |
| . | |
| |
| // Set Special Pack tag |
| // Set Special Pack tag |
| if($name == 'special_pack_type'){ |
| if($name == 'special_pack_type'){ |
| . | |
| |
| $newOrderTag[] = 'special pack:'. $value; |
| $newOrderTag[] = 'special pack:'. $value; |
| $newCustomerTag[] = 'special pack:'. $value; |
| $newCustomerTag[] = 'special pack:'. $value; |
| } |
| } |
| |
| |
| // Set Free Override Shipping tag |
| // Set Free Override Shipping tag |
| if($name == 'override_free_shipping' && $value == 'true'){ |
| if($name == 'override_free_shipping' && $value == 'true'){ |
| . | |
| |
| $newOrderTag[] = 'free-shipping-override'; |
| $newOrderTag[] = 'free-shipping-override'; |
| . | |
| |
| } |
| } |
| |
| |
| // Set Partner tag |
| // Set Partner tag |
| if($name == 'partner'){ |
| if($name == 'partner'){ |
| . | |
| |
| $newOrderTag[] = 'partner:'. $value; |
| $newOrderTag[] = 'partner:'. $value; |
| $newCustomerTag[] = 'partner:'. $value; |
| $newCustomerTag[] = 'partner:'. $value; |
| . | |
| |
| } |
| } |
| // Set campaign tag |
| // Set campaign tag |
| if($name == 'campaign'){ |
| if($name == 'campaign'){ |
| . | |
| |
| $newOrderTag[] = 'campaign:'. $value; |
| $newOrderTag[] = 'campaign:'. $value; |
| $newCustomerTag[] = 'campaign:'. $value; |
| $newCustomerTag[] = 'campaign:'. $value; |
| . | |
| |
| } |
| } |
| } |
| } |
| } |
| } |
| } |
| } |
| . | |
| |
| $newOrderTag = implode(',',$newOrderTag); |
| $newOrderTag = implode(',',$newOrderTag); |
| $newCustomerTag = implode(',',$newCustomerTag); |
| $newCustomerTag = implode(',',$newCustomerTag); |
| . | |
| |
| |
| |
| $newTag = $orderTags.", ".$newOrderTag; |
| $newTag = $orderTags.", ".$newOrderTag; |
| $params = array( |
| $params = array( |
| 'order' => array( |
| 'order' => array( |
| 'id' => $orderId, |
| 'id' => $orderId, |
| 'tags' => $newTag |
| 'tags' => $newTag |
| ) |
| ) |
| ); |
| ); |
| |
| |
| $method = 'PUT'; |
| $method = 'PUT'; |
| $url = '/admin/api/'.$this->shopify_api_version.'/orders/'.$orderId.'.json'; |
| $url = '/admin/api/'.$this->shopify_api_version.'/orders/'.$orderId.'.json'; |
| . | |
| |
| $result = $api->rest($method, $url, $params); |
| $retry = false; |
| |
| $try = 1; |
| |
| do{ |
| |
| $result = $api->rest($method, $url, $params); |
| |
| if(!isset($result->body->order)){ |
| |
| $retry = true; |
| |
| $try++; |
| |
| } |
| |
| } while($retry && $try <=3); |
| |
| |
| if(empty($result->errors) && !empty($result->body->order)){ // success |
| if(empty($result->errors) && !empty($result->body->order)){ // success |
| |
| |
| Log::channel('orderTag')->info("Successfuly updated the order tags - ".$orderId); |
| Log::channel('orderTag')->info("Successfuly updated the order tags - ".$orderId); |
| Log::channel('orderTag')->info( |
| Log::channel('orderTag')->info( |
| "Order Id = ".$orderId. |
| "Order Id = ".$orderId. |
| " => OrderTag => ".$orderTags. |
| " => OrderTag => ".$orderTags. |
| " => New tags => ". $newTag |
| " => New tags => ". $newTag |
| ); |
| ); |
| if(!empty($newCustomerTag)){ |
| if(!empty($newCustomerTag)){ |
| |
| |
| $this->seTCustomerTag($api, $customerId, $customerTags, $newCustomerTag, $orderId); |
| $this->seTCustomerTag($api, $customerId, $customerTags, $newCustomerTag, $orderId); |
| . | } |
| $this->setIterableTag($orderEmail, $customerTags, $newCustomerTag); |
| |
| } |
| |
| |
| } |
| } |
| else{ |
| else{ |
| Log::channel('orderTag')->info("Failed to update the order tags - ".$orderId); |
| Log::channel('orderTag')->info("Failed to update the order tags - ".$orderId); |
| . | Log::channel('orderTag')->info(json_encode($result)); |
| Log::channel('orderTag')->info(json_encode($result)); |
| } |
| } |
| |
| |
| } |
| } |
| |
| |
| |
| |
| } |
| } |
| |
| |
| function seTCustomerTag($api, $customerId, $customerTags, $newCustomerTag, $orderId){ |
| function seTCustomerTag($api, $customerId, $customerTags, $newCustomerTag, $orderId){ |
| |
| |
| $newTag = $customerTags.", ".$newCustomerTag; |
| $newTag = $customerTags.", ".$newCustomerTag; |
| |
| |
| $params = array( |
| $params = array( |
| 'customer' => array( |
| 'customer' => array( |
| 'id' => $customerId, |
| 'id' => $customerId, |
| 'tags' => $newTag |
| 'tags' => $newTag |
| ) |
| ) |
| ); |
| ); |
| |
| |
| $method = 'PUT'; |
| $method = 'PUT'; |
| $url = '/admin/api/'.$this->shopify_api_version.'/customers/'.$customerId.'.json'; |
| $url = '/admin/api/'.$this->shopify_api_version.'/customers/'.$customerId.'.json'; |
| . | |
| |
| $result = $api->rest($method, $url, $params); |
| $retry = false; |
| |
| $try = 1; |
| |
| do{ |
| |
| $result = $api->rest($method, $url, $params); |
| |
| if(!isset($result->body->customer->id)){ |
| |
| $retry = true; |
| |
| $try++; |
| |
| } |
| |
| } while($retry && $try <=3); |
| |
| |
| |
| Log::channel('orderTag')->info(json_encode($result)); |
| if(empty($result->errors) && !empty($result->body->customer)){ // success |
| if(empty($result->errors) && !empty($result->body->customer)){ // success |
| |
| |
| Log::channel('orderTag')->info("Successfuly updated the customer tags - ".$customerId." for the order ".$orderId); |
| Log::channel('orderTag')->info("Successfuly updated the customer tags - ".$customerId." for the order ".$orderId); |
| Log::channel('orderTag')->info( |
| Log::channel('orderTag')->info( |
| "Order Id = ".$orderId. |
| "Order Id = ".$orderId. |
| " => Customer Id => ".$customerId. |
| " => Customer Id => ".$customerId. |
| " => Customer tag => ".$customerTags. |
| " => Customer tag => ".$customerTags. |
| " => New tags => ". $newTag |
| " => New tags => ". $newTag |
| ); |
| ); |
| |
| |
| } |
| } |
| else{ |
| else{ |
| Log::channel('orderTag')->info("Failed to update the Customer tags - ".$customerId." for the order ".$orderId); |
| Log::channel('orderTag')->info("Failed to update the Customer tags - ".$customerId." for the order ".$orderId); |
| . | Log::channel('orderTag')->info(json_encode($result)); |
| Log::channel('orderTag')->info(json_encode($result)); |
| } |
| } |
| |
| |
| |
| } |
| |
| function setIterableTag($email, $customerTags, $newCustomerTag){ |
| |
| |
| |
| $iterableModel = new IterableApi(); |
| |
| $userData = $iterableModel->getIterableSubscription($email); |
| |
| |
| |
| Log::channel('orderTag')->info(json_encode($userData)); |
| |
| $newTag = $customerTags.", ".$newCustomerTag; |
| |
| $iterableRequest = array( |
| |
| "email" => $email, |
| |
| "dataFields" => array( |
| |
| "tags" => $newTag |
| |
| ) |
| |
| ); |
| |
| |
| |
| |
| . | |
| $userIterableUpdateResponse = $iterableModel->updateUserDataFields(json_encode($iterableRequest)); |
| |
| |
| |
| if(strtolower(trim($userIterableUpdateResponse->code)) == 'success') { |
| |
| Log::channel('orderTag')->info("Successfuly updated the customer tags for the customer - ".$email); |
| |
| } |
| |
| else{ |
| |
| Log::channel('orderTag')->info("Failed to update customer tags for the customer - ".$email); |
| |
| Log::channel('orderTag')->info(json_encode($userIterableUpdateResponse)); |
| |
| } |
| } |
| } |
| |
| |
| } |
| } |
| . | |
| |
| |
| |