Getting the paymentIntent set up route and handler in API
This commit is contained in:
parent
79215d5fe1
commit
41cd4cc055
89
cmd/api/api.go
Normal file
89
cmd/api/api.go
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
version = "1.0.0"
|
||||||
|
)
|
||||||
|
|
||||||
|
type config struct {
|
||||||
|
port int
|
||||||
|
env string
|
||||||
|
db struct {
|
||||||
|
dsn string
|
||||||
|
}
|
||||||
|
stripe struct {
|
||||||
|
secret string
|
||||||
|
key string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type application struct {
|
||||||
|
config config
|
||||||
|
infolog *log.Logger
|
||||||
|
errorLog *log.Logger
|
||||||
|
version string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *application) serve() error {
|
||||||
|
srv := &http.Server{
|
||||||
|
Addr: fmt.Sprintf(":%d", app.config.port),
|
||||||
|
Handler: app.routes(),
|
||||||
|
IdleTimeout: 30 * time.Second,
|
||||||
|
ReadTimeout: 10 * time.Second,
|
||||||
|
ReadHeaderTimeout: 5 * time.Second,
|
||||||
|
WriteTimeout: 5 * time.Second,
|
||||||
|
}
|
||||||
|
|
||||||
|
app.infolog.Printf(
|
||||||
|
"Starting Backend server in %s mode on port %d",
|
||||||
|
app.config.env,
|
||||||
|
app.config.port,
|
||||||
|
)
|
||||||
|
return srv.ListenAndServe()
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var cfg config
|
||||||
|
|
||||||
|
flag.IntVar(&cfg.port, "port", 4001, "Server port to listen on")
|
||||||
|
flag.StringVar(
|
||||||
|
&cfg.env,
|
||||||
|
"env",
|
||||||
|
"development",
|
||||||
|
"Application environment {development|production|maintenance}",
|
||||||
|
)
|
||||||
|
flag.StringVar(
|
||||||
|
&cfg.db.dsn,
|
||||||
|
"dsn",
|
||||||
|
"root@tcp(localhost:6379)/widgets?parseTime=true&tls=false",
|
||||||
|
"Application environment {development|production}",
|
||||||
|
)
|
||||||
|
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
cfg.stripe.key = os.Getenv("STRIPE_KEY")
|
||||||
|
cfg.stripe.secret = os.Getenv("STRIPE_SECRET")
|
||||||
|
|
||||||
|
infoLog := log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime)
|
||||||
|
errorLog := log.New(os.Stdout, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile)
|
||||||
|
|
||||||
|
app := &application{
|
||||||
|
version: version,
|
||||||
|
config: cfg,
|
||||||
|
infolog: infoLog,
|
||||||
|
errorLog: errorLog,
|
||||||
|
}
|
||||||
|
|
||||||
|
err := app.serve()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
32
cmd/api/handlers-api.go
Normal file
32
cmd/api/handlers-api.go
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type stripePayload struct {
|
||||||
|
Currency string `json:"currency"`
|
||||||
|
Amount string `json:"amount"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type jsonResponse struct {
|
||||||
|
OK bool `json:"ok"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
Content string `json:"content"`
|
||||||
|
ID int `json:"id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *application) GetPaymentIntent(w http.ResponseWriter, r *http.Request) {
|
||||||
|
j := jsonResponse{
|
||||||
|
OK: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
out, err := json.MarshalIndent(j, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
app.errorLog.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.Write(out)
|
||||||
|
}
|
23
cmd/api/routes-api.go
Normal file
23
cmd/api/routes-api.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
|
"github.com/go-chi/cors"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (app *application) routes() http.Handler {
|
||||||
|
mux := chi.NewRouter()
|
||||||
|
|
||||||
|
mux.Use(cors.Handler(cors.Options{
|
||||||
|
AllowedOrigins: []string{"https://*", "http://*"},
|
||||||
|
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
|
||||||
|
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
|
||||||
|
AllowCredentials: false,
|
||||||
|
MaxAge: 300,
|
||||||
|
}))
|
||||||
|
|
||||||
|
mux.Get("/api/payment-intent", app.GetPaymentIntent)
|
||||||
|
return mux
|
||||||
|
}
|
6
go.mod
6
go.mod
@ -2,4 +2,8 @@ module myapp
|
|||||||
|
|
||||||
go 1.22.5
|
go 1.22.5
|
||||||
|
|
||||||
require github.com/go-chi/chi/v5 v5.1.0
|
require (
|
||||||
|
github.com/go-chi/chi/v5 v5.1.0
|
||||||
|
github.com/go-chi/cors v1.2.1
|
||||||
|
github.com/stripe/stripe-go/v79 v79.6.0
|
||||||
|
)
|
||||||
|
22
go.sum
22
go.sum
@ -1,2 +1,24 @@
|
|||||||
|
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
|
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
|
||||||
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
|
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
|
||||||
|
github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4=
|
||||||
|
github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||||
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/stripe/stripe-go/v79 v79.6.0 h1:qSBV2f2rpLEEZTdTlVLzdmQJZNmfoo2E3hUEkFT8GBc=
|
||||||
|
github.com/stripe/stripe-go/v79 v79.6.0/go.mod h1:cuH6X0zC8peY6f1AubHwgJ/fJSn2dh5pfiCr6CjyKVU=
|
||||||
|
golang.org/x/net v0.0.0-20210520170846-37e1c6afe023 h1:ADo5wSpq2gqaCGQWzk7S5vd//0iyyLeAratkEoG5dLE=
|
||||||
|
golang.org/x/net v0.0.0-20210520170846-37e1c6afe023/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||||
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
|
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
|
||||||
|
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
75
internal/card/card.go
Normal file
75
internal/card/card.go
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
package card
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/stripe/stripe-go/v79"
|
||||||
|
"github.com/stripe/stripe-go/v79/paymentintent"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Card struct {
|
||||||
|
Secret string
|
||||||
|
Key string
|
||||||
|
Currency string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Transaction struct {
|
||||||
|
TransactionStatusID int
|
||||||
|
Amount int
|
||||||
|
Currency string
|
||||||
|
LastFour string
|
||||||
|
BankReturnCode string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Card) Charge(currency string, amount int) (*stripe.PaymentIntent, string, error) {
|
||||||
|
return c.CreatePaymentIntent(currency, amount)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Card) CreatePaymentIntent(
|
||||||
|
currency string,
|
||||||
|
amount int,
|
||||||
|
) (*stripe.PaymentIntent, string, error) {
|
||||||
|
stripe.Key = c.Secret
|
||||||
|
|
||||||
|
// create a payment intent
|
||||||
|
params := &stripe.PaymentIntentParams{
|
||||||
|
Amount: stripe.Int64(int64(amount)),
|
||||||
|
Currency: stripe.String(currency),
|
||||||
|
}
|
||||||
|
|
||||||
|
// params.AddMetadata("key", "value")
|
||||||
|
|
||||||
|
pi, err := paymentintent.New(params)
|
||||||
|
if err != nil {
|
||||||
|
msg := ""
|
||||||
|
if stripeErr, ok := err.(*stripe.Error); ok {
|
||||||
|
msg = cardErrorMessage(stripeErr.Code)
|
||||||
|
}
|
||||||
|
return nil, msg, err
|
||||||
|
}
|
||||||
|
return pi, "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func cardErrorMessage(code stripe.ErrorCode) string {
|
||||||
|
msg := ""
|
||||||
|
|
||||||
|
switch code {
|
||||||
|
case stripe.ErrorCodeCardDeclined:
|
||||||
|
msg = "Your card was declined"
|
||||||
|
case stripe.ErrorCodeExpiredCard:
|
||||||
|
msg = "Your card is expired"
|
||||||
|
case stripe.ErrorCodeIncorrectCVC:
|
||||||
|
msg = "Incorrect CVC code"
|
||||||
|
case stripe.ErrorCodeIncorrectZip:
|
||||||
|
msg = "Incorrect zip/postal code"
|
||||||
|
case stripe.ErrorCodeAmountTooLarge:
|
||||||
|
msg = "The amount is too large to charge to your card"
|
||||||
|
case stripe.ErrorCodeAmountTooSmall:
|
||||||
|
msg = "The amount is too small to charge to your card"
|
||||||
|
case stripe.ErrorCodeBalanceInsufficient:
|
||||||
|
msg = "Insufficient balance"
|
||||||
|
case stripe.ErrorCodePostalCodeInvalid:
|
||||||
|
msg = "Incorrect zip/postal code"
|
||||||
|
default:
|
||||||
|
msg = "Your card was declined"
|
||||||
|
}
|
||||||
|
return msg
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user