A Flutter plugin for WeChat Pay payment integration, supporting Android platform.
- ✅ Android platform WeChat Pay payment integration
- ✅ WeChat app installation detection
- ✅ Payment result callback handling
- ✅ Order query functionality
- ❌ iOS platform (not supported yet)
Add dependency to your pubspec.yaml
file:
dependencies:
flutter_wechatpay_plugin: ^0.0.1
import 'package:flutter_wechatpay_plugin/flutter_wechatpay_plugin.dart';
final wechatPayPlugin = FlutterWechatpayPlugin();
// Initialize WeChat Pay
final success = await wechatPayPlugin.initWechatPay(
appId: 'your_app_id',
partnerId: 'your_partner_id',
universalLink: 'your_universal_link', // optional
);
final success = await wechatPayPlugin.registerApp(
appId: 'your_app_id',
universalLink: 'your_universal_link', // optional
);
final isInstalled = await wechatPayPlugin.isWechatInstalled();
if (isInstalled) {
print('WeChat is installed');
} else {
print('WeChat is not installed');
}
// Payment parameters are usually generated by server
final result = await wechatPayPlugin.pay(
partnerId: 'your_partner_id',
prepayId: 'your_prepay_id',
packageValue: 'Sign=WXPay',
nonceStr: 'your_nonce_str',
timeStamp: 'your_timestamp',
sign: 'your_sign',
);
// Handle payment result
if (result['success'] == true) {
print('Payment request sent successfully');
} else {
print('Payment failed: ${result['message']}');
}
final result = await wechatPayPlugin.queryOrder(
orderId: 'your_order_id',
);
print('Query result: $result');
The payment parameters are generated by your server and should follow WeChat Pay's official documentation:
partnerId
: Your WeChat Pay partner IDprepayId
: Prepayment ID returned by WeChat Pay serverpackageValue
: Usually "Sign=WXPay"nonceStr
: Random string for securitytimeStamp
: Current timestampsign
: Signature generated by your server
Add to android/app/src/main/AndroidManifest.xml
:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Add to <application>
tag in android/app/src/main/AndroidManifest.xml
:
<activity
android:name="com.tencent.mm.opensdk.openapi.WXEntryActivity"
android:exported="true"
android:launchMode="singleTop"
android:taskAffinity="com.tencent.mm"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<activity
android:name="com.tencent.mm.opensdk.openapi.WXPayEntryActivity"
android:exported="true"
android:launchMode="singleTop"
android:taskAffinity="com.tencent.mm"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
In your main activity, handle WeChat callback:
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
setIntent(intent)
// Handle WeChat callback
val wxApi = WXAPIFactory.createWXAPI(this, "your_app_id", false)
wxApi.handleIntent(intent, object : IWXAPIEventHandler {
override fun onReq(baseReq: BaseReq) {
// Handle request from WeChat
}
override fun onResp(baseResp: BaseResp) {
// Handle response from WeChat
when (baseResp.type) {
ConstantsAPI.COMMAND_PAY_BY_WX -> {
val payResp = baseResp as PayResp
// Handle payment result
}
}
}
})
}
- Server Integration: This plugin only provides client-side integration, payment parameters need to be generated by the server
- Signature Verification: Payment completion requires signature verification on the server
- Test Environment: Use WeChat Pay sandbox environment for development testing
- Production Environment: Apply for formal application on WeChat Pay Open Platform before going live
- Order Query: Order query functionality requires server-side implementation using WeChat Pay's order query API
- iOS Support: iOS support is planned for future versions
MIT License
Copyright (c) 2025 PlaudAI. All rights reserved.