Complex Workflows
Now let's go into an even more complex example, recording your payments that happened on Stripe in Modernbanc.
To do this, we must set up a workflow with a webhook trigger so we have a URL for Stripe to point to when they send us events. Then, we can parse the event, and create the wanted interactions within our Modernbanc workspace using a workflow.
curl --location --request POST 'http://api.modernbanc.com/v1/triggers' \
--header 'Content-Type: application/json' \
--header 'Authorization: ApiKey YOUR_API_KEY_HERE' \
--header 'x-workspace: mdb' \
--header 'x-environment: test' \
--data-raw '{
"name": "Stripe Webhook Trigger",
"description": "Listens for incoming webhooks from Stripe",
"version": {
"status": "published",
"start_date": "2023-01-10T03:54:50.146Z",
"type": "webhook"
}
}'
Creating a workflow and attaching our webhook trigger generates a URL that we can use to receive events from Stripe.
curl --location --request POST 'https://api.modernbanc.com/v1/workflows' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "Stripe webhook event with Modernbanc actions",
"description": "Workflow to accept webhook events from Stripe. Alerts our Slack channel, parses the information from the Stripe standardized event json object, and proceeds with the intended functionality within Modernbanc, such as updating accounts or transactions.",
"version": {
"status": "published",
"variables": {},
"steps": [
{
"name": "Connection Query - call slack webhook",
"parameters": {
"type": "http_api",
"query": {
"url": "https://hooks.slack.com/services/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",
"method": "post",
"headers": {
"Content-Type": "application/json"
},
"body": {
"text": "Stripe webhook received! {{_trigger_version.input.body}}"
}
},
"fail_execution_on_error_response": true
},
"type": "connection_query",
"trigger_version_ids": [
"39752256-2f69-4fc6-b881-0ba49ee05ed8"
]
}
],
"active_in_current_environment": true
}
}'
Click on the Webhook
button on the workflow page. On the right, a webhook URL will be displayed. Copy it.
Now that we have our webhook URL, we need to add it to the Stripe dashboard here.
For this example, we will listen for 2 events: payment_intent.created
and payment_intent.succeeded
. You can see the full list of events here.
Stripe event objects are standardized. You can see a full example object here.
Using the Stripe CLI, we can create a transaction that emits an event using this terminal command: stripe trigger payment_intent.created
.
Our slack channel now receives a notification with the Stripe event object information.
With our workflow, we can access the event object passed to the webhook trigger using {{_trigger_version._input.body}}
. Let's take a look at the object we received, and test out accessing the values within our workflow.
{
"id": "evt_3MI3WyJwZkDD8BxT0jFwW9Bj",
"object": "event",
"api_version": "2022-11-15",
"created": 1671772252,
"data": {
"object": {
"id": "pi_3MI3WyJwZkDD8BxT0AG7dtbD",
"object": "payment_intent",
"amount": 2000,
"amount_capturable": 0,
"amount_details": {
"tip": {
}
},
"amount_received": 0,
"application": null,
"application_fee_amount": null,
"automatic_payment_methods": null,
"canceled_at": null,
"cancellation_reason": null,
"capture_method": "automatic",
"client_secret": "pi_3MI3WyJwZkDD8BxT0AG7dtbD_secret_CQ2OE650e5kjOjy3kqBBehEiv",
"confirmation_method": "automatic",
"created": 1671772252,
"currency": "usd",
"customer": null,
"description": "(created by Stripe CLI)",
"invoice": null,
"last_payment_error": null,
"livemode": false,
"metadata": {
},
"next_action": null,
"on_behalf_of": null,
"payment_method": null,
"payment_method_options": {
"card": {
"installments": null,
"mandate_options": null,
"network": null,
"request_three_d_secure": "automatic"
}
},
"payment_method_types": [
"card"
],
"processing": null,
"receipt_email": null,
"review": null,
"setup_future_usage": null,
"shipping": null,
"source": null,
"statement_descriptor": null,
"statement_descriptor_suffix": null,
"status": "requires_payment_method",
"transfer_data": null,
"transfer_group": null,
"latest_charge": null
}
},
"livemode": false,
"pending_webhooks": 1,
"request": {
"id": "req_Eq55ktWpS8DhfI",
"idempotency_key": "32511018-930b-48cd-8a95-d668098b2b5d"
},
"type": "payment_intent.created"
}
TODO: parse data and create a transaction
Congratulations, you now have an understanding of how you can use Modernbanc to listen and react to external events!