CustomerController.php (Before) CustomerController.php (After)
<?php <?php
   
namespace App\Http\Controllers; namespace App\Http\Controllers;
   
header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Origin: *");
   
use Log; use Log;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use OhMyBrew\BasicShopifyAPI; use OhMyBrew\BasicShopifyAPI;
use Config; use Config;
   
class CustomerController extends Controller { class CustomerController extends Controller {
.   
   private $helper;    private $helper;
   private $shopify_api_version;    private $shopify_api_version;
.            
   function __construct() {    function __construct() {
.            
    $this->shopify_api_version = env('SHOPIFY_API_VERSION', true);        $this->shopify_api_version = env('SHOPIFY_API_VERSION', true);
   }         } 
   
   public function getCustomerActivationStatus(Request $request) {    public function getCustomerActivationStatus(Request $request) {
   
       $api = new BasicShopifyAPI();        $api = new BasicShopifyAPI();
   
.        $password   = Config::get('shopify.password');        $password   = Config::get('shopify.passwordCustomer');
       $shopDomain = Config::get('shopify.shopDomain');        $shopDomain = Config::get('shopify.shopDomainCustomer');
   
       $api->setShop($shopDomain);        $api->setShop($shopDomain);
       $api->setAccessToken($password);        $api->setAccessToken($password);
   
       //Log::info($_REQUEST['email']);        //Log::info($_REQUEST['email']);
                 
       $email = $request->input('email');        $email = $request->input('email');
       if(empty($email)) {        if(empty($email)) {
           $returnData = [            $returnData = [
               'error' => 'Parameter missing - email'                 'error' => 'Parameter missing - email' 
           ];            ];
   
            $this->sendResponse(600, "error", $returnData );             $this->sendResponse(600, "error", $returnData );
       }        }
               
       $params = NULL;        $params = NULL;
       $result = $api->rest('GET',        $result = $api->rest('GET',
               htmlentities('/admin/api/'.$this->shopify_api_version.'/customers/search.json?query=email:'.$email), $params);                htmlentities('/admin/api/'.$this->shopify_api_version.'/customers/search.json?query=email:'.$email), $params);
   
                 
   
       if(!isset($result->body->customers[0]) || empty($result->body->customers[0])) {        if(!isset($result->body->customers[0]) || empty($result->body->customers[0])) {
                         
           $returnData = [            $returnData = [
               "customer_available" => false,                "customer_available" => false,
               "activation_status" => null                "activation_status" => null
                 
           ];            ];
   
           $this->sendResponse(200, "success", $returnData );            $this->sendResponse(200, "success", $returnData );
       }        }
       else {        else {
           $customer = [];            $customer = [];
           foreach($result->body->customers as $cust) {            foreach($result->body->customers as $cust) {
               if($cust->email == $email) {                if($cust->email == $email) {
                   $customer = $cust;                    $customer = $cust;
                   break;                    break;
               }                }
           }            }
           $status = $customer->state;            $status = $customer->state;
   
           $returnData = [            $returnData = [
               "customer_available" => true,                "customer_available" => true,
               "activation_status" => $status                "activation_status" => $status
                 
           ];            ];
   
           $this->sendResponse(200, "success", $returnData );            $this->sendResponse(200, "success", $returnData );
       }        }
   
   }    }
   
   public function sendResponse($status,$message, $body) {    public function sendResponse($status,$message, $body) {
       $response = array(        $response = array(
           "status"       => $status,            "status"       => $status,
           "message" => $message,            "message" => $message,
           "body" => $body            "body" => $body
       );        );
       echo json_encode($response);        echo json_encode($response);
       die();        die();
   
   }    }
   
   public function getActivationUrl(Request $request) {    public function getActivationUrl(Request $request) {
   
       $api = new BasicShopifyAPI();        $api = new BasicShopifyAPI();
   
.        $password   = Config::get('shopify.password');        $password   = Config::get('shopify.passwordCustomer');
       $shopDomain = Config::get('shopify.shopDomain');        $shopDomain = Config::get('shopify.shopDomainCustomer');
   
       $api->setShop($shopDomain);        $api->setShop($shopDomain);
       $api->setAccessToken($password);        $api->setAccessToken($password);
   
       //Log::info($_REQUEST['email']);        //Log::info($_REQUEST['email']);
               
       $email = $request->input('email');        $email = $request->input('email');
                 
       if (empty($email)) {        if (empty($email)) {
           $returnData = [            $returnData = [
               'error' => 'Parameter missing - email'                 'error' => 'Parameter missing - email' 
           ];            ];
   
            $this->sendResponse(600, "error", $returnData );             $this->sendResponse(600, "error", $returnData );
       }        }
               
       $params = NULL;        $params = NULL;
       $result = $api->rest('GET',        $result = $api->rest('GET',
               htmlentities('/admin/api/'.$this->shopify_api_version.'/customers/search.json?query=email:'.$email), $params);                htmlentities('/admin/api/'.$this->shopify_api_version.'/customers/search.json?query=email:'.$email), $params);
                                 
       if (!isset($result->body->customers[0]) || empty($result->body->customers[0])) {        if (!isset($result->body->customers[0]) || empty($result->body->customers[0])) {
                         
           $returnData = [            $returnData = [
               'error' => 'email not found'                 'error' => 'email not found' 
           ];            ];
   
           $this->sendResponse(600, "error", $returnData );            $this->sendResponse(600, "error", $returnData );
       }        }
       $customer = '';        $customer = '';
       foreach($result->body->customers as $cust) {        foreach($result->body->customers as $cust) {
           if(strtolower(trim($cust->email)) == strtolower(trim($email))) {            if(strtolower(trim($cust->email)) == strtolower(trim($email))) {
               $customer = $cust;                $customer = $cust;
               break;                break;
           }            }
       }        }
   
       if (empty($customer)) {        if (empty($customer)) {
   
          $returnData = [           $returnData = [
               'error' => 'email not found'                 'error' => 'email not found' 
           ];            ];
   
           $this->sendResponse(600, "error", $returnData );            $this->sendResponse(600, "error", $returnData );
       }        }
   
       if (isset($customer->state) && strtolower(trim($customer->state)) == "enabled") {        if (isset($customer->state) && strtolower(trim($customer->state)) == "enabled") {
           $returnData = [            $returnData = [
               "activation_status" => "enabled"                 "activation_status" => "enabled" 
           ];            ];
   
            $this->sendResponse(200, "success", $returnData );             $this->sendResponse(200, "success", $returnData );
       }        }
   
                 
       $result = $api->rest('POST',        $result = $api->rest('POST',
               htmlentities('/admin/api/'.$this->shopify_api_version.'/customers/'.$customer->id.'/account_activation_url.json'), $params);                htmlentities('/admin/api/'.$this->shopify_api_version.'/customers/'.$customer->id.'/account_activation_url.json'), $params);
                 
                 
       if (!isset($result->body->account_activation_url) || empty($result->body->account_activation_url)) {        if (!isset($result->body->account_activation_url) || empty($result->body->account_activation_url)) {
   
           $returnData = [            $returnData = [
               'error' => 'Failed to create activation URL'                 'error' => 'Failed to create activation URL' 
           ];            ];
            $this->sendResponse(600, "error", $returnData );             $this->sendResponse(600, "error", $returnData );
       }        }
       else {        else {
           // $returnData = [            // $returnData = [
           //     "activation_status" => "disabled",            //     "activation_status" => "disabled",
           //     "activation_url" => $result->body->account_activation_url            //     "activation_url" => $result->body->account_activation_url
           // ];            // ];
                         
           $activationURL = $result->body->account_activation_url;            $activationURL = $result->body->account_activation_url;
                         
           $iterableMailRequest = array(            $iterableMailRequest = array(
   
               "campaignId" => intval(env('ITERABLE_CUSTOMER_ACTIVATION_CAMPAIGN_ID',true)),                "campaignId" => intval(env('ITERABLE_CUSTOMER_ACTIVATION_CAMPAIGN_ID',true)),
               "recipientEmail" => $email,                "recipientEmail" => $email,
               "dataFields" => array(                "dataFields" => array(
                   "customUrl" => $activationURL,                    "customUrl" => $activationURL,
               )                )
   
           );            );
   
           $url = env('ITERABLE_URL',true).'email/target?api_key='.env('ITERABLE_API_KEY',true);            $url = env('ITERABLE_URL',true).'email/target?api_key='.env('ITERABLE_API_KEY',true);
           $method = 'POST';            $method = 'POST';
   
           $triggerResponse = $this->executeCurl($url,$method,json_encode($iterableMailRequest));            $triggerResponse = $this->executeCurl($url,$method,json_encode($iterableMailRequest));
   
            $returnData = [             $returnData = [
                "activation_status" => "disabled",                 "activation_status" => "disabled",
                "mail_status" => "sent"                  "mail_status" => "sent" 
            ];             ];
   
           $this->sendResponse(200, "success", $returnData );            $this->sendResponse(200, "success", $returnData );
       }        }
   
     }      }
   
   public function executeCurl($url,$type,$data) {    public function executeCurl($url,$type,$data) {
                 
       $ch = curl_init();        $ch = curl_init();
       curl_setopt($ch, CURLOPT_URL, $url);        curl_setopt($ch, CURLOPT_URL, $url);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
       curl_setopt($ch, CURLOPT_MAXREDIRS, 10);        curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
       curl_setopt($ch, CURLOPT_TIMEOUT, 30);        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
       curl_setopt($ch, CURLOPT_HEADER, FALSE);        curl_setopt($ch, CURLOPT_HEADER, FALSE);
       curl_setopt($ch, CURLOPT_HTTPHEADER, array(        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
               "Content-Type: application/json"                 "Content-Type: application/json" 
       ));        ));
       curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // temporarily disable SSL verification        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // temporarily disable SSL verification
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // temporarily disable SSL verification        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // temporarily disable SSL verification
   
       if ($type == "POST"){        if ($type == "POST"){
           curl_setopt($ch, CURLOPT_POST, TRUE);            curl_setopt($ch, CURLOPT_POST, TRUE);
           curl_setopt($ch, CURLOPT_POSTFIELDS, $data);            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
       }        }
   
       $result = curl_exec($ch);        $result = curl_exec($ch);
   
       if(curl_errno($ch)){        if(curl_errno($ch)){
           echo 'Request Error:' . curl_error($ch);            echo 'Request Error:' . curl_error($ch);
           return false;            return false;
       }        }
                 
       Log::info("curl result  => ".$result);        Log::info("curl result  => ".$result);
       return $result;        return $result;
   }    }
} }