|
8 | 8 | "time" |
9 | 9 |
|
10 | 10 | "github.com/alpacahq/alpacadecimal" |
| 11 | + "github.com/invopop/gobl/currency" |
11 | 12 | "github.com/samber/lo" |
12 | 13 | "github.com/stretchr/testify/assert" |
13 | 14 | "github.com/stretchr/testify/require" |
@@ -94,6 +95,64 @@ func (s *CreditPurchaseTestSuite) TestPromotionalCreditPurchase() { |
94 | 95 | s.Equal(creditpurchase.StatusFinal, updatedCPCharge.Status) |
95 | 96 | } |
96 | 97 |
|
| 98 | +func (s *CreditPurchaseTestSuite) TestCreditPurchaseRejectsMismatchedSettlementCurrency() { |
| 99 | + ctx := context.Background() |
| 100 | + ns := s.GetUniqueNamespace("charges-service-credit-purchase-mismatched-settlement-currency") |
| 101 | + |
| 102 | + cust := s.CreateTestCustomer(ns, "test-subject") |
| 103 | + s.NotEmpty(cust.ID) |
| 104 | + |
| 105 | + servicePeriod := timeutil.ClosedPeriod{ |
| 106 | + From: datetime.MustParseTimeInLocation(s.T(), "2026-01-01T00:00:00Z", time.UTC).AsTime(), |
| 107 | + To: datetime.MustParseTimeInLocation(s.T(), "2026-02-01T00:00:00Z", time.UTC).AsTime(), |
| 108 | + } |
| 109 | + |
| 110 | + for _, tc := range []struct { |
| 111 | + name string |
| 112 | + settlement creditpurchase.Settlement |
| 113 | + }{ |
| 114 | + { |
| 115 | + name: "external", |
| 116 | + settlement: creditpurchase.NewSettlement(creditpurchase.ExternalSettlement{ |
| 117 | + InitialStatus: creditpurchase.CreatedInitialPaymentSettlementStatus, |
| 118 | + GenericSettlement: creditpurchase.GenericSettlement{ |
| 119 | + Currency: currencyx.Code(currency.EUR), |
| 120 | + CostBasis: alpacadecimal.NewFromFloat(0.5), |
| 121 | + }, |
| 122 | + }), |
| 123 | + }, |
| 124 | + { |
| 125 | + name: "invoice", |
| 126 | + settlement: creditpurchase.NewSettlement(creditpurchase.InvoiceSettlement{ |
| 127 | + GenericSettlement: creditpurchase.GenericSettlement{ |
| 128 | + Currency: currencyx.Code(currency.EUR), |
| 129 | + CostBasis: alpacadecimal.NewFromFloat(0.5), |
| 130 | + }, |
| 131 | + }), |
| 132 | + }, |
| 133 | + } { |
| 134 | + s.Run(tc.name, func() { |
| 135 | + intent := CreateCreditPurchaseIntent(s.T(), createCreditPurchaseIntentInput{ |
| 136 | + customer: cust.GetID(), |
| 137 | + currency: USD, |
| 138 | + amount: alpacadecimal.NewFromFloat(100), |
| 139 | + servicePeriod: servicePeriod, |
| 140 | + settlement: tc.settlement, |
| 141 | + }) |
| 142 | + |
| 143 | + res, err := s.Charges.Create(ctx, charges.CreateInput{ |
| 144 | + Namespace: ns, |
| 145 | + Intents: charges.ChargeIntents{ |
| 146 | + intent, |
| 147 | + }, |
| 148 | + }) |
| 149 | + s.Error(err) |
| 150 | + s.ErrorContains(err, `settlement currency "EUR" must match credit currency "USD"`) |
| 151 | + s.Empty(res) |
| 152 | + }) |
| 153 | + } |
| 154 | +} |
| 155 | + |
97 | 156 | type createCreditPurchaseIntentInput struct { |
98 | 157 | customer customer.CustomerID |
99 | 158 | currency currencyx.Code |
|
0 commit comments