使用react-native-papal

时间:2016-11-24 11:44:55

标签: paypal react-native react-native-android

我使用了模块react-native-paypal,但是在Android中集成时,它会在Android运行期间抛出一些错误,这里是错误的屏幕截图。 enter image description here

以下是显示错误的代码 的 MainActivity.java

 package com.camp;

    import com.facebook.react.ReactActivity;
    import android.content.Intent; // <--


    public class MainActivity extends ReactActivity {
        //public static Activity activity;
    //private static final int PAY_PAL_REQUEST_ID = 9
        /**
         * Returns the name of the main component registered from JavaScript.
         * This is used to schedule rendering of the component.
         */

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

           if (requestCode == 9) {
                payPalPackage.handleActivityResult(requestCode, resultCode, data); // <--
            } else {
                otherModulesHandlers(requestCode, resultCode, data);
            }
        }

        @Override
        protected String getMainComponentName() {
            return "Camp";
        }

    }

MainApplication.java

package com.camp;

import android.app.Application;
import android.util.Log;

import com.facebook.react.ReactApplication;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import br.com.vizir.rn.paypal.PayPalPackage;

import java.util.Arrays;
import java.util.List;

public class MainApplication extends Application implements ReactApplication {
   private static final int PAY_PAL_REQUEST_ID = 9; // <-- Can be any unique number
    private PayPalPackage payPalPackage; // <--

  private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
    @Override
    protected boolean getUseDeveloperSupport() {
      return BuildConfig.DEBUG;
    }

    @Override
    protected List<ReactPackage> getPackages() {
      payPalPackage = new PayPalPackage(PAY_PAL_REQUEST_ID);  // <--
      return Arrays.<ReactPackage>asList(
        payPalPackage,  // <--
          new MainReactPackage()

      );
    }
  };

  @Override
  public ReactNativeHost getReactNativeHost() {
      return mReactNativeHost;
  }
}

请向我提供有关如何解决此错误的建议。非常感谢任何帮助

我的代码发生了所有变化后,但仍然面临一些问题,请参见下图 enter image description here

请给我建议如何解决这些类型的错误

paypalcode in react-native

paypal(){
    PayPalAndroid.paymentRequest({
      clientId: '............',
      environment: PayPalAndroid.SANDBOX,
      price: '0.01',
      currency: 'EUR',
      description: 'PayPal Test'
    }).then((confirm, payment) => console.log('Paid'))
    .catch((error_code) => console.error('Failed to pay through PayPal'));
  }

这里代替控制台我写了警报,但它没有显示警报,我多次尝试,我没有得到成功警报或抓住警报,请给我建议如何解决它

1 个答案:

答案 0 :(得分:1)

npm注册表及其存储库中的react-native-paypal版本尚未更新为最新的react-native版本。您可以使用此分支https://github.com/sharafat/react-native-paypal代替。

<强>的package.json

"dependencies": {
    "react": "15.4.1",
    "react-native": "0.38.0",
    "react-native-paypal": "https://github.com/sharafat/react-native-paypal.git"
  },

<强> MainActivity.java

...    
import android.content.Intent;

import com.facebook.react.ReactActivity;

public class MainActivity extends ReactActivity {

    /**
     * Returns the name of the main component registered from JavaScript.
     * This is used to schedule rendering of the component.
     */
    @Override
    protected String getMainComponentName() {
        return "ReactNativePayPalTest";
    }

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

    if (requestCode == MainApplication.PAY_PAL_REQUEST_ID) { // <--
        ((MainApplication) getApplication()).payPalPackage.handleActivityResult(requestCode, resultCode, data); // <--
    } else {
//        otherModulesHandlers(requestCode, resultCode, data);
    }
 }
}

<强> MainApplication.java

...

import android.app.Application;

import com.facebook.react.ReactApplication;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;

import java.util.Arrays;
import java.util.List;

import br.com.vizir.rn.paypal.PayPalPackage;

public class MainApplication extends Application implements ReactApplication {

  public static final int PAY_PAL_REQUEST_ID = 9; // <-- Can be any unique number
  public PayPalPackage payPalPackage;

  private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
    @Override
    protected boolean getUseDeveloperSupport() {
      return BuildConfig.DEBUG;
    }

    @Override
    protected List<ReactPackage> getPackages() {
      payPalPackage = new PayPalPackage(PAY_PAL_REQUEST_ID);
      return Arrays.<ReactPackage>asList(
          new MainReactPackage(),
          payPalPackage
      );
    }
  };

  @Override
  public ReactNativeHost getReactNativeHost() {
    return mReactNativeHost;
  }

  @Override
  public void onCreate() {
    super.onCreate();
    SoLoader.init(this, /* native exopackage */ false);
  }
}