KEMBAR78
Working | PDF | Android (Operating System) | Computing
0% found this document useful (0 votes)
10 views4 pages

Working

The document is a Java class for an Android application that handles payment processing using UPI. It retrieves the entry fee for a match from Firestore and initiates a UPI payment when the user clicks the pay button. The class also implements a listener to handle transaction status updates, displaying appropriate messages and images based on the payment outcome.

Uploaded by

kgaurav891
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

Working

The document is a Java class for an Android application that handles payment processing using UPI. It retrieves the entry fee for a match from Firestore and initiates a UPI payment when the user clicks the pay button. The class also implements a listener to handle transaction status updates, displaying appropriate messages and images based on the payment outcome.

Uploaded by

kgaurav891
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

package com.app.

khelo;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;

import dev.shreyaspatil.easyupipayment.listener.PaymentStatusListener;
import dev.shreyaspatil.easyupipayment.model.TransactionDetails;

import android.os.Bundle;
import android.widget.TextView;
import android.widget.ImageView;
import android.widget.Button;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import dev.shreyaspatil.easyupipayment.listener.PaymentStatusListener;
import dev.shreyaspatil.easyupipayment.model.TransactionDetails;

public class PaymentActivity extends AppCompatActivity implements PaymentStatusListener


{
private static final int UPI_PAYMENT = 0; // Use any unique value for the request code

private TextView paymentAmountTextView;


private ImageView imageView;
private static final String TAG = "PaymentActivity";
private double entryFee;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_payment);

paymentAmountTextView = findViewById(R.id.paymentAmountTextView);
imageView = findViewById(R.id.imageView);

String id = getIntent().getStringExtra("id");

FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection("match").document(id)
.get()
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
String entryFeeString = document.getString("entryFee");
try {
entryFee = Double.parseDouble(entryFeeString);
paymentAmountTextView.setText("Payment Amount: " + entryFee);
} catch (NumberFormatException e) {
paymentAmountTextView.setText("Invalid entry fee");
}
} else {
paymentAmountTextView.setText("No such document");
}
} else {
paymentAmountTextView.setText("Error fetching data");
}
}
});

Button payButton = findViewById(R.id.payButton);


payButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String amount = String.valueOf(entryFee);
String note = "Payment for Match Entry";
String name = "SHILPI KUMARI";
String upiId = "51075901@UBIN";

payUsingUpi(amount, upiId, name, note);


}
});
}

public void payUsingUpi(String amount, String upiId, String name, String note) {
Uri uri = Uri.parse("upi://pay").buildUpon()
.appendQueryParameter("pa", upiId)
.appendQueryParameter("pn", name)
.appendQueryParameter("tn", note)
.appendQueryParameter("am", amount)
.appendQueryParameter("cu", "INR")
.build();

Intent upiPayIntent = new Intent(Intent.ACTION_VIEW);


upiPayIntent.setData(uri);

Intent chooser = Intent.createChooser(upiPayIntent, "Pay with");

if (null != chooser.resolveActivity(getPackageManager())) {
startActivityForResult(chooser, UPI_PAYMENT);
} else {
Toast.makeText(this, "No UPI app found, please install one to continue",
Toast.LENGTH_SHORT).show();
}
}

@Override
public void onTransactionCompleted(TransactionDetails transactionDetails) {
switch (transactionDetails.getTransactionStatus()) {
case SUCCESS:
onTransactionSuccess();
break;
case FAILURE:
onTransactionFailed();
break;
case SUBMITTED:
onTransactionSubmitted();
break;
}
}

@Override
public void onTransactionCancelled() {
toast("Payment cancelled by user");
imageView.setImageResource(R.drawable.ic_failed);
}

private void onTransactionSuccess() {


toast("Payment success");
imageView.setImageResource(R.drawable.ic_success);
}

private void onTransactionSubmitted() {


toast("Payment submitted");
imageView.setImageResource(R.drawable.ic_failed);
}

private void onTransactionFailed() {


toast("Payment failed");
imageView.setImageResource(R.drawable.ic_failed);
}

private void toast(String message) {


Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
}

You might also like