-
-
Save MadFaill/9186c4d7f83df43fefc4890e32705a60 to your computer and use it in GitHub Desktop.
Stripe Checkout Go Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Checkout Example</title> | |
</head> | |
<body> | |
<form action="/charge" method="post" class="payment"> | |
<article> | |
<label class="amount"> | |
<span>Amount: $5.00</span> | |
</label> | |
</article> | |
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button" | |
data-key="{{ .Key }}" | |
data-description="A month's subscription" | |
data-amount="500" | |
data-locale="auto"></script> | |
</form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/stripe/stripe-go" | |
"github.com/stripe/stripe-go/charge" | |
"github.com/stripe/stripe-go/customer" | |
"html/template" | |
"net/http" | |
"os" | |
"path/filepath" | |
) | |
func main() { | |
publishableKey := os.Getenv("PUBLISHABLE_KEY") | |
stripe.Key = os.Getenv("SECRET_KEY") | |
tmpls, _ := template.ParseFiles(filepath.Join("templates", "index.html")) | |
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
tmpl := tmpls.Lookup("index.html") | |
tmpl.Execute(w, map[string]string{"Key": publishableKey}) | |
}) | |
http.HandleFunc("/charge", func(w http.ResponseWriter, r *http.Request) { | |
r.ParseForm() | |
customerParams := &stripe.CustomerParams{Email: r.Form.Get("stripeEmail")} | |
customerParams.SetSource(r.Form.Get("stripeToken")) | |
newCustomer, err := customer.New(customerParams) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
chargeParams := &stripe.ChargeParams{ | |
Amount: 500, | |
Currency: "usd", | |
Desc: "Sample Charge", | |
Customer: newCustomer.ID, | |
} | |
if _, err := charge.New(chargeParams); err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
fmt.Fprintf(w, "Charge completed successfully!") | |
}) | |
http.ListenAndServe(":4567", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment