PactSafe

The PactSafe Developer Hub

Welcome to the PactSafe developer hub. You'll find comprehensive guides and documentation to help you start working with PactSafe as quickly as possible, as well as support if you get stuck. Let's jump right in!

Get Started    Guides

Using PS.js with back-end API calls

You may want to use PS.js to render the contracts on the page, populate a contract with app data, and more—but send the API call on the back-end. Here's how to do it!

In this article, we'll cover how you can use the JavaScript library in tandem with our API to send acceptance of a contract or contracts on the back-end. You'll learn how to:

  • Load PS.js and render the contract on the page
  • Set the Signer ID of the signer programmatically
  • Disable sending when the checkbox on a click-through agreement is clicked
  • Send the payload that would be sent automatically to your back-end.

What you need

In order to complete this article, you'll need the following already working:

  • A published Contract that's been marked "Public"
  • A published Group with a Group Key
  • Working knowledge of JavaScript and of PS.js

Why should I do this?

By default, when you load an embedded contract onto a page, we'll automatically send acceptance of a contract as soon as your signer checks the box. Some people don't want to do this, so we've got a couple of different ways to handle sending acceptance. Below you can see our proposed flow for:

  1. Loading PS.js and embedding the contracts into your page, which will dynamically grab the right contracts and versions and inject them into the page.
  2. Send acceptance after a user completes the form using the Activity API.

Loading the embedded contract

To get the contract loaded into your page, simply use the snippet below and swap out with your own Site Access ID (found here) and your Group Key (found in your Group):

Notice the line in the embed code above that disables sending of the acceptance automatically:

_ps('create', 'eebbf489-fb84-4a0c-b624-51f2ead3104b', { disable_sending: true });

Sending acceptance manually

Disabling sending will prevent the acceptance from being sent automatically, so you'll need to send acceptance manually. One way to do this is as so:

_ps('set', 'signer_id', 'hi@pactsafe.com');

// send manually by overriding the disable_sending parameter
_ps('group-key:send', 'agreed', { disable_sending: false });

Sending acceptance on the back-end

Instead of sending acceptance in JavaScript, you can send acceptance on the back-end of your application on the server-side. This can enhance the security of your app and integration by not allowing for anything to be sent client-side in JavaScript.

The best way to do this is to grab the payload of the user and passing it into a hidden input field that's passed to the back-end or interstitial page that sits between your form and the redirect that the user is directed to after completing the form:

// set the payload that would be send in PS.js to a hidden variable
$('input#payload').attr('value', _ps.site.group.getPayload());

/* 
example: 
v=1&_v=ps1&uid=55e0820564a5846a5a0387c4&sid=eebbf489-fb84-4a0c-b624-
51f2ead3104b&sig=eric%40pactsafe.com&vid=5a206d6f64e7644d70331f35%2C5aa15dbe87e6e
94b950f9411&cid=12913%2C14576&gid=688&cus=%7B%22skus%22%3A%22SKU1234%2C%20SKU123%
22%7D&cnf=0&tm=1&pau=https%3A%2F%2Ffiddle.jshell.net%2Feprugh%2F7qngu0km%2Fshow%2
F&pad=fiddle.jshell.net&pap=%2Feprugh%2F7qngu0km%2Fshow%2F&pae=UTF-
8&ref=https%3A%2F%2Fjsfiddle.net%2Feprugh%2F7qngu0km%2F&btz=4&bl=en-
us&bje=0&os=MacOS&env=desktop&scd=24-bit&res=1920x1080&dim=937x568&jur=en-
us&_s=3&sess=5b5a18a1851cdf018fceaf60&rdid=5b5a18d4851cdf018fceb03d&nc=132126542 
*/

Then, all you have to do on your back-end is an HTTP POST to https://pactsafe.io/send and append the getPayload value as a query string:

<?

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, "https://pactsafe.io/send?" . $_POST['payload']);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($ch, CURLOPT_HEADER, FALSE);
  $response = curl_exec($ch);
  curl_close($ch);

?>

What's Next

Get more dynamic in how you load and track acceptance on embedded contracts:

How to use Dynamic Contracts with the Javascript Library
Dynamic Groups (and how to use them)