Added user currency to charge page

This commit is contained in:
Eduard Urbach 2018-04-18 14:01:51 +02:00
parent 40324d445a
commit 0ebcd46fb6
2 changed files with 40 additions and 5 deletions

View File

@ -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. p.text-center.mountable You can add balance via PayPal. 1 Japanese Yen equals 1 Gem.
.buttons .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") Icon("diamond")
span 1000 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") Icon("diamond")
span 2000 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") Icon("diamond")
span 3000 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") Icon("diamond")
span 6000 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") Icon("diamond")
span 12000 span 12000

View File

@ -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)
}