From e1bb4368460ebb3a0bb86151b143176797b686cc Mon Sep 17 00:00:00 2001
From: Uku Taht <uku.taht@gmail.com>
Date: Mon, 19 Apr 2021 14:44:17 +0300
Subject: [PATCH] Show better error message when changing plans fails

---
 .../controllers/billing_controller.ex              | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/lib/plausible_web/controllers/billing_controller.ex b/lib/plausible_web/controllers/billing_controller.ex
index 3ce1ea12..4f31637d 100644
--- a/lib/plausible_web/controllers/billing_controller.ex
+++ b/lib/plausible_web/controllers/billing_controller.ex
@@ -47,9 +47,23 @@ defmodule PlausibleWeb.BillingController do
         |> redirect(to: "/settings")
 
       {:error, e} ->
+        # https://developer.paddle.com/api-reference/intro/api-error-codes
+        msg =
+          case e do
+            %{"code" => 147} ->
+              "We were unable to charge your card. Make sure your payment details are up to date and try again."
+
+            %{"message" => msg} when not is_nil(msg) ->
+              msg
+
+            _ ->
+              "Something went wrong. Please try again or contact support at support@plausible.io"
+          end
+
         Sentry.capture_message("Error changing plans",
           extra: %{
             errors: inspect(e),
+            message: msg,
             new_plan_id: new_plan_id,
             user_id: conn.assigns[:current_user].id
           }
-- 
GitLab