Skip to content

Instantly share code, notes, and snippets.

@eprugh
Last active November 14, 2016 21:19
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 eprugh/b0b64125035053105171f787b1f3a39f to your computer and use it in GitHub Desktop.
Save eprugh/b0b64125035053105171f787b1f3a39f to your computer and use it in GitHub Desktop.
Loading PactSafe in your PHP app.
<?php
// see the attached pactsafe-functions.php below
include "pactsafe-functions.php";
// insert your token here
// get your token at https://pactsafe.gelato.io/api-explorer/pactsafe-api/versions/1.1
$token = "XXX";
// CREATE REQUEST
// Site ID must be set (https://app.pactsafe.com/settings/account)
// More examples on routes here: https://pactsafe.gelato.io/api-explorer/pactsafe-api/versions/1.1
// Request fields pass in standard metadata
// render_data is the dynamic data populated in the contract
$request_fields = array(
"name" => $_POST['company'] . " Order Form",
"subject" => $_POST['subject_line'],
"message" => $_POST['message'],
"render_data" => array (
"upgrade_product" => "Monthly Subscription",
"upgrade_cost" => $_POST['monthly_price'],
"total_cost" => $_POST['monthly_price'],
"payment_terms" => $_POST['payment_terms']
)
);
// create() calls a POST route https://api.pactsafe.com/v1.1/sites/{$site_id}/requests
// note the $token is set as the static token
$request = json_decode(create("/sites/{$site_id}/requests", $request_fields, $token));
// with json_decode, it converts a JSON response into a PHP object
$request_id = $request->data->id;
// now use a PATCH/"set" to add a signer to the request
$signer = "eric@pactsafe.com";
$add_signers = set("/requests/{$request_id}/signers/{$signer}", array(), $token);
// now try using a GET to retrieve the digitally signed URL for the signer
$url_object = get("/requests/{$request_id}/url?signer_id={$signer}", $token);
$url = $url_object->location;
// SEND request for signature using a POST to the /send route
$send_contract = create("/requests/{$request_id}/send", array(), $token);
header("location: {$url}");
?>
<?php
function get ($route, $token) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.pactsafe.com/v1.1" . $route);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"Authorization: Bearer " . $token
));
$response = curl_exec($ch);
return json_decode($response)->data;
curl_close($ch);
}
function set ($route, $fields, $token){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.pactsafe.com/v1.1" . $route);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"Authorization: Bearer " . $token
));
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
function create ($route, $fields, $token){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.pactsafe.com/v1.1" . $route);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"Authorization: Bearer " . $token
));
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment