Getting the paymentIntent
This commit is contained in:
parent
75f8e16cfe
commit
1b4d1aef58
@ -2,7 +2,9 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"myapp/internal/cards"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
type stripePayload struct {
|
type stripePayload struct {
|
||||||
@ -12,21 +14,54 @@ type stripePayload struct {
|
|||||||
|
|
||||||
type jsonResponse struct {
|
type jsonResponse struct {
|
||||||
OK bool `json:"ok"`
|
OK bool `json:"ok"`
|
||||||
Message string `json:"message"`
|
Message string `json:"message,omitempty"`
|
||||||
Content string `json:"content"`
|
Content string `json:"content,omitempty"`
|
||||||
ID int `json:"id"`
|
ID int `json:"id,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *application) GetPaymentIntent(w http.ResponseWriter, r *http.Request) {
|
func (app *application) GetPaymentIntent(w http.ResponseWriter, r *http.Request) {
|
||||||
j := jsonResponse{
|
var payload stripePayload
|
||||||
OK: true,
|
|
||||||
}
|
|
||||||
|
|
||||||
out, err := json.MarshalIndent(j, "", " ")
|
err := json.NewDecoder(r.Body).Decode(&payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.errorLog.Println(err)
|
app.errorLog.Println(err)
|
||||||
|
return // TODO: return a valid json
|
||||||
}
|
}
|
||||||
|
|
||||||
|
amount, err := strconv.Atoi(payload.Amount)
|
||||||
|
if err != nil {
|
||||||
|
app.errorLog.Println(err)
|
||||||
|
return // TODO: return a valid json
|
||||||
|
}
|
||||||
|
|
||||||
|
card := cards.Card{
|
||||||
|
Secret: app.config.stripe.secret,
|
||||||
|
Key: app.config.stripe.key,
|
||||||
|
Currency: payload.Currency,
|
||||||
|
}
|
||||||
|
|
||||||
|
pi, msg, err := card.Charge(payload.Currency, amount)
|
||||||
|
if err != nil {
|
||||||
|
j := jsonResponse{
|
||||||
|
OK: false,
|
||||||
|
Message: msg,
|
||||||
|
Content: "",
|
||||||
|
}
|
||||||
|
|
||||||
|
out, err := json.MarshalIndent(j, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
app.errorLog.Println(err)
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.Write(out)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
out, err := json.MarshalIndent(pi, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
app.errorLog.Println(err)
|
||||||
|
return // TODO: return a valid json
|
||||||
|
}
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.Write(out)
|
w.Write(out)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user