How do I set up a failed payment flow?
I want to make a flow for customers that had a failed credit card payment with Shopify Integration, how do I install the trigger?
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
Thank you for posting in the Community!
Let’s go over a possible solution!
Shopify provides webhooks to notify your server of various events, including order/transaction failures.
Go to your Shopify Admin:
Create a New Webhook:
transactions/create.JSON.Example:
https://your-server.com/webhooks/failed-payment
Save the Webhook:
Shopify's transactions/create webhook sends data every time a transaction is created. Within the payload:
status field for failed transactions."status": "failed".You’ll need a server to:
Here’s an example implementation in Python (using Flask):
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=['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 = data['customer']['email'] first_name = data['customer']['first_name'] last_name = data['customer']['last_name'] order_id = data['order_id'] amount = data['amount'] payment_method = data['payment_details']['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)
Once the Failed Payment metric appears in Klaviyo:
Hope that helps! :-)
Christian Nørbjerg Enger
Partner & CPO
Web: Segmento.dk
LinkedIn: @christianfromsegmento
Voldbjergvej 22b, 8240 Risskov
No account yet? Create an account
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.