Skip to main content

I want to make a flow for customers that had a failed credit card payment with Shopify Integration, how do I install the trigger?

Hi ​@sfdahj

Thank you for posting in the Community!

Let’s go over a possible solution!

Step 1: Create a Webhook in Shopify for Failed Payments:

Shopify provides webhooks to notify your server of various events, including order/transaction failures.

  1. Go to your Shopify Admin:

    • Navigate to Settings > Notifications > Webhooks.
  2. Create a New Webhook:

    • Click Create Webhook.
    • Event: Choose transactions/create.
    • Format: Select JSON.
    • Webhook URL: Enter your server's endpoint URL (where you'll process the webhook and send data to Klaviyo).

    Example:

    https://your-server.com/webhooks/failed-payment

  3. Save the Webhook:

    • Shopify will send test data to your server’s URL to confirm it’s receiving data.

Step 2: Identify Failed Payments in the Webhook Data:

Shopify's transactions/create webhook sends data every time a transaction is created. Within the payload:

  • Check the status field for failed transactions.
  • Example failed status: "status": "failed".

Step 3: Create a Server to Process the Webhook:

You’ll need a server to:

  1. Receive the Shopify webhook.
  2. Filter out events for failed payments.
  3. Format the relevant data.
  4. Send the data to Klaviyo.

Here’s an example implementation in Python (using Flask):

Example Server Code (Python):

 

from flask import Flask, request, jsonify import requests app = Flask(__name__) # Your Klaviyo API Key KLAVIYO_API_KEY = "YOUR_PUBLIC_API_KEY" @app.route('/webhooks/failed-payment', methods=e'POST']) def handle_failed_payment(): data = request.json # Check if the transaction status is "failed" if data.get('status') == 'failed': # Extract relevant customer and order details email = datas'customer']t'email'] first_name = datas'customer']t'first_name'] last_name = datas'customer']t'last_name'] order_id = datar'order_id'] amount = data 'amount'] payment_method = data_'payment_details']m'credit_card_company'] failure_reason = data.get('error_message', 'Payment failed') # Prepare the payload for Klaviyo klaviyo_payload = { "token": KLAVIYO_API_KEY, "event": "Failed Payment", "customer_properties": { "$email": email, "$first_name": first_name, "$last_name": last_name }, "properties": { "order_id": order_id, "order_total": amount, "payment_method": payment_method, "failure_reason": failure_reason, "retry_payment_link": f"https://yourstore.com/checkout/retry?order={order_id}" }, "time": int(time.time()) } # Send to Klaviyo Track API response = requests.post("https://a.klaviyo.com/api/track", json=klaviyo_payload) if response.status_code == 200: return jsonify({"status": "success"}), 200 else: return jsonify({"status": "error", "message": response.text}), 500 return jsonify({"status": "ignored"}), 200 if __name__ == '__main__': app.run(port=5000)

Step 4: Test the Webhook:

  1. Use Shopify's Send Test Notification button to test your webhook.
  2. Verify your server processes the data correctly and sends the event to Klaviyo.
  3. Check in Klaviyo under Analytics > Metrics for the new Failed Payment event.

Step 5: Create a Klaviyo Flow:

Once the Failed Payment metric appears in Klaviyo:

  1. Go to Flows > Create Flow.
  2. Select Metric-Triggered Flow.
  3. Choose Failed Payment as the trigger.
  4. Build out your flow to send an email/SMS to customers with a link to retry payment.

Hope that helps! :-) 

Christian Nørbjerg Enger
Partner & CPO
Web: Segmento.dk
LinkedIn: @christianfromsegmento
Voldbjergvej 22b, 8240 Risskov


Reply