| <?php |
| <?php |
| |
| |
| namespace App\Http\Controllers; |
| namespace App\Http\Controllers; |
| |
| |
| use DB; |
| use DB; |
| use Log; |
| use Log; |
| use App\Users; |
| use App\Users; |
| use Illuminate\Http\Request; |
| use Illuminate\Http\Request; |
| use App\Http\ResponseHelper; |
| use App\Http\ResponseHelper; |
| use App\Jobs\GenerateUserTokenJob; |
| use App\Jobs\GenerateUserTokenJob; |
| use Config; |
| use Config; |
. | |
| use OhMyBrew\BasicShopifyAPI; |
| |
| |
| class CustomerCreationWebhookController extends Controller |
| class CustomerCreationWebhookController extends Controller |
| { |
| { |
| |
| |
| private $helper; |
| private $helper; |
| private $shopifyProductDao; |
| private $shopifyProductDao; |
| private $sharedSecret; |
| private $sharedSecret; |
| private $randomString; |
| private $randomString; |
. | |
| private $shopify_api_version; |
| |
| |
| public function __construct() |
| public function __construct() |
| { |
| { |
| $this->helper = new ResponseHelper(); |
| $this->helper = new ResponseHelper(); |
. | |
| $this->shopify_api_version = env('SHOPIFY_API_VERSION', true); |
| } |
| } |
| |
| |
| function processWebhook() |
| function processWebhook() |
. | { |
| { |
| |
| |
| $this->sharedSecret = Config::get('shopify.secret'); |
| $this->sharedSecret = Config::get('shopify.secret'); |
| $this->webhookContent = $this->webhook($this->sharedSecret); |
| $this->webhookContent = $this->webhook($this->sharedSecret); |
. | |
| |
| $this->randomString = $this->generateRandomString(16); |
| $this->randomString = $this->generateRandomString(16); |
. | dispatch(new GenerateUserTokenJob($this->webhookContent, $this->randomString)); |
| |
| |
| $lastInsertedId = DB::table('webhooks')->insertGetId( |
| |
| [ |
| |
| 'webhookContent' => $this->webhookContent, |
| |
| 'authToken' => $this->randomString |
| |
| ] |
| |
| ); |
| |
| |
| |
| dispatch(new GenerateUserTokenJob($this->webhookContent, $this->randomString)); |
| |
| |
| return 'true'; |
| return 'true'; |
. | |
| |
| } |
| } |
| |
| |
| function webhook($my_shared_secret){ |
| function webhook($my_shared_secret){ |
. | |
| |
| define('SHOPIFY_APP_SECRET', 'my_shared_secret'); |
| define('SHOPIFY_APP_SECRET', 'my_shared_secret'); |
| $hmac_header = $_SERVER['HTTP_X_SHOPIFY_HMAC_SHA256']; |
| $hmac_header = $_SERVER['HTTP_X_SHOPIFY_HMAC_SHA256']; |
| $data = file_get_contents('php://input'); |
| $data = file_get_contents('php://input'); |
| $verified = $this->verify_webhook($data, $hmac_header); |
| $verified = $this->verify_webhook($data, $hmac_header); |
| error_log('Webhook verified: '.var_export($verified, true)); |
| error_log('Webhook verified: '.var_export($verified, true)); |
| return $data; |
| return $data; |
| |
| |
| } |
| } |
| |
| |
| function verify_webhook($data, $hmac_header){ |
| function verify_webhook($data, $hmac_header){ |
| |
| |
| $calculated_hmac = base64_encode(hash_hmac('sha256', $data, SHOPIFY_APP_SECRET, true)); |
| $calculated_hmac = base64_encode(hash_hmac('sha256', $data, SHOPIFY_APP_SECRET, true)); |
| return ($hmac_header==$calculated_hmac); |
| return ($hmac_header==$calculated_hmac); |
| |
| |
| } |
| } |
| |
| |
| function generateRandomString($length) { |
| function generateRandomString($length) { |
| $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| $charactersLength = strlen($characters); |
| $charactersLength = strlen($characters); |
| $randomString = ''; |
| $randomString = ''; |
| for ($i = 0; $i < $length; $i++) { |
| for ($i = 0; $i < $length; $i++) { |
| $randomString .= $characters[rand(0, $charactersLength - 1)]; |
| $randomString .= $characters[rand(0, $charactersLength - 1)]; |
| } |
| } |
| return $randomString; |
| return $randomString; |
| } |
| } |
. | |
| |
| |
| public function handleWebhookData() |
| |
| { |
| |
| Log::channel('cron')->info('testing webhook'); |
| |
| |
| |
| $api = new BasicShopifyAPI(); |
| |
| |
| |
| $password = Config::get('shopify.password'); |
| |
| $shopDomain = Config::get('shopify.shopDomain'); |
| |
| |
| |
| $api->setShop($shopDomain); |
| |
| $api->setAccessToken($password); |
| |
| |
| |
| $webhookDetails = DB::table('webhooks')->get(); |
| |
| |
| |
| if(!empty($webhookDetails)){ |
| |
| foreach($webhookDetails as $webhookData){ |
| |
| $webhookContent = $webhookData->webhookContent; |
| |
| $authToken = $webhookData->authToken; |
| |
| |
| |
| $webhook = json_decode($webhookContent); |
| |
| |
| |
| $email = $webhook->email; |
| |
| $customerId = $webhook->id; |
| |
| |
| |
| $metafieldArray = array(); |
| |
| |
| |
| $temp['key'] = 'authToken'; |
| |
| $temp['value'] = $authToken; |
| |
| $temp['value_type'] = 'string'; |
| |
| $temp['namespace'] = 'global'; |
| |
| |
| |
| array_push($metafieldArray, $temp); |
| |
| |
| |
| $user = DB::table('users')->where('user_email', '=', $email)->get(); |
| |
| |
| |
| if(!isset($user[0]->user_email)){ |
| |
| // create user in DB |
| |
| $userId = DB::table('users')->insertGetId([ |
| |
| 'user_email' => $email, |
| |
| 'status' => 1, |
| |
| 'auth_token' => $authToken |
| |
| ]); |
| |
| } |
| |
| else |
| |
| { |
| |
| $userId = DB::table('users')->where('user_email', $email)->update(['auth_token' => $authToken]); |
| |
| |
| |
| } |
| |
| |
| |
| $method = 'GET'; |
| |
| $url = '/admin/api/'.$this->shopify_api_version.'/customers/'.$customerId.'/metafields.json'; |
| |
| $params = null; |
| |
| $result = $api->rest($method, $url, $params); |
| |
| |
| |
| 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'; |
| |
| $result = $api->rest($method, $url, $params); |
| |
| |
| |
| if(!empty($result->body->metafield)){ |
| |
| // Updated succesfully |
| |
| $updated = true; |
| |
| Log::channel('cron')->info("Updated metafield successgully for the user ".$customerId." with value ".$authToken); |
| |
| |
| |
| } |
| |
| else{ |
| |
| // not updated |
| |
| $updated = false; |
| |
| Log::channel('cron')->info("Failed to update metafield for the user ".$customerId." with value ".$authToken); |
| |
| Log::channel('cron')->info(print_r($result,1)); |
| |
| } |
| |
| |
| |
| } |
| |
| |
| |
| } |
| |
| |
| |
| if($updated == false){ |
| |
| |
| |
| $this->createCustomerMetafield($metafieldArray, $customerId, $authToken, $api); |
| |
| } |
| |
| }else{ |
| |
| |
| |
| $this->createCustomerMetafield($metafieldArray, $customerId, $authToken, $api); |
| |
| |
| |
| } |
| |
| |
| |
| } |
| |
| |
| |
| } |
| |
| } |
| |
| |
| |
| } |
| |
| |
| |
| function createCustomerMetafield($metafield, $customerId, $authToken, $api){ |
| |
| |
| |
| $params = array( |
| |
| 'customer' => array( |
| |
| 'id' => $customerId, |
| |
| 'metafields' => $metafield |
| |
| ) |
| |
| ); |
| |
| |
| |
| $method = 'PUT'; |
| |
| $url = '/admin/api/'.$this->shopify_api_version.'/customers/'.$customerId.'.json'; |
| |
| |
| |
| $result = $api->rest($method, $url, $params); |
| |
| |
| |
| if(!empty($result->body->customer)){ |
| |
| // Updated customer succssfully by adding new metafield |
| |
| $updated = true; |
| |
| Log::channel('cron')->info("Updated customer succssfully by adding new metafield ".$customerId." with value ".$authToken); |
| |
| |
| |
| } |
| |
| else{ |
| |
| // not updated |
| |
| $updated = false; |
| |
| Log::channel('cron')->info("Failed to update customer metafield for the user ".$customerId." with value ".$authToken); |
| |
| Log::channel('cron')->info(print_r($result,1)); |
| |
| } |
| |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
| |
| |
. | |
| |
| |
| |
. | } |
| |