【React Native】react-native之集成支付宝支付、微信支付
一、在使用支付宝支付、微信支付之前导入桥接好的头文件
github地址:https://github.com/xujianfu/react-native-pay
二、集成支付宝支付流程
RN支付宝需要分别对iOS(Xcode)和安卓(AS)配置,与原生app相比只是将支付操作放在了RN当中。
流程大同小异,一般都是从后台获取支付字符串,然后传递支付字符串调用支付宝SDK,SDK再调用支付宝的支付模块。如果用户已安装支付宝App,会跳转到支付宝支付。如果用户没有安装支付宝App,商家App内会调起支付宝网页支付收银台,用户登录支付宝账户支付。
1、导入AlipaySDK(下载并引入)
下载AlipaySDK地址:https://docs.open.alipay.com/54/104509
2、iOS(Xcode的配置)
1)引入依赖库
2)配置URL Types
3、创建Alipay.js文件
import { NativeModules } from 'react-native'; module.exports = NativeModules.ReactNativePay;
4、在要支付界面引入Alipay.js文件
import Alipay from './Alipay'; export default class App extends Component<Props> { render() { return ( <View style={styles.container}> <TouchableOpacity onPress={()=>this.aliPayAction("app_id=2015052600090779&biz_content=%7B%22timeout_express%22%3A%2230m%22%2C%22seller_id%22%3A%22%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%2C%22total_amount%22%3A%220.02%22%2C%22subject%22%3A%221%22%2C%22body%22%3A%22%E6%88%91%E6%98%AF%E6%B5%8B%E8%AF%95%E6%95%B0%E6%8D%AE%22%2C%22out_trade_no%22%3A%22314VYGIAGG7ZOYY%22%7D&charset=utf-8&method=alipay.trade.app.pay&sign_type=RSA2×tamp=2016-08-15%2012%3A12%3A15&version=1.0&sign=MsbylYkCzlfYLy9PeRwUUIg9nZPeN9SfXPNavUCroGKR5Kqvx0nEnd3eRmKxJuthNUx4ERCXe552EV9PfwexqW%2B1wbKOdYtDIb4%2B7PL3Pc94RZL0zKaWcaY3tSL89%2FuAVUsQuFqEJdhIukuKygrXucvejOUgTCfoUdwTi7z%2BZzQ%3D")}> <Text>点击支付</Text> </TouchableOpacity> </View> ); } aliPayAction(payStr) { Alipay.onAliPay(payStr) .then((message)=>{ console.log("message" + message); if(message !== "") //支付成功的处理 this.refs.toast.show(message, DURATION.LENGTH_SHORT); }) .catch(e=>{ console.log("e.message" + e.message); if(e.message !== "") this.refs.toast.show(e.message, DURATION.LENGTH_SHORT); if(e.code === '-1' || e.message === '支付失败') { //支付失败 } }) } }
三、集成微信支付
1、导入微信支付SDK
下载微信支付SDK地址:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=11_1
2、具体配置,参考API文档就行,不多做描述了。
3、具体使用
// 微信支付 wxPayAction(){ this.setState({payInfo:Testing.getTestPay(responseJson.data)});//转成payInfo模型 let sign = this.getPaySignStrMethod(this.state.payInfo); if(sign==null){ this.refs.toast.show("支付信息请求错误", DURATION.LENGTH_SHORT); return; } var params = { partnerId:this.state.payInfo.partnerId, prepayId:this.state.payInfo.prepayId, package:this.state.payInfo.package, nonceStr:this.state.payInfo.nonceStr, timeStamp:this.state.payInfo.timeStamp, sign:sign, } Pay.onWxPay(params) .then((message)=>{ console.log("message" + message); if(message !== "") this.refs.toast.show(message, DURATION.LENGTH_SHORT); //支付成功的处理 }).catch(e=>{ console.log("e.message" + e.message); if(e.message !== "") this.refs.toast.show(e.message, DURATION.LENGTH_SHORT); if(e.code === '-1' || e.message === '支付失败') { //支付失败的处理 } }); } getPaySignStrMethod=(payInfo)=>{ if(payInfo.appId !== undefined && payInfo.appId !== '' && payInfo.nonceStr !== undefined && payInfo.nonceStr !== '' && payInfo.partnerId !== undefined && payInfo.partnerId !== '' && payInfo.prepayId !== undefined && payInfo.prepayId !== '' && payInfo.timeStamp !== undefined && payInfo.timeStamp !== '') { return "appid=" + payInfo.appId + "&noncestr=" + payInfo.nonceStr + "&package=" + payInfo.package + "&partnerid=" + payInfo.partnerId + "&prepayid=" + payInfo.prepayId + "×tamp=" + payInfo.timeStamp + "&key=" + C.weChatPayKey; }else { return null } }