saveinmed/backend-old/internal/payments/stripe.go
2026-01-16 10:51:52 -03: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) (*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
}