WIP Code cleanup
This commit is contained in:
1 parent
b4b0396b30
commit
dcc9754d43
14 files changed
+880
-902
No files matched your search
+94
-26
@@ -1,46 +1,114 @@
|
||||
package stripe
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/stripe/stripe-go/v72"
|
||||
"github.com/stripe/stripe-go/v72/checkout/session"
|
||||
"github.com/stripe/stripe-go/v72/paymentintent"
|
||||
"log"
|
||||
"payment-poc/database"
|
||||
"payment-poc/state"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
DB *sqlx.DB
|
||||
Provider *database.PaymentEntryProvider
|
||||
ApiKey string
|
||||
BackendUrl string
|
||||
}
|
||||
|
||||
func (s *Service) CreateEntry(totalAmount int64) (StripeDb, error) {
|
||||
id := uuid.Must(uuid.NewRandom())
|
||||
entry := StripeDb{
|
||||
Id: id,
|
||||
TotalAmount: totalAmount,
|
||||
func (s *Service) CreatePaymentUrl(amount int64) (url string, err error) {
|
||||
entry, err := s.Provider.CreateEntry(database.PaymentEntry{
|
||||
Gateway: state.GatewayVivaWallet,
|
||||
State: state.StateInitialized,
|
||||
}
|
||||
_, err := s.DB.Exec(`INSERT INTO "stripe" ("id", "total_amount", "payment_state") VALUES ($1, $2, $3)`,
|
||||
&entry.Id, &entry.TotalAmount, &entry.State,
|
||||
)
|
||||
TotalAmount: amount,
|
||||
})
|
||||
if err != nil {
|
||||
return StripeDb{}, err
|
||||
return "", err
|
||||
}
|
||||
return s.FetchById(id)
|
||||
entry, url, err = s.InitializePayment(entry)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
entry, err = s.Provider.UpdateEntry(entry)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return url, nil
|
||||
}
|
||||
|
||||
func (s *Service) FetchAll() ([]StripeDb, error) {
|
||||
var entries []StripeDb
|
||||
err := s.DB.Select(&entries, `SELECT * FROM "stripe"`)
|
||||
return entries, err
|
||||
func (s *Service) InitializePayment(entry database.PaymentEntry) (database.PaymentEntry, string, error) {
|
||||
|
||||
currency := string(stripe.CurrencyEUR)
|
||||
productName := "Example product"
|
||||
productDescription := "Simple example product"
|
||||
|
||||
params := &stripe.CheckoutSessionParams{
|
||||
LineItems: []*stripe.CheckoutSessionLineItemParams{
|
||||
{
|
||||
PriceData: &stripe.CheckoutSessionLineItemPriceDataParams{
|
||||
Currency: ¤cy,
|
||||
ProductData: &stripe.CheckoutSessionLineItemPriceDataProductDataParams{
|
||||
Name: &productName,
|
||||
Description: &productDescription,
|
||||
},
|
||||
UnitAmount: &entry.TotalAmount,
|
||||
},
|
||||
Quantity: stripe.Int64(1),
|
||||
},
|
||||
},
|
||||
Mode: stripe.String(string(stripe.CheckoutSessionModePayment)),
|
||||
PaymentIntentData: &stripe.CheckoutSessionPaymentIntentDataParams{
|
||||
CaptureMethod: stripe.String("manual"),
|
||||
},
|
||||
SuccessURL: stripe.String(s.BackendUrl + "/stripe/success?token=" + entry.Id.String()),
|
||||
CancelURL: stripe.String(s.BackendUrl + "/stripe/cancel?token=" + entry.Id.String()),
|
||||
}
|
||||
result, err := session.New(params)
|
||||
if err != nil {
|
||||
return database.PaymentEntry{}, "", err
|
||||
}
|
||||
entry.PaymentIntentId = &result.PaymentIntent.ID
|
||||
|
||||
return entry, result.URL, nil
|
||||
}
|
||||
|
||||
func (s *Service) FetchById(id uuid.UUID) (StripeDb, error) {
|
||||
entry := StripeDb{}
|
||||
err := s.DB.Get(&entry, `SELECT * FROM "stripe" WHERE "id" = $1`, id)
|
||||
return entry, err
|
||||
func (s *Service) CompleteTransaction(entry database.PaymentEntry, amount int64) (database.PaymentEntry, error) {
|
||||
params := &stripe.PaymentIntentCaptureParams{
|
||||
AmountToCapture: stripe.Int64(amount),
|
||||
}
|
||||
pi, err := paymentintent.Capture(*entry.PaymentIntentId, params)
|
||||
if err != nil {
|
||||
return database.PaymentEntry{}, err
|
||||
}
|
||||
log.Printf("received state on completion: %v", pi.Status)
|
||||
if pi.Status == stripe.PaymentIntentStatusSucceeded || pi.Status == stripe.PaymentIntentStatusProcessing {
|
||||
entry.TotalAmount = pi.Amount
|
||||
entry.State = state.StateCompleted
|
||||
}
|
||||
return entry, nil
|
||||
}
|
||||
|
||||
func (s *Service) Update(entry StripeDb) error {
|
||||
_, err := s.DB.Exec(`UPDATE "stripe" set "payment_intent_id" = $2, "payment_state" = $3 WHERE "id" = $1`,
|
||||
&entry.Id, &entry.PaymentIntentId, &entry.State,
|
||||
)
|
||||
return err
|
||||
func (s *Service) CancelTransaction(entry database.PaymentEntry) (database.PaymentEntry, error) {
|
||||
params := &stripe.PaymentIntentCancelParams{}
|
||||
pi, err := paymentintent.Cancel(*entry.PaymentIntentId, params)
|
||||
if err != nil {
|
||||
return database.PaymentEntry{}, err
|
||||
}
|
||||
log.Printf("received state on completion: %v", pi.Status)
|
||||
if pi.Status == stripe.PaymentIntentStatusCanceled {
|
||||
entry.State = state.StateCanceled
|
||||
}
|
||||
return entry, nil
|
||||
}
|
||||
|
||||
func (s *Service) HandleResponse(c *gin.Context, paymentState state.PaymentState) (string, error) {
|
||||
id := uuid.MustParse(c.Query("token"))
|
||||
entry, err := s.Provider.FetchById(id)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
entry.State = paymentState
|
||||
s.Provider.UpdateEntry(entry)
|
||||
return "/entries/" + entry.Id.String(), nil
|
||||
}
|
||||
Reference in new issue
Block a user