Woodshop for old agent generation
Try the new agent generation
  • What is woodshop
  • How to's
    • Smart Relationship
      • GetIdsFromRequest
    • Smart views
      • Display a calendar view
      • Create a custom tinder-like validation view
      • Create a custom moderation view
      • Create a dynamic calendar view for an event-booking use case
    • Configure environment variables
      • NodeJS/Express projects
    • Elasticsearch Integration
      • Interact with your Elasticsearch data
      • Elasticsearch service/utils
      • Another example
    • Zendesk Integration
      • Authentication, Filtering & Sorting
      • Display Zendesk tickets
      • Display Zendesk users
      • View tickets related to a user
      • Bonus: Direct link to Zendesk + change priority of a ticket
    • Dwolla integration
      • Display Dwolla customers
      • Display Dwolla funding sources
      • Display Dwolla transfers
      • Link users and Dwolla customers
      • Dwolla service
    • Make filters case insensitive
    • Use Azure Table Storage
    • Create multiple line charts
    • Create Charts with AWS Redshift
    • View soft-deleted records
    • Send Smart Action notifications to Slack
    • Authenticate a Forest Admin API against an OAuth protected API Backend
    • Translate your project into TypeScript
      • V8
        • Migrate Mongoose files
        • Migrate Sequelize files
      • v7
        • Migrate Mongoose files
        • Migrate Sequelize files
      • v6
    • Geocode an address with Algolia
    • Display/edit a nested document
    • Send an SMS with Zapier
    • Hash a password with bcrypt
    • Display a customized response
    • Search on a smart field with two joints
    • Override the count route
    • Make a field readOnly with Sequelize
    • Hubspot integration
      • Create a Hubspot company
      • Display Hubspot companies
    • Impersonate a user
    • Import data from a CSV file
    • Import data from a JSON file
    • Load smart fields using hook
    • Pre-fill a form with data from a relationship
    • Re-use a smart field logic
    • Link to record info in a smart view
    • Display data in html format
    • Upload files to AWS S3
    • Display AWS S3 files from signed URLs
    • Prevent record update
    • Display, search and update attributes from a JSON field
    • Add many existing records at the same time (hasMany-belongsTo relationship)
    • Track users’ logs with morgan
    • Search on relationship fields by default
    • Export related data as CSV
    • Run automated tests
  • Forest Admin Documentation
Powered by GitBook
On this page

Was this helpful?

  1. How to's
  2. Dwolla integration

Dwolla service

PreviousLink users and Dwolla customersNextMake filters case insensitive

Last updated 4 years ago

Was this helpful?

This service wraps the and provides the following implementation:

  • Pagination (on Customers & Transfers)

  • Fields to be displayed on the UI (select)

  • Search (on Customers & Transfers)

  • Filters (on Customers, cf isFilterable flag)

"use strict";

const dwolla = require('dwolla-v2');
var _ = require('lodash');

class DwollaService {
  // Allow to create a Dwolla Client based on the App Key a Secret
  constructor(appKey, appSecret, environment); 

  // Get a List of Customers based on the query (page, filter, search, sort)
  getCustomers (query);

  // Get a Customer by Id
  getCustomer (recordId);

  // Get a Customer for a local database user (by email)
  getCustomerSmartRelationship (user);

  // Get a list of Funding Sources for a customer Id
  getCustomerFundingSources (recordId, query);

  // Get a Funding Source by Id
  getFundingSource (recordId);

  // Get a list of Transfers for a customer Id
  getCustomerTransfers (recordId, query);

  // Get a Transfer by Id
  getTransfer (recordId);


}

module.exports = DwollaService;
"use strict";

const dwolla = require('dwolla-v2');
var _ = require('lodash');

class DwollaService {
  constructor(appKey, appSecret, environment) {
    this.client = new dwolla.Client({
      key: appKey,
      secret: appSecret,
      environment: environment // optional - defaults to production
    });
  } 

  getCustomers (query) {
    const limit = parseInt(query.page.size) || 20;
    const offset = (parseInt(query.page.number) - 1) * limit;
    const sortBy = query.sort; // Sort Parameter
    let fields = query.fields.dwollaCustomers.split(',');
    if (fields && !fields.includes('id')) {
      fields.push('id'); // id is required to get the ID
    }

    let opts = {
      search: query.search, // Search parameter
      limit,
      offset,
    };
    
    // Build the Filter parameter
    if (query.filters) {
      const filters = JSON.parse(query.filters);
      if (filters.aggregator) {
        for (const filter of filters.conditions) {
          opts[filter.field] = filter.value;
        }
      }
      else {
        opts[filters.field] = filters.value;
      }
    } 

    return this.client.auth.client()
    .then(appToken => appToken.get('customers', opts))
    .then(result => {
      if (!result.body && !result.body._embedded) return null;
      let dwollaCustomers = [];
      // Only populate the fields required by the UI
      for (const customer of result.body._embedded.customers) {
        const clonePicked = _.pick(customer, fields);
        dwollaCustomers.push(clonePicked);
      }
      const count = result.body.total;
      return { list: dwollaCustomers, count }
    });

  };

  async getCustomer (recordId) {

    return this.client.auth.client()
    .then(appToken => appToken.get(`customers/${recordId}`))
    .then(result => {
      if (!result.body) return null;
      let dwollaCustomer = result.body;
      return dwollaCustomer;
    });    
  }

  async getCustomerSmartRelationship (user) {
    return this.client.auth.client()
    .then(appToken => appToken.get('customers', { email: user.email })) // filter on email
    .then(result => {
      if (!result.body && !result.body._embedded && result.body._embedded.customers.length !== 1 ) return null;
      let dwollaCustomer = result.body._embedded.customers[0];
      // Only populate the fields required by the UI
      const clonePicked = _.pick(dwollaCustomer, ['id', 'email', 'firstName', 'lastName' ]); // We ask only for the reference field + ID
      return clonePicked;
    });
  }

  getCustomerFundingSources (recordId, query) {
    // No Pagingation available for this endpoint
    // const limit = parseInt(query.page.size) || 20;
    // const offset = (parseInt(query.page.number) - 1) * limit;

    let fields = query.fields.dwollaFundingSources.split(',');
    if (fields && !fields.includes('id')) {
      fields.push('id'); // id is required to get the ID
    }
    if (fields && !fields.includes('balance') && fields.includes('balanceReadable')) {
      fields.push('balance'); // balance is required for the amountReadable Smart Field
    }    
    return this.client.auth.client()
    .then(appToken => appToken.get(`customers/${recordId}/funding-sources`))
    .then(result => {
      if (!result.body && !result.body._embedded) return null;
      let fundingSources = [];
      // Only populate the fields required by the UI
      for (const fundingSource of result.body._embedded['funding-sources']) {
        const clonePicked = _.pick(fundingSource, fields);
        fundingSources.push(clonePicked);
      }
      const count = fundingSources.length;
      return { list: fundingSources, count }
    });

  };

  getFundingSource (recordId) {
    return this.client.auth.client()
    .then(appToken => appToken.get(`funding-sources/${recordId}`))
    .then(result => {
      if (!result.body) return null;
      let fundingSource = result.body;
      return fundingSource;
    });    
  };

  getCustomerTransfers (recordId, query) {
    const limit = parseInt(query.page.size) || 20;
    const offset = (parseInt(query.page.number) - 1) * limit;

    let fields = query.fields.dwollaTransfers.split(',');
    if (fields && !fields.includes('id')) {
      fields.push('id'); // id is required to get the ID
    }
    if (fields && !fields.includes('amount') && fields.includes('amountReadable')) {
      fields.push('amount'); // amount is required for the amountReadable Smart Field
    }

    let opts = {
      search: query.search, // Search parameter
      limit,
      offset,
    };

    return this.client.auth.client()
    .then(appToken => appToken.get(`customers/${recordId}/transfers`, opts))
    .then(result => {
      if (!result.body && !result.body._embedded) return null;
      let transfers = [];
      // Only populate the fields required by the UI
      for (const fundingSource of result.body._embedded['transfers']) {
        const clonePicked = _.pick(fundingSource, fields);
        transfers.push(clonePicked);
      }
      const count = transfers.length;
      return { list: transfers, count }
    });

  };

  getTransfer (recordId) {
    return this.client.auth.client()
    .then(appToken => appToken.get(`transfers/${recordId}`))
    .then(result => {
      if (!result.body) return null;
      let fundingSource = result.body;
      return fundingSource;
    });    
  };


}

module.exports = DwollaService;
Dwolla SDK