From 0ebcd46fb6b72a9fb5b7489d24a513ee526499d2 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Wed, 18 Apr 2018 14:01:51 +0200 Subject: [PATCH] Added user currency to charge page --- pages/charge/charge.pixy | 10 +++++----- utils/YenToUserCurrency.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 utils/YenToUserCurrency.go diff --git a/pages/charge/charge.pixy b/pages/charge/charge.pixy index 302db311..b60a9f0f 100644 --- a/pages/charge/charge.pixy +++ b/pages/charge/charge.pixy @@ -9,23 +9,23 @@ component Charge(user *arn.User) p.text-center.mountable You can add balance via PayPal. 1 Japanese Yen equals 1 Gem. .buttons - button.action.mountable(data-trigger="click", data-action="chargeUp", data-amount=1000) + button.action.tip.mountable(data-trigger="click", data-action="chargeUp", data-amount=1000, aria-label=utils.YenToUserCurrency(1000, user)) Icon("diamond") span 1000 - button.action.mountable(data-trigger="click", data-action="chargeUp", data-amount=2000) + button.action.tip.mountable(data-trigger="click", data-action="chargeUp", data-amount=2000, aria-label=utils.YenToUserCurrency(2000, user)) Icon("diamond") span 2000 - button.action.mountable(data-trigger="click", data-action="chargeUp", data-amount=3000) + button.action.tip.mountable(data-trigger="click", data-action="chargeUp", data-amount=3000, aria-label=utils.YenToUserCurrency(3000, user)) Icon("diamond") span 3000 - button.action.mountable(data-trigger="click", data-action="chargeUp", data-amount=6000) + button.action.tip.mountable(data-trigger="click", data-action="chargeUp", data-amount=6000, aria-label=utils.YenToUserCurrency(6000, user)) Icon("diamond") span 6000 - button.action.mountable(data-trigger="click", data-action="chargeUp", data-amount=12000) + button.action.tip.mountable(data-trigger="click", data-action="chargeUp", data-amount=12000, aria-label=utils.YenToUserCurrency(12000, user)) Icon("diamond") span 12000 diff --git a/utils/YenToUserCurrency.go b/utils/YenToUserCurrency.go new file mode 100644 index 00000000..db20329d --- /dev/null +++ b/utils/YenToUserCurrency.go @@ -0,0 +1,35 @@ +package utils + +import ( + "fmt" + + "github.com/animenotifier/arn" + "github.com/pariz/gountries" +) + +// Current currency rates +const ( + yenToEuro = 0.0075 + yenToDollar = 0.0093 +) + +var countryQuery = gountries.New() + +// YenToUserCurrency converts the Yen price to the user currency. +func YenToUserCurrency(amount int, user *arn.User) string { + if user == nil || user.Location.CountryName == "" { + return fmt.Sprintf("%.2f $", float64(amount)*yenToDollar) + } + + country, err := countryQuery.FindCountryByName(user.Location.CountryName) + + if err != nil { + return fmt.Sprintf("%.2f $", float64(amount)*yenToDollar) + } + + if arn.Contains(country.Currencies, "EUR") { + return fmt.Sprintf("%.2f €", float64(amount)*yenToEuro) + } + + return fmt.Sprintf("%.2f $", float64(amount)*yenToDollar) +}