Compare commits
No commits in common. "41cd4cc0555f8c0c549bf9d69fd41184b0485e4a" and "a5f92f7ffae4db6a32c595aba979e6f38f140897" have entirely different histories.
41cd4cc055
...
a5f92f7ffa
@ -7,11 +7,11 @@ tmp_dir = "tmp"
|
|||||||
|
|
||||||
[build]
|
[build]
|
||||||
# Just plain old shell command. You could use `make` as well.
|
# Just plain old shell command. You could use `make` as well.
|
||||||
cmd = "make"
|
cmd = "go build -o ./tmp/main ./cmd/web/"
|
||||||
# Binary file yields from `cmd`.
|
# Binary file yields from `cmd`.
|
||||||
bin = "dist/gostripe"
|
bin = "tmp main"
|
||||||
# Customize binary.
|
# Customize binary.
|
||||||
full_bin = "STRIPE_SECRET= STRIPE_KEY= ./dist/gostripe"
|
full_bin = "STRIPE_SECRET= STRIPE_KEY= ./tmp/main"
|
||||||
# Watch these filename extensions.
|
# Watch these filename extensions.
|
||||||
include_ext = ["go", "tpl", "tmpl", "html", "gohtml"]
|
include_ext = ["go", "tpl", "tmpl", "html", "gohtml"]
|
||||||
# Ignore these filename extensions or directories.
|
# Ignore these filename extensions or directories.
|
||||||
|
2
.gitignore
vendored
2
.gitignore
vendored
@ -2,5 +2,3 @@
|
|||||||
**/c.html
|
**/c.html
|
||||||
database.yml
|
database.yml
|
||||||
docker/docker-compose.yml
|
docker/docker-compose.yml
|
||||||
cred.txt
|
|
||||||
dist/
|
|
||||||
|
60
Makefile
60
Makefile
@ -1,60 +0,0 @@
|
|||||||
STRIPE_SECRET=$(shell sed '2q;d' cred.txt)
|
|
||||||
STRIPE_KEY=$(shell sed '2q;d' cred.txt)
|
|
||||||
GOSTRIPE_PORT=4000
|
|
||||||
API_PORT=4001
|
|
||||||
DSN=root@tcp(localhost:6379)/widgets?parseTime=true&tls=false
|
|
||||||
|
|
||||||
## build: builds all binaries
|
|
||||||
build: clean build_front build_back
|
|
||||||
@printf "All binaries built!\n"
|
|
||||||
|
|
||||||
## clean: cleans all binaries and runs go clean
|
|
||||||
clean:
|
|
||||||
@echo "Cleaning..."
|
|
||||||
@- rm -f dist/*
|
|
||||||
@go clean
|
|
||||||
@echo "Cleaned!"
|
|
||||||
|
|
||||||
## build_front: builds the front end
|
|
||||||
build_front:
|
|
||||||
@echo "Building front end..."
|
|
||||||
@go build -o dist/gostripe ./cmd/web
|
|
||||||
@echo "Front end built!"
|
|
||||||
|
|
||||||
## build_back: builds the back end
|
|
||||||
build_back:
|
|
||||||
@echo "Building back end..."
|
|
||||||
@go build -o dist/gostripe_api ./cmd/api
|
|
||||||
@echo "Back end built!"
|
|
||||||
|
|
||||||
## start: starts front and back end
|
|
||||||
start: start_front start_back
|
|
||||||
|
|
||||||
## start_front: starts the front end
|
|
||||||
start_front: build_front
|
|
||||||
@echo "Starting the front end..."
|
|
||||||
@env STRIPE_KEY=${STRIPE_KEY} STRIPE_SECRET=${STRIPE_SECRET} ./dist/gostripe -port=${GOSTRIPE_PORT} -dsn="${DSN}" &
|
|
||||||
@echo "Front end running!"
|
|
||||||
|
|
||||||
## start_back: starts the back end
|
|
||||||
start_back: build_back
|
|
||||||
@echo "Starting the back end..."
|
|
||||||
@env STRIPE_KEY=${STRIPE_KEY} STRIPE_SECRET=${STRIPE_SECRET} ./dist/gostripe_api -port=${API_PORT} -dsn="${DSN}" &
|
|
||||||
@echo "Back end running!"
|
|
||||||
|
|
||||||
## stop: stops the front and back end
|
|
||||||
stop: stop_front stop_back
|
|
||||||
@echo "All applications stopped"
|
|
||||||
|
|
||||||
## stop_front: stops the front end
|
|
||||||
stop_front:
|
|
||||||
@echo "Stopping the front end..."
|
|
||||||
@-pkill -SIGTERM -f "gostripe -port=${GOSTRIPE_PORT}"
|
|
||||||
@echo "Stopped front end"
|
|
||||||
|
|
||||||
## stop_back: stops the back end
|
|
||||||
stop_back:
|
|
||||||
@echo "Stopping the back end..."
|
|
||||||
@-pkill -SIGTERM -f "gostripe_api -port=${API_PORT}"
|
|
||||||
@echo "Stopped back end"
|
|
||||||
|
|
@ -1,89 +0,0 @@
|
|||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,32 +0,0 @@
|
|||||||
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)
|
|
||||||
}
|
|
@ -1,23 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
@ -64,12 +64,6 @@ func main() {
|
|||||||
"development",
|
"development",
|
||||||
"Application environment {development|production}",
|
"Application environment {development|production}",
|
||||||
)
|
)
|
||||||
flag.StringVar(
|
|
||||||
&cfg.db.dsn,
|
|
||||||
"dsn",
|
|
||||||
"root@tcp(localhost:6379)/widgets?parseTime=true&tls=false",
|
|
||||||
"Application environment {development|production}",
|
|
||||||
)
|
|
||||||
flag.StringVar(&cfg.api, "api", "http://localhost:4001", "URL to api")
|
flag.StringVar(&cfg.api, "api", "http://localhost:4001", "URL to api")
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
@ -20,11 +20,11 @@
|
|||||||
<div class="col">{{ block "content" . }} {{ end }}</div>
|
<div class="col">{{ block "content" . }} {{ end }}</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"
|
||||||
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
|
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
|
||||||
crossorigin="anonymous"></script>
|
crossorigin="anonymous"></script>
|
||||||
{{ block "js" . }}
|
{{ block "js" . }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
@ -3,120 +3,7 @@
|
|||||||
Virtual Terminal
|
Virtual Terminal
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{ define "content" }}
|
{{ define "content" }}
|
||||||
<h2 class="mt-3 text-center">Virtual Terminal</h2>
|
<h2>Virtual Terminal</h2>
|
||||||
<hr>
|
|
||||||
<div class="alert alert-danger text-center d-none" id="card-messages"></div>
|
|
||||||
<form action="/payment-succecded"
|
|
||||||
method="post"
|
|
||||||
name="charge_form"
|
|
||||||
id="charge_form"
|
|
||||||
class="d-blick needs-validation charge-form"
|
|
||||||
autocomplete="off"
|
|
||||||
novalidate="">
|
|
||||||
<div class="mb-3">
|
|
||||||
<label for="amount" class="form-label">Amount</label>
|
|
||||||
<input type="text"
|
|
||||||
id="amount"
|
|
||||||
name="amount"
|
|
||||||
autocomplete="amount-new"
|
|
||||||
required=""
|
|
||||||
class="form-control">
|
|
||||||
</div>
|
|
||||||
<div class="mb-3">
|
|
||||||
<label for="cardholder-name" class="form-label">Cardholder Name</label>
|
|
||||||
<input type="text"
|
|
||||||
id="cardholder-name"
|
|
||||||
name="cardholder_name"
|
|
||||||
autocomplete="cardholder-name-new"
|
|
||||||
required=""
|
|
||||||
class="form-control">
|
|
||||||
</div>
|
|
||||||
<div class="mb-3">
|
|
||||||
<label for="cardholder-email" class="form-label">Cardholder Email</label>
|
|
||||||
<input type="text"
|
|
||||||
id="cardholder-email"
|
|
||||||
name="cardholder_email"
|
|
||||||
autocomplete="cardholder-email-new"
|
|
||||||
required=""
|
|
||||||
class="form-control">
|
|
||||||
</div>
|
|
||||||
<!-- card number will be built by stripe -->
|
|
||||||
<div class="mb-3">
|
|
||||||
<label for="card-element" class="form-label">Credit Card</label>
|
|
||||||
<div class="form-control" id="card-element"></div>
|
|
||||||
<div class="alert-danger text-center" id="card-errors" role="alert"></div>
|
|
||||||
<div class="alert-success text-center" id="card-success" role="alert"></div>
|
|
||||||
</div>
|
|
||||||
<hr>
|
|
||||||
<a href="javascript:void(0)"
|
|
||||||
id="pay-button"
|
|
||||||
class="btn btn-primary"
|
|
||||||
onclick="val()">Charge Card</a>
|
|
||||||
<div class="text-center d-none" id="processing-payment">
|
|
||||||
<div class="spinner-border text-primary" role="status">
|
|
||||||
<span class="visually-hidden">Loading...</span>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</form>
|
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{ define "js" }}
|
{{ define "js" }}
|
||||||
<script src="https://js.stripe.com/v3/"></script>
|
|
||||||
<script>
|
|
||||||
|
|
||||||
let card;
|
|
||||||
let stripe;
|
|
||||||
const cardMessages = document.getElementById("card-messages");
|
|
||||||
const payButton = document.getElementById("pay-button");
|
|
||||||
const processing = document.getElementById("processing-payment");
|
|
||||||
|
|
||||||
stripe = Stripe(''); // TODO: Publish key to be added
|
|
||||||
|
|
||||||
function hidePayButton() {
|
|
||||||
payButton.classList.add("d-none");
|
|
||||||
processing.classList.remove("d-none");
|
|
||||||
}
|
|
||||||
|
|
||||||
function val() {
|
|
||||||
let form = document.getElementById("charge_form");
|
|
||||||
|
|
||||||
if (form.checkValidity() === false) {
|
|
||||||
this.event.preventDefault();
|
|
||||||
this.event.stopPropagation();
|
|
||||||
form.classList.add("was-validated");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
form.classList.add("was-validated");
|
|
||||||
hidePayButton();
|
|
||||||
}
|
|
||||||
|
|
||||||
(function () {
|
|
||||||
// create stripe & elements
|
|
||||||
const elements = stripe.elements();
|
|
||||||
const style = {
|
|
||||||
base: {
|
|
||||||
fontSize: '16px',
|
|
||||||
lineHeight: '24px',
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// create card entry
|
|
||||||
card = elements.create('card', {
|
|
||||||
style:style,
|
|
||||||
hidePostalCode: true,
|
|
||||||
});
|
|
||||||
card.mount("#card-element");
|
|
||||||
|
|
||||||
// check for input errors
|
|
||||||
card.addEventListener('change', function(event) {
|
|
||||||
var displayError = document.getElementById("card-errors");
|
|
||||||
if (event.error) {
|
|
||||||
displayError.classList.remove('d-none');
|
|
||||||
displayError.textContent = event.error.message;
|
|
||||||
} else {
|
|
||||||
displayError.classList.add('d-none');
|
|
||||||
displayError.textContent = "";
|
|
||||||
}
|
|
||||||
});
|
|
||||||
})();
|
|
||||||
</script>
|
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
6
go.mod
6
go.mod
@ -2,8 +2,4 @@ module myapp
|
|||||||
|
|
||||||
go 1.22.5
|
go 1.22.5
|
||||||
|
|
||||||
require (
|
require github.com/go-chi/chi/v5 v5.1.0
|
||||||
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,24 +1,2 @@
|
|||||||
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=
|
|
||||||
|
@ -1,75 +0,0 @@
|
|||||||
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…
x
Reference in New Issue
Block a user