@gyjshow/react-native-wechat
v1.0.8
Published
待测试验证!!! React Native WeChat plugin for WeChat Pay and Share (iOS & Android)
Downloads
851
Maintainers
Readme
@gyjshow/react-native-wechat
A lightweight React Native plugin for integrating WeChat Pay & WeChat Share on both iOS and Android.
Supports:
- ✅ WeChat Pay
- ✅ Share Text
- ✅ Share Webpage
- ✅ Share Image
- ✅ Share to Friends & Moments
- ✅ Get WeChat SDK Version
Compatible with React Native >= 0.83
Features
- 🔹 Unified API for iOS & Android
- 🔹 Supports WeChat Pay
- 🔹 Share text / webpage / image
- 🔹 Share to friends (
session) & Moments (timeline) - 🔹 Fetch WeChat SDK version
- 🔹 Lightweight & easy to integrate
Installation
# Using npm
npm install @gyjshow/react-native-wechat
# Using pnpm
pnpm add @gyjshow/react-native-wechatPlatform Setup
iOS Setup
1️⃣ Add URL Scheme (WeChat AppID)
Open ios/YourProject/Info.plist and add:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>wx1234567890abcdef</string>
</array>
</dict>
</array>⚠️ Replace wx1234567890abcdef with your real WeChat AppID.
2️⃣ Install Pods
cd ios
pod installAndroid Setup
No additional configuration required beyond normal React Native setup.
Make sure:
- Your AppID is registered in WeChat Open Platform
- You are using official SDK version
6.8.0 - Your package name matches the one registered in WeChat
Usage
Import
import WeChat from '@gyjshow/react-native-wechat';Register App (Required)
You must register your AppID before using any feature.
WeChat.registerApp('wx1234567890abcdef');Call this once when your app starts.
WeChat Pay
WeChat Pay requires server-side order generation.
const payParams = {
appId: 'wx123456',
partnerId: '1900000109',
prepayId: 'wx201411101639507cbf6ffd8b0779950874',
nonceStr: '1101000000140429eb40476f8896f4c9',
timeStamp: '1398746574',
package: 'Sign=WXPay',
sign: 'C380BEC2BFD727A4B6845133519F3AD6'
};
try {
const result = await WeChat.wxpay(payParams);
console.log('Pay result:', result);
} catch (error) {
console.error('Pay failed:', error);
}Share Text
await WeChat.shareText('Hello WeChat!', 'session'); // Share to friend
await WeChat.shareText('Hello Moments!', 'timeline'); // Share to MomentsShare Webpage
await WeChat.shareWebpage(
'https://your-website.com',
'Website Title',
'Website description',
null, // optional thumbnail (base64, ≤32KB)
'session'
);Share Image
await WeChat.shareImage(base64ImageString, 'timeline');Image must be Base64 format.
Get WeChat SDK Version
const version = await WeChat.getVersion();
console.log('WeChat SDK version:', version);API Reference
| Method | Params | Returns | Description |
|--------|--------|---------|------------|
| registerApp(appId: string) | WeChat AppID | void | Register WeChat App |
| pay(params) | WeChat pay params | Promise<string> | Initiate WeChat payment |
| shareText(text, scene) | text, session / timeline | Promise<string> | Share text |
| shareWebpage(url, title, desc, thumbBase64, scene) | webpage params | Promise<string> | Share webpage |
| shareImage(base64, scene) | base64 image | Promise<string> | Share image |
| getVersion() | - | Promise<string> | Get WeChat SDK version |
Example
import React, { useEffect } from 'react';
import { Button, View, Alert } from 'react-native';
import WeChat from '@gyjshow/react-native-wechat';
export default function App() {
useEffect(() => {
WeChat.registerApp('wx1234567890abcdef');
}, []);
const handlePay = async () => {
try {
const result = await WeChat.pay({
appId: 'wx123456',
partnerId: '1900000109',
prepayId: 'prepay_id',
nonceStr: 'nonce',
timeStamp: '1234567890',
package: 'Sign=WXPay',
sign: 'xxxx'
});
Alert.alert('Pay Result', result);
} catch (e: any) {
Alert.alert('Pay Failed', e.message);
}
};
return (
<View>
<Button title="Pay with WeChat" onPress={handlePay} />
</View>
);
}Notes
- You must register your app in WeChat Open Platform.
- WeChat Pay requires backend order generation.
- Thumbnail size must be ≤ 32KB (WeChat official requirement).
- Ensure your AppID matches your bundle identifier (iOS) and package name (Android).
- Test in sandbox before production.
License
MIT
