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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,16 +1,16 @@
package main
import (
"fmt"
"log"
"os"
"io/ioutil"
"encoding/json"
"net/http"
"strings"
"strconv"
"fmt"
"github.com/stripe/stripe-go"
"github.com/stripe/stripe-go/paymentintent"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
)
func getStripeKey() string {
@ -34,7 +34,7 @@ func getstripepaymentintentHandler(response http.ResponseWriter, request *http.R
var items []string
for _, scale := range scales {
items = strings.Split(scale, ":")
if (len(items) == 2) {
if len(items) == 2 {
abo_count, err := strconv.Atoi(items[1])
if err == nil {
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) {
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, ",") {
res := strings.Split(token, ":")
if (len(res)) == 2 {
@ -73,9 +75,13 @@ func HandlePayment(user string, charge_data string, amount int64) {
years, _ := strconv.Atoi(res[1])
fmt.Printf("prolongActivation %s: %d\n", 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) {