From ee61c4c9c00a2521add1bcaf06ffa0920f689ff6 Mon Sep 17 00:00:00 2001 From: Joerg Lehmann Date: Fri, 17 Jan 2020 15:31:00 +0100 Subject: [PATCH] abo payment logic --- mail.go | 36 ++++++++++++++++++++++++++++++++- metrics.go | 8 +++++++- persistence.go | 9 +++++++++ snippets/graph.html | 8 ++++++++ snippets/scales.html | 48 ++++++++++++++++++++++++++++++++++++++++---- stripe.go | 15 ++++++++++++++ 6 files changed, 118 insertions(+), 6 deletions(-) diff --git a/mail.go b/mail.go index e93d9db..20d081e 100644 --- a/mail.go +++ b/mail.go @@ -41,4 +41,38 @@ mini-beieli.ch` if _, err = buf.WriteTo(wc); err != nil { log.Fatal(err) } -} +} + +func sendPaymentConfirmationEmail(username,charge_data string, amount int64) { + c, err := smtp.Dial("127.0.0.1:25") + if err != nil { + log.Fatal(err) + } + defer c.Close() + // Set the sender and recipient. + c.Mail("info@mini-beieli.ch") + c.Rcpt(username) + // Send the email body. + wc, err := c.Data() + if err != nil { + log.Fatal(err) + } + defer wc.Close() + mail_message := "To: " + username + ` +Subject: Zahlungsbestaetigung mini-beieli.ch + +Lieber Benutzer von mini-beieli.ch + +Sie haben soeben erfolgreich folgende Abo-Verlaengerungen bezahlt: + +` + charge_data + ` + +Mit freundlichen GrĂ¼ssen +-- +mini-beieli.ch` + + buf := bytes.NewBufferString(mail_message) + if _, err = buf.WriteTo(wc); err != nil { + log.Fatal(err) + } +} diff --git a/metrics.go b/metrics.go index 3288677..f8069cd 100644 --- a/metrics.go +++ b/metrics.go @@ -59,6 +59,12 @@ func metricsHandler(response http.ResponseWriter, request *http.Request) { return } + if AboExpired(mydeveui) { + log.Println("specified 'deveui' has an expired abo") + fmt.Fprintf(response, "{ \"msg\": \"specified deveui has an expired abo\" }") + return + } + log.Println("Url Param 'deveui' is: " + string(mydeveui)) // Format of start and stop: YYYY-MM-DDTHH:MI:SSZ @@ -258,7 +264,7 @@ func lastmetricsHandler(response http.ResponseWriter, request *http.Request) { func CalcDaysUntil(mydate string) int { var days int - layout := "01.01.2006" + layout := "02.01.2006" t, err := time.Parse(layout, mydate) if err != nil { diff --git a/persistence.go b/persistence.go index d0aa5e8..5245f5a 100644 --- a/persistence.go +++ b/persistence.go @@ -228,6 +228,15 @@ func getActiveUntil(deveui string) string { return res } +func AboExpired(deveui string) bool { + active_until := getActiveUntil(deveui); + + layout := "02.01.2006" + t, _ := time.Parse(layout, active_until) + + return t.Before(time.Now()) +} + func prolongActivation(deveui string, years int) (string, error) { conn := globalPool.Get() defer conn.Close() diff --git a/snippets/graph.html b/snippets/graph.html index bd2c2c2..a041a6b 100644 --- a/snippets/graph.html +++ b/snippets/graph.html @@ -206,6 +206,14 @@ function drawGraph(deveui, alias, property, start, stop, create_graph) { } $.getJSON('https://mini-beieli.ch/metrics?deveui=' + deveui + '&alias=' + alias + '&property=' + property + range, function(mydata) { + if ("msg" in mydata) { + $('#chart').html(`
+
+ Abo ist abgelaufen! +
+
`); + return; + } var options = { chart: { type: 'line', diff --git a/snippets/scales.html b/snippets/scales.html index 899eff5..3ecb834 100644 --- a/snippets/scales.html +++ b/snippets/scales.html @@ -3,7 +3,6 @@ {{end}} {{define "body_content"}} {{ if ne .UserName "" }} - + + {{range .LastMetrics}}

{{.Alias}}

@@ -108,6 +129,14 @@ Device ID:
+{{ if (lt .DaysUntilDeactivated 0) }} +
+
+

+

Abo ist abgelaufen

+
+
+{{ else }}
@@ -116,6 +145,7 @@ Device ID:
+{{ end }}
@@ -135,7 +165,7 @@ Device ID:
- aktiv bis + Abo aktiv bis
{{ if (lt .DaysUntilDeactivated 1095) }} @@ -205,6 +235,11 @@ $("#cart-close").click(function() { $("#cart").removeClass("is-active"); }); +$("#payment_notifier_close").click(function() { + $("#payment_notifier").removeClass("is-active"); + location.reload(true); +}); + $(".abo_plus").click(function() { console.log("abo_plus"); el = $(this).parent().find(".abo_add_years"); @@ -255,7 +290,12 @@ $(".abo_pay").click(function() { this_count = Number($( this ).find(".abo_add_years").html()); if (this_count > 0) { counter += this_count; - this_date = $( this ).find(".paid_until").html(); + paid_until = $( this ).find(".paid_until").html(); + if (moment(paid_until,'DD.MM.YYYY') < moment()) { + this_date = moment().format('DD.MM.YYYY'); + } else { + this_date = paid_until; + } abo_table += '' + $( this ).find('.alias').html() + '' + moment(this_date,'DD.MM.YYYY').add('years', this_count).format('DD.MM.YYYY') + '' + (this_count * 24).toFixed(2) + ''; if (charge_data == '') { charge_data = $( this ).find("div").first().attr('id') + ":" + this_count; @@ -366,7 +406,6 @@ var card = elements.create("card", { style: style }); card.mount("#card-element"); $("#card-errors-article").hide(); - card.addEventListener('change', ({error}) => { if (error) { $("#card-errors").text(error.message); @@ -406,6 +445,7 @@ function ConfirmPayment(clientSecret) { $('#card-errors-article').hide(); $("#cart").removeClass("is-active"); EndPaymentProgress(); + $('#payment_notifier').addClass('is-active'); } } }); diff --git a/stripe.go b/stripe.go index d0ff9c3..3faca83 100644 --- a/stripe.go +++ b/stripe.go @@ -64,6 +64,20 @@ 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) + for _, token := range strings.Split(charge_data,",") { + res := strings.Split(token,":") + if (len(res)) == 2 { + deveui := res[0] + years, _ := strconv.Atoi(res[1]) + fmt.Printf("prolongActivation %s: %d\n", deveui, years) + prolongActivation(deveui, years) + } + } + sendPaymentConfirmationEmail(user,charge_data,amount) +} + func stripeWebhookHandler(w http.ResponseWriter, req *http.Request) { const MaxBodyBytes = int64(65536) req.Body = http.MaxBytesReader(w, req.Body, MaxBodyBytes) @@ -93,6 +107,7 @@ func stripeWebhookHandler(w http.ResponseWriter, req *http.Request) { return } fmt.Printf("PaymentIntent was successful (charge_data: %s, amount: %d)!\n", paymentIntent.Metadata["charge_data"], paymentIntent.Amount) + HandlePayment(paymentIntent.ReceiptEmail, paymentIntent.Metadata["charge_data"], paymentIntent.Amount) // ... handle other event types default: fmt.Fprintf(os.Stderr, "Unexpected event type: %s\n", event.Type)