Show combined RSVP and donation confirmation after Stripe checkout
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
14
main.go
14
main.go
@@ -14,6 +14,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
@@ -252,7 +253,7 @@ func handleRSVPPost(w http.ResponseWriter, r *http.Request) {
|
|||||||
resp := map[string]any{"numPeople": numPeople, "donation": donation}
|
resp := map[string]any{"numPeople": numPeople, "donation": donation}
|
||||||
|
|
||||||
if req.DonationCents > 0 {
|
if req.DonationCents > 0 {
|
||||||
stripeURL, err := createCheckoutSession(eventID, email, req.DonationCents)
|
stripeURL, err := createCheckoutSession(eventID, email, req.DonationCents, numPeople)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("[ERROR] failed to create checkout session:", err)
|
log.Println("[ERROR] failed to create checkout session:", err)
|
||||||
http.Error(w, "failed to create checkout session", http.StatusInternalServerError)
|
http.Error(w, "failed to create checkout session", http.StatusInternalServerError)
|
||||||
@@ -265,7 +266,7 @@ func handleRSVPPost(w http.ResponseWriter, r *http.Request) {
|
|||||||
json.NewEncoder(w).Encode(resp)
|
json.NewEncoder(w).Encode(resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
func createCheckoutSession(eventID, email string, amountCents int64) (string, error) {
|
func createCheckoutSession(eventID, email string, amountCents int64, numPeople int) (string, error) {
|
||||||
baseURL := os.Getenv("BASE_URL")
|
baseURL := os.Getenv("BASE_URL")
|
||||||
params := &stripe.CheckoutSessionParams{
|
params := &stripe.CheckoutSessionParams{
|
||||||
CustomerEmail: stripe.String(email),
|
CustomerEmail: stripe.String(email),
|
||||||
@@ -290,6 +291,7 @@ func createCheckoutSession(eventID, email string, amountCents int64) (string, er
|
|||||||
Metadata: map[string]string{
|
Metadata: map[string]string{
|
||||||
"event_id": eventID,
|
"event_id": eventID,
|
||||||
"email": email,
|
"email": email,
|
||||||
|
"num_people": fmt.Sprintf("%d", numPeople),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -371,7 +373,13 @@ func handleDonateSuccess(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
amount := float64(sess.AmountTotal) / 100
|
amount := float64(sess.AmountTotal) / 100
|
||||||
http.Redirect(w, r, fmt.Sprintf("/%s?donated=%.2f", eventID, amount), http.StatusSeeOther)
|
redirectURL := fmt.Sprintf("/%s?donated=%.2f", eventID, amount)
|
||||||
|
if numPeopleStr := sess.Metadata["num_people"]; numPeopleStr != "" {
|
||||||
|
if numPeople, err := strconv.Atoi(numPeopleStr); err == nil && numPeople > 0 {
|
||||||
|
redirectURL += fmt.Sprintf("&num_people=%d", numPeople)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
http.Redirect(w, r, redirectURL, http.StatusSeeOther)
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleStripeWebhook(w http.ResponseWriter, r *http.Request) {
|
func handleStripeWebhook(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|||||||
@@ -141,12 +141,7 @@
|
|||||||
<span class="spacer"></span>
|
<span class="spacer"></span>
|
||||||
<wa-button variant="neutral" size="small" id="logout-btn">Switch User</wa-button>
|
<wa-button variant="neutral" size="small" id="logout-btn">Switch User</wa-button>
|
||||||
</div>
|
</div>
|
||||||
<div id="thank-you" class="thank-you" style="display: none;">
|
<div id="thank-you" class="thank-you" style="display: none;"></div>
|
||||||
Thank you for your donation!
|
|
||||||
</div>
|
|
||||||
<div id="rsvp-confirmed" class="thank-you" style="display: none;">
|
|
||||||
Your RSVP has been updated!
|
|
||||||
</div>
|
|
||||||
<wa-card>
|
<wa-card>
|
||||||
<div class="event-header">
|
<div class="event-header">
|
||||||
<img src="/afac26-logo.png" alt="Applause for a Cause">
|
<img src="/afac26-logo.png" alt="Applause for a Cause">
|
||||||
@@ -199,10 +194,26 @@
|
|||||||
|
|
||||||
const eventId = 'afac26';
|
const eventId = 'afac26';
|
||||||
|
|
||||||
const donatedAmount = new URLSearchParams(location.search).get('donated');
|
function showConfirmation(numPeople, donatedAmount) {
|
||||||
|
const parts = [];
|
||||||
|
if (numPeople > 0) {
|
||||||
|
const word = numPeople === 1 ? 'person' : 'people';
|
||||||
|
parts.push(`Your RSVP for ${numPeople} ${word} is confirmed.`);
|
||||||
|
}
|
||||||
if (donatedAmount) {
|
if (donatedAmount) {
|
||||||
document.getElementById('thank-you').textContent = `Thank you for your $${parseFloat(donatedAmount).toFixed(2)} donation!`;
|
parts.push(`Thank you for your $${donatedAmount.toFixed(2)} donation!`);
|
||||||
|
}
|
||||||
|
if (parts.length) {
|
||||||
|
document.getElementById('thank-you').textContent = parts.join(' ');
|
||||||
document.getElementById('thank-you').style.display = 'block';
|
document.getElementById('thank-you').style.display = 'block';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const params = new URLSearchParams(location.search);
|
||||||
|
const donatedParam = params.get('donated');
|
||||||
|
const numPeopleParam = params.get('num_people');
|
||||||
|
if (donatedParam || numPeopleParam) {
|
||||||
|
showConfirmation(parseInt(numPeopleParam) || 0, donatedParam ? parseFloat(donatedParam) : null);
|
||||||
history.replaceState({}, '', location.pathname);
|
history.replaceState({}, '', location.pathname);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -291,9 +302,7 @@
|
|||||||
location.href = data.url;
|
location.href = data.url;
|
||||||
} else {
|
} else {
|
||||||
updateUI(data);
|
updateUI(data);
|
||||||
const word = data.numPeople === 1 ? 'person' : 'people';
|
showConfirmation(data.numPeople, null);
|
||||||
document.getElementById('rsvp-confirmed').textContent = `Your RSVP has been updated to ${data.numPeople} ${word}.`;
|
|
||||||
document.getElementById('rsvp-confirmed').style.display = 'block';
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user