Extending PaymentType
Sometimes you may need to extend your own payment type to fit your need. You can do that by extending PaymentType
class.
public class CustomPaymentType extends PaymentType {
// you can define your own variables
private Object yourVariable;
public CustomPaymentType(float amount) {
super(amount);
// don't forget to set the transaction reference (tx-ref)
setTx_ref(ChapaUtil.generateTransactionRef(30, "Tx-"));
setCurrency(Currency.USD);
}
// you can define your own methods
public <T> void yourMethod(T data) {
}
/**
* this method is executed automatically when payment is successful
*/
@Override
public void onPaymentSuccess() {
}
/**
* this method is executed automatically when payment is failed
*
* @param error chapaError
*/
@Override
public void onPaymentFail(ChapaError error) {
}
/**
* this method is executed automatically when payment is canceled
*/
@Override
public void onPaymentCancel() {
}
}
class CustomPaymentType(amount: Float) : PaymentType(amount) {
// you can define your own variables
private lateinit var yourVariable : Any
init {
// don't forget to set the transaction reference (tx-ref)
tx_ref = ChapaUtil.generateTransactionRef(30,"Tx-")
currency = Currency.USD
}
// you can define your own methods
fun youMethod(data: Any){
yourVariable = data
}
/**
* this method is executed automatically when payment is successful
*/
override fun onPaymentSuccess() {
// TODO your code here
// save data to remote server or other task based on your need
}
/**
* this method is executed automatically when payment is failed
*/
override fun onPaymentFail(error: ChapaError?) {
// TODO your code here
}
/**
* this method is executed automatically when payment is canceled
*/
override fun onPaymentCancel() {
// TODO your code here
}
}
PaymentType
methods
Method | Description | Required | Default |
---|---|---|---|
setAmount(amount: Float) | payment amount | Yes | |
setCurrency(currency: Currency) | payment currency Currency.ETB or Currency.USD | Yes | ETB |
setTxRef(tx_ref: String) | payment unique transaction reference | Yes | |
setCustomer(customer: Customer) | customer's information. | NO (customer can fill their information) | |
setCustomizations(customizations: Customizations) | payment modal customizations | No | null |
setCallbackUrl(callback_url: String) | Api that trigger when payment is successful | No | null |