-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
599 additions
and
331 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package appstripeentityapp | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/alpacahq/alpacadecimal" | ||
"github.com/openmeterio/openmeter/pkg/currencyx" | ||
) | ||
|
||
// NewStripeCalculator creates a new StripeCalculator. | ||
func NewStripeCalculator(currency currencyx.Code) (StripeCalculator, error) { | ||
calculator, err := currencyx.Code(currency).Calculator() | ||
if err != nil { | ||
return StripeCalculator{}, fmt.Errorf("failed to get stripe calculator: %w", err) | ||
} | ||
|
||
return StripeCalculator{ | ||
calculator: calculator, | ||
multiplier: alpacadecimal.NewFromInt(10).Pow(alpacadecimal.NewFromInt(int64(calculator.Def.Subunits))), | ||
}, nil | ||
} | ||
|
||
// StripeCalculator provides a currency calculator object. | ||
type StripeCalculator struct { | ||
calculator currencyx.Calculator | ||
multiplier alpacadecimal.Decimal | ||
} | ||
|
||
// RoundToAmount rounds the amount to the precision of the Stripe currency in Stripe amount. | ||
func (c StripeCalculator) RoundToAmount(amount alpacadecimal.Decimal) int64 { | ||
return amount.Mul(c.multiplier).Round(0).IntPart() | ||
} | ||
|
||
// IsInteger checks if the amount is an integer in the Stripe currency. | ||
func (c StripeCalculator) IsInteger(amount alpacadecimal.Decimal) bool { | ||
return amount.Mul(c.multiplier).IsInteger() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package appstripe | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/openmeterio/openmeter/openmeter/app" | ||
appentity "github.com/openmeterio/openmeter/openmeter/app/entity" | ||
appentitybase "github.com/openmeterio/openmeter/openmeter/app/entity/base" | ||
appstripeentity "github.com/openmeterio/openmeter/openmeter/app/stripe/entity" | ||
"github.com/openmeterio/openmeter/openmeter/customer" | ||
customerentity "github.com/openmeterio/openmeter/openmeter/customer/entity" | ||
) | ||
|
||
func NewFixture(app app.Service, customer customer.Service) *Fixture { | ||
return &Fixture{ | ||
app: app, | ||
customer: customer, | ||
} | ||
} | ||
|
||
type Fixture struct { | ||
app app.Service | ||
customer customer.Service | ||
} | ||
|
||
// setupAppWithCustomer creates a stripe app and a customer with customer data | ||
func (s *Fixture) setupAppWithCustomer(ctx context.Context, namespace string) (appentity.App, *customerentity.Customer, appstripeentity.CustomerData, error) { | ||
app, err := s.setupApp(ctx, namespace) | ||
if err != nil { | ||
return nil, nil, appstripeentity.CustomerData{}, fmt.Errorf("setup app failed: %w", err) | ||
} | ||
|
||
customer, err := s.setupCustomer(ctx, namespace) | ||
if err != nil { | ||
return nil, nil, appstripeentity.CustomerData{}, fmt.Errorf("setup customer failed: %w", err) | ||
} | ||
|
||
data, err := s.setupAppCustomerData(ctx, app, customer) | ||
if err != nil { | ||
return nil, nil, appstripeentity.CustomerData{}, fmt.Errorf("setup app customer data failed: %w", err) | ||
} | ||
|
||
return app, customer, data, nil | ||
} | ||
|
||
// Create a stripe app first | ||
func (s *Fixture) setupApp(ctx context.Context, namespace string) (appentity.App, error) { | ||
app, err := s.app.InstallMarketplaceListingWithAPIKey(ctx, appentity.InstallAppWithAPIKeyInput{ | ||
MarketplaceListingID: appentity.MarketplaceListingID{ | ||
Type: appentitybase.AppTypeStripe, | ||
}, | ||
|
||
Namespace: namespace, | ||
APIKey: TestStripeAPIKey, | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("install stripe app failed: %w", err) | ||
} | ||
|
||
return app, nil | ||
} | ||
|
||
// Create test customers | ||
func (s *Fixture) setupCustomer(ctx context.Context, namespace string) (*customerentity.Customer, error) { | ||
customer, err := s.customer.CreateCustomer(ctx, customerentity.CreateCustomerInput{ | ||
Namespace: namespace, | ||
CustomerMutate: customerentity.CustomerMutate{ | ||
Name: "Test Customer", | ||
}, | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("create customer failed: %w", err) | ||
} | ||
|
||
return customer, nil | ||
} | ||
|
||
// Add customer data to the app | ||
func (s *Fixture) setupAppCustomerData(ctx context.Context, app appentity.App, customer *customerentity.Customer) (appstripeentity.CustomerData, error) { | ||
data := appstripeentity.CustomerData{ | ||
StripeCustomerID: "cus_123", | ||
} | ||
|
||
err := app.UpsertCustomerData(ctx, appentity.UpsertAppInstanceCustomerDataInput{ | ||
CustomerID: customer.GetID(), | ||
Data: data, | ||
}) | ||
if err != nil { | ||
return data, fmt.Errorf("Upsert customer data failed: %w", err) | ||
} | ||
|
||
return data, nil | ||
} |
Oops, something went wrong.