-
Notifications
You must be signed in to change notification settings - Fork 199
fix: wire CreateDefaultBillingProfile through on v1 marketplace app install #4783
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vsengar-79
wants to merge
5
commits into
openmeterio:main
Choose a base branch
from
vsengar-79:sandbox-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dbcbf51
Implement default billing profile provisioning logic
vsengar-79 b62d756
Refactor billing profile provisioning logic
vsengar-79 2bc969b
Add CreateDefaultBillingProfile to install requests
vsengar-79 68a1f8f
Delete app/billingprofile directory
vsengar-79 d115497
Add default billing profile provisioning logic
vsengar-79 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,98 @@ | ||
| // Package billingprofile provisions the default billing profile created when an app | ||
| // is installed with CreateDefaultBillingProfile set. It is shared by every HTTP driver | ||
| // that installs apps (currently the v1 marketplace endpoints and the v3 apps endpoint) | ||
| // so the provisioning rules stay identical across API versions. | ||
| package billingprofile | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/samber/lo" | ||
|
|
||
| "github.com/openmeterio/openmeter/openmeter/app" | ||
| appstripe "github.com/openmeterio/openmeter/openmeter/app/stripe" | ||
| "github.com/openmeterio/openmeter/openmeter/billing" | ||
| ) | ||
|
|
||
| // CreateDefault creates a default billing profile for the installed app based on its type. | ||
| // Assign it to app.InstallAppV3Input.CreateDefaultBillingProfileFn (bound to concrete | ||
| // billingService/stripeAppService instances) to enable CreateDefaultBillingProfile. | ||
| func CreateDefault(ctx context.Context, billingService billing.Service, stripeAppService appstripe.Service, installedApp app.App) ([]app.CapabilityType, error) { | ||
| switch installedApp.GetType() { | ||
| case app.AppTypeStripe: | ||
| return makeStripeDefaultBillingApp(ctx, billingService, stripeAppService, installedApp) | ||
| case app.AppTypeSandbox: | ||
| namespace := installedApp.GetID().Namespace | ||
| if err := billingService.ProvisionDefaultBillingProfile(ctx, namespace); err != nil { | ||
| return nil, fmt.Errorf("provision default billing profile: %w", err) | ||
| } | ||
| return []app.CapabilityType{ | ||
| app.CapabilityTypeCalculateTax, | ||
| app.CapabilityTypeInvoiceCustomers, | ||
| app.CapabilityTypeCollectPayments, | ||
| }, nil | ||
| case app.AppTypeCustomInvoicing: | ||
| // TODO: Implement custom invoicing billing profile creation | ||
| return nil, nil | ||
| default: | ||
| return nil, fmt.Errorf("unknown app type: %s", installedApp.GetType()) | ||
| } | ||
| } | ||
|
|
||
| // Make Stripe app the default billing app if current one is Sandbox app | ||
| func makeStripeDefaultBillingApp(ctx context.Context, billingService billing.Service, stripeAppService appstripe.Service, stripeApp app.App) ([]app.CapabilityType, error) { | ||
| defaultForCapabilityTypes := []app.CapabilityType{} | ||
|
|
||
| appID := stripeApp.GetID() | ||
|
|
||
| // Check if it's a Stripe app | ||
| if stripeApp.GetType() != app.AppTypeStripe { | ||
| return defaultForCapabilityTypes, fmt.Errorf("app is not a stripe app: %s", appID.ID) | ||
| } | ||
|
|
||
| // Check if the default billing profile is a sandbox app type | ||
| defaultBillingProfile, err := billingService.GetDefaultProfile(ctx, billing.GetDefaultProfileInput{ | ||
| Namespace: appID.Namespace, | ||
| }) | ||
| if err != nil { | ||
| return defaultForCapabilityTypes, fmt.Errorf("failed to get default billing profile: %w", err) | ||
| } | ||
|
|
||
| // Set default billing profile if the current default is the sandbox | ||
| setDefaultBillingProfile := defaultBillingProfile != nil && defaultBillingProfile.Apps != nil && defaultBillingProfile.Apps.Invoicing.GetType() == app.AppTypeSandbox | ||
|
|
||
| // Get supplier contract from stripe app | ||
| supplierContract, err := stripeAppService.GetSupplierContact(ctx, appstripe.GetSupplierContactInput{ | ||
| AppID: appID, | ||
| }) | ||
| if err != nil { | ||
| return defaultForCapabilityTypes, fmt.Errorf("failed to get supplier contract for stripe app %s: %w", appID.ID, err) | ||
| } | ||
|
|
||
| // Create new default billing profile | ||
| _, err = billingService.CreateProfile(ctx, billing.CreateProfileInput{ | ||
| Namespace: appID.Namespace, | ||
| Name: "Stripe Billing Profile", | ||
| Description: lo.ToPtr("Stripe Billing Profile, created automatically"), | ||
| Default: setDefaultBillingProfile, | ||
| Supplier: supplierContract, | ||
| WorkflowConfig: billing.DefaultWorkflowConfig, | ||
| Apps: billing.ProfileAppReferences{ | ||
| Tax: appID, | ||
| Invoicing: appID, | ||
| Payment: appID, | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| return defaultForCapabilityTypes, fmt.Errorf("failed to create billing profile for stripe app %s: %w", appID.ID, err) | ||
| } | ||
|
|
||
| defaultForCapabilityTypes = []app.CapabilityType{ | ||
| app.CapabilityTypeCalculateTax, | ||
| app.CapabilityTypeInvoiceCustomers, | ||
| app.CapabilityTypeCollectPayments, | ||
| } | ||
|
|
||
| return defaultForCapabilityTypes, nil | ||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a Custom Invoicing app is installed with
createBillingProfileenabled or omitted, this branch returns success without creating a billing profile, causing the installation to complete while the documented flag remains ineffective and the response reports no default capabilities.Knowledge Base Used: App Framework (Marketplace Integrations)
Prompt To Fix With AI