package com.example.payget1;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.razorpay.Checkout;
import org.json.JSONObject;
import java.util.zip.CheckedOutputStream;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Checkout.preload(getApplicationContext());
Button button = (Button) findViewById(R.id.btn_pay);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startPayment();
Log.i("snt","onclick");
}
});
TextView privacyPolicy = (TextView) findViewById(R.id.txt_privacy_policy);
privacyPolicy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent httpIntent = new Intent(Intent.ACTION_VIEW);
httpIntent.setData(Uri.parse("https://razorpay.com/sample-application/"));
startActivity(httpIntent);
}
});
}
public void startPayment() {
/*
You need to pass current activity in order to let Razorpay create CheckoutActivity
*/
final Activity activity = this;
final Checkout co = new Checkout();
co.setKeyID("rzp_testxxxxxxxxxxx");
try {
JSONObject options = new JSONObject();
options.put("name", "xxxx");
options.put("description", "Fare");
//You can omit the image option to fetch the image from dashboard
//options.put("image", "");
options.put("currency", "INR");
options.put("amount", "100");
JSONObject preFill = new JSONObject();
preFill.put("email", "[email protected]");
preFill.put("contact", "9999999999");
options.put("prefill", preFill);
co.open(activity, options);
} catch (Exception e) {
Toast.makeText(activity, "Error in payment: " + e.getMessage(), Toast.LENGTH_SHORT)
.show();
e.printStackTrace();
}
}
}