1

In my react native project ,I have implemented payment native sdk in android (that does not supported in react native).so i am tried to call native sdk with native modules..

i am able calling the payment SDKUI from react native native module ,but when the time of results can not send results back to react native component..

Payment gateway is -> PAYUBIZ

for more details pls find below code..

at the end of payment gateway i have displayed payment response in android native alert..

Code used..

1. Created NATIVE MODULES in react native side..



     import {NativeModules} from 'react-native';
        module.exports = NativeModules.PayUBizAccess;

        in button action following code to call native method from android
        PayUBizAccess.showPayuBiz();

2. Created ReactContextBaseJavaModule based PayUBizModule



@ReactMethod
  public void showPayuBiz() {

    final Activity activity = getCurrentActivity();


    Intent intent = new Intent(activity, PayuActivity.class);


    getReactApplicationContext().startActivity(intent);
   }

PayuActivity.class is the payment activity class

3. Display results after payment success or failure..



     @Override
            public void onActivityResult(int requestCode, int resultCode, final Intent data) {



                if (requestCode == PayuConstants.PAYU_REQUEST_CODE) {
                    if (data != null) {


                        new AlertDialog.Builder(this)
                                .setCancelable(false)
                                .setMessage("Payu's Data : " + data.getStringExtra("payu_response") + "\n\n\n Merchant's Data: " + data.getStringExtra("result"))
                                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int whichButton) {
                                        dialog.dismiss();
                                        finish();
                                    }
                                }).show();

                    } else {
                        Toast.makeText(this, getString(R.string.could_not_receive_data), Toast.LENGTH_LONG).show();
                    }
                }
            }

4. After alert clicking button in alert it directly moves to react native component..

So now i want results data's to react native ,Kindly suggest me any solutions

thanks in advance

abdul sathar
  • 2,395
  • 2
  • 28
  • 38

3 Answers3

2

You can send an event from your native code like this:

private void sendEvent(ReactContext reactContext,
                   String eventName,
                   @Nullable WritableMap params) {
  reactContext
    .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
    //supply the result in params
    .emit(eventName, params);
}

And in your react native code you can receive the event like this:

componentWillMount: function() {
   DeviceEventEmitter.addListener('keyboardWillShow', function(e: Event) {
  // handle event.
});

}

Check full docs here

Another way of doing this is given here

Ayush Khare
  • 1,802
  • 1
  • 15
  • 26
2

I would suggest use of promise.
In your native module have a property as Promise mPromise;(also include import com.facebook.react.bridge.Promise;) And accept promise in your react native method as

@ReactMethod
public void showPayuBiz(Promise promise) {
  mPromise = promise;
  final Activity activity = getCurrentActivity();
  Intent intent = new Intent(activity, PayuActivity.class);
  getReactApplicationContext().startActivity(intent);
}

And in your onActivityResult you can use it as follows.

@Override
public void onActivityResult(int requestCode, int resultCode, final Intent data) 
{
  if (requestCode == PayuConstants.PAYU_REQUEST_CODE) {
    //example for handling success response
    this.promise.resolve(data.getDataString()); // you can further process this data in react native component.
  }
  else{
    //example for handling error response
    this.promise.reject(data.getDataString());
  }
}

Then you can use it as follows

PayUBizAccess.showPayuBiz()
    .then(data => {
      console.log(data);
      //success 
    })
    .catch(error => {
      console.log(data);
      //failure
    });

Edit

If onActivityResult() is in another file. add mReactInstanceManager.onActivityResult(requestCode, resultCode, data); in onActivityResult() which is in MainActivity.

And inside you native module, add following method.

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    this.mPromise.resolve(data.getDataString());
}
Nakul
  • 1,313
  • 19
  • 26
0

I have used @Ayush Khare responses..faced few issues while debugging ..so i post here exact answer

1.React Native Side

Component will mount add following

    DeviceEventEmitter.addListener('PAYUEVENT', this.payuResponseGet);debugger

Add following method for trigger event

payuResponseGet = (payUData) => {
    console.log(payUData);debugger // logging twice
    // this.setState({
    //   modalVisible: args.visible
    // })
  }

2. Activity side

import com.facebook.react.ReactInstanceManager;
//import com.facebook.react.ReactApplicationContext;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.modules.core.DeviceEventManagerModule;


Add following on result method
//  mPromise.resolve(data.getDataString());
 ReactInstanceManager mReactInstanceManager = getReactNativeHost().getReactInstanceManager();
 ReactApplicationContext context = (ReactApplicationContext) mReactInstanceManager.getCurrentReactContext();

 WritableMap payuData = Arguments.createMap();
        payuData.putString("PayuResponses", data.getStringExtra("payu_response"));
        payuData.putString("Merchant's Data", data.getStringExtra("result"));

 sendEvent(context,"PAYUEVENT",payuData);

//send event method
private void sendEvent(ReactContext reactContext,
                   String eventName,
                   @Nullable WritableMap params) {
  reactContext
    .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
    //supply the result in params
    .emit(eventName, params);
}

once added above, run and get responses from trigger event method..

abdul sathar
  • 2,395
  • 2
  • 28
  • 38