refactor payment process - second step
This commit is contained in:
parent
bf9485087d
commit
4c064240d1
2
main.go
2
main.go
|
|
@ -88,9 +88,7 @@ func main() {
|
||||||
http.HandleFunc("/metrics", metricsHandler)
|
http.HandleFunc("/metrics", metricsHandler)
|
||||||
http.HandleFunc("/lastmetrics", lastmetricsHandler)
|
http.HandleFunc("/lastmetrics", lastmetricsHandler)
|
||||||
http.HandleFunc("/save_scale_settings", save_scale_settingsHandler)
|
http.HandleFunc("/save_scale_settings", save_scale_settingsHandler)
|
||||||
http.HandleFunc("/getstripesession", getstripesessionHandler)
|
|
||||||
http.HandleFunc("/getstripepaymentintent", getstripepaymentintentHandler)
|
http.HandleFunc("/getstripepaymentintent", getstripepaymentintentHandler)
|
||||||
http.HandleFunc("/stripewebhook", stripeWebhookHandler)
|
|
||||||
|
|
||||||
logit("Starting Web Application...")
|
logit("Starting Web Application...")
|
||||||
http.ListenAndServe("127.0.0.1:4000", nil)
|
http.ListenAndServe("127.0.0.1:4000", nil)
|
||||||
|
|
|
||||||
|
|
@ -69,7 +69,10 @@ Device ID: <span id="deveui"></span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- We'll put the error messages in this element -->
|
<!-- We'll put the error messages in this element -->
|
||||||
<div id="card-errors" role="alert"></div>
|
<article class="message is-danger has-margin-top-20" id="card-errors-article" role="alert">
|
||||||
|
<div class="message-body" id="card-errors">
|
||||||
|
</div>
|
||||||
|
</article>
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
<footer class="modal-card-foot">
|
<footer class="modal-card-foot">
|
||||||
|
|
@ -350,7 +353,7 @@ $("#modal-save").click(function() {
|
||||||
// Set your publishable key: remember to change this to your live publishable key in production
|
// Set your publishable key: remember to change this to your live publishable key in production
|
||||||
// See your keys here: https://dashboard.stripe.com/account/apikeys
|
// See your keys here: https://dashboard.stripe.com/account/apikeys
|
||||||
var stripe = Stripe('pk_test_YkSGqH3Tk9WKK9HrlY63GhAg');
|
var stripe = Stripe('pk_test_YkSGqH3Tk9WKK9HrlY63GhAg');
|
||||||
var elements = stripe.elements();
|
var elements = stripe.elements({ locale: "de" });
|
||||||
|
|
||||||
// Set up Stripe.js and Elements to use in checkout form
|
// Set up Stripe.js and Elements to use in checkout form
|
||||||
var style = {
|
var style = {
|
||||||
|
|
@ -361,47 +364,20 @@ var style = {
|
||||||
|
|
||||||
var card = elements.create("card", { style: style });
|
var card = elements.create("card", { style: style });
|
||||||
card.mount("#card-element");
|
card.mount("#card-element");
|
||||||
|
$("#card-errors-article").hide();
|
||||||
|
|
||||||
|
|
||||||
card.addEventListener('change', ({error}) => {
|
card.addEventListener('change', ({error}) => {
|
||||||
const displayError = document.getElementById('card-errors');
|
|
||||||
if (error) {
|
if (error) {
|
||||||
displayError.textContent = error.message;
|
$("#card-errors").text(error.message);
|
||||||
|
$("#card-errors-article").show();
|
||||||
} else {
|
} else {
|
||||||
displayError.textContent = '';
|
$("#card-errors").text("");
|
||||||
|
$("#card-errors-article").hide();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function ConfirmPayment(clientSecret) {
|
||||||
function GetClientSecret() {
|
|
||||||
var result = "";
|
|
||||||
$.ajax({
|
|
||||||
async: false,
|
|
||||||
url: "getstripepaymentintent",
|
|
||||||
type: "get", //send it through get method
|
|
||||||
dataType: "json",
|
|
||||||
data: {
|
|
||||||
charge_data: $("#charge_data").html()
|
|
||||||
},
|
|
||||||
success: function(response) {
|
|
||||||
console.log('pay success');
|
|
||||||
console.log(response.stripesessionid);
|
|
||||||
console.log('rc: '+response.rc);
|
|
||||||
if (response.rc == 0) {
|
|
||||||
result = response.stripeclientsecret;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
error: function(xhr) {
|
|
||||||
console.log('getstripepaymentintent error');
|
|
||||||
//Do Something to handle error
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
$('#cart-pay').on('click', function(ev) {
|
|
||||||
var clientSecret = GetClientSecret();
|
|
||||||
stripe.confirmCardPayment(clientSecret, {
|
stripe.confirmCardPayment(clientSecret, {
|
||||||
payment_method: {
|
payment_method: {
|
||||||
card: card,
|
card: card,
|
||||||
|
|
@ -413,6 +389,10 @@ $('#cart-pay').on('click', function(ev) {
|
||||||
if (result.error) {
|
if (result.error) {
|
||||||
// Show error to your customer (e.g., insufficient funds)
|
// Show error to your customer (e.g., insufficient funds)
|
||||||
console.log(result.error.message);
|
console.log(result.error.message);
|
||||||
|
$('#card-errors').text(result.error.message);
|
||||||
|
$('#card-errors-article').show();
|
||||||
|
//Do Something to handle error
|
||||||
|
EndPaymentProgress();
|
||||||
} else {
|
} else {
|
||||||
// The payment has been processed!
|
// The payment has been processed!
|
||||||
if (result.paymentIntent.status === 'succeeded') {
|
if (result.paymentIntent.status === 'succeeded') {
|
||||||
|
|
@ -422,10 +402,51 @@ $('#cart-pay').on('click', function(ev) {
|
||||||
// payment_intent.succeeded event that handles any business critical
|
// payment_intent.succeeded event that handles any business critical
|
||||||
// post-payment actions.
|
// post-payment actions.
|
||||||
console.log("Payment succeeded!!!");
|
console.log("Payment succeeded!!!");
|
||||||
|
$('#card-errors').text("");
|
||||||
|
$('#card-errors-article').hide();
|
||||||
$("#cart").removeClass("is-active");
|
$("#cart").removeClass("is-active");
|
||||||
|
EndPaymentProgress();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function StartPaymentProgress() {
|
||||||
|
$("#cart-pay").attr("disabled", true).addClass("is-loading");
|
||||||
|
$("#cart-close").attr("disabled", true);
|
||||||
|
}
|
||||||
|
|
||||||
|
function EndPaymentProgress() {
|
||||||
|
$("#cart-pay").attr("disabled", false).removeClass("is-loading");
|
||||||
|
$("#cart-close").attr("disabled", false);
|
||||||
|
}
|
||||||
|
|
||||||
|
function PayMe() {
|
||||||
|
$.ajax({
|
||||||
|
url: "getstripepaymentintent",
|
||||||
|
type: "get", //send it through get method
|
||||||
|
dataType: "json",
|
||||||
|
data: {
|
||||||
|
charge_data: $("#charge_data").html()
|
||||||
|
},
|
||||||
|
success: function(response) {
|
||||||
|
console.log('pay success');
|
||||||
|
console.log('rc: '+response.rc);
|
||||||
|
if (response.rc == 0) {
|
||||||
|
ConfirmPayment(response.stripeclientsecret);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error: function(xhr) {
|
||||||
|
console.log('getstripepaymentintent error');
|
||||||
|
//Do Something to handle error
|
||||||
|
EndPaymentProgress();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
$('#cart-pay').on('click', function(ev) {
|
||||||
|
StartPaymentProgress();
|
||||||
|
PayMe();
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
|
||||||
115
stripe.go
115
stripe.go
|
|
@ -3,16 +3,11 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
|
||||||
"io/ioutil"
|
|
||||||
"encoding/json"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"strconv"
|
"strconv"
|
||||||
"github.com/stripe/stripe-go"
|
"github.com/stripe/stripe-go"
|
||||||
"github.com/stripe/stripe-go/checkout/session"
|
|
||||||
"github.com/stripe/stripe-go/paymentintent"
|
"github.com/stripe/stripe-go/paymentintent"
|
||||||
"github.com/stripe/stripe-go/webhook"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func getStripeKey() string {
|
func getStripeKey() string {
|
||||||
|
|
@ -64,113 +59,3 @@ func getstripepaymentintentHandler(response http.ResponseWriter, request *http.R
|
||||||
fmt.Fprintf(response, "{ \"rc\": 6, \"msg\": \"Only available for logged in users\" }")
|
fmt.Fprintf(response, "{ \"rc\": 6, \"msg\": \"Only available for logged in users\" }")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getstripesessionHandler(response http.ResponseWriter, request *http.Request) {
|
|
||||||
name := getUserName(request)
|
|
||||||
if name != "" {
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
scales := strings.Split(charge_data[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 {
|
|
||||||
abo_years += abo_count
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
abo_amount := int64(abo_years * 2400)
|
|
||||||
|
|
||||||
stripe.Key = getStripeKey()
|
|
||||||
|
|
||||||
params := &stripe.CheckoutSessionParams{
|
|
||||||
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("Jahresabos mini-beieli.ch"),
|
|
||||||
Description: stripe.String("Blablabla Description"),
|
|
||||||
Amount: stripe.Int64(abo_amount),
|
|
||||||
Currency: stripe.String(string(stripe.CurrencyCHF)),
|
|
||||||
Quantity: stripe.Int64(1),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
SuccessURL: stripe.String("https://mini-beieli.ch/payment_received.html"),
|
|
||||||
CancelURL: stripe.String("https://mini-beieli.ch/payment_cancelled.html"),
|
|
||||||
}
|
|
||||||
|
|
||||||
session, err := session.New(params)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(response,"{ \"rc\": 5, \"stripesessionid\": \"%s\" }\n","ERROR")
|
|
||||||
} else {
|
|
||||||
fmt.Fprintf(response,"{ \"rc\": 0, \"stripesessionid\": \"%s\" }\n",session.ID )
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
fmt.Fprintf(response, "{ \"rc\": 6, \"msg\": \"Only available for logged in users\" }")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func stripeWebhookHandler(response http.ResponseWriter, request *http.Request) {
|
|
||||||
body, err := ioutil.ReadAll(request.Body)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "Error reading request body: %v\n", err)
|
|
||||||
response.WriteHeader(http.StatusServiceUnavailable)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pass the request body & Stripe-Signature header to ConstructEvent, along with the webhook signing key
|
|
||||||
// You can find your endpoint's secret in your webhook settings
|
|
||||||
endpointSecret := "whsec_b1OdRuu9aK6zXt6M1EQRxZ4lhl3rrVtN";
|
|
||||||
event, err := webhook.ConstructEvent(body, request.Header.Get("Stripe-Signature"), endpointSecret)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "Error verifying webhook signature: %v\n", err)
|
|
||||||
response.WriteHeader(http.StatusBadRequest) // Return a 400 error on a bad signature
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle the checkout.session.completed event
|
|
||||||
if event.Type == "checkout.session.completed" {
|
|
||||||
var session stripe.CheckoutSession
|
|
||||||
err := json.Unmarshal(event.Data.Raw, &session)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "Error parsing webhook JSON: %v\n", err)
|
|
||||||
response.WriteHeader(http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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