mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-10 12:46:53 +00:00
Introduce Stripe integration for automatic payment ingestion and refund tracking. Adds new fields to the payment model for Stripe IDs and status, Stripe client driver, sync service, cron job, manual API endpoint, and public webhook handler for real-time updates. Includes tests and documentation. Manual cash entry remains supported.
85 lines
2.3 KiB
Go
85 lines
2.3 KiB
Go
package booking
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log/slog"
|
|
"testing"
|
|
|
|
stripe "github.com/stripe/stripe-go/v79"
|
|
|
|
"github.com/rjNemo/rentease/internal/config"
|
|
)
|
|
|
|
func TestHandleChargeRefundedUpdatesAmount(t *testing.T) {
|
|
store := &mockStore{}
|
|
stripeID := "pi_123"
|
|
status := "succeeded"
|
|
_, _ = store.UpsertStripePayment(&Payment{
|
|
BookingID: 42,
|
|
Amount: 100,
|
|
PaymentMethod: config.PaymentMethod("Card"),
|
|
StripePaymentID: &stripeID,
|
|
StripeStatus: &status,
|
|
})
|
|
|
|
svc, err := NewService(slog.New(slog.DiscardHandler), store, nil, nil, nil)
|
|
if err != nil {
|
|
t.Fatalf("NewService returned error: %v", err)
|
|
}
|
|
|
|
charge := &stripe.Charge{
|
|
ID: "ch_123",
|
|
Amount: 10000,
|
|
AmountRefunded: 2500,
|
|
PaymentIntent: &stripe.PaymentIntent{ID: stripeID},
|
|
}
|
|
|
|
if err := svc.HandleChargeRefunded(context.Background(), charge); err != nil {
|
|
t.Fatalf("HandleChargeRefunded returned error: %v", err)
|
|
}
|
|
|
|
updated, err := store.FindStripePayment(stripeID)
|
|
if err != nil {
|
|
t.Fatalf("expected payment to be present: %v", err)
|
|
}
|
|
|
|
if updated.Amount != 75 {
|
|
t.Fatalf("expected amount 75, got %v", updated.Amount)
|
|
}
|
|
if updated.StripeStatus == nil || *updated.StripeStatus != "refunded" {
|
|
t.Fatalf("expected status refunded, got %v", updated.StripeStatus)
|
|
}
|
|
}
|
|
|
|
func TestHandleChargeRefundedUnknownPayment(t *testing.T) {
|
|
store := &mockStore{}
|
|
svc, _ := NewService(slog.New(slog.DiscardHandler), store, nil, nil, nil)
|
|
|
|
charge := &stripe.Charge{PaymentIntent: &stripe.PaymentIntent{ID: "pi_missing"}}
|
|
|
|
if err := svc.HandleChargeRefunded(context.Background(), charge); err != nil {
|
|
t.Fatalf("expected no error, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestHandleChargeRefundedStoreError(t *testing.T) {
|
|
store := &mockStore{}
|
|
stripeID := "pi_321"
|
|
status := "succeeded"
|
|
_, _ = store.UpsertStripePayment(&Payment{
|
|
BookingID: 1,
|
|
Amount: 10,
|
|
PaymentMethod: config.PaymentMethod("Card"),
|
|
StripePaymentID: &stripeID,
|
|
StripeStatus: &status,
|
|
})
|
|
store.err = errors.New("db error")
|
|
|
|
svc, _ := NewService(slog.New(slog.DiscardHandler), store, nil, nil, nil)
|
|
charge := &stripe.Charge{PaymentIntent: &stripe.PaymentIntent{ID: stripeID}}
|
|
|
|
if err := svc.HandleChargeRefunded(context.Background(), charge); err == nil {
|
|
t.Fatalf("expected error when store fails")
|
|
}
|
|
}
|