A UpiPayment library for integrating upi payments using existing upi supported apps like googple pay, bhim etc.
Step 1: Add the dependency
dependencies {
...
/*Upi Payment */
implementation 'com.wangsun.upi.payment:upi-payment:0.0.2'
}
Step 1: Declare and Initialize UpiPayment.
// note: always create new instance of PaymentDetail for every new payment/order
var payment = PaymentDetail(
"wangsunhakhun@oksbi", //vpa/upi = your vpa/upi
"Wangsun Hakhun", //name = your name
"", //payeeMerchantCode = only if you have merchantCode else pass empty string
"", //txnRefId = if you pass empty string we will generate txnRefId for you
"description", //description =
"2.00") //amount = format of amount should be in decimal format x.x (eg 530.00)
// note: always create new instance of UpiPayment for every new payment/order
new UpiPayment(this)
.setPaymentDetail(payment)
.setUpiApps(UpiPayment.getUPI_APPS())
.setCallBackListener(new UpiPayment.OnUpiPaymentListener() {
@Override
public void onSubmitted(@NotNull TransactionDetails data) {
//transaction pending: use data to get TransactionDetails
}
@Override
public void onError(@NotNull String message) {
//user backpress or transaction failed
}
@Override
public void onSuccess(@NotNull TransactionDetails data) {
//transaction success: use data to get TransactionDetails
}
}).pay();
// note: always create new instance of PaymentDetail for every new payment/order
var payment = PaymentDetail(
vpa="wangsunhakhun@oksbi",
name = "Wangsun Hakhun",
payeeMerchantCode = "", // only if you have merchantCode else pass empty string
txnRefId = "", // if you pass empty string we will generate txnRefId for you
description = "description",
amount = "2.00") // format of amount should be in decimal format x.x (eg 530.00), max. 2 decimal places
// note: always create new instance of UpiPayment for every new payment/order
UpiPayment(this)
.setPaymentDetail(payment)
.setUpiApps(UpiPayment.UPI_APPS)
.setCallBackListener(object : UpiPayment.OnUpiPaymentListener{
override fun onSubmitted(data: TransactionDetails) {
//transaction pending: use data to get TransactionDetails
}
override fun onSuccess(data: TransactionDetails) {
//transaction success: use data to get TransactionDetails
}
override fun onError(message: String) {
//user backpress or transaction failed
}
}).pay()
Set all payment details like vpa/upi, amount, name etc. Note: always create new instance of PaymentDetail for every new payment/order.
Set selected upiApps.
eg.
//adding others upi apps to our default selected apps
//todo: check names of all apps first
//todo: name should be in lowercase
ArrayList<String> appList = UpiPayment.getUPI_APPS();
appList.add("new app name1");
appList.add("new app name2");
//adding new set of apps
ArrayList<String> newList = new ArrayList<String>();
newList.add("paytm")
newList.add("google pay")
newList.add("bhim")
//and pass this to: setUpiApps(newList): or setUpiApps(appList):
this will listen to the result of payment transaction(Only one callback will trigger for a single transaction).
- .onSuccess(): trigger whenever transaction is successfully completed
- .onSubmitted(): trigger whenever transaction is pending
- .onError(): trigger whenever transaction is failed/user backpress/or other error
If you want to implement callBackListener() globally then implements UpiPayment.OnUpiPaymentListener.
eg.
public class YourActivity extends AppCompatActivity implements UpiPayment.OnUpiPaymentListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//override below functions
@Override
public void onSuccess(@NotNull TransactionDetails data) {}
@Override
public void onSubmitted(@NotNull TransactionDetails data) {}
@Override
public void onError(@NotNull String message) {}
}
To check existing upi apps. eg. Developer can hide(visibility) "Pay using Upi App" button if there is no upi app present.
eg.
ArrayList<String> existingUpiAppNames = UpiPayment.getExistingUpiApps(context);