Android Question Error While trying to implement Razorpay payment Gateway

AndroidMadhu

Active Member
Licensed User
Hello,
I am from India and I am trying to impment Razorpay gateway for one of my App.
I am getting below error while compile with Razorpay details :
B4X:
Logger connected to:   Xiaomi Redmi 6
--------- beginning of system
--------- beginning of main
* Service (starter) Create *
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
main_button1_click (java line: 372)
java.lang.RuntimeException: Object should first be initialized (JavaObject).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:67)
at anywheresoftware.b4j.object.JavaObject.getCurrentClass(JavaObject.java:258)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:118)
at anywheresoftware.b4j.object.JavaObject.RunMethodJO(JavaObject.java:138)
at b4a.example.main._button1_click(main.java:372)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:197)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:6614)
at android.view.View.performClickInternal(View.java:6587)
at android.view.View.access$3100(View.java:787)
at android.view.View$PerformClick.run(View.java:26122)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6820)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:922)
main_button1_click (java line: 372)
java.lang.RuntimeException: Object should first be initialized (JavaObject).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:67)
at anywheresoftware.b4j.object.JavaObject.getCurrentClass(JavaObject.java:258)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:118)
at anywheresoftware.b4j.object.JavaObject.RunMethodJO(JavaObject.java:138)
at b4a.example.main._button1_click(main.java:372)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:197)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:6614)
at android.view.View.performClickInternal(View.java:6587)
at android.view.View.access$3100(View.java:787)
at android.view.View$PerformClick.run(View.java:26122)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6820)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:922)

The below is my Code Module details :

B4X:
#AdditionalJar: checkout-1.5.16.aar

Sub Process_Globals
Private xui As XUI
End Sub

Sub Globals
End Sub

Sub GetCheckOut As JavaObject
Dim jo As JavaObject
'com.squareup.picasso.Picasso.with(context)
Return jo.InitializeStatic("com.razorpay.Checkout").RunMethod("preload", Array(GetContext))

End Sub

Sub GetContext As JavaObject
Return GetBA.GetField("context")
End Sub

Sub GetBA As JavaObject
Dim jo As JavaObject
Dim cls As String = Me
cls = cls.SubString("class ".Length)
jo.InitializeStatic(cls)
Return jo.GetFieldJO("processBA")
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Layout")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
Dim jo As JavaObject
Dim key As String="rzp_test_XXXXXXXX"
GetCheckOut.RunMethodJO("setKeyID",Array(key))
End Sub


Please advice


Thanks
 

DonManfred

Expert
Licensed User
CodeModule?
You should use a Activity, Service or Class instantiated in a Activity or Service. CodeModules does not have a Context
 
Last edited:
Upvote 0

AndroidMadhu

Active Member
Licensed User
I have tried at Android Studio. And the code is running fine.
But I am not sure about how to use the same at B4A...
The below is the code from Android Studio....
B4X:
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", "test@razorpay.com");
                      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();
              }
      }

}

Please advice... I am stuck on this

Thanks
 
Upvote 0
Top