line

Amazon Dash Button Workflow

This example shows how to create a workflow similar to an Amazon Dash Button. An Amazon Dash Button triggers actions when someone clicks on it. It is often used for something you need regularly. During the process, the customer needs to be charged and receive the billing according to the payment process. Stripe - in charge of payment - and Quickbooks -for the billing - are two connectors used in the example to interact with external services.

This example shows how to create a workflow similar to an Amazon Dash Button. An Amazon Dash Button triggers actions when someone clicks on it. It is often used for something you need regularly. During the process, the customer needs to be charged and receive the billing according to the payment process. Stripe - in charge of payment - and Quickbooks -for the billing - are two connectors used in the example to interact with external services.

  • Charge customer for the order processed.
  • If the payment information is not up to date, then ask the customer to update their payment details.
  • If payment details are not updated within two weeks, then cancel the order
  • Send customer an invoice when their payment is processed and send the order to shipping

Flowchart of the workflow

This flowchart helps to visualize different tasks within the workflow.

Workflow Code

This workflow is the code that orchestrates tasks (through the Zenaton workflow engine) and executes them on your servers. Tasks will be dispatched as soon as the user presses the button to order something.

const { workflow, duration } = require("zenaton");

module.exports = workflow("OrderFromDashButton", {
  *handle(order) {
    const stripe = this.connector(
      'stripe',
      'your-stripe-connector-id-from-zenaton-dashboard'
    );
    const quickbooks = this.connector(
      'quickbooks',
      'your-quickbooks-connector-id-from-zenaton-dashboard'
    );

    const charge = yield stripe.post(`/v1/charges`, {amount: order.total});
    const capture_response = yield stripe.post(`/v1/charges/${charge.id}/capture`);
    const charged = capture_response.captured

    let event = null;

    if (!charged) {
      this.run.task("AskForNewPaymentDetails", order);
      event = yield this.wait.event("OrderPaid").for(duration.weeks(2));
    }

    if (charged || event) {
      const realmID = order.realmID
      const email = order.email

      // Create an invoice
      const invoice_lines = this.invoice_lines(order);
      const invoice = yield quickbooks.post(`/v3/company/${realmID}/invoice`, invoice_lines);
      
      // Send the invoice
      yield quickbooks.post(`/v3/company/${realmID}/invoice/${invoice.Invoice.Id}/send?sendTo=${email}`);

      this.run.task("SendOrderToShipping", order);
    } else {
      this.run.task("CancelOrder", order);
    }
  },
  invoice_lines: function(order) {
    // ...
  }
});