Java Function Return to React-Native

I'm developing an application with an integration Smart-POS. My application is in React-Native and the card reading or printing options are created through Java functions.

In other words, I'm calling a function in React-Native that is requesting a function in a Java file that communicates with the device and gives me a return.

Problem: I'm not getting any return after executing the functions in Java.

Here are code details:

React-Native Code:

import Apos from '../../../Apos';
const verifyService = async () => {
try {
  let info = {
    type: 1,
    value: 380,
    payment: 1,
    codvenda: "1",
  };
  const services = await Apos.startPayment(info);
  console.log('retorno: ',services);
} catch (error) {
  console.log('erro:', error);
}};

After executing this function, I call the Apos.js file that contains this information:

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

This file then calls a .java file with the function I'm requesting:

@ReactMethod
public String startPayment(ReadableMap data) {
    // Define os dados do pagamento
    PlugPagPaymentData paymentData =
    new PlugPagPaymentData(
    data.getInt("type"),
    data.getInt("value"),
    data.getInt("payment"),
    1,
    data.getString("codvenda"));
    // Cria a identificação do aplicativo
    PlugPagAppIdentification appIdentification =
    new PlugPagAppIdentification("MeuApp", "1.0.7");

    PlugPag plugPag = new PlugPag(reactContext, appIdentification);

  
    PlugPagInitializationResult initResult = plugPag.initializeAndActivatePinpad(new PlugPagActivationData("1170496755"));
    
    PlugPagTransactionResult result = plugPag.doPayment(paymentData);
    
    return String.valueOf(result);
}

In this return, through adb logcat I can see if putting one System.out.println(result); is returning all the transaction data to the console, but it goes back to React Native with return undefined.

This is an example of a return:

PlugPagTransactionResult(message=ERRO NO CARTAO - NAO TENTE NOVAMENTE, errorCode=R 30, transactionCode=null, transactionId=null, date=null, time=null, hostNsu=null, cardBrand=null, bin=null, holder=null, userReference=null, terminalSerialNumber=null, amount=null, availableBalance=null, cardApplication=null, cardCryptogram=null, label=null, holderName=null, extendedHolderName=null, result=-1004)

For details, I'm integrating PagSeguro's Moderninha API.

Has anyone been through this situation?

Upvotes: 1

Views: 793

Answers (1)

Anderson Vanzo
Anderson Vanzo

Reputation: 30

You just need to change your function from String to void and send the response as a promise, like this:

@ReactMethod
public void startPayment(final Promise promise, ReadableMap data) {
  // ... 
  promise.resolve(String.valueOf(result));
  return;
}

Upvotes: 1

Related Questions