Search Unity

How can create java class in unity, using android studio,

Discussion in 'Getting Started' started by vijitendra, Mar 4, 2020.

  1. vijitendra

    vijitendra

    Joined:
    Jan 23, 2016
    Posts:
    6
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;

    import com.razorpay.Checkout;
    import com.razorpay.PaymentResultListener;

    import org.json.JSONObject;

    public class PaymentActivity extends Activity implements PaymentResultListener {
    private static final String TAG = PaymentActivity.class.getSimpleName();

    @override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_payment);

    /*
    To ensure faster loading of the Checkout form,
    call this method as early as possible in your checkout flow.
    */
    Checkout.preload(getApplicationContext());

    // Payment button created by you in XML layout
    Button button = (Button) findViewById(R.id.btn_pay);

    button.setOnClickListener(new View.OnClickListener() {
    @override
    public void onClick(View v) {
    startPayment();
    }
    });
    }

    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();

    try {
    JSONObject options = new JSONObject();
    options.put("name", "Razorpay Corp");
    options.put("description", "Demoing Charges");
    //You can omit the image option to fetch the image from dashboard
    options.put("image", "https://s3.amazonaws.com/rzp-mobile/images/rzp.png");
    options.put("currency", "INR");
    options.put("amount", "100");

    JSONObject preFill = new JSONObject();
    preFill.put("email", "test@razorpay.com");
    preFill.put("contact", "9876543210");

    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();
    }
    }

    /**
    * The name of the function has to be
    * onPaymentSuccess
    * Wrap your code in try catch, as shown, to ensure that this method runs correctly
    */
    @SuppressWarnings("unused")
    @override
    public void onPaymentSuccess(String razorpayPaymentID) {
    try {
    Toast.makeText(this, "Payment Successful: " + razorpayPaymentID, Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
    Log.e(TAG, "Exception in onPaymentSuccess", e);
    }
    }

    /**
    * The name of the function has to be
    * onPaymentError
    * Wrap your code in try catch, as shown, to ensure that this method runs correctly
    */
    @SuppressWarnings("unused")
    @override
    public void onPaymentError(int code, String response) {
    try {
    Toast.makeText(this, "Payment failed: " + code + " " + response, Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
    Log.e(TAG, "Exception in onPaymentError", e);
    }
    }
    }