feat: Add RetrievePaymentMethodFromToken to extract card details from Stripe tokens

- Add RetrievePaymentMethodFromToken to create payment method from token and extract card details
- Import paymentmethod package from stripe-go/v82
- Import payment package for PaymentMethodDetails type
- Return PaymentMethodDetails with ID, type, last4, brand, expiry dates, fingerprint, and creation timestamp
- Add error handling for payment method creation and missing card details
This commit is contained in:
nutcas3
2026-04-17 15:51:50 +03:00
parent 3d0f0188fb
commit d24aa2cef0
@@ -12,10 +12,12 @@ import (
"github.com/stripe/stripe-go/v82/invoice"
"github.com/stripe/stripe-go/v82/invoiceitem"
"github.com/stripe/stripe-go/v82/paymentintent"
"github.com/stripe/stripe-go/v82/paymentmethod"
"github.com/stripe/stripe-go/v82/refund"
"github.com/stripe/stripe-go/v82/webhook"
"github.com/nutcas3/telecom-platform/apps/api-server/internal/models"
"github.com/nutcas3/telecom-platform/apps/api-server/internal/payment"
)
// StripeGateway implements payment processing via Stripe
@@ -67,6 +69,38 @@ func (g *StripeGateway) ProcessPayment(ctx context.Context, req *PaymentRequest)
}, nil
}
// RetrievePaymentMethodFromToken retrieves payment method details from a Stripe token
func (g *StripeGateway) RetrievePaymentMethodFromToken(ctx context.Context, token string) (*payment.PaymentMethodDetails, error) {
// Create payment method from token
params := &stripe.PaymentMethodParams{
Type: stripe.String("card"),
Card: &stripe.PaymentMethodCardParams{
Token: stripe.String(token),
},
}
pm, err := paymentmethod.New(params)
if err != nil {
return nil, fmt.Errorf("failed to create payment method from token: %w", err)
}
// Extract card details
if pm.Card == nil {
return nil, fmt.Errorf("no card details found in payment method")
}
return &payment.PaymentMethodDetails{
PaymentMethodID: pm.ID,
Type: string(pm.Type),
Last4: pm.Card.Last4,
Brand: string(pm.Card.Brand),
ExpiryMonth: int(pm.Card.ExpMonth),
ExpiryYear: int(pm.Card.ExpYear),
Fingerprint: pm.Card.Fingerprint,
CreatedAt: time.Unix(pm.Created, 0),
}, nil
}
// CreateCustomer creates a Stripe customer
func (g *StripeGateway) CreateCustomer(ctx context.Context, subscriber *models.Subscriber) (*CustomerResponse, error) {
params := &stripe.CustomerParams{