saveinmed/backend/internal/payments/stripe.go
Gabbriiel 90467db1ec refactor: substitui backend Medusa por backend Go e corrige testes do marketplace
- Remove backend Medusa.js (TypeScript) e substitui pelo backend Go (saveinmed-performance-core)
- Corrige testes auth.test.ts: alinha paths de API (v1/ sem barra inicial) e campo access_token
- Corrige GroupedProductCard.test.tsx: ajusta distância formatada (toFixed) e troca userEvent por fireEvent com fakeTimers
- Corrige AuthContext.test.tsx: usa vi.hoisted() para mocks e corrige parênteses no waitFor

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 04:56:37 -06:00

74 lines
2.2 KiB
Go

package payments
import (
"context"
"fmt"
"time"
"github.com/saveinmed/backend-go/internal/domain"
)
// StripeGateway implements payment processing via Stripe.
// In production, this would use the Stripe Go SDK.
type StripeGateway struct {
APIKey string
MarketplaceCommission float64
}
func NewStripeGateway(apiKey string, commission float64) *StripeGateway {
return &StripeGateway{
APIKey: apiKey,
MarketplaceCommission: commission,
}
}
func (g *StripeGateway) CreatePreference(ctx context.Context, order *domain.Order, payer *domain.User, sellerAcc *domain.SellerPaymentAccount) (*domain.PaymentPreference, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
// Calculate marketplace fee
fee := int64(float64(order.TotalCents) * (g.MarketplaceCommission / 100))
// In production, this would:
// 1. Create a Stripe PaymentIntent with transfer_data for connected accounts
// 2. Set application_fee_amount for marketplace commission
// 3. Return the client_secret for frontend confirmation
pref := &domain.PaymentPreference{
OrderID: order.ID,
Gateway: "stripe",
CommissionPct: g.MarketplaceCommission,
MarketplaceFee: fee,
SellerReceivable: order.TotalCents - fee,
PaymentURL: fmt.Sprintf("https://checkout.stripe.com/pay/%s", order.ID.String()),
// In production: would include client_secret, payment_intent_id
}
// Simulate API latency
time.Sleep(15 * time.Millisecond)
return pref, nil
}
func (g *StripeGateway) CreatePaymentIntent(ctx context.Context, order *domain.Order) (map[string]interface{}, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
fee := int64(float64(order.TotalCents) * (g.MarketplaceCommission / 100))
// Simulated Stripe PaymentIntent response
return map[string]interface{}{
"id": fmt.Sprintf("pi_%s", order.ID.String()[:8]),
"client_secret": fmt.Sprintf("pi_%s_secret_%d", order.ID.String()[:8], time.Now().UnixNano()),
"amount": order.TotalCents,
"currency": "brl",
"status": "requires_payment_method",
"application_fee": fee,
"transfer_data": map[string]interface{}{"destination": order.SellerID.String()},
}, nil
}