Jump to content

Trustpilot’s automatic invitation


Recommended Posts

We've got this on the roadmap.

In the meantime I actually wrote a standalone script for this as a proof of concept using the Upmind API just to test how the AFS worked. It's very very basic and was simply a proof of concept, but should work fine for you until we do a proper integration into Upmind

You'll need to configure the settings in Trustpilot

--

<?php

class upmindAuth {

    public $api_url;
    public $origin;
    public $token;
    
    function __construct($origin){
        $this->api_url = 'api.upmind.io'; //live api url
        $this->origin = $origin; // Brand Origin
    }
    
    public function validate($username,$password){
        if (!$this->api_token(array('username'=>$username,'password'=>$password,'grant_type'=>'admin'))){
            return false;
        }
        return true;
    }
    
    public function api_token($params){
    
        $curl = curl_init();
        curl_setopt_array($curl, array(
          CURLOPT_URL => "https://".$this->api_url."/oauth/access_token",
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => "",
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 30,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => "POST",
          CURLOPT_POSTFIELDS => json_encode($params),
          CURLOPT_HTTPHEADER => array(
            "accept: application/vnd.pub.v1+json",
            "cache-control: no-cache",
            "content-type: application/json",
            "origin: $this->origin",
          ),
        ));
    
        $response = curl_exec($curl);
        $err = curl_error($curl);
    
        if ($err) {
            return array('success'=>false,'error'=>"cURL Error #:" . $err);
        } else {
            $actor = json_decode($response);           
            $this->token = $actor->access_token;
            return true;
        }
    }
    
    public function get($endpoint){
      
        $curl = curl_init();
        curl_setopt_array($curl, array(
        CURLOPT_URL => "https://".$this->api_url.$endpoint,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "GET",
        CURLOPT_HTTPHEADER => array(
          "authorization: Bearer ".$this->token,
          "cache-control: no-cache",
          "origin: ".$this->origin,
        ),
      ));
    
      $response = curl_exec($curl);
    
      $err = curl_error($curl);
      
      if ($err) {
        echo "cURL Error #:" . $err;
      } else {
          return json_decode($response);
      }
    }
    
    public function post($endpoint,$params){
    
      $curl = curl_init();
      curl_setopt_array($curl, array(
      CURLOPT_URL => "https://".$this->api_url.$endpoint,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "POST",
      CURLOPT_POSTFIELDS => $params,
      CURLOPT_HTTPHEADER => array(
        "authorization: Bearer ".$this->token,
        "cache-control: no-cache",
        "origin: ".$this->origin,
      ),
    ));
    
    $response = curl_exec($curl);
    $err = curl_error($curl);
    
    if ($err) {
      echo "cURL Error #:" . $err;
    } else {
        return json_decode($response);
    }
    }
    
}

// Upmind API Creds
$instance = ""; // Your Upmind Brand URL (E.g. https://my.domain.com)
$user = ""; // username
$pass = ""; // password

//Trustpilot email
$tpemail = ""; // Trustpilot invite email

// Overrides
$templateId = false;
$senderEmail = ""; // e.g. [email protected]
$senderName = ""; // e.g. Brand Name
$replyTo = ""; // e.g. [email protected]

$delay = 90; // Number of days after signup to send the email

// Now we get latest orders from Upmind
$upmind = new upmindAuth($instance);
if (!$upmind->validate($user,$pass)){
    echo "Username and password incorrect"; die;
}

// Single API call to get the data we need
$endpoint = "/api/admin/invoices?filter[category.slug]=new_contract&with=client,status,products,products.contracts_product,products.product.image&limit=50&offset=0&order=-created_at";
if ($delay){
    $startdate = date("Y-m-d",time()-(86400*$delay));
    $endpoint = $endpoint."&filter[created_at%7Clte]=".$startdate;
}
    
$output = $upmind->get($endpoint);

// Loop through data
foreach ($output->data as $d){
    $skip = false;
    empty($client);
    $client['email'] = $d->client->email;//
    $client['name'] = $d->client->public_name;
    $client['orderid'] = $d->number;
    $client['products'] = array();
    
    foreach ($d->products as $p){
        $client['products'][] =
        array("name" => $p->product_name,
            "sku" => $p->product_id,
            "brand" => $p->invoice_brand->name,
            "productUrl" => $instance."/order/product?pid=".$p->product_id,
            "imageUrl" => $p->product->image->full_url
        );
        if ($p->contracts_product->status->code !== 'contract_active') {
            // This service is not active so do not request review
            $skip = true;
        }
    }


    $snippet = array(
        "recipientEmail" => $client['email'],
        "recipientName"=> $client['name'],
        "referenceId"=> "Order " . $client['orderid'],
        "locale" => "en-GB",
        "senderEmail" => $senderEmail,
        "senderName" => $senderName,
        "replyTo" => $replyTo,
        "products" => $client['products']
    );

// Set overrride values if they exist
    if ($templateId){
        $snippet['templateId'] = $templateId;
    }

if (!$skip){
    $emailContent = "<script type=\"application/json+trustpilot\">" . json_encode($snippet) . "</script>";
    mail($tpemail,"Invoice " . $client['orderid'],$emailContent);
}

}

 

 

  • Thanks 1
Link to comment
Share on other sites

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...