flutter_alipay 0.1.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 86

flutter_alipay #

A flutter plugin to use alipay.

有任何问题,欢迎加入qq群854192563交流

Features #

Install #

Add this to your package's pubspec.yaml file:

dependencies:
  flutter_alipay: "^0.1.0"

Getting Started #

  • Android

  • Add following permissions to your 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" />

  • ios

  • Add a URL scheme in info.plist

    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLName</key>
            <string>alipay</string>
            <key>CFBundleTypeRole</key>
            <string>Editor</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>YOUR APP SCHEME NAME</string>
            </array>
        </dict>
    </array>

Make sure you have a CFBundleURLName=alipay in CFBundleURLTypes.

  • In AppDelegate.m, do header import
 #import "FlutterAlipayPlugin.h"

and add following code

     // ios 8.x or older
-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{

    return [FlutterAlipayPlugin handleOpenURL:url];
}
// ios 9.0+
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
     return [FlutterAlipayPlugin handleOpenURL:url];
}

How to use #

import 'package:flutter_alipay/flutter_alipay.dart';
var result = await FlutterAlipay.pay("you pay info from server");

Using flutter_alipay in Swift #

Edit Runner-Bridging-Header.h,add

#import <flutter_alipay/FlutterAlipayPlugin.h>

Edit AppDelegate.swift,add


 override func application(_ application: UIApplication, handleOpen url: URL) -> Bool {
        return FlutterAlipayPlugin.handleOpen(url);
    }

[0.1.1] #

  • 使用强类型保存支付结果

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_alipay/flutter_alipay.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _payInfo = "";
  AlipayResult _payResult;

  final myController = new TextEditingController();

  void _loadData(){
    _payInfo = "";
    _payResult = null;

    http
        .post("http://120.79.190.42:8071/pay/test_pay/create",
        body: json.encode({"fee": 1, "title": "test pay"}))
        .then((http.Response response) {
      if (response.statusCode == 200) {
        print(response.body);
        var map = json.decode(response.body);
        int flag = map["flag"];
        if (flag == 0) {
          var result = map["result"];
          setState(() {
            _payInfo = result["credential"]["payInfo"];
            myController.text = _payInfo;
          });
          return;
        }
      }
      throw new Exception("创建订单失败");
    }).catchError((e) {
      setState(() {
        _payInfo = e.toString();
        myController.text = _payInfo;
      });
    });

    setState(() {

    });
  }

  @override
  initState() {
    super.initState();

    _loadData();
  }

  onChanged(String value) {
    _payInfo = value;
  }

  callAlipay() async {
    dynamic payResult;
    try {
      print("The pay info is : " + _payInfo);
      payResult = await FlutterAlipay.pay(_payInfo);
    } on Exception catch (e) {
      payResult = null;
    }

    if (!mounted) return;

    setState(() {
      _payResult = payResult;
    });
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text('Alipay example'),
        ),
        body: new SingleChildScrollView(
          child: new Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              new Text("输入调用字符串,如何生成请查阅支付宝官方文档:https://docs.open.alipay.com/"),
              new TextField(
                  maxLines: 15, onChanged: onChanged, controller: myController),
              new RaisedButton(onPressed: callAlipay, child: new Text("调用支付宝")),
              new RaisedButton(onPressed: (){

                _loadData();

              }, child: new Text("重新下单")),
              new Text(_payResult == null ? "" : _payResult.toString())
            ],
          ),
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutter_alipay: ^0.1.2

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_alipay/flutter_alipay.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
79
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
86
Learn more about scoring.

We analyzed this package on Mar 27, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Health suggestions

Format lib/flutter_alipay.dart.

Run flutter format to format lib/flutter_alipay.dart.

Maintenance suggestions

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.48.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8