Design Payment Activity and Store data in Firebase Database in Android Studio.
Fisrt Activity:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="@drawable/money"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/make_donation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="5dp"
android:layout_marginBottom="161dp"
android:background="@drawable/button_custom"
android:text="Make A Donation"
android:padding="8dp"
android:paddingEnd="8dp"
android:onClick="connectToCollege"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="516dp" />
<EditText
android:id="@+id/message_edit"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="5dp"
android:layout_marginTop="159dp"
android:background="@drawable/textedit_custom"
android:ems="10"
android:gravity="start|top"
android:hint="........"
android:inputType="textMultiLine"
android:padding="8dp"
android:paddingEnd="8dp" />
<EditText
android:id="@+id/pay_amount"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="5dp"
android:layout_marginTop="286dp"
android:background="@drawable/textedit_custom"
android:ems="10"
android:gravity="start|top"
android:hint="........"
android:inputType="numberSigned"
android:padding="8dp"
android:paddingEnd="8dp" />
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="30dp"
android:textColor="#0009ff"
android:layout_marginTop="88dp"
android:textStyle="bold"
android:textSize="40dp"
android:text="Fund Description" />
<TextView
android:id="@+id/amout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="29dp"
android:layout_marginTop="228dp"
android:text="Amount"
android:textColor="#0009ff"
android:textSize="40dp"
android:textStyle="bold" />
</RelativeLayout>
MainActivity.java
package com.example.alumni.Charity;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.alumni.R;
public class MainActivity extends AppCompatActivity {
final String SHARED_PREFS="sharedPrefs";
String number="",charityTitle,charityAmount;
final static String DEFAULT="N/A";
private EditText charity_description,charity_amount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_charity);
final SharedPreferences sharedPreferences=getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE);
number=sharedPreferences.getString("PhoneNumber",DEFAULT);
charity_description=findViewById(R.id.message_edit);
charity_amount=findViewById(R.id.pay_amount);
}
public void connectToCollege(View view) {
charityTitle=charity_description.getText().toString();
charityAmount=charity_amount.getText().toString();
if(TextUtils.isEmpty(charityTitle)) {
Toast.makeText(CharityActivity.this, "please enter charityTitle..", Toast.LENGTH_SHORT).show();
}else if(TextUtils.isEmpty(charityAmount)) {
Toast.makeText(CharityActivity.this, "please enter charityAmount..", Toast.LENGTH_SHORT).show();
}
else {
Intent intent=new Intent(CharityActivity.this, pay_to_college.class);
intent.putExtra("CharityTitle",charityTitle);
intent.putExtra("CharityAmount",charityAmount);
startActivity(intent);
}
charity_description.setText("");
charity_amount.setText("");
}
}
Second Activity:
activity_Payment.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/login"
tools:context=".payment">
<TextView
android:id="@+id/payment_mode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginTop="5dp"
android:text="PAYMENT MODE:"
android:textSize="20sp" />
<Spinner
android:id="@+id/paymentOption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="200dp"
android:layout_marginTop="5dp"
/>
<FrameLayout
android:id="@+id/frame_upi"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="1dp"
android:layout_marginTop="124dp"
android:layout_marginBottom="7dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login To Phone Pay"
android:textStyle="bold"
android:textSize="30dp"/>
<EditText
android:id="@+id/upiNumber"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="80dp"
android:background="@drawable/textedit_custom"
android:ems="10"
android:hint="phone number"
android:inputType="textPersonName"
android:padding="10dp" />
<Button
android:id="@+id/sendOtp"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginTop="170dp"
android:layout_marginStart="120dp"
android:background="@drawable/button_custom"
android:onClick="SendOtpForUpiNumber"
android:text="SEND OTP " />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="_________________OR___________________"
android:textSize="20dp"
android:layout_marginTop="250dp"
android:textStyle="bold"/>
<EditText
android:id="@+id/upiUsername"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="10dp"
android:layout_marginTop="400dp"
android:background="@drawable/textedit_custom"
android:ems="10"
android:hint="username@upi"
android:inputType="textPersonName"
android:padding="8dp"
android:paddingEnd="8dp" />
<Button
android:id="@+id/verifyUsername"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginTop="480dp"
android:layout_marginStart="120dp"
android:onClick="verifyUpiUserName"
android:background="@drawable/button_custom"
android:text="VERIFY" />
</FrameLayout>
<FrameLayout
android:id="@+id/frame_card"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="1dp"
android:layout_marginTop="124dp"
android:layout_marginBottom="7dp">
<EditText
android:id="@+id/cardNumber"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="10dp"
android:layout_marginTop="80dp"
android:background="@drawable/textedit_custom"
android:ems="10"
android:hint="Card Number"
android:inputType="textPersonName"
android:padding="8dp"
android:paddingEnd="8dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="valid thru"
android:layout_marginStart="30dp"
android:layout_marginTop="150dp"/>
<EditText
android:id="@+id/cardMonth"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="10dp"
android:layout_marginTop="180dp"
android:background="@drawable/textedit_custom"
android:ems="10"
android:hint="MM"
android:inputType="textPersonName"
android:padding="8dp"
android:paddingEnd="10dp" />
<EditText
android:id="@+id/cardyear"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="90dp"
android:layout_marginTop="180dp"
android:background="@drawable/textedit_custom"
android:ems="10"
android:hint="year"
android:inputType="textPersonName"
android:padding="8dp"
android:paddingEnd="10dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="/"
android:textSize="40dp"
android:layout_marginStart="70dp"
android:layout_marginTop="180dp"/>
<EditText
android:id="@+id/cardCvv"
android:layout_width="130dp"
android:layout_height="60dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="190dp"
android:layout_marginTop="180dp"
android:background="@drawable/textedit_custom"
android:ems="10"
android:hint="cvv"
android:inputType="textPersonName"
android:padding="8dp"
android:paddingEnd="10dp" />
<Button
android:id="@+id/paybycard"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginTop="300dp"
android:layout_marginStart="120dp"
android:onClick="PayByCard"
android:background="@drawable/button_custom"
android:text="Pay" />
</FrameLayout>
</RelativeLayout>
Payment.java:
package com.example.alumni.Charity;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.Spinner;
import android.widget.Toast;
import com.example.alumni.R;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
public class Payment extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
Spinner Dropdown1;
FrameLayout Upiframe,Cardframe;
private EditText CardNumer,CardMonth,CardYear,CardCvv,UpiNumber,UpiUsername;
private String cardNumer,cardMonth,cardYear,cardCvv,upiNumber,upiUsername,PHONENUMBER;
final String SHARED_PREFS="sharedPrefs";
final static String DEFAULT="N/A";
String charityTitle,charityAmount;
private DatabaseReference databaseReference;
String saveCurrentDate,saveCurrentTime,fundRandomKey;
private ProgressDialog loadingbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pay_to_college);
loadingbar = new ProgressDialog(this);
final SharedPreferences sharedPreferences=getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE);
PHONENUMBER=sharedPreferences.getString("PhoneNumber",DEFAULT);
databaseReference= FirebaseDatabase.getInstance().getReference();
Dropdown1=findViewById(R.id.paymentOption);
Upiframe=findViewById(R.id.frame_upi);
Cardframe=findViewById(R.id.frame_card);
Calendar calendar=Calendar.getInstance();
SimpleDateFormat currentDate=new SimpleDateFormat("MMM, dd, yyyy");
saveCurrentDate=currentDate.format(calendar.getTime());
SimpleDateFormat currentTime=new SimpleDateFormat("HH:mm:ss a");
saveCurrentTime=currentTime.format(calendar.getTime());
fundRandomKey=PHONENUMBER + saveCurrentDate + saveCurrentTime;
CardNumer=findViewById(R.id.cardNumber);
CardCvv=findViewById(R.id.cardCvv);
CardMonth=findViewById(R.id.cardMonth);
CardYear=findViewById(R.id.cardyear);
UpiNumber=findViewById(R.id.upiNumber);
UpiUsername=findViewById(R.id.upiUsername);
ArrayAdapter<CharSequence> adapter= ArrayAdapter.createFromResource(this,R.array.cardoptions,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Dropdown1.setAdapter(adapter);
Dropdown1.setOnItemSelectedListener(this);
charityTitle=getIntent().getStringExtra("CharityTitle");
charityAmount=getIntent().getStringExtra("CharityAmount");
}
public void PayByCard(View view) {
cardNumer=CardNumer.getText().toString();
cardMonth=CardMonth.getText().toString();
cardYear=CardYear.getText().toString();
cardCvv=CardCvv.getText().toString();
if(TextUtils.isEmpty(cardNumer)) {
Toast.makeText(pay_to_college.this, "please enter Card Number..", Toast.LENGTH_SHORT).show();
}else if(TextUtils.isEmpty(cardMonth)) {
Toast.makeText(pay_to_college.this, "please enter expiry month of Card..", Toast.LENGTH_SHORT).show();
}else if(TextUtils.isEmpty(cardYear)) {
Toast.makeText(pay_to_college.this, "please enter expiry year of Card..", Toast.LENGTH_SHORT).show();
} else if(TextUtils.isEmpty(cardCvv)) {
Toast.makeText(pay_to_college.this, "please enter Card cvv..", Toast.LENGTH_SHORT).show();
}else {
loadingbar.setTitle("Processing");
loadingbar.setMessage("please wait...");
loadingbar.setCanceledOnTouchOutside(false);
loadingbar.show();
HashMap<String,Object> hashMap=new HashMap<>();
hashMap.put("UserPhone",PHONENUMBER);
hashMap.put("Description",charityTitle);
hashMap.put("Amount",charityAmount);
hashMap.put("CardNumber",cardNumer);
hashMap.put("PaymentDate",saveCurrentDate);
hashMap.put("PaymentTime",saveCurrentTime);
databaseReference.child("Charity").child("CardPayment").child(fundRandomKey).updateChildren(hashMap)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()) {
loadingbar.dismiss();
Toast.makeText(pay_to_college.this,"Payment Successful.. ",Toast.LENGTH_SHORT).show();
} else {
loadingbar.dismiss();
Toast.makeText(pay_to_college.this, "try again", Toast.LENGTH_SHORT).show();
}
CardNumer.setText("");
CardMonth.setText("");
CardYear.setText("");
CardCvv.setText("");
}
});
}
}
public void verifyUpiUserName(View view) {
upiUsername=UpiUsername.getText().toString();
if(TextUtils.isEmpty(upiUsername)) {
Toast.makeText(pay_to_college.this, "please enter UpiUsername..", Toast.LENGTH_SHORT).show();
}else {
loadingbar.setTitle("Processing");
loadingbar.setMessage("please wait...");
loadingbar.setCanceledOnTouchOutside(false);
loadingbar.show();
HashMap<String,Object> hashMap=new HashMap<>();
hashMap.put("UserUpiUsername",upiUsername);
hashMap.put("Description",charityTitle);
hashMap.put("Amount",charityAmount);
hashMap.put("PaymentDate",saveCurrentDate);
hashMap.put("PaymentTime",saveCurrentTime);
databaseReference.child("Charity").child("UpiUserName").child(fundRandomKey).updateChildren(hashMap)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()) {
loadingbar.dismiss();
Toast.makeText(pay_to_college.this,"Payment Successful.. ",Toast.LENGTH_SHORT).show();
} else {
loadingbar.dismiss();
Toast.makeText(pay_to_college.this, "try again", Toast.LENGTH_SHORT).show();
}
UpiUsername.setText("");
}
});
}
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String text=parent.getItemAtPosition(position).toString();
if(text.equals("PhonePe")) {
Cardframe.setVisibility(View.GONE);
Upiframe.setVisibility(View.VISIBLE);
}else if (text.equals("Credit card/Debit Card")) {
Upiframe.setVisibility(View.GONE);
Cardframe.setVisibility(View.VISIBLE);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
public void SendOtpForUpiNumber(View view) {
upiNumber=UpiNumber.getText().toString();
if(TextUtils.isEmpty(upiNumber)) {
Toast.makeText(pay_to_college.this, "please enter UpiNumber..", Toast.LENGTH_SHORT).show();
}else {
loadingbar.setTitle("Processing");
loadingbar.setMessage("please wait...");
loadingbar.setCanceledOnTouchOutside(false);
loadingbar.show();
HashMap<String,Object> hashMap=new HashMap<>();
hashMap.put("UserUpiNumber",upiNumber);
hashMap.put("Description",charityTitle);
hashMap.put("Amount",charityAmount);
hashMap.put("PaymentDate",saveCurrentDate);
hashMap.put("PaymentTime",saveCurrentTime);
databaseReference.child("Charity").child("UpiNumber").child(fundRandomKey).updateChildren(hashMap)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()) {
loadingbar.dismiss();
Toast.makeText(pay_to_college.this,"Payment Successful.. ",Toast.LENGTH_SHORT).show();
} else {
loadingbar.dismiss();
Toast.makeText(pay_to_college.this, "try again", Toast.LENGTH_SHORT).show();
}
UpiNumber.setText("");
}
});
}
}
}
Add Custom Button to your drawable :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#938709"/>
<corners android:radius="25dp"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#9874a7"/>
<corners android:radius="25dp"/>
</shape>
Comments
Post a Comment