stripe payment

This commit is contained in:
Joerg Lehmann 2021-04-05 13:20:19 +02:00
parent 062f11ff24
commit 8c8fb055e8
3 changed files with 86 additions and 5 deletions

81
static/js/checkout.js Normal file
View File

@ -0,0 +1,81 @@
// 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
var stripe = Stripe('pk_test_51Icq29K2XyHQRTs2DeltUIWrbFb5evfJVGaQzMv4r50g8Q7HAUkSFr7BBGv0aP1damXIhM6fZ4Yf9Wz0qW6TpTVi00dWcW7J1O');
var elements = stripe.elements();
// Set up Stripe.js and Elements to use in checkout form
var style = {
base: {
color: "#32325d",
}
};
var card = elements.create("card", { style: style });
card.mount("#card-element");
card.addEventListener('change', ({error}) => {
const displayError = document.getElementById('card-errors');
if (error) {
displayError.textContent = error.message;
} else {
displayError.textContent = '';
}
});
var submitButton = document.getElementById('submit');
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;
}
submitButton.addEventListener('click', function(ev) {
var clientSecret = GetClientSecret();
stripe.confirmCardPayment(clientSecret, {
payment_method: {
card: card,
billing_details: {
name: 'Jenny Rosen'
}
}
}).then(function(result) {
if (result.error) {
// Show error to your customer (e.g., insufficient funds)
console.log(result.error.message);
} else {
// The payment has been processed!
if (result.paymentIntent.status === 'succeeded') {
// Show a success message to your customer
// There's a risk of the customer closing the window before callback
// execution. Set up a webhook or plugin to listen for the
// payment_intent.succeeded event that handles any business critical
// post-payment actions.
alert("Payment succeeded!!!");
}
}
});
});

View File

@ -77,7 +77,7 @@ $(document).ready(function () {
} else {
this_date = paid_until;
}
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>';
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 * 60).toFixed(2) + '</td></tr>';
if (charge_data == '') {
charge_data = $(this).find("div").first().attr('id') + ":" + this_count;
} else {
@ -85,7 +85,7 @@ $(document).ready(function () {
}
}
});
abo_table += '<tr><td>Total CHF</td><td></td><td class="has-text-right">' + (counter * 24).toFixed(2) + '</td></tr>';
abo_table += '<tr><td>Total CHF</td><td></td><td class="has-text-right">' + (counter * 60).toFixed(2) + '</td></tr>';
abo_table += "</table>";
if (counter > 0) {
$("#abos_verlaengern").html(abo_table);
@ -157,7 +157,7 @@ function loadStripeLibrary() {
function SetupStripe() {
// 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
stripe = Stripe('pk_test_YkSGqH3Tk9WKK9HrlY63GhAg');
stripe = Stripe('pk_test_51Icq29K2XyHQRTs2DeltUIWrbFb5evfJVGaQzMv4r50g8Q7HAUkSFr7BBGv0aP1damXIhM6fZ4Yf9Wz0qW6TpTVi00dWcW7J1O');
elements = stripe.elements({ locale: "de" });
// Set up Stripe.js and Elements to use in checkout form

View File

@ -41,7 +41,7 @@ func getstripepaymentintentHandler(response http.ResponseWriter, request *http.R
}
}
}
abo_amount := int64(abo_years * 2400)
abo_amount := int64(abo_years * 6000)
stripe.Key = getStripeKey()
@ -75,7 +75,7 @@ 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))
line := fmt.Sprintf("%-30s %20s %10.2f\n", getDevAlias(deveui), getActiveUntil(deveui), float64(60*years))
charge_data_email_text = charge_data_email_text + line
}
}