Add proxy support for WSO2 default stripe extension implementation - Monetizing an API

Lakshan Thilakarathne
3 min readMar 29, 2022
GIF generated from https://www.youtube.com/watch?v=zPWD24Zy3lQ

API Monetization

With the rise of digital transformation, API based business models are getting popular across the globe. In this context, it is important to generate revenue from APIs for the betterment of your business. This is where API monetization comes into play. [1]

API Monetization with WSO2 APIM

WSO2 API Manager (WSO2 API-M) allows API Publishers to manage, govern, and monetize their APIs based on their business monetization goals. API Publishers can use the monetization capability in WSO2 API Manager to define various business plans for the same service; therefore, API subscribers have the freedom of selecting a preferred business plan as their subscription. [2]

WSO2 API Manager provides an extendable interface that allows API Management solution developers to provide custom implementations with any third-party billing engine for the purpose of monetizing APIs based on paid business plans. [2]

Please refer to the official documentation for detailed information on the implementation.

Here, we need to implement the respective monetization interface and add the JAR into the <API-M_HOME>/repository/components/lib directory in order to communicate with the billing engine(Ex: Stripe) when it comes to configuring the APIM.

There, we need to build the wso2-am-stripe-plugin repository (This is a sample implementation) and achieve the above if we are configuring the billing engine as Stripe.

Let’s assume we have a security requirement to block the internet access for the VM/Node which is hosted the APIM server.

But it is obvious that we need to call stripe APIs from the APIM server in order to function properly.

So, what would be the solution??????

Yes, the normal approach will be setting up a proxy server in between the server and the internet and whitelisting the Stripe endpoints.

WSO2 API Manager supports using proxy profiles.
But it is not addressed here in the sample stripe extension and we are ending up with the UnknownHostException error when the server is trying to invoke stripe APIs.

TID: [5] [api/am/publisher] [2022-03-28 22:45:14,990] ERROR {org.wso2.carbon.apimgt.rest.api.publisher.v1.impl.ApisApiServiceImpl} - Error while changing monetization status for API ID : 51a95f02-45ae-4b61-9689-5ebe510775d8 org.wso2.carbon.apimgt.api.MonetizationException: Unable to create product in billing engine for : TestLakshan....
Caused by: com.stripe.exception.ApiConnectionException: IOException during API request to Stripe (https://api.stripe.com): api.stripe.com Please check your internet connection and try again. If this problem persists,you should check Stripe's service status at https://twitter.com/stripestatus, or let us know at support@stripe.com.
....
Caused by: java.net.UnknownHostException: api.stripe.com

OK…
Let’s get into the work and implement what is missing….

Proxy support implementation

stripe-java is the underline SDK that we use for invoking stripe functions. This SDK supports proxy usage as per the implementation [3].

Therefore, we can customize the wso2 stripe extension to achieve the requirement.
Here, I have extended the default StripeMonetizationImpl class and initialize the proxy configurations prior to calling all the stripe related methods.

....public class StripeMonetizationImplExt extends StripeMonetizationImpl{

private Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", 3131));

public void settingProxyConfigs(){
//Set the proxy configs
Stripe.setConnectionProxy(proxy);
}

@Override
public boolean createBillingPlan(SubscriptionPolicy subscriptionPolicy) throws MonetizationException {
settingProxyConfigs();
return super.createBillingPlan(subscriptionPolicy);
}

@Override
public boolean deleteBillingPlan(SubscriptionPolicy subscriptionPolicy) throws MonetizationException {
settingProxyConfigs();
return super.deleteBillingPlan(subscriptionPolicy);
}
....

Please refer to the sample implementation on the below-forked repo,

Then build the project and place the JAR in <API-M_HOME>/repository/components/lib directory and engage the extension by adding the below configuration to the <API-M_HOME>/repository/conf/deployment.toml file as follows.

[apim.monetization] monetization_impl = "org.wso2.apim.monetization.impl.StripeMonetizationImplExt"

That’s it 👏 ✅
Then we just need to follow the other steps mentioned in [2].

Hope that you will find this content useful.

CHEERS!
Happy Integration!

[1] https://wso2.com/api-monetization/

[2] https://apim.docs.wso2.com/en/3.2.0/learn/api-monetization/monetizing-an-api/#c-configure-wso2-api-manager

[3] https://github.com/stripe/stripe-java/blob/acfa8becef3e73bfe3e9d8880bea3f3f30dadeac/src/main/java/com/stripe/Stripe.java#L81

--

--