From d24aa2cef0e37e082113808e66c27129f85317b9 Mon Sep 17 00:00:00 2001 From: nutcas3 Date: Fri, 17 Apr 2026 15:51:50 +0300 Subject: [PATCH] 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 --- .../internal/payment/gateways/stripe.go | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/apps/api-server/internal/payment/gateways/stripe.go b/apps/api-server/internal/payment/gateways/stripe.go index c071116..dbdab0c 100644 --- a/apps/api-server/internal/payment/gateways/stripe.go +++ b/apps/api-server/internal/payment/gateways/stripe.go @@ -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{