1

在我的 react native 项目中,我已经在 android 中实现了支付原生 sdk(react native 不支持)。所以我试图用原生模块调用原生 sdk。

我可以从反应原生模块调用支付SDKUI,但是当结果的时候不能将结果发送回反应原生组件..

支付网关是 -> PAYUBIZ

有关更多详细信息,请在下面找到代码..

在支付网关结束时,我在 android native alert 中显示了支付响应。

使用的代码..

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..

所以现在我希望结果数据能够反应原生,请建议我任何解决方案

提前致谢

4

3 回答 3

2

您可以像这样从本机代码发送事件:

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

在您的反应本机代码中,您可以接收如下事件:

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

}

在此处查看完整文档

这里给出了另一种方法

于 2018-10-25T06:00:06.260 回答
2

我建议使用承诺。
在您的本机模块中有一个属性作为Promise mPromise;(也包括import com.facebook.react.bridge.Promise;)并在您的反应本机方法中接受承诺作为

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

在你的onActivityResult你可以按如下方式使用它。

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

然后你可以按如下方式使用它

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

编辑

如果onActivityResult()在另一个文件中。添加mReactInstanceManager.onActivityResult(requestCode, resultCode, data);onActivityResult()其中MainActivity

在您的本机模块中,添加以下方法。

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    this.mPromise.resolve(data.getDataString());
}
于 2018-10-25T06:28:35.907 回答
0

我使用了@Ayush Khare 回复..在调试时遇到了一些问题..所以我在这里发布了确切的答案

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

一旦在上面添加,运行并从触发事件方法获取响应..

于 2018-10-25T09:45:42.060 回答