Skip to content

Instantly share code, notes, and snippets.

@spigotdesign
Last active December 7, 2022 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spigotdesign/53f950ae0b7ea94576e719182d6f3b2e to your computer and use it in GitHub Desktop.
Save spigotdesign/53f950ae0b7ea94576e719182d6f3b2e to your computer and use it in GitHub Desktop.
ninjaquote
add_action( 'gform_after_submission_3', 'post_to_ninjaquote', 10, 2 );
$parsed_json = "";
function post_to_ninjaquote( $entry, $form ) {
$endpoint_url = 'https://92jllb1dk7.execute-api.us-east-1.amazonaws.com/dev/tdk-quoter';
$product_type = rgar( $entry, '1' );
$state = rgar( $entry, '8' );
$gender = rgar( $entry, '6' );
$birthdate = rgar( $entry, '4' );
$rate_class = rgar( $entry, '20' );
$smoker = rgar( $entry, '19' );
$coverage = rgar( $entry, '17' );
$term = rgar( $entry, '10' );
$body = '{
"state": "{$state}",
"birthdate": "{$birthdate}",
"gender": "{$gender}",
"rate_class": "pp",
"smoker" : "{$smoker}",
"coverage" : "{$coverage}",
"term" : "{$term}"
}';
// This is dummy test data
$quoteInputData = '{
"state": "TN",
"birthdate": "1952-01-20",
"gender": "m",
"rate_class": "pp",
"smoker" : "true",
"coverage" : "1000000",
"term" : "10"
}';
// GFCommon::log_debug( 'gform_after_submission: body => ' . print_r( $body, true ) );
// $response = wp_remote_post( $endpoint_url, array( 'body' => $body ) );
// GFCommon::log_debug( 'gform_after_submission: response => ' . print_r( $response, true ) );
function buildURL($rawURL, $queryParams) {
$formattedData = json_decode($queryParams, true);
foreach($formattedData as $key => $value) {
$rawURL .= "$key=$value&";
};
$urlWithQueryParams = rtrim($rawURL, "&");
return $urlWithQueryParams;
}
function getQuote($url, $params) {
$url = buildURL($url, $params);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
global $response;
$response = getQuote($endpoint_url, $quoteInputData);
$parsed_json = json_decode($response, true);
$parsed_json = $parsed_json['results'];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment