### Configuring RevenueCat SDK with API Key - BrightScript Source: https://github.com/revenuecat/purchases-roku/blob/main/README.md This BrightScript code initializes the RevenueCat SDK with your unique API key. It is typically called within the init() method of your main SceneGraph scene to ensure the SDK is ready for use when the application starts. Replace roku_XXXXX with your actual RevenueCat public API key. ```brightscript sub init() Purchases().configure({ "apiKey": "roku_XXXXX" }) end sub ``` -------------------------------- ### Handling Async Operations with Callbacks (Anonymous & Named) - BrightScript Source: https://github.com/revenuecat/purchases-roku/blob/main/README.md This BrightScript example illustrates two methods for handling asynchronous SDK operations: using an anonymous subroutine directly as a callback, or passing the name of a separate function. Both approaches receive result and error parameters, allowing for robust error handling and processing of successful outcomes. The onSubscriberReceived function demonstrates how to access data from the event object when using a named function callback. ```brightscript sub init() Purchases().logIn(my_user_id, sub(subscriber, error) if error <> invalid print "there was en error" else print subscriber end if end sub) ' To use a function as callback, pass its name as second parameter Purchases().logIn(my_user_id, "onSubscriberReceived") end sub sub onSubscriberReceived(e as object) data = e.GetData() if data.error <> invalid print "there was en error" else print data.result end if end sub ``` -------------------------------- ### Setting Up Development Environment with Fastlane (Bash) Source: https://github.com/revenuecat/purchases-roku/blob/main/CONTRIBUTING.md This command executes the `setup_dev` lane using `fastlane` via `bundle exec`. Its primary function is to configure the development environment, including linking a pre-commit hook that runs `detekt` to ensure code quality before committing, thereby saving time during CI checks. ```bash bundle exec fastlane setup_dev ``` -------------------------------- ### Initializing RevenueCat SDK and Logging In User - BrightScript Source: https://github.com/revenuecat/purchases-roku/blob/main/README.md This snippet initializes the RevenueCat SDK with an API key and logs in a user. It then checks if a specific entitlement is active; if not, it proceeds to fetch offerings to display a paywall. ```BrightScript sub init() ' Initialize the SDK Purchases().configure({ "apiKey": "roku_XXXXX", }) ' Login the user Purchases().logIn(m.my_user_id, sub(subscriber, error) if error = invalid ' If my entitlement is not active, fetch offerings to show the paywall if subscriber.entitlements.my_entitlement.isActive = false fetchOfferings() end if end if end sub) end sub ``` -------------------------------- ### Making a Purchase with RevenueCat in BrightScript Source: https://github.com/revenuecat/purchases-roku/blob/main/README.md This BrightScript code demonstrates how to initiate a purchase using the `Purchases().purchase()` method, accepting a product ID, product, or package as a parameter. It also shows how to handle the asynchronous result, distinguishing between successful transactions and errors, including user cancellations, and provides access to the raw transaction and updated subscriber objects. ```brightscript Purchases().purchase({ code: "product_id" }, sub(result, error) ' error will be present if the transaction could not be finished if error <> invalid if result <> invalid and result.userCancelled = true print "The user cancelled the purchase" end if print "The purchase could not be completed" else ' The raw transaction generated by the purchase result.transaction ' { ' amount: "$0.00" ' code: "yearly_subscription_product" ' description: "Yearly Subscription" ' externalCode: "" ' freeTrialQuantity: 0 ' freeTrialType: "None" ' name: "Yearly Subscription" ' originalAmount: "0" ' productType: "YearlySub" ' promotionApplied: false ' purchaseId: "00000000-0000-0000-0000-000000000000" ' qty: 1 ' replacedOffers: [] ' replacedSubscriptionId: "" ' rokuCustomerId: "00000000-0000-0000-0000-000000000000" ' total: "$0.00" ' trialCost: "$0.99" ' trialQuantity: 1 ' trialType: "Years" ' } ' The subscriber object result.subscriber end end sub) ``` -------------------------------- ### Making a Purchase - BrightScript Source: https://github.com/revenuecat/purchases-roku/blob/main/README.md This function initiates a purchase for a specified product. Upon successful completion, it prints details of the transaction and the updated subscriber information to the console. ```BrightScript sub purchaseProduct(product) Purchases().purchase(product, sub(result, error) if error = invalid print "Purchase successful" print result.transaction print result.subscriber end if end sub) end sub ``` -------------------------------- ### Fetching All Offerings - RevenueCat - BrightScript Source: https://github.com/revenuecat/purchases-roku/blob/main/README.md This snippet demonstrates how to retrieve all available offerings from RevenueCat. It includes error handling and provides a detailed structure of the `offerings` object, showing how current and all offerings, along with their associated packages and store products, are organized. The `offerings` object contains `current` (the current offering) and `all` (an associative array of all offerings keyed by identifier). ```BrightScript Purchases().getOfferings(sub(offerings, error) if error <> invalid print "There was an error fetching offerings else ' The offerings object ' { ' current: { ' identifier: "my_id", ' metadata: { }, ' Metadata set in the Offering configuration ' description: "Offering description", ' annual: {}, ' The configured Annual package, if available ' monthly: {}, ' The configured Monthly package, if available ' ' A list of all available packages ' availablePackages: [ ' { ' identifier: "package_identifier", ' packageType: "custom", ' ' The raw Roku store product ' storeProduct: { ' code: "yearly_subscription_product" ' cost: "$1.99" ' description: "Yearly Subscription" ' freeTrialQuantity: 0 ' freeTrialType: "None" ' HDPosterUrl: "" ' id: "00000000-0000-0000-0000-000000000000" ' inStock: "true" ' name: "Yearly Subscription" ' offerEndDate: "" ' offerStartDate: "" ' productImagePortrait: "" ' productImageUrl: "" ' productType: "YearlySub" ' qty: 0 ' SDPosterUrl: "" ' trialCost: "$0.99" ' trialQuantity: 12 ' trialType: "Months" ' } ' } ' ], ' }, ' all: { ' ' An associative array of all the offerings, keyed by their identifier ' } ' } end if end sub) ``` -------------------------------- ### Fetching Offerings - BrightScript Source: https://github.com/revenuecat/purchases-roku/blob/main/README.md This subroutine retrieves the current product offerings from RevenueCat. The fetched offerings can then be used to construct and display a paywall UI, prompting the user to select a product for purchase. ```BrightScript sub fetchOfferings() Purchases().getOfferings(sub(offerings, error) if error = invalid ' Use offerings to build your paywall UI. ' Then call purchaseProduct with the one selected by the user purchaseProduct(offerings.current.annual) end if end sub) end sub ``` -------------------------------- ### Cloning RevenueCat Roku SDK Repository - Bash Source: https://github.com/revenuecat/purchases-roku/blob/main/README.md This command clones the RevenueCat Roku SDK repository from GitHub to your local machine. It's the first step in obtaining the SDK files for integration into your Roku application. ```bash git clone https://github.com/RevenueCat/purchases-roku ``` -------------------------------- ### Fetching Customer Information - RevenueCat - BrightScript Source: https://github.com/revenuecat/purchases-roku/blob/main/README.md This snippet demonstrates how to retrieve the current customer's information using `Purchases().getCustomerInfo()`. The callback provides the `subscriber` object containing detailed customer data upon success or an `error` if the process fails. This method is essential for checking entitlements and subscription status. ```BrightScript Purchases().getCustomerInfo(sub(subscriber, error) end sub) ``` -------------------------------- ### Importing RevenueCat SDK in SceneGraph Component - XML Source: https://github.com/revenuecat/purchases-roku/blob/main/README.md This XML snippet demonstrates how to import the RevenueCat SDK's BrightScript file into a SceneGraph component's XML definition. This makes the SDK's functions available within that component. It should be placed within the tag of your SceneGraph XML file. ```xml