-
Notifications
You must be signed in to change notification settings - Fork 1
Add Practical Examples for PayPal Subscriptions Connector #5
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
Merged
ThisaruGuruge
merged 10 commits into
ballerina-platform:main
from
RNViththagan:examples
Jun 27, 2025
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4325b10
Add initial Ballerina.toml and main.bal files for create_and_list_pla…
RNViththagan 8dc3f58
Add create-and-list-plans example and documentation
RNViththagan e141665
Add initial Ballerina.toml and main.bal files for monitor_and_manage_…
RNViththagan 573cb54
Add monitor-and-manage-subscription example and documentation
RNViththagan 3ec5222
Rename files
RNViththagan 0886214
Update README.md to include example descriptions and prerequisites
RNViththagan d8f1210
Update .gitignore to ignore Examples Dependencies file
RNViththagan 12ae592
[Automated] Update the toml files
RNViththagan 5291fff
Set default value for isLiveServer and add STS service for token mana…
RNViththagan e1ad016
Add license header to example Ballerina files
RNViththagan 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
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,52 @@ | ||
| // Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com). | ||
| // | ||
| // WSO2 LLC. licenses this file to you under the Apache License, | ||
| // Version 2.0 (the "License"); you may not use this file except | ||
| // in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| import ballerina/http; | ||
|
|
||
| configurable int HTTP_SERVER_PORT = 9444; | ||
| configurable int TOKEN_VALIDITY_PERIOD = 10000; | ||
|
|
||
| http:Listener stsListener = check new (HTTP_SERVER_PORT); | ||
|
|
||
| http:Service sts = service object { | ||
| resource function post token() returns json { | ||
| return { | ||
| "access_token": "test-access-token", | ||
| "token_type": "example", | ||
| "expires_in": TOKEN_VALIDITY_PERIOD, | ||
| "example_parameter": "example_value" | ||
| }; | ||
| } | ||
|
|
||
| resource function post introspect() returns json { | ||
| return { | ||
| "active": true, | ||
| "scope": "read write", | ||
| "client_id": "test-client-id", | ||
| "username": "test-user", | ||
| "token_type": "example", | ||
| "exp": TOKEN_VALIDITY_PERIOD, | ||
| "iat": 1419350238, | ||
| "nbf": 1419350238, | ||
| "sub": "Z5O3upPC88QrAjx00dis", | ||
| "aud": "https://protected.example.net/resource", | ||
| "iss": "https://server.example.com/", | ||
| "jti": "JlbmMiOiJBMTI4Q0JDLUhTMjU2In", | ||
| "extension_field": "twenty-seven", | ||
| "scp": "admin" | ||
| }; | ||
| } | ||
| }; |
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
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,8 @@ | ||
| [package] | ||
| org = "wso2" | ||
| name = "create_and_list_plans" | ||
| version = "0.1.0" | ||
| distribution = "2201.12.7" | ||
|
|
||
| [build-options] | ||
| observabilityIncluded = true |
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,27 @@ | ||
| ## Create and List Subscription Plans | ||
|
|
||
| This use case demonstrates how the PayPal Subscriptions API can be used to create a new subscription plan and list all available plans. It showcases the ability to define billing plans for services and retrieve plan details, which is useful for applications managing subscription-based offerings, such as SaaS platforms. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| ### 1. Set up PayPal Developer account | ||
|
|
||
| Refer to the [Setup Guide](https://github.com/ballerina-platform/module-ballerinax-paypal.subscriptions#setup-guide) to obtain necessary credentials (`clientId`, `clientSecret`) and create a product to get a `productId`. | ||
|
|
||
| ### 2. Configuration | ||
|
|
||
| Create a `Config.toml` file in the example's root directory and provide your PayPal credentials and product ID as follows: | ||
|
|
||
| ```toml | ||
| clientId = "<your_client_id>" | ||
| clientSecret = "<your_client_secret>" | ||
| productId = "<your_product_id>" | ||
| ``` | ||
|
|
||
| ## Run the Example | ||
|
|
||
| Execute the following command to run the example: | ||
|
|
||
| ```bash | ||
| bal run | ||
| ``` |
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,69 @@ | ||
| // Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com). | ||
| // | ||
| // WSO2 LLC. licenses this file to you under the Apache License, | ||
| // Version 2.0 (the "License"); you may not use this file except | ||
| // in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| import ballerina/io; | ||
| import ballerinax/paypal.subscriptions as paypal; | ||
|
|
||
| configurable string clientId = ?; | ||
| configurable string clientSecret = ?; | ||
| configurable string productId = ?; | ||
|
|
||
| final paypal:Client paypal = check new paypal:Client({auth: {clientId, clientSecret}}); | ||
|
|
||
| public function main() returns error? { | ||
| // Create a subscription plan | ||
| paypal:PlanRequestPOST planPayload = { | ||
| product_id: productId, | ||
| name: "Basic Monthly Plan", | ||
| description: "Monthly subscription for premium access", | ||
| status: "ACTIVE", | ||
| billing_cycles: [ | ||
| { | ||
| frequency: { | ||
| interval_unit: "MONTH", | ||
| interval_count: 1 | ||
| }, | ||
| tenure_type: "REGULAR", | ||
| sequence: 1, | ||
| total_cycles: 12, | ||
| pricing_scheme: { | ||
| fixed_price: { | ||
| value: "10.00", | ||
| currency_code: "USD" | ||
| } | ||
| } | ||
| } | ||
| ], | ||
| payment_preferences: { | ||
| auto_bill_outstanding: true, | ||
| setup_fee: { | ||
| value: "0.00", | ||
| currency_code: "USD" | ||
| }, | ||
| setup_fee_failure_action: "CONTINUE", | ||
| payment_failure_threshold: 3 | ||
| } | ||
| }; | ||
| paypal:Plan createdPlan = check paypal->/plans.post(planPayload); | ||
| io:println("Created Plan ID: ", createdPlan.id); | ||
|
|
||
| // List all plans | ||
| paypal:PlanCollection planList = check paypal->/plans(); | ||
| io:println("Available Plans:"); | ||
| foreach var plan in planList.plans ?: [] { | ||
| io:println("- Plan ID: ", plan.id, ", Name: ", plan.name); | ||
| } | ||
| } | ||
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,8 @@ | ||
| [package] | ||
| org = "wso2" | ||
| name = "monitor_and_manage_subscription" | ||
| version = "0.1.0" | ||
| distribution = "2201.12.7" | ||
|
|
||
| [build-options] | ||
| observabilityIncluded = true |
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,55 @@ | ||
| // Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com). | ||
| // | ||
| // WSO2 LLC. licenses this file to you under the Apache License, | ||
| // Version 2.0 (the "License"); you may not use this file except | ||
| // in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| import ballerina/io; | ||
RNViththagan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import ballerinax/paypal.subscriptions as paypal; | ||
|
|
||
| configurable string clientId = ?; | ||
| configurable string clientSecret = ?; | ||
| configurable string subscriptionId = ?; | ||
|
|
||
| final paypal:Client paypal = check new ({ | ||
| auth: { | ||
| clientId, | ||
| clientSecret | ||
| } | ||
| }); | ||
|
|
||
| public function main() returns error? { | ||
| // Retrieve subscription details | ||
| paypal:SubscriptionsGetQueries queries = { | ||
| fields: "id,plan_id,status" | ||
| }; | ||
| paypal:Subscription subscription = check paypal->/subscriptions/[subscriptionId].get(queries = queries); | ||
| io:println("Subscription ID: ", subscription.id, ", Status: ", subscription.status); | ||
|
|
||
| // Suspend or reactivate based on status | ||
| if subscription.status == "ACTIVE" { | ||
| paypal:SubscriptionSuspendRequest suspendPayload = { | ||
| reason: "Temporary pause due to customer request" | ||
| }; | ||
| check paypal->/subscriptions/[subscriptionId]/suspend.post(suspendPayload); | ||
| io:println("Subscription suspended successfully"); | ||
| } else if subscription.status == "SUSPENDED" { | ||
| paypal:SubscriptionActivateRequest activatePayload = { | ||
| reason: "Reactivating subscription per customer request" | ||
| }; | ||
| check paypal->/subscriptions/[subscriptionId]/activate.post(activatePayload); | ||
| io:println("Subscription reactivated successfully"); | ||
| } else { | ||
| io:println("Subscription status is ", subscription.status, "; no action taken"); | ||
| } | ||
| } | ||
27 changes: 27 additions & 0 deletions
27
examples/monitor-and-manage-subscription/monitor-and-manage-subscription.md
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,27 @@ | ||
| ## Monitor and Manage Subscription Status | ||
|
|
||
| This use case demonstrates how the PayPal Subscriptions API can be used to monitor a subscription's status and manage it by suspending or reactivating it. It is useful for applications that need to handle customer subscription status changes, such as pausing a subscription due to payment issues or reactivating it upon resolution, in scenarios like SaaS or subscription-based services. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| ### 1. Set up PayPal Developer account | ||
|
|
||
| Refer to the [Setup Guide](https://github.com/ballerina-platform/module-ballerinax-paypal.subscriptions#setup-guide) to obtain necessary credentials (`clientId`, `clientSecret`) and create a subscription to get a `subscriptionId`. | ||
|
|
||
| ### 2. Configuration | ||
|
|
||
| Create a `Config.toml` file in the example's root directory and provide your PayPal credentials and subscription ID as follows: | ||
|
|
||
| ```toml | ||
| clientId = "<your_client_id>" | ||
| clientSecret = "<your_client_secret>" | ||
| subscriptionId = "<your_subscription_id>" | ||
| ``` | ||
|
|
||
| ## Run the Example | ||
|
|
||
| Execute the following command to run the example: | ||
|
|
||
| ```bash | ||
| bal run | ||
| ``` |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.