react-native 支付宝手机支付模块
##iOS
npm install https://github.com/mingkang1993/rn-walipay.git --save
- 去官方下ios android 包,放入 android/libs下面
- 在XCode中右击项目的
Libraries
文件夹 ➜Add Files to
- 进入
node_modules
➜rn-alipay
➜ios
➜ 选择RNAlipay.xcodeproj
- 展开
RNAlipay.xcodeproj
➜Products
➜ 添加libRNAlipay.a
到Build Phases -> Link Binary With Libraries
- 在
Build Phases
选项卡的Link Binary With Libraries
中,点击“+”号增加以下依赖: - 将
RNAlipay.xcodeproj
下AlipaySDK.framework
、libssl.a
、libcrypto.a
文件拖入复制到项目文件夹下: - 编译运行
##Android
npm install https://github.com/mingkang1993/rn-walipay.git --save
// file: android/settings.gradle
...
include ':rn-alipay' // <- add
project(':rn-alipay').projectDir = new File(settingsDir, '../node_modules/rn-alipay/android') // <- add
// file: android/app/build.gradle
...
dependencies {
...
compile project(':rn-alipay') // <- add
}
- 注册模块
- 对于 react-native 版本低于 0.19.0 (use cat ./node_modules/react-native/package.json | grep version)
// file: MainActivity.java
import com.alipay.RNAlipayPackage; // <- import
public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mReactRootView = new ReactRootView(this);
mReactInstanceManager = ReactInstanceManager.builder()
.setApplication(getApplication())
.setBundleAssetName("index.android.bundle")
.setJSMainModuleName("index.android")
.addPackage(new MainReactPackage())
.addPackage(new RNAlipayPackage(this)) // <- add package
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState(LifecycleState.RESUMED)
.build();
mReactRootView.startReactApplication(mReactInstanceManager, "ExampleRN", null);
setContentView(mReactRootView);
}
...
}
- 对于 react-native 0.19.0及以上版本
// file: MainActivity.java
...
import com.alipay.RNAlipayPackage;//<- import package
public class MainActivity extends ReactActivity {
/**
* A list of packages used by the app. If the app uses additional views
* or modules besides the default ones, add more packages here.
*/
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(), //<- Add comma
new RNAlipayPackage(this) //<- Add package
);
}
...
}
##使用说明
- 导入模块
const Alipay = require('rn-walipay');
- 基本使用
const data = {
payInfo,
appSchemeIOS: 'testapp20', //应用注册scheme,Info.plist定义URL types
};
Alipay.pay(data).then((msg) => {
console.log(msg);
}, (e) => {
console.log(e);
});
##参考支付宝文档