This Go module provides a wrapper to Stigg’s GraphQL API based on the operations that are in use by the Stigg’s Node.js SDK. It leverages the gqlgenc library to generate the types under the hood and by default utilizes the http.Client module to execute the API requests.
Import the Stigg client and initialize it with the API key:
import ( "context" "github.com/stiggio/api-client-go/v2" "os")// Ptr is a helper function that returns a pointer to whatever // value we pass to it, will be widely used in the examples belowfunc Ptr[T any](v T) *T { return &v}func main() { apiKey := os.Getenv("STIGG_SERVER_API_KEY") client := stigg.NewStiggClient(apiKey, nil, nil) // client is initialized}
When a new customer is provisioned within your application (for example: as part of your registration or onboarding process), you should also provision them in Stigg.
The customer’s billing information can also be passed to Stigg when a customer is created or updated. When Stigg is integrated with additional service, this information will be propagated to the active integrations. The billing information is not persisted on Stigg’s servers.
You can optionally pass subscriptionParams to create subscription for that customer using a single operation. Doing so, will allow Stigg admins to override the requested subscription with no-code from the Stigg Console using the product’s Customer Journey configuration.
Customer information can also be updated whenever you like assuming it already exists, for example: In order to update the customer’s email, you only need to send the new email value without the rest of the customer. Due to the fact that the customer’s billing information is not persisted on Stigg’s servers, in order to update it, the entire billing information object must be passed each time.
result, err := client.GetActiveSubscriptions(context.Background(), stigg.GetActiveSubscriptionsInput{ CustomerID: "customer-f9942a", // Optional, pass it to get subscription of specific resource ResourceID: Ptr("resource-01"),})
{"getActiveSubscriptions": [ { "id": "subscription-plan-revvenu-basic-4a9432", "status": "ACTIVE", "startDate": "2022-12-13T17:27:05.873Z", "endDate": null, "trialEndDate": null, "cancellationDate": null, "effectiveEndDate": null, "currentBillingPeriodEnd": "2023-01-13T17:27:15.000Z", "metadata": null, "billingId": "sub_1MEcG8AnAO1PFouUcqM98PA5", "billingLinkUrl": "https://dashboard.stripe.com/test/subscriptions/sub_1MEcG8AnAO1PFouUcqM98PA5", "prices": [], "totalPrice": null, "pricingType": "FREE", "plan": { "id": "plan-revvenu-basic", "displayName": "Basic", "description": null, "metadata": null, "product": { "id": "product-revvenu", "displayName": "Revvenu", "description": null }, "basePlan": null, "entitlements": [ { "usageLimit": 3, "hasUnlimitedUsage": null, "featureId": "cbcae3e8-a0ab-4b67-a093-426a2f98a72f", "resetPeriod": null, "feature": { "id": "feature-01-templates", "featureType": "NUMBER", "meterType": "Fluctuating", "featureUnits": "template", "featureUnitsPlural": "templates", "displayName": "Templates", "description": null } }, { "usageLimit": 6, "hasUnlimitedUsage": null, "featureId": "d50cf34b-8f04-450b-a2d5-07088f2ef162", "resetPeriod": "MONTH", "feature": { "id": "feature-02-campaigns", "featureType": "NUMBER", "meterType": "Incremental", "featureUnits": "campaign", "featureUnitsPlural": "campaigns", "displayName": "Campaigns", "description": null } } ], "inheritedEntitlements": [], "compatibleAddons": [], "prices": [], "pricingType": "FREE", "defaultTrialConfig": null }, "addons": [], "paymentCollection": "NOT_REQUIRED", // status of the payment collection of the subscription - can be either 'NOT_REQUIRED', 'FAILED', 'ACTION_REQUIRED' or 'PROCESSING' "latestInvoice": { "billingId": "invoice-01", // ID of the invoice of the billing provider "status": "PAID", // can be either 'OPEN', 'CANCELED' or 'PAID' "createdAt": "2023-08-22T11:27:41.000Z", "updatedAt": "2023-08-22T11:27:44.608Z", "requiresAction": false, // indicate of the payment requires action to complete the payment "paymentUrl": "https://...", // payment url that can be sent to the customer to complete the payment "paymentSecret": "secret-01", // secret to be used to initiate next action with billing provider "errorMessage": "", // optinal error message, if the status is FAILED }, }]}
When a customer subscribes to a new plan (for example: during an upgrade from a free plan to a paid plan, or downgrade from a higher tier to a lower tier), a subscription needs to be created in Stigg.
When provisioning of a paid subscription is attempted, Stigg is integrated with a billing solution and payment details have not been previously provided by the customer, Stigg will auto-magically redirect customers to a the billing solution’s checkout page. After the customer enters the required payment details in the presented checkout page, the relevant subscription will be created in both Stigg and the billing solution.
When no payment is required or when Stigg is not integrated with a billing solution, the subscription will be immediately created in Stigg.
When a customer subscribes to a new plan (free, paid, trial, etc.), provision a subscription in Stigg. The provision result can be a successfully created subscription, or a redirect link to stripe checkout in case payment is needed.
result, err := client.ProvisionSubscription(context.Background(), stigg.ProvisionSubscriptionInput{ // Mandatory, subscription customer ID CustomerID: "customer-demo-01", // Mandatory, subscription plan ID PlanID: "plan-revvenu-essentials", // Optional, required only for paid subscriptions BillingPeriod: Ptr(stigg.BillingPeriodMonthly), // Optional, required for multiple subscription for same product ResourceID: Ptr("resource-01"), // Optional, required for subscriptions with per unit pricing BillableFeatures: []*stigg.BillableFeatureInput{ { FeatureID: "feature-01-templates", Quantity: 20, }, }, // Optional, list of subscription addons and quantities Addons: []*stigg.SubscriptionAddonInput{ { AddonID: "addon-revvenu-extra-campaigns", Quantity: Ptr(int64(3)), }, } // Optional, required for price localization, must be in the ISO-3166-1 format BillingCountryCode: Ptr("DK"), // Optional, parameters used for the hosted checkout CheckoutOptions: &stigg.CheckoutOptions{ SuccessURL: "https://your-success-url.com", CancelURL: "https://your-cancel-url.com", }})
A customer can have both a non-trial (free or paid) subscription and trial subscription to different plans of the same product in parallel - this logic allows customers to trial a higher tier plan, while still paying for an existing plan.
When the customer has a trial subscription for plan X of product A and a new subscription is created for the same plan, the new subscription will be created as as non-trial (paid) subscription - this logic follows an upgrade flow of a trial subscription to a paid subscription of a specific plan.
Except in the above mentioned cases, when a customer has an active subscription for product X, and another subscription for the same product is created with start date S, the existing subscription will automatically be cancelled and the new subscription will start on start date S.
It’s also possible to explicitly skip the trial period of the selected plan by providing the skipTrial: true parameter to the provisionSubscription() method.
{"updateSubscription": { "id": "subscription-plan-revvenu-essentials-dc847c", "status": "ACTIVE", "metadata": null, "billingId": "sub_1MEcdyAnAO1PFouUtuDIYy2f", "billingLinkUrl": "https://dashboard.stripe.com/test/subscriptions/sub_1MEcdyAnAO1PFouUtuDIYy2f", "effectiveEndDate": null, "currentBillingPeriodEnd": "2023-01-13T17:51:53.000Z", "pricingType": "PAID", "prices": [ { "usageLimit": null, "price": { "billingModel": "FLAT_FEE", "billingPeriod": "MONTHLY", "price": { "amount": 20, "currency": "USD" }, "feature": null } } ], "totalPrice": { "subTotal": { "amount": 20, "currency": "USD" }, "total": { "amount": 20, "currency": "USD" } }, "plan": { "id": "plan-revvenu-essentials" }, "addons": [], "customer": { "id": "customer-demo-01" }, "paymentCollection": "NOT_REQUIRED", // status of the payment collection of the subscription - can be either 'NOT_REQUIRED', 'FAILED', 'ACTION_REQUIRED' or 'PROCESSING' "latestInvoice": { "billingId": "invoice-01", // ID of the invoice of the billing provider "status": "PAID", // can be either 'OPEN', 'CANCELED' or 'PAID' "createdAt": "2023-08-22T11:27:41.000Z", "updatedAt": "2023-08-22T11:27:44.608Z", "requiresAction": false, // indicate of the payment requires action to complete the payment "paymentUrl": "https://...", // payment url that can be sent to the customer to complete the payment "paymentSecret": "secret-01", // secret to be used to initiate next action with billing provider "errorMessage": "", // optinal error message, if the status is FAILED },}}
Used to check to what features the customer has access to, the usage limit , and the current usage of each entitlement.
result, err := client.GetEntitlements(context.Background(), stigg.FetchEntitlementsQuery{ CustomerID: "customer-demo-01", // Optional, pass it to get entitlements of specific resource ResourceID: Ptr("resource-01")})
The Stigg SDK allows you to report usage measurements for metered features. The reported usage will be used to track, limit and bill the customer’s usage of metered features.
Stigg supports metering of usage from the following data sources:
Calculated usage - usage that has been aggregated and calculated by your application. This type is useful for features, such as: seats.
Raw events - raw events from your application, which Stigg filters and aggregates aggregate to calculate customer usage. This type is useful for features, such as: monthly active users (MAUs).
More details about Stigg’s metering and aggregation capabilities can be found here:
Reporting of measurements to Stigg should be done only after the relevant resources have been provisioned in your application.
To ensure that the provisioned resources are aligned with the measurements that are reported to Stigg, ensure that customers are not allowed to allocate new resources until an acknowledgement about the processed measurement is received from the Stigg backend.
Validating that a measurement was successfully reported to Stigg is also possible in the customer details section of the relevant customer in the Stigg app.
You can also estimate the cost of updating an existing subscription using the updateSubscription method, which also returns a breakdown of the proration amounts: