Skip to content

Instantly share code, notes, and snippets.

@deniszgonjanin
Last active February 22, 2024 18:22
Show Gist options
  • Save deniszgonjanin/c988fd0cfbf705b8e2175f1d03db23c3 to your computer and use it in GitHub Desktop.
Save deniszgonjanin/c988fd0cfbf705b8e2175f1d03db23c3 to your computer and use it in GitHub Desktop.
Flow: Trigger Downpay payment using Order tags

Shopify Flow: Triggering deferred payment collection through Order tags

This workflow will cause the deferred payment on orders made through Downpay to be collected once a specific tag is added to those orders.

The Shopify Flow workflow for this has four steps:

  • Schedule a run
  • Query for orders tagged with a specific tag
  • Split the results of the query into individual orders
  • For each of the orders, call the Downpay API to trigger deferred payment collection

Link to exported flow. You can import this file into the flow app installed into your shop and run it. Just remember to change the Downpay API key in the final action, as well as the tag you'd like to use. Screenshot of the whole flow.

Detailed explanation of the flow below:

Scheduling a run

Often it's best to use a webhook trigger to start a run, but since Shopify Flow does not expose a trigger for when an order is tagged, we will setup a simple clock-based trigger.

The schedule trigger is found under the 'Flow' selection of triggers.

In our example, the trigger is set to run once an hour. That may not be the most appropriate for your use case, so you can alter this as needed based on when you'd like deferred payment collection to take place - perhaps at a specific time every day or every week.

Querying for orders

The scheduler will trigger a Get Order Data action, which is under the Shopify Admin API set of Actions. This action will allow us to query for orders based on some criteria. In our case, we're going to fetch the 50 latest updated orders. In the filtering section of the action, we're going to choose 'Advanced', which allows us to write a custom query. For the query, write down:

tag:collect

Once this action runs, it will query for the last 50 orders tagged with collect. You may be using a different tag to trigger this, so make sure to adjust accordingly.

The configuration for our Get Order Data action looks like this.

Splitting up orders

Each order we want to collect for requires a single API call to the orderCollectPayment Downpay API endpoint. So the next thing we need to do is to split up the results of the previous action into separate orders. After we've done that we'll be able to execute a single API call for each.

To split up the orders from the previous step, use the For each Loop (iterate) action, which is found in the Flow set of available actions.

All you need to do to configure that action is to select the Get Order Data list as the input configuration.

Call the Downpay API

The Downpay API action is actually a Send HTTP request action, which is also found under the Flow set of available actions.

There are a few configuration options to enter here:

  • HTTP Method: Set to POST
  • URL: Set to https://downpay.hypehound.app/graphql

For Headers, there are two to set:

  • Key: Content-Type, value: Application/json
  • Key: Authorization, Value: Token <your_api_key>

You can find the Downpay API key for your shop inside the Downpay app, under the Settings page.

For the Body field, enter the following:

{
  "query": "mutation orderCollectPayment($input: OrderCollectPaymentInput!){orderCollectPayment(input: $input) { errors { field message } } }",
  "operationName": "orderCollectPayment",
  "variables": {
    "input": {
      "id": "{{getOrderDataForeachitem.id}}"
    }
  }
}

This query will take the id of each of the orders we are passing to the action, and send it to Downpay as part of the orderCollectPayment API call.

That's it, your Flow is now ready. You can activate it and then test it by tagging some of your Downpay-enabled orders. The flow will retrieve the latest tagged orders and attempt to collect payment for them. Note that if the payment has already been collected for those orders, nothing will happen. So you don't need to worry about trying to collect payment for orders which have already been collected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment