react-native-wxpay插件的使用(iOS)

功能:

通過微信SDK(v1.8.2)實(shí)現(xiàn)微信支付功能

使用步驟:

一、鏈接WXPay庫

參考:https://reactnative.cn/docs/0.50/linking-libraries-ios.html#content

手動添加:

1、添加react-native-wxpay插件到你工程的node_modules文件夾下
2、添加WXPay庫中的.xcodeproj文件在你的工程中
3、點(diǎn)擊你的主工程文件,選擇Build Phases,然后把剛才所添加進(jìn)去的.xcodeproj下的Products文件夾中的靜態(tài)庫文件(.a文件),拖到Link Binary With Libraries組內(nèi)。

自動添加:
npm install react-native-wxpay --save 
或
yarn add react-native-wxpay

react-native link

由于AppDelegate中使用WXPay庫,所以我們需要打開你的工程文件,選擇Build Settings,然后搜索Header Search Paths,然后添加庫所在的目錄 $(SRCROOT)/../node_modules/react-native-wxpay/ios/WXPay

二、開發(fā)環(huán)境配置

參考:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=1417694084&token=&lang=zh_CN

1、引入系統(tǒng)庫

左側(cè)目錄中選中工程名,在TARGETS->Build Phases-> Link Binary With Libaries中點(diǎn)擊“+”按鈕,在彈出的窗口中查找并選擇如下所需的庫,單擊“Add”按鈕,將庫文件添加到工程中。

  • SystemConfiguration.framework
  • libz.dylib
  • libsqlite3.0.dylib
  • libc++.dylib
  • Security.framework
  • CoreTelephony.framework
  • CFNetwork.framework

如圖所示:

2、環(huán)境配置

Xcode中,選擇你的工程設(shè)置項(xiàng),選中“TARGETS”一欄,在“info”標(biāo)簽欄的“URL type”添加“URL scheme”為你所注冊的應(yīng)用程序id

在工程文件中選擇Build Setting,在"Other Linker Flags"中加入"-Objc -all_load"

三、配置plist文件

iOS9中為了能正常調(diào)起微信支付的功能,必須在"Info.plist"中將微信的URL scheme列為白名單,否則無法調(diào)起,配置如下:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>weixin</string>
</array>

四、簡單使用

方法
Event Name Returns Notes
registerApp res 向微信終端程序注冊第三方應(yīng)用
isWXAppSupportApi res 判斷當(dāng)前微信的版本是否支持OpenApi
isWXAppSupport res 判斷當(dāng)前微信的版本是否支持該插件
isWXAppInstalled res 檢測是否已安裝微信
getApiVersion res 獲取當(dāng)前微信SDK的版本號
getWXAppInstallUrl res 獲取微信的itunes安裝地址
openWXApp res 打開微信
sendAuthReq res 發(fā)起認(rèn)證請求
pay res 支付
finishedPay res 支付結(jié)果監(jiān)聽事件
1、重寫AppDelegate的openURL方法:
#import "WXPay.h"

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
  if ([sourceApplication isEqualToString:@"com.tencent.xin"]) {
    return [[WXPay shareInstance] handleOpenURL: url];
  }else{
    return NO;
  }
}


// NOTE: 9.0以后使用新API接口
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
  if ([options[@"UIApplicationOpenURLOptionsSourceApplicationKey"]  isEqualToString:@"com.tencent.xin"]) {
    return [[WXPay shareInstance] handleOpenURL: url];
  }else{
    return NO;
  }
}
2、js文件
//index.ios.js
import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  Dimensions,
  AlertIOS,
  ScrollView,
  TouchableHighlight,
  NativeAppEventEmitter
} from 'react-native';

import WXPay from 'react-native-wxpay';

let appid = 'wxd930ea5d5a258f4f';

function show(title, msg) {
    AlertIOS.alert(title+'', msg+'');
}

export default class TextReactNative extends Component {
  componentDidMount() {
        this.registerApp();
        NativeAppEventEmitter.addListener(
            'finishedPay',
            (response) => {
                if (parseInt(response.errCode) === 0) {
                    alert('支付成功');
                }else if (parseInt(response.errCode) === -2){
                    alert('用戶取消');
                } else {
                    alert(response.strMsg);
                }
            }
        );
    }
    registerApp() {
        WXPay.registerApp(appid, (res) => {
            show('registerApp', res);
        });
    }
    isWXAppInstalled() {
        WXPay.isWXAppInstalled((res) => {
            show('isWXAppInstalled', res);
        });
    }
    isWXAppSupportApi() {
        WXPay.isWXAppSupportApi((res) => {
            show('isWXAppSupportApi', res);
        });
    }
    wechatPay() {
        WXPay.pay({
            'timestamp': '1412000000',
            'partnerid': '1900000109',
            'prepayid': 'WX1217752501201407033233368018',
            'noncestr': '5K8264ILTKCH16CQ2502SI8ZNMTM67VS',
            'package': 'Sign=WXPay',
            'sign': 'C380BEC2BFD727A4B6845133519F3AD6'
        },(res) => {
            
        });
    }
    render() {
        return (
            <ScrollView contentContainerStyle={styles.wrapper}>
                
                <Text style={styles.pageTitle}>WeChat SDK for React Native (iOS)</Text>

                <TouchableHighlight 
                    style={styles.button} underlayColor="#f38"
                    onPress={this.registerApp}>
                    <Text style={styles.buttonTitle}>registerApp</Text>
                </TouchableHighlight>
                
                <TouchableHighlight 
                    style={styles.button} underlayColor="#f38"
                    onPress={this.isWXAppInstalled}>
                    <Text style={styles.buttonTitle}>isWXAppInstalled</Text>
                </TouchableHighlight>

                <TouchableHighlight 
                    style={styles.button} underlayColor="#f38"
                    onPress={this.isWXAppSupportApi}>
                    <Text style={styles.buttonTitle}>isWXAppSupportApi</Text>
                </TouchableHighlight>

                <TouchableHighlight 
                    style={styles.button} underlayColor="#f38"
                    onPress={this.wechatPay}>
                    <Text style={styles.buttonTitle}>wechatPay</Text>
                </TouchableHighlight>
                
            </ScrollView>
        );
    }
}

const styles = StyleSheet.create({
  wrapper: {
        paddingTop: 60,
        paddingBottom: 20,
        alignItems: 'center',
    },
    pageTitle: {
        paddingBottom: 40
    },
    button: {
        width: 200,
        height: 40,
        marginBottom: 10,
        borderRadius: 6,
        backgroundColor: '#f38',
        alignItems: 'center',
        justifyContent: 'center',
    },
    buttonTitle: {
        fontSize: 16,
        color: '#fff'
    }
  
});

github鏈接:
https://github.com/zhoumeitong/react-native-wxpay

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,715評論 19 139
  • 功能: 通過高德地圖SDK實(shí)現(xiàn)定位、返回地理位置經(jīng)緯度坐標(biāo)、關(guān)鍵字檢索及周邊檢索功能。 使用步驟: 一、鏈接AMa...
    街角仰望閱讀 3,194評論 0 1
  • 有時候 在某一瞬間 經(jīng)歷的從前 仿佛晴天閃電 耀眼一現(xiàn) 那一幕幕定格的畫面 教人可笑又汗顏 有時候 站在現(xiàn)在 捧著...
    詩與生活閱讀 211評論 0 1
  • 這里有每個家庭需要的內(nèi)容,點(diǎn)藍(lán)字關(guān)注↑』 文/圖|大道 孩子,你是那么幸福! 從小,便是家里最閃亮的那顆星,所有人...
    大道聚焦閱讀 390評論 0 5

友情鏈接更多精彩內(nèi)容