h5pay 0.5.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 64

h5pay #

build status pub package

A H5 payment (such as Alipay, WeChat Pay) plugin for flutter.

Usage #

Use the showH5PayDialog method to show a loading dialog and jump to payment app. When user switches from payment app back to your app, you can check payment result with your server in the verifyResult callback (Optional).

import 'package:h5pay/h5pay.dart';

final PaymentStatus status = await showH5PayDialog(
  context: context,
  // You can get payment url (normally is http or payment app scheme) from server in the getPaymentArguments callback
  getPaymentArguments: () async => PaymentArguments(
    url: 'https://is.gd/4cLE6j',
    redirectSchemes: ['alipay', 'alipays', 'weixin', 'wechat'],
  ),
  verifyResult: () async => true, // check order result with your server
);
if (status == PaymentStatus.success) {
  // Do something
}

Values of PaymentStatus:

enum PaymentStatus {
  idle,
  gettingArguments,
  getArgumentsFail,
  launchingUrl,
  cantLaunchUrl, // Maybe target payment app is not installed
  launchUrlTimeout, // Maybe redirecting url is fail
  jumping,
  jumpTimeout,
  verifying,
  success,
  fail,
}

Notes #

  • In iOS, for allowing to jump to the payment app from your app, you must add schemes of the payment apps into the Info.plist file. Just like:
<key>LSApplicationQueriesSchemes</key>
<array>
	<string>wechat</string>
	<string>weixin</string>
	<string>alipay</string>
	<string>alipays</string>
</array>

Advanced #

If you have more complex requirements, you can use the H5PayWidget. Check the example for more detail.

0.5.0 #

  • Refactor: getPaymentUrl => getPaymentArguments

0.4.2 #

  • Just update the README

0.4.1 #

  • Revert: use future to get payment url

0.4.0 #

  • Replace the 'getH5Url' argument with 'paymentUrl'
  • Expose some util methods from platform plugins

0.2.0 #

  • Handle unmounted state of widget

0.1.0 #

  • Initial release

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:h5pay/h5pay.dart';

void main() => runApp(MaterialApp(home: IndexPage()));

class IndexPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Example app'),
      ),
      body: Center(
        child: FlatButton(
          child: Text('Plugin Test'),
          textColor: Colors.white,
          color: Colors.blue,
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => MainPage()),
            );
          },
        ),
      ),
    );
  }
}

class MainPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Plugin Test'),
      ),
      body: MainBody(),
    );
  }
}

class MainBody extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          H5PayWidget(
            getPaymentArguments: () async => PaymentArguments(
              url: 'https://is.gd/4cLE6j',
              redirectSchemes: ['sms'],
            ),
            verifyResult: () async => true,
            builder: (context, status, controller) => FlatButton(
              textColor: Colors.white,
              color: Colors.blue,
              onPressed: status != PaymentStatus.gettingArguments &&
                      status != PaymentStatus.launchingUrl &&
                      status != PaymentStatus.jumping &&
                      status != PaymentStatus.verifying
                  ? () async {
                      controller.launch();
                    }
                  : null,
              child: Text(status.toString()),
            ),
          ),
          SizedBox(height: 10),
          FlatButton(
            textColor: Colors.white,
            color: Colors.blue,
            onPressed: () async {
              final status = await showH5PayDialog(
                context: context,
                getPaymentArguments: () async => PaymentArguments(
                  url: 'https://is.gd/4cLE6j',
                  redirectSchemes: ['sms'],
                ),
                verifyResult: () async => true,
              );
              Scaffold.of(context).showSnackBar(SnackBar(
                content: Text(status.toString()),
              ));
            },
            child: Text('showH5PayDialog'),
          ),
        ],
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  h5pay: ^0.5.0

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:h5pay/h5pay.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
29
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
64
Learn more about scoring.

We analyzed this package on Mar 31, 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 issues and suggestions

Document public APIs. (-0.23 points)

37 out of 38 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Format lib/src/h5pay_channel.dart.

Run flutter format to format lib/src/h5pay_channel.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.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
Dev dependencies
flutter_test