Converting the vt post to use the back end
This commit is contained in:
parent
8ef4282393
commit
4a982cc73d
@ -343,3 +343,63 @@ func (app *application) CheckAuthentication(w http.ResponseWriter, r *http.Reque
|
||||
payload.Message = fmt.Sprintf("authenticated user %s", user.Email)
|
||||
app.writeJSON(w, http.StatusOK, payload)
|
||||
}
|
||||
|
||||
func (app *application) VirtualTerminalPaymentSucceeded(w http.ResponseWriter, r *http.Request) {
|
||||
var txnData struct {
|
||||
PaymentAmount int `json:"amount"`
|
||||
PaymentCurrency string `json:"currency"`
|
||||
FirstName string `json:"first_name"`
|
||||
LastName string `json:"last_name"`
|
||||
Email string `json:"email"`
|
||||
PaymentIntent string `json:"payment_intent"`
|
||||
PaymentMethod string `json:"payment_method"`
|
||||
BankReturnCode string `json:"bank_return_code"`
|
||||
ExpiryMonth int `json:"expiry_month"`
|
||||
ExpiryYear int `json:"expiry_year"`
|
||||
LastFour string `json:"last_four"`
|
||||
}
|
||||
|
||||
err := app.readJSON(w, r, &txnData)
|
||||
if err != nil {
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
card := cards.Card{
|
||||
Secret: app.config.stripe.secret,
|
||||
Key: app.config.stripe.key,
|
||||
}
|
||||
|
||||
pi, err := card.RetrievePaymentIntent(txnData.PaymentIntent)
|
||||
if err != nil {
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
pm, err := card.GetPaymentMethod(txnData.PaymentMethod)
|
||||
if err != nil {
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
txnData.LastFour = pm.Card.Last4
|
||||
txnData.ExpiryMonth = int(pm.Card.ExpMonth)
|
||||
txnData.ExpiryYear = int(pm.Card.ExpYear)
|
||||
|
||||
txn := models.Transaction{
|
||||
Amount: txnData.PaymentAmount,
|
||||
Currency: txnData.PaymentCurrency,
|
||||
LastFour: txnData.LastFour,
|
||||
ExpiryMonth: txnData.ExpiryMonth,
|
||||
ExpiryYear: txnData.ExpiryYear,
|
||||
BankReturnCode: pi.LatestCharge.ID,
|
||||
TransactionStatusID: 2,
|
||||
}
|
||||
|
||||
_, err = app.SaveTransaction(txn)
|
||||
if err != nil {
|
||||
app.badRequest(w, r, err)
|
||||
}
|
||||
|
||||
app.writeJSON(w, http.StatusOK, txn)
|
||||
}
|
||||
|
@ -30,6 +30,8 @@ func (app *application) routes() http.Handler {
|
||||
mux.Get("/test", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("got in"))
|
||||
})
|
||||
|
||||
mux.Post("/virtual-terminal-succedded", app.VirtualTerminalPaymentSucceeded)
|
||||
})
|
||||
|
||||
return mux
|
||||
|
Loading…
Reference in New Issue
Block a user