Skip to content

Commit

Permalink
stripe: use payment links (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
sleeping-h authored Feb 22, 2024
1 parent e101537 commit 718d7f1
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 139 deletions.
39 changes: 39 additions & 0 deletions data/stripe_products.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
- label: "€ 10.00"
id: "eur:1000"
payment_link: "test_cN28zq9FF4KU5NebII"

- label: "€ 20.00"
id: "eur:2000"
payment_link: "test_bIY7vmaJJ2CM4Ja7st"

- label: "€ 50.00"
id: "eur:5000"
payment_link: "test_cN28zq9FF4KU5NebII"

- label: "€ 100.00"
id: "eur:10000"
payment_link: "test_bIY7vmaJJ2CM4Ja7st"

- label: "€ 250.00"
id: "eur:25000"
payment_link: "test_bIY7vmaJJ2CM4Ja7st"

- label: "$ 10.00"
id: "usd:1000"
payment_link: "test_cN28zq9FF4KU5NebII"

- label: "$ 20.00"
id: "usd:2000"
payment_link: "test_bIY7vmaJJ2CM4Ja7st"

- label: "$ 50.00"
id: "usd:5000"
payment_link: "test_cN28zq9FF4KU5NebII"

- label: "$ 100.00"
id: "usd:10000"
payment_link: "test_bIY7vmaJJ2CM4Ja7st"

- label: "$ 250.00"
id: "usd:25000"
payment_link: "test_bIY7vmaJJ2CM4Ja7st"
131 changes: 18 additions & 113 deletions themes/hugo-bulma-blocks-theme/assets/js/donate.js
Original file line number Diff line number Diff line change
@@ -1,123 +1,28 @@
let stripe = null;
let stripeBaseUrl = 'https://192.168.1.8:1313/stripe';
//let stripeBaseUrl = 'https://qgis.org/stripe';
let stripeKeyUrl = stripeBaseUrl + '/stripe-key';
let recaptchaKeyUrl = stripeBaseUrl + '/recaptcha-key';
let stripeCheckoutUrl = stripeBaseUrl + '/create-checkout-session';

let recaptchaToken = null;

(function () {
setTimeout(function () {
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const success = urlParams.get('payment_success')
if (success && success === 'True') {
window.history.pushState("object or string", "Title", '{{- "{{ donation_url }}" -}}');
document.getElementById('thankyou').style.display = 'block';
}
}, 500)

/*
// Render recaptcha
fetch(recaptchaKeyUrl).then(response => response.json()).then(
json => {
const key = json['key'];
tTimeout(function () {
grecaptcha.render('recaptcha-container', {
'jsUrl': 'https://www.google.com/recaptcha/api.js?render=explicit',
'sitekey': key,
'callback': 'enableSubmitBtn'
});
1000);
}
)
*/

// Initialize Stripe
fetch(stripeKeyUrl).then(function (response) {
return response.json();
}).then(function(json) {
const key = json['key'];
if (key.includes("test")) {
$('.testText').show();
}
stripe = Stripe(key);
let elements = stripe.elements();
})
})();

let checkoutButton = document.getElementById('submit-button');

checkoutButton.addEventListener('click', function () {
//if (!isCaptchaChecked) {
// return;
//}
let donationValue = document.getElementById('donate-stripe').querySelector('input[name="amount"]:checked').value.split(':');
let donationCurrency = donationValue[0];
let donationAmount = donationValue[1];
const postData = {
donationCurrency: donationCurrency,
donationAmount: donationAmount,
// recaptchaToken: recaptchaToken
}

alert('aaaaaaaaa');
fetch(stripeCheckoutUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(postData)
})
.then(function (response) {
console.log(response);
if (response.ok) {
return response.json();
} else {
document.getElementById('stripeErrorMessage').style.display = 'block';
document.getElementById('stripeErrorMessage').innerHTML = response.statusText;
}
})
.then(function (session) {
console.log(session);
return stripe.redirectToCheckout({ sessionId: session.id });
})
.then(function (result) {
console.log(result);
// If `redirectToCheckout` fails due to a browser or network
// error, you should display the localized error message to your
// customer using `error.message`.
if (result.error) {
document.getElementById('stripeErrorMessage').style.display = 'block';
document.getElementById('stripeErrorMessage').innerHTML = result.error.message;
}
})
.catch(function (error) {
console.log(error);
});
});

/*
function enableSubmitBtn(response) {
document.getElementById("submit-button").disabled = false;
recaptchaToken = response;
}
function isCaptchaChecked() {
return grecaptcha && grecaptcha.getResponse().length !== 0 && recaptchaToken;
}
*/
function updatePaymentLink() {
// set href to "Donate" button depending on selected value
let paymentLink = document.getElementById('donate-stripe').querySelector('input[name="amount"]:checked').value;
let donateBtn = document.getElementById('submit-button');
donateBtn.href = "https://donate.stripe.com/" + paymentLink;
};

function updateCurrency() {
// function to toggle set of donate options depending on selected currency
let toShow = document.getElementById('currency').value;
let toHide = toShow == 'usd' ? 'eur' : 'usd';
document.getElementById('donate-stripe')
.querySelectorAll(`[for^="${toHide}:"]`)
.forEach(option => option.style.display = "none");
document.getElementById('donate-stripe')
.querySelectorAll(`[for^="${toShow}:"]`)
.forEach(option => option.style.display = "inline-block");
let visible = document.getElementById('donate-stripe')
.querySelectorAll(`[for^="${toShow}:"]`);
visible.forEach(option => option.style.display = "inline-block");
// check first visible element
document.getElementById(visible[0].getAttribute('for')).checked = true;
}

let radioButtons = document.getElementById('donate-stripe').querySelectorAll('input[name="amount"]');
radioButtons.forEach(function(radio) {
radio.addEventListener('change', updatePaymentLink);
});

updateCurrency();
updatePaymentLink();
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,20 @@
<p>One-Time Payment</p>
<hr>
<form id="donate-stripe">
<!-- NOTE: amount value is in CENTS !!!-->
<input value="eur:1000" id="eur:1000" type="radio" name="amount">
<label for="eur:1000" class="button is-primary1 is-outlined picker">€ 10.00</label>
<input value="eur:2500" id="eur:2500" type="radio" name="amount">
<label for="eur:2500" class="button is-primary1 is-outlined picker">€ 25.00</label>
<input value="eur:5000" id="eur:5000" type="radio" name="amount">
<label for="eur:5000" class="button is-primary1 is-outlined picker">€ 50.00</label>
<input value="eur:10000" id="eur:10000" type="radio" name="amount">
<label for="eur:10000" class="button is-primary1 is-outlined picker">€ 100.00</label>
<input value="eur:25000" id="eur:25000" type="radio" name="amount">
<label for="eur:25000" class="button is-primary1 is-outlined picker">€ 250.00</label>
<input value="usd:1000" id="usd:1000" type="radio" name="amount" checked>
<label for="usd:1000" class="button is-primary1 is-outlined picker">$ 10.00</label>
<input value="usd:2500" id="usd:2500" type="radio" name="amount">
<label for="usd:2500" class="button is-primary1 is-outlined picker">$ 25.00</label>
<input value="usd:5000" id="usd:5000" type="radio" name="amount">
<label for="usd:5000" class="button is-primary1 is-outlined picker">$ 50.00</label>
<input value="usd:10000" id="usd:10000" type="radio" name="amount">
<label for="usd:10000" class="button is-primary1 is-outlined picker">$ 100.00</label>
<input value="usd:25000" id="usd:25000" type="radio" name="amount">
<label for="usd:25000" class="button is-primary1 is-outlined picker">$ 250.00</label>
<br>
<button id='submit-button' class="button is-primary1 submit-button" style="width: 100%">Donate</button>
{{ range sort $.Site.Data.stripe_products }}
<input value="{{ .payment_link }}" id="{{ .id }}" type="radio" name="amount">
<label for="{{ .id }}" class="button is-primary1 is-outlined picker">{{ .label }}</label>
{{ end }}
</form>
<br>
<a
id="submit-button"
class="button is-primary1"
style="width: 100%; border-radius: 10px"
rel="noopener noreferrer"
target="_blank"
>Donate
</a>

<div class="is-flex is-justify-content-space-between">
{{ if .Get "otherMethods" }}
Expand All @@ -38,9 +28,7 @@
</div>
</div>

<script src="https://js.stripe.com/v3/"></script>
{{ $donatejs := resources.Get "js/donate.js" | resources.ExecuteAsTemplate "donate.js" . | resources.Fingerprint }}
{{ $donatejs := resources.Get "js/donate.js" }}
<script
src="{{ $donatejs.RelPermalink }}"
integrity="{{ $donatejs.Data.Integrity }}"
></script>

0 comments on commit 718d7f1

Please sign in to comment.