summaryrefslogtreecommitdiff
path: root/internal/billing/auto_topup_test.go
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--internal/billing/auto_topup_test.go43
1 files changed, 43 insertions, 0 deletions
diff --git a/internal/billing/auto_topup_test.go b/internal/billing/auto_topup_test.go
index 85edb3d..55dc22d 100644
--- a/internal/billing/auto_topup_test.go
+++ b/internal/billing/auto_topup_test.go
@@ -25,6 +25,49 @@ func TestAutoTopUpReturnURL(t *testing.T) {
}
}
+func TestAutoTopUpStripeContracts(t *testing.T) {
+ service := &Service{
+ currency: "usd",
+ stripeSuccessURL: "https://console.example.test/billing?topup=success",
+ stripeCancelURL: "https://console.example.test/billing?topup=cancel",
+ integrationIdentifier: "aigw_balance_abcdefgh",
+ }
+ setup := service.autoTopUpSetupSessionParams(AutoTopUpSetupInput{
+ TenantID: "tenant-123", CustomerEmail: " billing@example.test ",
+ }, "")
+ if setup.Mode == nil || *setup.Mode != string(stripe.CheckoutSessionModeSetup) || setup.Currency == nil || *setup.Currency != "usd" {
+ t.Fatalf("unexpected setup contract %+v", setup)
+ }
+ if len(setup.PaymentMethodTypes) != 0 || setup.CustomerCreation == nil || *setup.CustomerCreation != string(stripe.CheckoutSessionCustomerCreationAlways) {
+ t.Fatal("setup Checkout must create a customer and use Dashboard-managed payment methods")
+ }
+ if setup.CustomerEmail == nil || *setup.CustomerEmail != "billing@example.test" || setup.Metadata["aigw_action"] != autoTopUpAction {
+ t.Fatal("setup Checkout customer or metadata contract is incomplete")
+ }
+ if setup.IdempotencyKey == nil || !strings.HasPrefix(*setup.IdempotencyKey, "aigw_autotopup_setup_") {
+ t.Fatalf("setup idempotency key = %v", setup.IdempotencyKey)
+ }
+
+ payment := service.autoTopUpPaymentIntentParams(
+ "tenant-123", "order-123", "cus_123", " billing@example.test ", "pm_123", "usd", 2000,
+ )
+ if payment.Amount == nil || *payment.Amount != 2000 || payment.Currency == nil || *payment.Currency != "usd" ||
+ payment.Customer == nil || *payment.Customer != "cus_123" || payment.PaymentMethod == nil || *payment.PaymentMethod != "pm_123" {
+ t.Fatalf("unexpected automatic top-up PaymentIntent %+v", payment)
+ }
+ if payment.Confirm == nil || !*payment.Confirm || payment.OffSession == nil || !*payment.OffSession ||
+ payment.ErrorOnRequiresAction == nil || !*payment.ErrorOnRequiresAction {
+ t.Fatal("automatic top-up must be confirmed off-session and stop on required customer action")
+ }
+ if payment.ReceiptEmail == nil || *payment.ReceiptEmail != "billing@example.test" ||
+ payment.Metadata["aigw_topup_order_id"] != "order-123" {
+ t.Fatal("automatic top-up receipt or reconciliation metadata is incomplete")
+ }
+ if payment.IdempotencyKey == nil || *payment.IdempotencyKey != "aigw_autotopup_order-123" {
+ t.Fatalf("payment idempotency key = %v", payment.IdempotencyKey)
+ }
+}
+
func TestAutomaticTopUpSetupAndCreditAreIdempotentPostgres(t *testing.T) {
databaseURL := os.Getenv("AIGW_TEST_DATABASE_URL")
if databaseURL == "" {