Last active
March 2, 2023 20:43
-
-
Save timthesinner/6d194b3ed9817a4b281a90be166bf0c3 to your computer and use it in GitHub Desktop.
Creating a FUB embedded application for CaaS
This file contains 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
// Making some assumptions that we are using word press and PHP | |
add_action( 'rest_api_init', 'create_custom_route' ); | |
function create_custom_route() { | |
register_rest_route( 'custom/v1', '/template', array( | |
'methods' => 'GET', | |
'callback' => 'custom_caas_html_callback', | |
)); | |
} | |
function lookup_ids( $fub_account_id, $fub_user_id, $fub_lead_id ) { | |
$account_id = 12345; | |
$sales_person_id = 67890; | |
$lead_id = 54321; | |
// Perform any necessary lookups here to retrieve the actual values of these fields. | |
return array( | |
'account_id' => $account_id, | |
'sales_person_id' => $sales_person_id, | |
'lead_id' => $lead_id, | |
); | |
} | |
function custom_caas_html_callback( WP_REST_Request $request ) { | |
$body = $request->get_param( 'context' ); | |
$signature = $request->get_param( 'signature' ); | |
$secret_key = getenv( 'FUB_SECRET_KEY' ); | |
if ( empty( $secret_key ) ) { | |
return new WP_Error( 'missing_secret_key', 'Secret key not found.', array( 'status' => 500 ) ); | |
} | |
$expected_signature = hash_hmac( 'sha256', $body, $secret_key ); | |
if ( !hash_equals( $signature, $expected_signature ) ) { | |
return new WP_Error( 'invalid_signature', 'Invalid signature.', array( 'status' => 403 ) ); | |
} else { | |
$decoded_body = json_decode( base64_decode( $body ) ); | |
if ( ! $decoded_body ) { | |
return new WP_Error( 'invalid_body', 'Unable to decode body.', array( 'status' => 400 ) ); | |
} | |
$account = $decoded_body->account; | |
$owner = $account->owner; | |
$user = $decoded_body->user; | |
$person = $decoded_body->person; | |
$ids = lookup_ids($account->id, $user->id, $person->id); | |
$account_id = $ids['account_id']; | |
$sales_person_id = $ids['sales_person_id']; | |
$lead_id = $ids['lead_id']; | |
$html = '<html> | |
<head> | |
<script src="https://eia.followupboss.com/embeddedApps-v1.0.1.js"></script> | |
<script src="https://your-domain.caas.structurely.com"></script> | |
</head> | |
<body> | |
<structurely-caas | |
client-key="' . getenv( 'CAAS_CLIENT_KEY' ) . '" | |
client-host="https://your-domain.caas.structurely.com" | |
account-id="' . esc_attr($account_id) . '" | |
account-name="' . esc_attr($account->domain) . '" | |
sales-person-id="' . esc_attr($sales_person_id) . '" | |
sales-person-name="' . esc_attr($user->name) . '" | |
lead-id="' . esc_attr($lead_id) . '" | |
lead-name="' . esc_attr($person->firstName) . ' ' . esc_attr($person->lastName) . '" | |
lead-email="' . esc_attr(reset($person->emails)->value) . '" | |
lead-phone="' . esc_attrreset($person->phones)->value) . '" | |
/> | |
</body> | |
</html>'; | |
return $html; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment