udemy-go-web-2/internal/cards/cards.go

76 lines
1.7 KiB
Go
Raw Normal View History

package cards
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
}