Created
March 24, 2017 15:51
-
-
Save rpaul-stripe/82e34d107b0f8624ee46109a3d841990 to your computer and use it in GitHub Desktop.
Stripe Checkout Go Example
This file contains hidden or 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 hidden or 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
there are some errors in code...but I try to remove them ....new code of main.go file is here......
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"
"path/filepath"
)
func main() {
publishableKey := "pk_test_RbEX8nfbG46rtdIFjveIM7SS00pLCspbDp"
stripe.Key = "sk_test_KgVJC0IJtSVeZmmuuHu9aS0f005iVmCFmi"
}