Flutter calls WeChat payment

Fluwx It is a WeChat SDK plug-in, which allows developers to call  the native SDK of WeChat 

  • Share pictures, texts, music, videos and more. Support sharing to conversations, circle of friends and favorites.
  • WeChat Pay.
  • Get the Auth Code when logging in on WeChat.
  • Pull up the applet.
  • Subscribe to newsletter.
  • Open WeChat.
  • Open the app from the WeChat tab

 1. Add dependencies in pubspec.yaml

  # 微信SDK插件
  fluwx: ^3.8.1+1

get plugin

flutter pub get

2. Import the header file

import 'package:fluwx/fluwx.dart' as fluwx;

3. Wechat payment code implementation

  //注册微信
  void initWxApi() {
    fluwx.registerWxApi(
      appId: "开放平台appId",
      universalLink: "https://help.wechat.com/app",
    );
  }

  //微信支付
  Future<void> openWxPay() async {
    //是否安装微信
    bool isInstalled = await fluwx.isWeChatInstalled;
    if (!isInstalled) {
      Toast.showMsg("请先安装微信");
      return;
    }
    //调起支付
    fluwx.payWithWeChat(
      appId: "开放平台appId",
      partnerId: "partnerId",
      prepayId: "prepayId",
      packageValue: "packageValue",
      nonceStr: "nonceStr",
      timeStamp: 1597927308,
      sign: "sign",
    );
    //监听微信回调
    fluwx.weChatResponseEventHandler.listen((event) {
      if(event.isSuccessful) {
        Toast.showMsg("微信支付成功");
      } else {
        Toast.showMsg(event.errStr??"微信支付成功");
      }
    });
  }

Attached:

App call WeChat payment steps:

1. The user selects the product in the client APP, submits the order, selects WeChat payment, and initiates a payment request to the merchant's background.

2. The merchant background calls the WeChat payment API, sends a request to the WeChat payment system, and the WeChat payment system returns the prepaid receipt.

3. The merchant background regenerates the prepay_id returned by the prepayment slip according to the signature specification, and then transmits the data to the client.

4. The client displays the payment information, uses the SDK to invoke WeChat payment, and initiates a payment request to the WeChat payment system.

5. The WeChat payment system verifies the signature, and after judging the payment authority, returns the information to WeChat, and WeChat pops up a payment confirmation window, and the user enters and exits the password to confirm the payment.

6. After the payment is completed, the WeChat payment system will asynchronously notify the merchant of the background payment result, and the client executes the callback method to query the payment result from the background through the interface, and then display the payment result to the user

WeChat payment flow chart icon-default.png?t=M276https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_3 

  

Guess you like

Origin blog.csdn.net/lqw200931116/article/details/123819477