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:
18
main.go
18
main.go
@@ -14,6 +14,7 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
@@ -252,7 +253,7 @@ func handleRSVPPost(w http.ResponseWriter, r *http.Request) {
|
||||
resp := map[string]any{"numPeople": numPeople, "donation": donation}
|
||||
|
||||
if req.DonationCents > 0 {
|
||||
stripeURL, err := createCheckoutSession(eventID, email, req.DonationCents)
|
||||
stripeURL, err := createCheckoutSession(eventID, email, req.DonationCents, numPeople)
|
||||
if err != nil {
|
||||
log.Println("[ERROR] failed to create checkout session:", err)
|
||||
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)
|
||||
}
|
||||
|
||||
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")
|
||||
params := &stripe.CheckoutSessionParams{
|
||||
CustomerEmail: stripe.String(email),
|
||||
@@ -288,8 +289,9 @@ func createCheckoutSession(eventID, email string, amountCents int64) (string, er
|
||||
SuccessURL: stripe.String(fmt.Sprintf("%s/api/donate/success/%s?session_id={CHECKOUT_SESSION_ID}", baseURL, eventID)),
|
||||
CancelURL: stripe.String(fmt.Sprintf("%s/%s", baseURL, eventID)),
|
||||
Metadata: map[string]string{
|
||||
"event_id": eventID,
|
||||
"email": email,
|
||||
"event_id": eventID,
|
||||
"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
|
||||
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) {
|
||||
|
||||
@@ -141,12 +141,7 @@
|
||||
<span class="spacer"></span>
|
||||
<wa-button variant="neutral" size="small" id="logout-btn">Switch User</wa-button>
|
||||
</div>
|
||||
<div id="thank-you" class="thank-you" style="display: none;">
|
||||
Thank you for your donation!
|
||||
</div>
|
||||
<div id="rsvp-confirmed" class="thank-you" style="display: none;">
|
||||
Your RSVP has been updated!
|
||||
</div>
|
||||
<div id="thank-you" class="thank-you" style="display: none;"></div>
|
||||
<wa-card>
|
||||
<div class="event-header">
|
||||
<img src="/afac26-logo.png" alt="Applause for a Cause">
|
||||
@@ -199,10 +194,26 @@
|
||||
|
||||
const eventId = 'afac26';
|
||||
|
||||
const donatedAmount = new URLSearchParams(location.search).get('donated');
|
||||
if (donatedAmount) {
|
||||
document.getElementById('thank-you').textContent = `Thank you for your $${parseFloat(donatedAmount).toFixed(2)} donation!`;
|
||||
document.getElementById('thank-you').style.display = 'block';
|
||||
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) {
|
||||
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';
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -291,9 +302,7 @@
|
||||
location.href = data.url;
|
||||
} else {
|
||||
updateUI(data);
|
||||
const word = data.numPeople === 1 ? 'person' : 'people';
|
||||
document.getElementById('rsvp-confirmed').textContent = `Your RSVP has been updated to ${data.numPeople} ${word}.`;
|
||||
document.getElementById('rsvp-confirmed').style.display = 'block';
|
||||
showConfirmation(data.numPeople, null);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user