Item Payment
It is a payment type that allow you to charge your customers to buy in-app Items like coins, gems, etc.
Secure
: Item is encrypted withAES-256
algorithm using device unique id.Easy to Manipulate
: It hasREPLACE
,ADD
,SUBTRACT
andMULTIPLY
,ItemProperties
that allow you to manipulate item easily.ItemProperties
: defaultItemProperties.REPLACE
REPLACE
: Replace item value with new valueADD
: Adds current value to previous item valueSUBTRACT
: Subtract new value from previous item valueMULTIPLY
: Multiply item value with new value if exist otherwise set new value as item value
Setup Item Payment
- Create
ItemPayment
object
try {
ItemPayment coin = new ItemPayment(this, 1.99, "Coin", 100, ItemProperties.ADD);
ItemPayment disableADS = new ItemPayment(this, 5.0, "disableAds", true); // default itemProperty is REPLACE
Chapa chapa = Chapa.getInstance();
// simply call pay method without callback
chapa.pay(this, disableADS);
// or with callback
chapa.pay(this, coin, new ChapaPaymentCallback<ItemPayment>() {
@Override
public void onSuccess(@NonNull String tx_ref, @NonNull ItemPayment paymentType) {
// TODO your code here
}
@Override
public void onError(@NonNull ChapaError chapaError) {
// TODO your code here
}
@Override
public void onCancel() {
// TODO your code here
}
});
} catch (ChapaError e) {
// throws UNSUPPORTED_DATA_TYPE if the dataType of 'value' is not String,Boolean,Integer,Float or Double
}
val coin = ItemPayment(/* context */ this,/* amount */ 1.99,/* item key */ "Coin",/* item value */ 100,/* item property */ ItemProperty.ADD)
val disableADS = ItemPayment(this , 5.0, "ads", true) // default itemProperty is REPLACE
// get chapa instance
val chapa = Chapa.getInstance()
// simply call pay method without callback
chapa.pay(this,disableADS)
// or with callback
chapa.pay(this,coin,object : ChapaPaymentCallback<ItemPayment>{
override fun onSuccess(tx_ref: String, paymentType: ItemPayment) {
// TODO your code here
}
override fun onError(chapaError: ChapaError) {
// TODO your code here
}
override fun onCancel() {
// TODO your code here
}
})
Get PurchasedItem Value
PurchasedItems purchasedItems = new PurchasedItems(/*context*/ this);
// get item value
// method one (Recommended)
int coin = purchasedItems.getInt("Coin"); // returns item value if exist otherwise returns 0
// alternatively you can use
int _coin = purchasedItems.getValue("Coin", /*default value*/ 0); // returns item value if exist otherwise returns default value
val purchasedItems = PurchasedItems(/*context*/ this)
// get item value
// method one (Recommended for security)
val coin = purchasedItems.getInt("Coin") // returns item value if exist otherwise returns 0
// alternatively you can use
val _coin = purchasedItems.getValue("Coin", /*default value*/ 0) // returns item value if exist otherwise returns default value
Update PurchasedItem Value
// to update item value
PurchasedItems purchasedItems = new PurchasedItems(/*context*/ this);
int coin = purchasedItems.getInt("Coin")
if(coin>5){
// it subtracts 5 coin from current coin value
int newCoinValue = purchasedItems.updateValue("Coin",5,ItemProperties.SUBTRACT);
Log.d("Coin",newCoinValue + "coins left");
} else
Log.d("Coin","You don't have enough coins");
// to update item value
val purchasedItems = PurchasedItems(/*context*/ this)
val coin = purchasedItems.getInt("Coin")
if(coin>5){
// it subtracts 5 coin from current coin value
val newCoinValue = purchasedItems.updateValue("Coin",5,ItemProperties.SUBTRACT)
Log.d("Coin","$newCoinValue coins left")
} else
Log.d("Coin","You don't have enough coins")
Remove PurchasedItem
// to remove payed item
PurchasedItems payedItem = PurchasedItems(/*context*/ this);
payedItem.removeItem("Coin");
// to remove payed item
val purchasedItems = PurchasedItems(/*context*/ this)
purchasedItems.removeItem("Coin")