Last active
November 6, 2017 16:22
-
-
Save OwenMelbz/01b3fa28556436669b41ef26ebc9af59 to your computer and use it in GitHub Desktop.
HubSpot Contact Form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Statamic\Addons\ContactService; | |
use Statamic\API\Email; | |
use Statamic\Extend\API; | |
class ContactServiceAPI extends API | |
{ | |
protected $enquiry; | |
const DEFAULT_NAME = 'no name provided'; | |
const DEFAULT_EMAIL = 'no email provided'; | |
const DEFAULT_COMPANY = 'no company provided'; | |
const DEFAULT_MESSAGE = 'no message provided'; | |
const DEFAULT_TELEPHONE = 'no telephone number provided'; | |
public function storeNewEnquiry($enquiry) | |
{ | |
$this->enquiry = array_merge([ | |
'name' => static::DEFAULT_NAME, | |
'email' => static::DEFAULT_EMAIL, | |
'company' => static::DEFAULT_COMPANY, | |
'message' => static::DEFAULT_MESSAGE, | |
'telephone' => static::DEFAULT_TELEPHONE, | |
], $enquiry); | |
$this->sendAdminConfirmation(); | |
$this->pushToHubSpot(); | |
$this->sendUserConfirmation(); | |
} | |
private function pushToHubSpot() | |
{ | |
$contactId = $this | |
->api('HubSpot') | |
->updateContact($this->enquiry); | |
$this | |
->api('HubSpot') | |
->addEngagement($contactId, $this->enquiry['message']); | |
} | |
private function sendAdminConfirmation() | |
{ | |
$email = Email::create() | |
->to(env('CONTACT_FORM_RECIPIENT')) | |
->replyTo($this->enquiry['email']) | |
->subject('Website Contact Enquiry') | |
->with($this->enquiry) | |
->template('contact-admin') | |
->send(); | |
} | |
private function sendUserConfirmation() | |
{ | |
$email = Email::create() | |
->to($this->enquiry['email']) | |
->subject('We’ve got your message!') | |
->with($this->enquiry) | |
->template('contact-user') | |
->send(); | |
} | |
public function nameStringToParts($string) | |
{ | |
$nameParts = preg_replace('/\s+/', ' ', trim($string)); | |
$nameParts = explode(' ', $nameParts); | |
$firstName = array_shift($nameParts); | |
$otherNames = implode(' ', $nameParts); | |
return [ | |
$firstName, | |
$otherNames, | |
]; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Statamic\SiteHelpers; | |
use Exception; | |
use Validator; | |
use Illuminate\Http\Request; | |
use Statamic\Extend\Controller as AbstractController; | |
class LetsStartTalkingController extends AbstractController | |
{ | |
public function create(Request $request) | |
{ | |
$validator = Validator::make($request->all(), [ | |
'name' => 'required', | |
'message' => 'required', | |
'email' => 'required|email|kickbox', | |
]); | |
if ($validator->fails()) { | |
return $this->api('JsonService')->errorsFromValidator($validator); | |
} | |
try { | |
$this->api('ContactService')->storeNewEnquiry($request->all()); | |
} catch (Exception $e) { | |
return response(['success' => false, 'message' => 'Oh dear, something’s gone a bit wrong. Please email us on [email protected] instead, and we’ll get back to you very shortly.'], 400); | |
} | |
return [ | |
'success' => true, | |
'message' => 'Messaged received! Thanks for getting in touch. We’ll get back to you as soon as possible. 👌', | |
]; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Statamic\Addons\HubSpot; | |
use Exception; | |
use Statamic\Extend\API; | |
use GuzzleHttp\Client as GuzzleClient; | |
use Statamic\Addons\ContactService\ContactServiceAPI; | |
class HubSpotAPI extends API | |
{ | |
public function updateContact($enquiry = []) | |
{ | |
$email = trim(strtolower($enquiry['email'])); | |
$company = trim($enquiry['company']); | |
$telephone = trim($enquiry['telephone']); | |
list($firstName, $lastName) = $this | |
->api('ContactService') | |
->nameStringToParts($enquiry['name']); | |
$data = [ | |
'properties' => [ | |
['property' => 'email', 'value' => $email], | |
['property' => 'firstname', 'value' => $firstName], | |
] | |
]; | |
// Make sure we only include the last name if we have it | |
// as if the user is already in HubSpot we would not want to overwrite it. | |
if (!empty($lastName)) { | |
$data['properties'][] = ['property' => 'lastname', 'value' => $lastName]; | |
} | |
// Same as last name, only import it if we have it as | |
// we we would not want to overwrite any decent hubtspot data. | |
if ($telephone !== ContactServiceAPI::DEFAULT_TELEPHONE) { | |
$data['properties'][] = ['property' => 'phone', 'value' => $telephone]; | |
} | |
if ($company !== ContactServiceAPI::DEFAULT_COMPANY) { | |
$data['properties'][] = ['property' => 'company', 'value' => $company]; | |
} | |
$contact = $this->sendRequest( | |
'contacts/v1/contact/createOrUpdate/email/' . $email, | |
$data | |
); | |
if (isset($contact->vid) && is_numeric($contact->vid)) { | |
return $contact->vid; | |
} | |
throw new Exception('Failed to insert contact into HubSpot'); | |
} | |
public function addEngagement($contactId, $message) | |
{ | |
$data = [ | |
'engagement' => [ | |
'type' => 'NOTE', | |
], | |
'associations' => [ | |
'contactIds' => [$contactId], | |
], | |
'metadata' => [ | |
'body' => $message, | |
] | |
]; | |
$engagement = $this->sendRequest( | |
'engagements/v1/engagements', | |
$data | |
); | |
if (isset($engagement->engagement->id) && is_numeric($engagement->engagement->id)) { | |
return $engagement->engagement; | |
} | |
throw new Exception('Failed to create engagement in HubSpot'); | |
} | |
private function sendRequest($endpoint, $data, $method = 'POST') | |
{ | |
$client = new GuzzleClient([ | |
'base_uri' => 'https://api.hubapi.com/', | |
]); | |
$response = $client->request($method, $endpoint, [ | |
'query' => ['hapikey' => env('HUBSPOT_API_KEY')], | |
'json' => $data | |
]); | |
$body = (string) $response->getBody(); | |
return json_decode($body); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment