Android SDK
Pelcro Android SDK helps you to have easier access to the Pelcro platform.
The SDK is written in Kotlin, but it is compatible with Java projects, of course.
Setup
Add with JitPack
Step 1
Add jitpack.io to your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Step 2
Add Internet permission to your AndroidManifest.xml
:
<manifest>
...
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
Step 3
Add Pelcro SDK dependency:
Our latest Android SDK package version is 1.1.7
dependencies {
implementation 'org.bitbucket.michael-pelcro:android-sdk:1.1.1'
}
Usage
Initialize
Initialize Pelcro SDK with your parameters:
In Java:
Pelcro.INSTANCE.setSiteID("Your site ID");
Pelcro.INSTANCE.setAccountID("Your account ID");
Pelcro.INSTANCE.setStripeSandbox(false); // false - for Production, true - for Development
Pelcro.INSTANCE.setPelcroStaging(false); // false - for Production, true - for Development
In Koltin:
Pelcro.siteID = "Your site ID"
Pelcro.accountID = "Your account ID"
Pelcro.isStripeSandbox = false // false - for Production, true - for Development
Pelcro.isPelcroStaging = false // false - for Production, true - for Development
Create Site manager
In Java:
PelcroSiteManager siteManager = new PelcroSiteManager(this);
In Koltin:
val siteManager = PelcroSiteManager(this)
Get site info
In Java:
siteManager.getSite(new ResultHandler() {
@Override
public void onResult(PelcroResult result) {
Log.i("PelcroResult: ", result.toString());
}
});
In Koltin:
siteManager.getSite(object : ResultHandler {
override fun onResult(result: PelcroResult) {
Log.i("PelcroResult: ", result.toString())
}
})
Note: once you've called siteManager.getSite(...)
, you can get result data using method siteManager.read()
. This data will be kept in the memory during your Application's session.
Create User manager
In Java:
PelcroUserManager userManager = new PelcroUserManager(this);
In Koltin:
val userManager = PelcroUserManager(this)
Register
In Java:
userManager.registerWith("Your email", "Your password", new ResultHandler() {
@Override
public void onResult(PelcroResult result) {
Log.i("PelcroResult: ", result.toString());
}
});
In Koltin:
userManager.registerWith("Your email", "Your password", object: ResultHandler {
override fun onResult(result: PelcroResult) {
Log.i("PelcroResult: ", result.toString())
}
})
Login
In Java:
userManager.loginWith("Your email", "Your password", new ResultHandler() {
@Override
public void onResult(PelcroResult result) {
Log.i("PelcroResult: ", result.toString());
}
});
In Koltin:
userManager.loginWith("Your email", "Your password", object: ResultHandler {
override fun onResult(result: PelcroResult) {
Log.i("PelcroResult: ", result.toString())
}
})
Refresh
In Java:
userManager.refreshWith(new ResultHandler() {
@Override
public void onResult(PelcroResult result) {
Log.i("PelcroResult: ", result.toString());
}
});
In Koltin:
userManager.refreshWith(object : ResultHandler {
override fun onResult(result: PelcroResult) {
Log.i("PelcroResult: ", result.toString())
}
})
Logout
In Java:
userManager.logout();
// Then you can check auth token, it should be empty
String authToken = userManager.getAuthToken();
In Koltin:
userManager.logout()
// Then you can check auth token, it should be empty
val authToken = userManager.authToken
Note: once you've called one of methods of userManager
(except for logout()
method), you can get result data using method userManager.read()
. This data will be kept in the memory during your Application's session.
Create Subscription manager
In Java:
PelcroSubscriptionManager subscriptionManager = new PelcroSubscriptionManager(this);
In Koltin:
val subscriptionManager = PelcroSubscriptionManager(this)
Create a subscription
Step 1
Implement PaymentTokenCallback
in your Activity and override onSuccess(stripeToken: String)
, onError(message: String?)
and onCancelled()
methods. These methods allow to get callbacks from PaymentMethodDialogFragment
to your Activity. If you enter valid card information on PaymentMethodDialogFragment
you should get Stripe Token to the method onSuccess(stripeToken: String)
, in case you are cancelled or something went wrong you should get callbacks to onCancelled()
or onError(message: String?)
methods.
Step 2
Create PaymentMethodDialogFragment
and show this dialog:
In Java:
PaymentMethodDialogFragment.newInstance().show(getSupportFragmentManager(), "tag");
In Koltin:
PaymentMethodDialogFragment.newInstance().show(supportFragmentManager, "tag")
Step 3
Create a subscription
In Java:
subscriptionManager.createWithStripe(-1L /*Plan ID*/, "Coupon code", "Stripe token", new ResultHandler() {
@Override
public void onResult(PelcroResult result) {
Log.i("PelcroResult: ", result.toString());
}
});
In Koltin:
subscriptionManager.createWithStripe(-1L /*Plan ID*/, "Coupon code", "Stripe token", object : ResultHandler {
override fun onResult(result: PelcroResult) {
Log.i("PelcroResult: ", result.toString())
}
})
Cancel a subscription
In Java:
subscriptionManager.cancelWith(-1L /*Subscription ID*/, new ResultHandler() {
@Override
public void onResult(PelcroResult result) {
Log.i("PelcroResult: ", result.toString());
}
});
In Koltin:
subscriptionManager.cancelWith(-1L /*Subscription ID*/, object : ResultHandler {
override fun onResult(result: PelcroResult) {
Log.i("PelcroResult: ", result.toString())
}
})
Reactivate a subscription
In Java:
subscriptionManager.reactivateWith(-1L /*Subscription ID*/, new ResultHandler() {
@Override
public void onResult(PelcroResult result) {
Log.i("PelcroResult: ", result.toString());
}
});
In Koltin:
subscriptionManager.reactivateWith(-1L /*Subscription ID*/, object : ResultHandler {
override fun onResult(result: PelcroResult) {
Log.i("PelcroResult: ", result.toString())
}
})
Check if user is subscribed to site
In Java:
Boolean isSubscribedToSite = subscriptionManager.isSubscribedToSite();
In Koltin:
val isSubscribedToSite = subscriptionManager.isSubscribedToSite()
Note: Before using isSubscribedToSite()
method you need to call one of userManager
methods: registerWith(...), loginWith(...) or refreshWith(...)
. In other words userManager.read()
shouldn't return null. If userManager.read()
returns null or Pelcro.siteID
didn't set then method isSubscribedToSite()
also will return null, otherwise it will return expected result - true or false.
Testing
We created custom shadows in order to test objects from the Android ecosystem. Therefore, if you want to test something related to these objects, please check these shadows. So far we added shadows for:
android.os.AsyncTask
java.io.InputStream
javax.net.ssl.HttpsURLConnection
org.json.JSONObject
Note: We recommend using a real device for testing during the development stage. That's because data, which is cached in the Pelcro SDK, might not be read from a virtual device's storage.
License
The Pelcro Android SDK is open source and available under the MIT license. See the LICENSE file for more info.
Updated over 1 year ago