Skip to content

Plaud-AI/flutter_wechatpay_plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

flutter_wechatpay_plugin

A Flutter plugin for WeChat Pay payment integration, supporting Android platform.

Features

  • ✅ Android platform WeChat Pay payment integration
  • ✅ WeChat app installation detection
  • ✅ Payment result callback handling
  • ✅ Order query functionality
  • ❌ iOS platform (not supported yet)

Installation

Add dependency to your pubspec.yaml file:

dependencies:
  flutter_wechatpay_plugin: ^0.0.1

Usage

1. Initialize WeChat Pay

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
);

2. Register app for WeChat

final success = await wechatPayPlugin.registerApp(
  appId: 'your_app_id',
  universalLink: 'your_universal_link', // optional
);

3. Check if WeChat is installed

final isInstalled = await wechatPayPlugin.isWechatInstalled();
if (isInstalled) {
  print('WeChat is installed');
} else {
  print('WeChat is not installed');
}

4. Initiate payment

// 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']}');
}

5. Query order

final result = await wechatPayPlugin.queryOrder(
  orderId: 'your_order_id',
);

print('Query result: $result');

WeChat Pay Parameters

The payment parameters are generated by your server and should follow WeChat Pay's official documentation:

  • partnerId: Your WeChat Pay partner ID
  • prepayId: Prepayment ID returned by WeChat Pay server
  • packageValue: Usually "Sign=WXPay"
  • nonceStr: Random string for security
  • timeStamp: Current timestamp
  • sign: Signature generated by your server

Android Configuration

1. Add permissions

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" />

2. Add WeChat Activities

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" />

3. Handle WeChat callback

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
                }
            }
        }
    })
}

Notes

  1. Server Integration: This plugin only provides client-side integration, payment parameters need to be generated by the server
  2. Signature Verification: Payment completion requires signature verification on the server
  3. Test Environment: Use WeChat Pay sandbox environment for development testing
  4. Production Environment: Apply for formal application on WeChat Pay Open Platform before going live
  5. Order Query: Order query functionality requires server-side implementation using WeChat Pay's order query API
  6. iOS Support: iOS support is planned for future versions

License

MIT License

Copyright

Copyright (c) 2025 PlaudAI. All rights reserved.

Author

Neo.Wang@plaud.ai

WeChat Pay Documents

微信支付开放平台文档

微信支付 Android SDK 文档

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •