go fmt; email message after payment

This commit is contained in:
Joerg Lehmann 2020-04-09 14:41:41 +02:00
parent 2cac4d56e2
commit 5548949e9d
9 changed files with 791 additions and 795 deletions

View File

@ -1,11 +1,11 @@
package main package main
import ( import (
"github.com/gorilla/securecookie"
"net/http"
"fmt"
"crypto/md5" "crypto/md5"
"encoding/hex" "encoding/hex"
"fmt"
"github.com/gorilla/securecookie"
"net/http"
) )
// cookie handling // cookie handling
@ -78,7 +78,6 @@ func loginHandler(response http.ResponseWriter, request *http.Request) {
http.Redirect(response, request, redirectTarget, 302) http.Redirect(response, request, redirectTarget, 302)
} }
// resetPassword handler // resetPassword handler
func resetPasswordHandler(response http.ResponseWriter, request *http.Request) { func resetPasswordHandler(response http.ResponseWriter, request *http.Request) {

View File

@ -1,8 +1,8 @@
package main package main
import ( import (
"log"
"bytes" "bytes"
"log"
"net/smtp" "net/smtp"
) )

View File

@ -44,7 +44,7 @@ func serveTemplate(w http.ResponseWriter, r *http.Request) {
query_values := r.URL.Query() query_values := r.URL.Query()
if (r.URL.Path == "/scales.html") { if r.URL.Path == "/scales.html" {
// wir holen noch die letzten Metriken // wir holen noch die letzten Metriken
for _, v := range scales { for _, v := range scales {
last_metric := getLastMetrics(v) last_metric := getLastMetrics(v)

View File

@ -1,15 +1,15 @@
package main package main
import ( import (
"bufio"
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil"
"log" "log"
"net/http" "net/http"
"time"
"io/ioutil"
"bufio"
"strings"
"strconv" "strconv"
"strings"
"time"
) )
type OneMetric struct { type OneMetric struct {
@ -28,7 +28,6 @@ type OneMetric struct {
DaysUntilDeactivated int // berechneter Wert DaysUntilDeactivated int // berechneter Wert
} }
// metrics handler // metrics handler
func metricsHandler(response http.ResponseWriter, request *http.Request) { func metricsHandler(response http.ResponseWriter, request *http.Request) {
@ -135,7 +134,7 @@ func metricsHandler(response http.ResponseWriter, request *http.Request) {
first := true first := true
for scanner.Scan() { for scanner.Scan() {
s := strings.Split(scanner.Text(), ",") s := strings.Split(scanner.Text(), ",")
if ((len(s) >= 7) && !(strings.HasPrefix(s[5],"_"))) { if (len(s) >= 7) && !(strings.HasPrefix(s[5], "_")) {
t, err := time.Parse(time.RFC3339, s[5]) t, err := time.Parse(time.RFC3339, s[5])
if err != nil { if err != nil {
continue continue
@ -227,7 +226,7 @@ func lastmetricsHandler(response http.ResponseWriter, request *http.Request) {
location, err := time.LoadLocation("Europe/Zurich") location, err := time.LoadLocation("Europe/Zurich")
for scanner.Scan() { for scanner.Scan() {
s := strings.Split(scanner.Text(), ",") s := strings.Split(scanner.Text(), ",")
if ((len(s) >= 7) && !(strings.HasPrefix(s[5],"_"))) { if (len(s) >= 7) && !(strings.HasPrefix(s[5], "_")) {
mytime, err := time.Parse(time.RFC3339, s[5]) mytime, err := time.Parse(time.RFC3339, s[5])
if err != nil { if err != nil {
continue continue
@ -318,7 +317,7 @@ func getLastMetrics(deveui string) OneMetric {
location, err := time.LoadLocation("Europe/Zurich") location, err := time.LoadLocation("Europe/Zurich")
for scanner.Scan() { for scanner.Scan() {
s := strings.Split(scanner.Text(), ",") s := strings.Split(scanner.Text(), ",")
if ((len(s) >= 7) && !(strings.HasPrefix(s[5],"_"))) { if (len(s) >= 7) && !(strings.HasPrefix(s[5], "_")) {
mytime, err := time.Parse(time.RFC3339, s[5]) mytime, err := time.Parse(time.RFC3339, s[5])
if err != nil { if err != nil {
continue continue
@ -333,7 +332,7 @@ func getLastMetrics(deveui string) OneMetric {
} else if field == "w" { } else if field == "w" {
res.Weight = value res.Weight = value
i, err := strconv.Atoi(value) i, err := strconv.Atoi(value)
if (err == nil) { if err == nil {
res.Weight_kg = fmt.Sprintf("%.2f", float64(i)/1000.0) res.Weight_kg = fmt.Sprintf("%.2f", float64(i)/1000.0)
} else { } else {
res.Weight_kg = "ERR" res.Weight_kg = "ERR"

View File

@ -1,13 +1,13 @@
package main package main
import ( import (
"time"
"strings"
"fmt"
"log"
"crypto/rand" "crypto/rand"
"golang.org/x/crypto/bcrypt" "fmt"
"github.com/gomodule/redigo/redis" "github.com/gomodule/redigo/redis"
"golang.org/x/crypto/bcrypt"
"log"
"strings"
"time"
) )
var globalPool *redis.Pool var globalPool *redis.Pool
@ -73,7 +73,7 @@ func initDB() {
// Wir legen einen initialen Admin User an, falls es diesen noch nicht gibt // Wir legen einen initialen Admin User an, falls es diesen noch nicht gibt
if checkUserAvailable("joerg.lehmann@nbit.ch") { if checkUserAvailable("joerg.lehmann@nbit.ch") {
insertUser("joerg.lehmann@nbit.ch","changeme123","Y"); insertUser("joerg.lehmann@nbit.ch", "changeme123", "Y")
} }
} }
@ -82,7 +82,6 @@ func closeDB() {
globalPool.Close() globalPool.Close()
} }
func updateScaleSettings(scaleSettings Dev) error { func updateScaleSettings(scaleSettings Dev) error {
conn := globalPool.Get() conn := globalPool.Get()
defer conn.Close() defer conn.Close()
@ -96,14 +95,13 @@ func updateScaleSettings(scaleSettings Dev) error {
return nil return nil
} }
func checkUserAvailable(username string) bool { func checkUserAvailable(username string) bool {
logit("checkUserAvailable: User: " + username) logit("checkUserAvailable: User: " + username)
conn := globalPool.Get() conn := globalPool.Get()
defer conn.Close() defer conn.Close()
_, err := redis.String(conn.Do("GET", userPrefix+username)) _, err := redis.String(conn.Do("GET", userPrefix+username))
if (err == redis.ErrNil) { if err == redis.ErrNil {
logit("User does not exist and is therefore available:" + username) logit("User does not exist and is therefore available:" + username)
return true return true
} else if err != nil { } else if err != nil {
@ -120,7 +118,6 @@ func getMyDevs(username string) []string {
return res return res
} }
conn := globalPool.Get() conn := globalPool.Get()
defer conn.Close() defer conn.Close()
@ -143,7 +140,6 @@ func getDevAlias(deveui string) string {
return res return res
} }
conn := globalPool.Get() conn := globalPool.Get()
defer conn.Close() defer conn.Close()
@ -166,7 +162,6 @@ func getDevAlarmactive(deveui string) string {
return res return res
} }
conn := globalPool.Get() conn := globalPool.Get()
defer conn.Close() defer conn.Close()
@ -189,7 +184,6 @@ func getDevSmsnumber(deveui string) string {
return res return res
} }
conn := globalPool.Get() conn := globalPool.Get()
defer conn.Close() defer conn.Close()
@ -212,7 +206,6 @@ func getActiveUntil(deveui string) string {
return res return res
} }
conn := globalPool.Get() conn := globalPool.Get()
defer conn.Close() defer conn.Close()
@ -229,7 +222,7 @@ func getActiveUntil(deveui string) string {
} }
func AboExpired(deveui string) bool { func AboExpired(deveui string) bool {
active_until := getActiveUntil(deveui); active_until := getActiveUntil(deveui)
layout := "02.01.2006" layout := "02.01.2006"
t, _ := time.Parse(layout, active_until) t, _ := time.Parse(layout, active_until)
@ -393,4 +386,3 @@ func confirmUser(confirm_id string) {
return return
} }
} }

View File

@ -3,8 +3,8 @@ package main
import ( import (
"fmt" "fmt"
"log" "log"
"regexp"
"net/http" "net/http"
"regexp"
) )
// scales handler // scales handler
@ -24,7 +24,7 @@ func save_scale_settingsHandler(response http.ResponseWriter, request *http.Requ
// we only want the single item. // we only want the single item.
mydeveui := deveui[0] mydeveui := deveui[0]
if (len(mydeveui) != 16) { if len(mydeveui) != 16 {
log.Println("specified 'deveui' has invalid length") log.Println("specified 'deveui' has invalid length")
fmt.Fprintf(response, "{ \"rc\": 8, \"msg\": \"specified deveui has invalid length\" }") fmt.Fprintf(response, "{ \"rc\": 8, \"msg\": \"specified deveui has invalid length\" }")
return return

View File

@ -1,16 +1,16 @@
package main package main
import ( import (
"fmt"
"log"
"os"
"io/ioutil"
"encoding/json" "encoding/json"
"net/http" "fmt"
"strings"
"strconv"
"github.com/stripe/stripe-go" "github.com/stripe/stripe-go"
"github.com/stripe/stripe-go/paymentintent" "github.com/stripe/stripe-go/paymentintent"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
) )
func getStripeKey() string { func getStripeKey() string {
@ -34,7 +34,7 @@ func getstripepaymentintentHandler(response http.ResponseWriter, request *http.R
var items []string var items []string
for _, scale := range scales { for _, scale := range scales {
items = strings.Split(scale, ":") items = strings.Split(scale, ":")
if (len(items) == 2) { if len(items) == 2 {
abo_count, err := strconv.Atoi(items[1]) abo_count, err := strconv.Atoi(items[1])
if err == nil { if err == nil {
abo_years += abo_count abo_years += abo_count
@ -66,6 +66,8 @@ func getstripepaymentintentHandler(response http.ResponseWriter, request *http.R
func HandlePayment(user string, charge_data string, amount int64) { func HandlePayment(user string, charge_data string, amount int64) {
fmt.Printf("HandlePayment for %s (charge_data: %s, amount: %d)!\n", user, charge_data, amount) fmt.Printf("HandlePayment for %s (charge_data: %s, amount: %d)!\n", user, charge_data, amount)
charge_data_email_text := fmt.Sprintf("%-30s %20s %10s\n", "Alias", "verlängern bis", "Betrag")
charge_data_email_text = charge_data_email_text + strings.Repeat("-", 62) + "\n"
for _, token := range strings.Split(charge_data, ",") { for _, token := range strings.Split(charge_data, ",") {
res := strings.Split(token, ":") res := strings.Split(token, ":")
if (len(res)) == 2 { if (len(res)) == 2 {
@ -73,9 +75,13 @@ func HandlePayment(user string, charge_data string, amount int64) {
years, _ := strconv.Atoi(res[1]) years, _ := strconv.Atoi(res[1])
fmt.Printf("prolongActivation %s: %d\n", deveui, years) fmt.Printf("prolongActivation %s: %d\n", deveui, years)
prolongActivation(deveui, years) prolongActivation(deveui, years)
line := fmt.Sprintf("%-30s %20s %10.2f\n", getDevAlias(deveui), getActiveUntil(deveui), float64(24*years))
charge_data_email_text = charge_data_email_text + line
} }
} }
sendPaymentConfirmationEmail(user,charge_data,amount) charge_data_email_text = charge_data_email_text + strings.Repeat("-", 62) + "\n"
charge_data_email_text = charge_data_email_text + fmt.Sprintf("%-30s %20s %10.2f\n", "Total CHF", "", float64(amount/100))
sendPaymentConfirmationEmail(user, charge_data_email_text, amount)
} }
func stripeWebhookHandler(w http.ResponseWriter, req *http.Request) { func stripeWebhookHandler(w http.ResponseWriter, req *http.Request) {