Skip to content

Commit

Permalink
Merge pull request #10 from chamindias/monetization-dev-master-api-pr…
Browse files Browse the repository at this point in the history
…oduct

Monetization for API product subscription add and deletion.
  • Loading branch information
uvindra authored Oct 21, 2019
2 parents 61a7a65 + 250e90e commit 8569554
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 37 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
</repositories>

<properties>
<carbon.apimgt.version>6.5.200-SNAPSHOT</carbon.apimgt.version>
<carbon.apimgt.version>6.5.317-SNAPSHOT</carbon.apimgt.version>
<commons.lang.version>2.6</commons.lang.version>
<stripe.version>9.8.0</stripe.version>
</properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import org.wso2.carbon.apimgt.api.MonetizationException;
import org.wso2.carbon.apimgt.api.model.API;
import org.wso2.carbon.apimgt.api.model.APIIdentifier;
import org.wso2.carbon.apimgt.api.model.APIProduct;
import org.wso2.carbon.apimgt.api.model.APIProductIdentifier;
import org.wso2.carbon.apimgt.api.model.Monetization;
import org.wso2.carbon.apimgt.api.model.MonetizationUsagePublishInfo;
import org.wso2.carbon.apimgt.api.model.SubscribedAPI;
Expand Down Expand Up @@ -400,7 +402,7 @@ public boolean enableMonetization(String tenantDomain, API api, Map<String, Stri
platformAccountKey = getStripePlatformAccountKey(tenantDomain);
} catch (StripeMonetizationException e) {
String errorMessage = "Failed to get Stripe platform account key for tenant : " +
tenantDomain + " when enabling monetization for API : " + api.getId().getApiName();
tenantDomain + " when enabling monetization for : " + api.getId().getApiName();
//throw MonetizationException as it will be logged and handled by the caller
throw new MonetizationException(errorMessage, e);
}
Expand All @@ -411,7 +413,7 @@ public boolean enableMonetization(String tenantDomain, API api, Map<String, Stri
connectedAccountKey = monetizationProperties.get
(StripeMonetizationConstants.BILLING_ENGINE_CONNECTED_ACCOUNT_KEY);
if (StringUtils.isBlank(connectedAccountKey)) {
String errorMessage = "Connected account stripe key was not found for API : " + api.getId().getApiName();
String errorMessage = "Connected account stripe key was not found for : " + api.getId().getApiName();
//throw MonetizationException as it will be logged and handled by the caller
throw new MonetizationException(errorMessage);
}
Expand Down Expand Up @@ -470,7 +472,7 @@ public boolean enableMonetization(String tenantDomain, API api, Map<String, Stri
return false;
}
} catch (APIManagementException e) {
String errorMessage = "Failed to get API ID from database for : " + apiName;
String errorMessage = "Failed to get ID from database for : " + apiName;
//throw MonetizationException as it will be logged and handled by the caller
throw new MonetizationException(errorMessage, e);
} catch (StripeMonetizationException e) {
Expand Down Expand Up @@ -498,7 +500,7 @@ public boolean disableMonetization(String tenantDomain, API api, Map<String, Str
platformAccountKey = getStripePlatformAccountKey(tenantDomain);
} catch (StripeMonetizationException e) {
String errorMessage = "Failed to get Stripe platform account key for tenant : " +
tenantDomain + " when disabling monetization for API : " + api.getId().getApiName();
tenantDomain + " when disabling monetization for : " + api.getId().getApiName();
//throw MonetizationException as it will be logged and handled by the caller
throw new MonetizationException(errorMessage, e);
}
Expand All @@ -509,7 +511,7 @@ public boolean disableMonetization(String tenantDomain, API api, Map<String, Str
connectedAccountKey = monetizationProperties.get
(StripeMonetizationConstants.BILLING_ENGINE_CONNECTED_ACCOUNT_KEY);
if (StringUtils.isBlank(connectedAccountKey)) {
String errorMessage = "Billing engine connected account key was not found for API : " +
String errorMessage = "Billing engine connected account key was not found for : " +
api.getId().getApiName();
//throw MonetizationException as it will be logged and handled by the caller
throw new MonetizationException(errorMessage);
Expand Down Expand Up @@ -541,10 +543,10 @@ public boolean disableMonetization(String tenantDomain, API api, Map<String, Str
//after deleting all the associated plans, then delete the product
Product product = Product.retrieve(billingProductIdForApi, requestOptions);
product.delete(requestOptions);
log.debug("Successfully deleted billing product : " + billingProductIdForApi + " of API : " + apiName);
log.debug("Successfully deleted billing product : " + billingProductIdForApi + " of : " + apiName);
//after deleting plans and the product, clean the database records
stripeMonetizationDAO.deleteMonetizationData(apiId);
log.debug("Successfully deleted monetization database records for API : " + apiName);
log.debug("Successfully deleted monetization database records for : " + apiName);
} catch (StripeException e) {
String errorMessage = "Failed to delete products and plans in the billing engine.";
//throw MonetizationException as it will be logged and handled by the caller
Expand All @@ -554,7 +556,7 @@ public boolean disableMonetization(String tenantDomain, API api, Map<String, Str
//throw MonetizationException as it will be logged and handled by the caller
throw new MonetizationException(errorMessage, e);
} catch (APIManagementException e) {
String errorMessage = "Failed to get API ID from database for : " + api.getId().getApiName() +
String errorMessage = "Failed to get ID from database for : " + api.getId().getApiName() +
" when disabling monetization.";
//throw MonetizationException as it will be logged and handled by the caller
throw new MonetizationException(errorMessage, e);
Expand Down Expand Up @@ -587,7 +589,7 @@ public Map<String, String> getMonetizedPoliciesToPlanMapping(API api) throws Mon
//throw MonetizationException as it will be logged and handled by the caller
throw new MonetizationException(errorMessage, e);
} catch (APIManagementException e) {
String errorMessage = "Failed to get API ID from database for : " + api.getId().getApiName() +
String errorMessage = "Failed to get ID from database for : " + api.getId().getApiName() +
" when getting tier to billing engine plan mapping.";
//throw MonetizationException as it will be logged and handled by the caller
throw new MonetizationException(errorMessage, e);
Expand Down Expand Up @@ -628,11 +630,11 @@ public boolean publishMonetizationUsageRecords(MonetizationUsagePublishInfo last
jsonObj = apiUsageStatisticsRestClient.getUsageCountForMonetization(lastPublishInfo.getLastPublishTime(),
currentTimestamp);
} catch (APIMgtUsageQueryServiceClientException e) {
String errorMessage = "Failed to get the API Usage count for Monetization";
String errorMessage = "Failed to get the usage count for monetization.";
//throw MonetizationException as it will be logged and handled by the caller
throw new MonetizationException(errorMessage, e);
}
log.info("Usage record publisher is running");
log.info("Usage record publisher is running.");
SubscriptionItem subscriptionItem = null;
try {
if (jsonObj != null) {
Expand Down Expand Up @@ -660,7 +662,7 @@ public boolean publishMonetizationUsageRecords(MonetizationUsagePublishInfo last
Stripe.apiKey = getStripePlatformAccountKey(tenantDomain);
} catch (StripeMonetizationException e) {
String errorMessage = "Failed to get Stripe platform account key for tenant : " +
tenantDomain + " when disabling monetization for API : " + apiName;
tenantDomain + " when disabling monetization for : " + apiName;
//throw MonetizationException as it will be logged and handled by the caller
throw new MonetizationException(errorMessage, e);
} finally {
Expand All @@ -685,7 +687,7 @@ public boolean publishMonetizationUsageRecords(MonetizationUsagePublishInfo last
connectedAccountKey = monetizationProperties.get
(StripeMonetizationConstants.BILLING_ENGINE_CONNECTED_ACCOUNT_KEY);
if (StringUtils.isBlank(connectedAccountKey)) {
String errorMessage = "Connected account stripe key was not found for API : "
String errorMessage = "Connected account stripe key was not found for : "
+ api.getId().getApiName();
//throw MonetizationException as it will be logged and handled by the caller
throw new MonetizationException(errorMessage);
Expand All @@ -697,7 +699,7 @@ public boolean publishMonetizationUsageRecords(MonetizationUsagePublishInfo last
}
} catch (APIManagementException e) {
String errorMessage = "Failed to get the Stripe key of the connected account from "
+ "the API : " + apiName;
+ "the : " + apiName;
//throw MonetizationException as it will be logged and handled by the caller
throw new MonetizationException(errorMessage, e);
} finally {
Expand Down Expand Up @@ -791,18 +793,42 @@ public Map<String, String> getCurrentUsageForSubscription(String subscriptionUUI
try {
SubscribedAPI subscribedAPI = ApiMgtDAO.getInstance().getSubscriptionByUUID(subscriptionUUID);
APIIdentifier apiIdentifier = subscribedAPI.getApiId();
API api = apiProvider.getAPI(apiIdentifier);
apiName = apiIdentifier.getApiName();
if (api.getMonetizationProperties() == null) {
String errorMessage = "Monetization properties are empty for API : " + apiName;
//throw MonetizationException as it will be logged and handled by the caller
throw new MonetizationException(errorMessage);
}
HashMap monetizationDataMap = new Gson().fromJson(api.getMonetizationProperties().toString(), HashMap.class);
if (MapUtils.isEmpty(monetizationDataMap)) {
String errorMessage = "Monetization data map is empty for API : " + apiName;
//throw MonetizationException as it will be logged and handled by the caller
throw new MonetizationException(errorMessage);
APIProductIdentifier apiProductIdentifier;
API api;
APIProduct apiProduct;
HashMap monetizationDataMap;
int apiId;
if (apiIdentifier != null) {
api = apiProvider.getAPI(apiIdentifier);
apiName = apiIdentifier.getApiName();
if (api.getMonetizationProperties() == null) {
String errorMessage = "Monetization properties are empty for : " + apiName;
//throw MonetizationException as it will be logged and handled by the caller
throw new MonetizationException(errorMessage);
}
monetizationDataMap = new Gson().fromJson(api.getMonetizationProperties().toString(), HashMap.class);
if (MapUtils.isEmpty(monetizationDataMap)) {
String errorMessage = "Monetization data map is empty for : " + apiName;
//throw MonetizationException as it will be logged and handled by the caller
throw new MonetizationException(errorMessage);
}
apiId = ApiMgtDAO.getInstance().getAPIID(apiIdentifier, null);
} else {
apiProductIdentifier = subscribedAPI.getProductId();
apiProduct = apiProvider.getAPIProduct(apiProductIdentifier);
apiName = apiProductIdentifier.getName();
if (apiProduct.getMonetizationProperties() == null) {
String errorMessage = "Monetization properties are empty for : " + apiName;
//throw MonetizationException as it will be logged and handled by the caller
throw new MonetizationException(errorMessage);
}
monetizationDataMap = new Gson().fromJson(apiProduct.getMonetizationProperties().toString(), HashMap.class);
if (MapUtils.isEmpty(monetizationDataMap)) {
String errorMessage = "Monetization data map is empty for : " + apiName;
//throw MonetizationException as it will be logged and handled by the caller
throw new MonetizationException(errorMessage);
}
apiId = ApiMgtDAO.getInstance().getAPIProductId(apiProductIdentifier);
}
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
//get billing engine platform account key
Expand All @@ -811,19 +837,18 @@ public Map<String, String> getCurrentUsageForSubscription(String subscriptionUUI
String connectedAccountKey = monetizationDataMap.get
(StripeMonetizationConstants.BILLING_ENGINE_CONNECTED_ACCOUNT_KEY).toString();
if (StringUtils.isBlank(connectedAccountKey)) {
String errorMessage = "Connected account stripe key was not found for API : " + apiName;
String errorMessage = "Connected account stripe key was not found for : " + apiName;
//throw MonetizationException as it will be logged and handled by the caller
throw new MonetizationException(errorMessage);
}
Stripe.apiKey = platformAccountKey;
//create request options to link with the connected account
RequestOptions requestOptions = RequestOptions.builder().setStripeAccount(connectedAccountKey).build();
int apiId = ApiMgtDAO.getInstance().getAPIID(apiIdentifier, null);
int applicationId = subscribedAPI.getApplication().getId();
String billingPlanSubscriptionId = stripeMonetizationDAO.getBillingEngineSubscriptionId(apiId, applicationId);
Subscription billingEngineSubscription = Subscription.retrieve(billingPlanSubscriptionId, requestOptions);
if (billingEngineSubscription == null) {
String errorMessage = "No billing engine subscription was found for API : " + apiName;
String errorMessage = "No billing engine subscription was found for : " + apiName;
//throw MonetizationException as it will be logged and handled by the caller
throw new MonetizationException(errorMessage);
}
Expand Down Expand Up @@ -946,11 +971,11 @@ public Map<String, String> getTotalRevenue(API api, APIProvider apiProvider) thr
billingEngineUsageData.get("amount_due"));
}
} catch (APIManagementException e) {
String errorMessage = "Failed to get subscriptions of API : " + apiIdentifier.getApiName();
String errorMessage = "Failed to get subscriptions of : " + apiIdentifier.getApiName();
//throw MonetizationException as it will be logged and handled by the caller
throw new MonetizationException(errorMessage, e);
} catch (StripeMonetizationException e) {
String errorMessage = "Failed to get subscription UUID of API : " + apiIdentifier.getApiName();
String errorMessage = "Failed to get subscription UUID of : " + apiIdentifier.getApiName();
//throw MonetizationException as it will be logged and handled by the caller
throw new MonetizationException(errorMessage, e);
}
Expand Down
Loading

0 comments on commit 8569554

Please sign in to comment.