Skip to content

Instantly share code, notes, and snippets.

@rpaul-stripe
Created March 24, 2017 15:51
Show Gist options
  • Save rpaul-stripe/82e34d107b0f8624ee46109a3d841990 to your computer and use it in GitHub Desktop.
Save rpaul-stripe/82e34d107b0f8624ee46109a3d841990 to your computer and use it in GitHub Desktop.
Stripe Checkout Go Example
<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>
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)
}
@junaidkhan126072
Copy link

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"

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()
	email := r.Form.Get("stripeEmail")

	customerParams := &stripe.CustomerParams{Email: &email}
	customerParams.SetSource(r.Form.Get("stripeToken"))

	newCustomer, err := customer.New(customerParams)

	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	} 

	var amount int64
	amount = 500
	value := "usd"
	id := newCustomer.ID
	disc := "Sample Charge"
	chargeParams := &stripe.ChargeParams{
		Amount:      &amount,
		Currency:    &value,
		Description: &disc,
		Customer:    &id,
	}

	if _, err := charge.New(chargeParams); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	fmt.Fprintf(w, "Charge completed successfully!")
})
fmt.Println("checking .....start")
http.ListenAndServe(":4567", nil)

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment