YouCanPay Android SDK makes it quick and easy to build an excellent payment experience in your Android app with YouCan Pay API.
This integration requires endpoints on your server in order to communicate with YouCanPay API. Use our official libraries for access to the YouCanPay API from your server: the following steps in our Documentation will guide you through.
Step 1: Add the JitPack repository to your root build.gradle :
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Step 2: Then add the dependency.
dependencies {
implementation 'com.github.youcan-shop:youcan-pay-android-sdk:0.5.0'
}
The first step is to initialize YouCanPay SDK by creating an instance using the following parameters : pub_key
and this
as a context
YCPay ycPay = new YCPay(this, "pub_key");
The seller's YouCanPay account public key. This lets us know who is receiving the payment.
After we initials ycPay
we can load the supprted payment methods using:
this.ycPay.getAccountConfigLiveData().observe(this, accountConfig -> {
if (!accountConfig.isSuccess()) {
// load config failed.
return;
}
if (accountConfig.isAcceptsCashPlus()) {
// CashPlus is available
}
if (accountConfig.isAcceptsCreditCards()) {
// Credit cards are available
}
});
When you get accountConfig.isAcceptsCreditCards() == true
it means that the Credit Card payment method is allowed.
YCPayCardInformation cardInformation = new YCPayCardInformation("card-holder-name", "1234123412341234", "MM", "YY", "CVV");
PayCallbackImpl is the callback that you can get status of your payment if is success or failure.
PayCallbackImpl payCallback = new PayCallbackImpl() {
@Override
public void onSuccess(String transactionId) {
// ... pay success
}
@Override
public void onFailure(String message) {
// ... pay Failure with reason
}
};
Once the onSuccess callback invoked it means that the transaction is processeded successfully, a transaction ID will be recieved as a parameter that you can submit with your order details. Similarly, onFailure is called when an error is occurred during the payment, and you get the error message as a parameter to show to customer.
You can use ycPay.payWithCard
to proceed your payment use as parametrs the token_id
it can be generated from your server side and received through an endpoint to the mobile application, to generate a token please refer to the Tokenization section.
try {
this.ycPay.payWithCard("token_id", ycPayCardInformation, payCallback);
} catch (Exception e) {
e.printStackTrace();
}
If you you get accountConfig.isAcceptsCashPlus() == true
that's mean that you have CashPlus as a payment method
CashPlusCallbackImpl is the callback that provides payment status.
CashPlusCallbackImpl cashPlusCallbackImpl = new CashPlusCallbackImpl() {
@Override
public void onSuccess(String transactionId, String cashPlusToken) {
// ... getting token is success
}
@Override
public void onFailure(String message) {
// ... getting token Failure with reason
}
};
You can use ycPay.payWithCashPlus
to proceed your payment use as parametrs the token_id
it can be generated from your server side and received through an endpoint to the mobile application, to generate a token please refer to the Tokenization section.
try {
this.ycPay.payWithCashPlus("token_id", cashPlusCallbackImpl);
} catch (Exception e) {
e.printStackTrace();
}
YouCan Pay Sandbox offers an easy way for developers to test YouCan Pay in their test environment.
// setting the sandbox mode
this.ycPay.setSandboxMode(true)