Skip to content

Commit

Permalink
Merge pull request #43 from cb-amutha/bugfix/dev_release_v1.0.6
Browse files Browse the repository at this point in the history
(Fix)[Updated Retrieve subscription and readme file]
  • Loading branch information
cb-amutha authored Jul 18, 2022
2 parents 3cb4349 + 90f29e3 commit 79c3d18
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 41 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ The following requirements must be set up before installing Chargebee’s Androi
The `Chargebee-Android` SDK can be installed by adding below dependency to the `build.gradle` file:

```kotlin
implementation 'com.chargebee:chargebee-android:1.0.5'
implementation 'com.chargebee:chargebee-android:1.0.6'
```

## Example project
Expand Down Expand Up @@ -327,7 +327,7 @@ Chargebee is available under the [MIT license](https://opensource.org/licenses/M
To install Chargebee's Android SDK, add the following dependency to the build.gradle file.

```
implementation 'com.chargebee:chargebee-android:1.0.5'
implementation 'com.chargebee:chargebee-android:1.0.6'
```
Example project
---------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class BillingViewModel : ViewModel() {
Log.i(
TAG,
"subscription status: ${(it.data as SubscriptionDetailsWrapper).cb_subscription.status} ,activated_at : ${(it.data as SubscriptionDetailsWrapper).cb_subscription.activated_at}" +
" subscription id : ${(it.data as SubscriptionDetailsWrapper).cb_subscription.id}" +
" subscription id : ${(it.data as SubscriptionDetailsWrapper).cb_subscription.subscription_id}" +
" customer_id : ${(it.data as SubscriptionDetailsWrapper).cb_subscription.customer_id}" +
" current_term_start : ${(it.data as SubscriptionDetailsWrapper).cb_subscription.current_term_start} " +
" current_term_end : ${(it.data as SubscriptionDetailsWrapper).cb_subscription.current_term_end}"
Expand All @@ -66,8 +66,8 @@ class BillingViewModel : ViewModel() {
}
}
}
fun retrieveSubscriptionsByCustomerId(queryParam: Array<String>) {
Chargebee.retrieveSubscriptions(queryParam) {
fun retrieveSubscriptionsByCustomerId(queryParams: Map<String, String>) {
Chargebee.retrieveSubscriptions(queryParams) {
when(it){
is ChargebeeResult.Success -> {
subscriptionList.postValue((it.data as CBSubscription).list)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,26 @@ class SubscriptionActivity : BaseActivity() {
}
this.mBillingViewModel!!.subscriptionList.observeForever {
hideProgressDialog()
Log.i(javaClass.simpleName, "Subscriptions by using Customer Id: $it")
val subscriptionStatus = it?.get(0)?.cb_subscription?.status+"\nPlan Price : "+it?.get(0)?.cb_subscription?.plan_amount;
alertSuccess(subscriptionStatus)
Log.i(javaClass.simpleName, "Subscriptions by using queryParams: $it")
if(it?.size!! >0) {
val subscriptionStatus =
it?.get(0).cb_subscription.status + "\nPlan Price : " + it?.get(0)?.cb_subscription?.plan_amount;
alertSuccess(subscriptionStatus)
}else{
alertSuccess("Subscriptions not found in Chargebee System")
}
}

mCustomerIdButton.setOnClickListener{
showProgressDialog()
val id = mCustomerIdInput.text.toString()
val queryParam = arrayOf(id, Chargebee.channel)
mBillingViewModel?.retrieveSubscriptionsByCustomerId(queryParam)
val userInput = mCustomerIdInput.text.toString() // Sample QueryParams: "customer_id":"Abc","channel":"play_store","status":"Active"

val queryParams = userInput.split(",").associate {
val (left, right) = it.split(":")
left to right
}

mBillingViewModel?.retrieveSubscriptionsByCustomerId(queryParams)
}

mSubscriptionIdButton.setOnClickListener{
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_subscription.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="Subscriptions List By Customer Id"
android:text="Subscriptions List By QueryParams"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
Expand Down
3 changes: 1 addition & 2 deletions chargebee/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0.5"
versionName "1.0.6"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
Expand Down Expand Up @@ -43,7 +43,6 @@ dependencies {

// Google play billing library
implementation 'com.android.billingclient:billing-ktx:4.0.0'
implementation 'com.squareup.okhttp3:logging-interceptor:4.2.1'

// AssertJ
testImplementation "org.assertj:assertj-core:$assertj_version"
Expand Down
15 changes: 4 additions & 11 deletions chargebee/src/main/java/com/chargebee/android/Chargebee.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,15 @@ object Chargebee {
}
/* Get the subscriptions list from chargebee system by using Customer Id */
@Throws(InvalidRequestException::class, OperationFailedException::class)
fun retrieveSubscriptions(queryParam: Array<String>, completion: (ChargebeeResult<Any>) -> Unit) {
fun retrieveSubscriptions(queryParams: Map<String, String>, completion: (ChargebeeResult<Any>) -> Unit) {
val logger = CBLogger(name = "Subscription", action = "Fetch Subscription by using CustomerId")
if (queryParam.isNotEmpty()) {
if (TextUtils.isEmpty(queryParam[0])){
completion(ChargebeeResult.Error(
exp = CBException(
error = ErrorDetail(message = "Customer ID is empty", apiErrorCode = "400")
)
))
}else {
if (queryParams.isNotEmpty()) {

ResultHandler.safeExecuter(
{ SubscriptionResource().retrieveSubscriptions(queryParam) },
{ SubscriptionResource().retrieveSubscriptions(queryParams) },
completion,
logger
)
}
}else{
completion(ChargebeeResult.Error(
exp = CBException(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.chargebee.android.models

class SubscriptionDetail(val id: String,val customer_id: String, val status: String, val current_term_start: String, val current_term_end: String,
class SubscriptionDetail(val subscription_id: String,val customer_id: String, val status: String, val current_term_start: String, val current_term_end: String,
val activated_at: String, val plan_amount: String)

data class SubscriptionDetailsWrapper(val cb_subscription: SubscriptionDetail)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import com.chargebee.android.models.CBSubscription
import com.chargebee.android.models.KeyValidationWrapper
import com.chargebee.android.models.SubscriptionDetailsWrapper
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Header
import retrofit2.http.Path
import retrofit2.http.Query
import retrofit2.http.*

internal interface PurchaseRepository {
@GET("v2/plans/{sdkKey}")
Expand All @@ -27,12 +24,11 @@ internal interface PurchaseRepository {
@Path("subscription_id") subscriptionId: String
): Response<SubscriptionDetailsWrapper?>

@GET("v2/in_app_subscriptions/")
@GET("v2/in_app_subscriptions")
suspend fun retrieveSubscriptions(
@Header("Authorization") token: String = Chargebee.encodedApiKey,
@Header("platform") platform: String = Chargebee.platform,
@Header("version") sdkVersion: String = Chargebee.sdkVersion,
@Query("customer_id") customerId: String,
@Query("channel") channel: String
@QueryMap queryParams: Map<String, String>
): Response<CBSubscription?>
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,20 @@ package com.chargebee.android.resources
import com.google.gson.FieldNamingPolicy
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

internal open class BaseResource(baseUrl: String) {
var apiClient: Retrofit

init {
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
val client = OkHttpClient.Builder().addInterceptor(interceptor).build()

val gson: Gson = GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create()

apiClient = Retrofit.Builder()
.baseUrl(baseUrl)
//.client(client)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,28 @@ internal class SubscriptionResource : BaseResource(Chargebee.baseUrl) {
subscriptionResponse
)
}
suspend fun retrieveSubscriptions(queryParam: Array<String>): ChargebeeResult<Any> {
suspend fun retrieveSubscriptions(queryParam: Map<String, String>): ChargebeeResult<Any> {
var queryParams = HashMap<String,String>()
val result: (HashMap<String,String>) -> Unit= {
map: HashMap<String,String> -> queryParams = map
} //lambda function
queryParamSanitizer(queryParam,result)

val subscriptionResponse = apiClient.create(PurchaseRepository::class.java)
.retrieveSubscriptions(customerId = queryParam[0], channel = queryParam[1])
.retrieveSubscriptions(queryParams=queryParams)
Log.i(javaClass.simpleName, " Response :$subscriptionResponse")
return responseFromServer(
subscriptionResponse
)
}

private fun queryParamSanitizer(queryParam: Map<String,String>,result:(HashMap<String,String>) -> Unit){
val map = HashMap<String,String>()
if (queryParam.isNotEmpty()) {
for ((key, value) in queryParam) {
map[key.replace("\"", "").replace(" ","")] = value.replace("\"", "").replace(" ","")
}
}
result(map)
}
}

0 comments on commit 79c3d18

Please sign in to comment.