Brightback SPA JS Example

Single Page App example JS

by brightback

HTML

<script type="text/javascript" src="https://app.brightback.com/js/current/brightback.js"></script>

<button onclick="goToBrightbackCancel()" type="button">
  Cancel
</button>

JavaScript

/***********
Steps to implement
- Change app_id to your own. Find it in Settings page within Brightback
- Update fields
- Add error handling and fallback path if Brightback does not respond

************/
function goToBrightbackCancel() {

  window.Brightback.handleDataPromise({
    app_id: 'X70vmA5v4L', //UPDATE with your app id
    first_name: 'John',
    last_name: 'Doe',
    email: 'jdoe@example.com',
    save_return_url: 'https://site.com/account',
    cancel_confirmation_url: 'https://site.com/account/cancel',
    account: {
      company_name: 'Acme Products',
      company_domain: 'acme.com',
      internal_id: '1234AZ55',
      billing_id: 'cus_FfV4CXxpR8nAqB',
      plan: 'enterprise',
      created_at: 1312182000
    },
    custom: {
      activity: {
        emails: 42054,
        templates: 81,
        contacts: 102444
      }
    }
  }).then(result => {
    console.log('result = ' + JSON.stringify(result));

    if (result.valid) {
      // Redirect to Brightback
      window.location = result.url;
    } else {
      // Fallback path
    }
  });

}