Skip to main content

magic_payments 1.0.2 copy "magic_payments: ^1.0.2" to clipboard
magic_payments: ^1.0.2 copied to clipboard

Subscription billing for Magic apps: one entitlement answer across web checkout and App Store or Play purchases, with no rail logic in your widgets.

example/lib/main.dart

import 'package:flutter/widgets.dart';
import 'package:magic/magic.dart';
import 'package:magic_payments/magic_payments.dart';

const Color _ink = Color(0xFF111827);
const Color _accent = Color(0xFF4F46E5);
const TextStyle _text = TextStyle(color: _ink, fontSize: 16);

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await Magic.init(
    configs: [
      {
        'network': {
          'drivers': {
            'api': {
              // The backend that serves the /billing endpoints.
              'base_url': 'http://127.0.0.1:8000/api/v1',
            },
          },
        },
        'payments': {
          // `platform` is the only mode the package serves: the build decides
          // which driver answers.
          'driver': 'platform',
        },
        'app': {
          'providers': [
            (app) => NetworkServiceProvider(app),
            (app) => PaymentsServiceProvider(app),
          ],
        },
      },
    ],
  );

  runApp(const ExampleApp());
}

/// One page: the resolved rails, and a button that reads the entitlement.
class ExampleApp extends StatelessWidget {
  const ExampleApp({super.key});

  @override
  Widget build(BuildContext context) {
    return WidgetsApp(
      title: 'magic_payments example',
      color: _accent,
      textStyle: _text,
      pageRouteBuilder: <T>(RouteSettings settings, WidgetBuilder builder) {
        return PageRouteBuilder<T>(
          settings: settings,
          pageBuilder: (context, _, _) => builder(context),
        );
      },
      home: const BillingPage(),
    );
  }
}

class BillingPage extends StatefulWidget {
  const BillingPage({super.key});

  @override
  State<BillingPage> createState() => _BillingPageState();
}

class _BillingPageState extends State<BillingPage> {
  String _result = 'Not read yet.';

  Future<void> _readEntitlement() async {
    setState(() => _result = 'Reading...');

    try {
      final BillingEntitlement entitlement =
          await Payments.currentEntitlement();
      setState(() {
        _result =
            'plan: ${entitlement.plan}, status: ${entitlement.planStatus.name}, '
            'manage via: ${entitlement.manageVia.name}';
      });
    } on BillingException catch (error) {
      setState(() => _result = '${error.code.name}: ${error.message}');
    }
  }

  @override
  Widget build(BuildContext context) {
    // A rail is checked, never assumed: null is a build that cannot serve it.
    final bool hasWeb = Payments.web != null;
    final bool hasStore = Payments.store != null;

    return SafeArea(
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            Text('driver: ${Payments.billing.runtimeType}', style: _text),
            Text(
              'payments.driver: ${Config.get<String>('payments.driver')}',
              style: _text,
            ),
            Text('web rail: ${hasWeb ? 'present' : 'absent'}', style: _text),
            Text(
              'store rail: ${hasStore ? 'present' : 'absent'}',
              style: _text,
            ),
            const SizedBox(height: 16),
            _Button(label: 'Read entitlement', onTap: _readEntitlement),
            const SizedBox(height: 16),
            Text(_result, style: _text),
          ],
        ),
      ),
    );
  }
}

/// A button that takes focus, activates on Enter and Space, and shows a border
/// while it is focused, so the page works from the keyboard as well as a tap.
class _Button extends StatefulWidget {
  const _Button({required this.label, required this.onTap});

  final String label;
  final VoidCallback onTap;

  @override
  State<_Button> createState() => _ButtonState();
}

class _ButtonState extends State<_Button> {
  bool _focused = false;

  @override
  Widget build(BuildContext context) {
    return FocusableActionDetector(
      mouseCursor: SystemMouseCursors.click,
      onShowFocusHighlight: (bool value) => setState(() => _focused = value),
      actions: <Type, Action<Intent>>{
        ActivateIntent: CallbackAction<ActivateIntent>(
          onInvoke: (ActivateIntent intent) {
            widget.onTap();

            return null;
          },
        ),
      },
      child: GestureDetector(
        onTap: widget.onTap,
        child: Container(
          padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
          decoration: BoxDecoration(
            color: _accent,
            border: Border.all(
              color: _focused ? _ink : const Color(0x00000000),
              width: 2,
            ),
          ),
          child: Text(
            widget.label,
            style: const TextStyle(color: Color(0xFFFFFFFF)),
          ),
        ),
      ),
    );
  }
}
0
likes
160
points
4.43k
downloads

Documentation

API reference

Publisher

verified publisheranilcancakir.com

Weekly Downloads

Subscription billing for Magic apps: one entitlement answer across web checkout and App Store or Play purchases, with no rail logic in your widgets.

Homepage
Repository (GitHub)
View/report issues

Topics

#payments #billing #in-app-purchase #magic-framework #flutter-plugin

License

MIT (license)

Dependencies

dowel, flutter, magic, purchases_flutter

More

Packages that depend on magic_payments