implement payment
This commit is contained in:
parent
b6156be6af
commit
a1fe007728
13
main.go
13
main.go
|
|
@ -8,19 +8,6 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
type Scale struct {
|
||||
Deveui string
|
||||
Alias string
|
||||
Alarmactive string
|
||||
Smsnumber string
|
||||
Last_measurement string
|
||||
Last_temp string
|
||||
Last_humidity string
|
||||
Last_weight string
|
||||
Last_pressure string
|
||||
Last_accu_level string
|
||||
}
|
||||
|
||||
func serveTemplate(w http.ResponseWriter, r *http.Request) {
|
||||
logit("Called URL: "+r.URL.Path)
|
||||
// wennn kein File angegeben ist: index.html
|
||||
|
|
|
|||
17
metrics.go
17
metrics.go
|
|
@ -22,6 +22,8 @@ type OneMetric struct {
|
|||
Weight string
|
||||
Pressure string
|
||||
BatteryPercent string
|
||||
ActiveUntil string
|
||||
DaysUntilDeactivated int // berechneter Wert
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -252,6 +254,19 @@ func lastmetricsHandler(response http.ResponseWriter, request *http.Request) {
|
|||
}
|
||||
}
|
||||
|
||||
func CalcDaysUntil(mydate string) int {
|
||||
var days int
|
||||
layout := "01.01.2006"
|
||||
t, err := time.Parse(layout, mydate)
|
||||
|
||||
if err != nil {
|
||||
days = 0
|
||||
}
|
||||
days = int(t.Sub(time.Now()).Hours() / 24)
|
||||
|
||||
return days
|
||||
}
|
||||
|
||||
func getLastMetrics(deveui string) OneMetric {
|
||||
var res OneMetric
|
||||
|
||||
|
|
@ -320,5 +335,7 @@ func getLastMetrics(deveui string) OneMetric {
|
|||
res.Alias = getDevAlias(deveui)
|
||||
res.Alarmactive = getDevAlarmactive(deveui)
|
||||
res.Smsnumber = getDevSmsnumber(deveui)
|
||||
res.ActiveUntil = getActiveUntil(deveui)
|
||||
res.DaysUntilDeactivated = CalcDaysUntil(res.ActiveUntil)
|
||||
return res
|
||||
}
|
||||
|
|
|
|||
|
|
@ -205,6 +205,29 @@ func getDevSmsnumber(deveui string) string {
|
|||
return res
|
||||
}
|
||||
|
||||
func getActiveUntil(deveui string) string {
|
||||
res := ""
|
||||
|
||||
if deveui == "" {
|
||||
return res
|
||||
}
|
||||
|
||||
|
||||
conn := globalPool.Get()
|
||||
defer conn.Close()
|
||||
|
||||
logit("getActiveUntil: Deveui: "+deveui)
|
||||
activeuntil, err := redis.String(conn.Do("HGET", devPrefix+deveui, "active_until"))
|
||||
if err == nil {
|
||||
logit("getActiveUntil: activeuntil: "+activeuntil)
|
||||
res = activeuntil
|
||||
} else {
|
||||
log.Print(err)
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func prolongActivation(deveui string, years int) (string, error) {
|
||||
conn := globalPool.Get()
|
||||
defer conn.Close()
|
||||
|
|
@ -226,9 +249,9 @@ func prolongActivation(deveui string, years int) (string, error) {
|
|||
|
||||
var t_new time.Time
|
||||
if t.Before(time.Now()) {
|
||||
t_new = time.Now().AddDate(1, 0, 0)
|
||||
t_new = time.Now().AddDate(years, 0, 0)
|
||||
} else {
|
||||
t_new = t.AddDate(1, 0, 0)
|
||||
t_new = t.AddDate(years, 0, 0)
|
||||
}
|
||||
active_until_new := t_new.Format(layout)
|
||||
|
||||
|
|
|
|||
|
|
@ -53,9 +53,10 @@ Device ID: <span id="deveui"></span>
|
|||
<div class="modal-background"></div>
|
||||
<div class="modal-card">
|
||||
<header class="modal-card-head">
|
||||
<p class="modal-card-title">Abos</p>
|
||||
<p class="modal-card-title">Abo verlängern</p>
|
||||
</header>
|
||||
<section class="modal-card-body">
|
||||
<p id="charge_data" hidden>0</p>
|
||||
<section class="modal-card-body" id="abos_verlaengern">
|
||||
<!-- Content ... -->
|
||||
</section>
|
||||
<footer class="modal-card-foot">
|
||||
|
|
@ -66,8 +67,8 @@ Device ID: <span id="deveui"></span>
|
|||
</div>
|
||||
|
||||
{{range .LastMetrics}}
|
||||
<div class="column is-full notification is-warning">
|
||||
<p class="is-size-2 is-size-5-mobile has-text-centered has-text-weight-bold" ><span id="alias_{{.Deveui}}">{{.Alias}}</span><a class="show-modal" class="block-link"><span style="float:right;" class="icon is-size-4 is-size-5-mobile"><i class="fa fa-cog"></i></span></a></p>
|
||||
<div class="column waage is-full notification is-warning">
|
||||
<p class="is-size-2 is-size-5-mobile has-text-centered has-text-weight-bold" ><span class="alias" id="alias_{{.Deveui}}">{{.Alias}}</span><a class="show-modal" class="block-link"><span style="float:right;" class="icon is-size-4 is-size-5-mobile"><i class="fa fa-cog"></i></span></a></p>
|
||||
<p id="alarmactive_{{.Deveui}}" hidden>{{.Alarmactive}}</p>
|
||||
<p id="smsnumber_{{.Deveui}}" hidden>{{.Smsnumber}}</p>
|
||||
<p id="lastmeasurement_{{.Deveui}}" class="has-text-centered">letzte übermittelte Messung: {{.Timestamp}}</p>
|
||||
|
|
@ -117,9 +118,10 @@ Device ID: <span id="deveui"></span>
|
|||
</nav>
|
||||
</div>
|
||||
<div class="has-text-centered">
|
||||
<span class="is-size-6 has-text-centered" id="paid_until_{{.Deveui}}">aktiv bis</span>
|
||||
<span class="is-size-6 has-text-centered" id="paid_until_{{.Deveui}}">01.01.1234</span>
|
||||
<span class="is-size-6 has-text-centered">aktiv bis</span>
|
||||
<span class="paid_until is-size-6 has-text-centered">{{.ActiveUntil}}</span>
|
||||
</div>
|
||||
{{ if (lt .DaysUntilDeactivated 1095) }}
|
||||
<div class="has-text-centered is-size-7">
|
||||
<p class="abo_add_years_text" id="abo_add_years_text_{.Deveui}}"> </p>
|
||||
</div>
|
||||
|
|
@ -141,11 +143,14 @@ Device ID: <span id="deveui"></span>
|
|||
</span>
|
||||
</a>
|
||||
</div>
|
||||
{{ end }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{{end}}
|
||||
|
||||
<script type="text/javascript" src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
|
||||
|
||||
<script>
|
||||
|
||||
function validate(what, text) {
|
||||
|
|
@ -218,11 +223,40 @@ $(".abo_minus").click(function() {
|
|||
}
|
||||
});
|
||||
|
||||
function add_years(dt,n)
|
||||
{
|
||||
return new Date(dt.setFullYear(dt.getFullYear() + n));
|
||||
}
|
||||
|
||||
$(".abo_pay").click(function() {
|
||||
console.log("pay...");
|
||||
el = $(this).parent().find(".abo_add_years");
|
||||
counter = Number(el.html());
|
||||
counter = 0;
|
||||
charge_data = '';
|
||||
abo_table = '<table class="table is-bordered is-fullwidth" >';
|
||||
abo_table += '<tr><th class="is-2">Alias</th><th>verlängern bis</th><th class="has-text-right">Betrag</th></tr>';
|
||||
$(".waage").each(function( index ) {
|
||||
console.log( index + ": " + $( this ).find(".alias").html() );
|
||||
this_count = Number($( this ).find(".abo_add_years").html());
|
||||
if (this_count > 0) {
|
||||
counter += this_count;
|
||||
this_date = $( this ).find(".paid_until").html();
|
||||
abo_table += '<tr><td>' + $( this ).find('.alias').html() + '</td><td>' + moment(this_date,'DD.MM.YYYY').add('years', this_count).format('DD.MM.YYYY') + '</td><td class="has-text-right">' + (this_count * 24).toFixed(2) + '</td></tr>';
|
||||
if (charge_data == '') {
|
||||
charge_data = $( this ).find("div").first().attr('id') + ":" + this_count;
|
||||
} else {
|
||||
charge_data += "," + $( this ).find("div").first().attr('id') + ":" + this_count;
|
||||
}
|
||||
}
|
||||
console.log( counter );
|
||||
});
|
||||
abo_table += '<tr><td>Total CHF</td><td></td><td class="has-text-right">' + (counter * 24).toFixed(2) + '</td></tr>';
|
||||
console.log("Counter: "+counter);
|
||||
abo_table += "</table>";
|
||||
if (counter > 0) {
|
||||
console.log(abo_table);
|
||||
console.log("charge_data: "+charge_data);
|
||||
$("#abos_verlaengern").html(abo_table);
|
||||
$("#charge_data").html(charge_data);
|
||||
$("#cart").addClass("is-active");
|
||||
}
|
||||
});
|
||||
|
|
@ -317,10 +351,7 @@ $("#cart-pay").click(function() {
|
|||
type: "get", //send it through get method
|
||||
dataType: "json",
|
||||
data: {
|
||||
charge_name: "Abo",
|
||||
charge_description: "Desription",
|
||||
charge_amount_rappen: 500,
|
||||
charge_quantity: 1
|
||||
charge_data: $("#charge_data").html()
|
||||
},
|
||||
success: function(response) {
|
||||
console.log('pay success');
|
||||
|
|
|
|||
70
stripe.go
70
stripe.go
|
|
@ -3,11 +3,12 @@ package main
|
|||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strconv"
|
||||
"os"
|
||||
"io/ioutil"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
"strconv"
|
||||
"github.com/stripe/stripe-go"
|
||||
"github.com/stripe/stripe-go/checkout/session"
|
||||
"github.com/stripe/stripe-go/webhook"
|
||||
|
|
@ -21,45 +22,27 @@ func getstripesessionHandler(response http.ResponseWriter, request *http.Request
|
|||
name := getUserName(request)
|
||||
if name != "" {
|
||||
|
||||
charge_name, ok := request.URL.Query()["charge_name"]
|
||||
if !ok || len(charge_name[0]) < 1 {
|
||||
log.Println("Url Param 'charge_name' is missing")
|
||||
fmt.Fprintf(response, "{ \"rc\": 1, \"msg\": \"charge_name must be specified in URL\" }")
|
||||
charge_data, ok := request.URL.Query()["charge_data"]
|
||||
if !ok || len(charge_data[0]) < 1 {
|
||||
log.Println("Url Param 'charge_data' is missing")
|
||||
fmt.Fprintf(response, "{ \"rc\": 1, \"msg\": \"charge_data must be specified in URL\" }")
|
||||
return
|
||||
}
|
||||
|
||||
charge_description, ok := request.URL.Query()["charge_description"]
|
||||
if !ok || len(charge_description[0]) < 1 {
|
||||
log.Println("Url Param 'charge_description' is missing")
|
||||
fmt.Fprintf(response, "{ \"rc\": 2, \"msg\": \"charge_description must be specified in URL\" }")
|
||||
return
|
||||
}
|
||||
scales := strings.Split(charge_data[0],",")
|
||||
|
||||
charge_quantity, ok := request.URL.Query()["charge_quantity"]
|
||||
if !ok || len(charge_quantity[0]) < 1 {
|
||||
log.Println("Url Param 'charge_quantity' is missing")
|
||||
fmt.Fprintf(response, "{ \"rc\": 3, \"msg\": \"charge_quantity must be specified in URL\" }")
|
||||
return
|
||||
}
|
||||
charge_quantity_int64, err := strconv.Atoi(charge_quantity[0])
|
||||
var abo_years = 0
|
||||
var items []string
|
||||
for _,scale := range scales {
|
||||
items = strings.Split(scale,":")
|
||||
if (len(items) == 2) {
|
||||
abo_count, err := strconv.Atoi(items[1])
|
||||
if err == nil {
|
||||
fmt.Println(charge_quantity_int64)
|
||||
} else {
|
||||
fmt.Println(charge_quantity[0], "is not an integer.")
|
||||
abo_years += abo_count
|
||||
}
|
||||
|
||||
charge_amount_rappen, ok := request.URL.Query()["charge_amount_rappen"]
|
||||
if !ok || len(charge_amount_rappen[0]) < 1 {
|
||||
log.Println("Url Param 'charge_amount_rappen' is missing")
|
||||
fmt.Fprintf(response, "{ \"rc\": 4, \"msg\": \"charge_amount_rappen must be specified in URL\" }")
|
||||
return
|
||||
}
|
||||
charge_amount_rappen_int64, err := strconv.Atoi(charge_amount_rappen[0])
|
||||
if err == nil {
|
||||
fmt.Println(charge_amount_rappen_int64,)
|
||||
} else {
|
||||
fmt.Println(charge_amount_rappen[0], "is not an integer.")
|
||||
}
|
||||
abo_amount := int64(abo_years * 2400)
|
||||
|
||||
stripe.Key = getStripeKey()
|
||||
|
||||
|
|
@ -67,13 +50,16 @@ func getstripesessionHandler(response http.ResponseWriter, request *http.Request
|
|||
PaymentMethodTypes: stripe.StringSlice([]string{
|
||||
"card",
|
||||
}),
|
||||
ClientReferenceID: stripe.String(charge_data[0]),
|
||||
CustomerEmail: stripe.String(name),
|
||||
Locale: stripe.String("de"),
|
||||
LineItems: []*stripe.CheckoutSessionLineItemParams{
|
||||
&stripe.CheckoutSessionLineItemParams{
|
||||
Name: stripe.String(charge_name[0]),
|
||||
Description: stripe.String(charge_description[0]),
|
||||
Amount: stripe.Int64(int64(charge_amount_rappen_int64)),
|
||||
Name: stripe.String("Jahresabos mini-beieli.ch"),
|
||||
Description: stripe.String("Blablabla Description"),
|
||||
Amount: stripe.Int64(abo_amount),
|
||||
Currency: stripe.String(string(stripe.CurrencyCHF)),
|
||||
Quantity: stripe.Int64(int64(charge_quantity_int64)),
|
||||
Quantity: stripe.Int64(1),
|
||||
},
|
||||
},
|
||||
SuccessURL: stripe.String("https://mini-beieli.ch/payment_received.html"),
|
||||
|
|
@ -123,7 +109,19 @@ func stripeWebhookHandler(response http.ResponseWriter, request *http.Request) {
|
|||
|
||||
// Fulfill the purchase...
|
||||
//handleCheckoutSession(session)
|
||||
scales := strings.Split(session.ClientReferenceID,",")
|
||||
var items []string
|
||||
for _,scale := range scales {
|
||||
items = strings.Split(scale,":")
|
||||
if (len(items) == 2) {
|
||||
abo_count, err := strconv.Atoi(items[1])
|
||||
if err == nil {
|
||||
_,_ = prolongActivation(items[0],abo_count)
|
||||
}
|
||||
}
|
||||
}
|
||||
log.Println("handleCheckoutSession "+session.ID)
|
||||
log.Println("handleCheckoutSession "+session.ClientReferenceID)
|
||||
}
|
||||
|
||||
response.WriteHeader(http.StatusOK)
|
||||
|
|
|
|||
Loading…
Reference in New Issue