3. Code Integration

Invoke the payWithUPI() method for init payment.

String transactionId;
private OneUPIPayment easyUpiPayment;

@SuppressLint("NonConstantResourceId")
 private void payWithUpi() {
  transactionId = "TID" + System.currentTimeMillis();

  PaymentApp paymentApp = PaymentApp.ALL;

  // START PAYMENT INITIALIZATION
  OneUPIPayment.Builder builder = new OneUPIPayment.Builder(this)
    .with(paymentApp)
    .setPayeeVpa("oneupiexample@ybl")
    .setPayeeName("Elon Musk")
    .setTransactionId(transactionId)
    .setTransactionRefId(transactionId)
    .setPayeeMerchantCode("xxxxxxxx-5f41-xxxx-b9b0-xxxxxxxxx029")
    .setDescription("planName")
    .setAmount("1000.00");
  // END INITIALIZATION

  try {
   // Build instance
   easyUpiPayment = builder.build();

   // Register Listener for Events
   easyUpiPayment.setPaymentStatusListener(this);

   // Start payment / transaction
   easyUpiPayment.startPayment();
  } catch (Exception exception) {
   exception.printStackTrace();
   toast("Error: " + exception.getMessage());
  }
 }

 @Override
 public void onTransactionCompleted(TransactionDetails transactionDetails) {
  // Transaction Completed
  Log.d("TransactionDetails", transactionDetails.toString());

  switch (transactionDetails.getTransactionStatus()) {
   case SUCCESS:
    onTransactionSuccess();
    break;
   case FAILURE:
    onTransactionFailed();
    break;
   case SUBMITTED:
    onTransactionSubmitted();
    break;
  }
 }

 @Override
 public void onTransactionCancelled() {
  // Payment Cancelled by User
  toast("Cancelled by user");

 }

 private void onTransactionSuccess() {
  // Payment Success
  toast("Success");
  Log.d("UPI", "responseStr: " + transactionId);
//  new Transaction(SelectPlanActivity.this).purchasedItem(planId, transactionId, "UPI");
 }

 private void onTransactionSubmitted() {
  // Payment Pending
  toast("Pending | Submitted");

 }

 private void onTransactionFailed() {
  // Payment Failed
  toast("Failed");

 }

 private void toast(String message) {
  Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
 }

And make the calling activity implements PaymentStatusListener

Note: Merchant Key will be generated after signing into OneUPI's dashboard.

Last updated