Flutter在线支付插件instamojo的使用

Flutter在线支付插件instamojo的使用

Instamojo插件

License pub package

用于在您的Flutter应用中实现Instamojo支付网关的插件。

注意:此插件仍在积极开发中,某些功能可能尚不可用。我们正在努力添加更多功能。 反馈和Pull请求总是受欢迎的。

注意:对于Android和iOS构建导入:

import 'package:instamojo/instamojo.dart';

特性

  • ✅ 基于Instamojo API
  • ✅ 支持空安全(Null Safety)
  • ✅ 支持流式支付状态并带有适当的消息
  • ✅ 支持UPI支付
  • ✅ 登录错误带适当的错误码
  • ✅ 支持所有支付选项,包括钱包
  • ✅ 支持iOS和Android
  • ✅ 支持Web
  • ✅ 自定义样式
  • ❌ UI改进
  • ❌ UPI应用集成

安装

首先,在pubspec.yaml文件中添加以下依赖:

dependencies:
  instamojo: ^1.0.0+2

注意:在实时模式下,SDK服务器设置是必需的。您可以找到以下示例SDK服务器代码。

示例

Instamojo屏幕用于测试模式下的支付初始化

class InstamojoScreen extends StatefulWidget {
  final CreateOrderBody? body;
  final String? orderCreationUrl;
  final bool? isLive;

  const InstamojoScreen(
      {Key? key, this.body, this.orderCreationUrl, this.isLive = false})
      : super(key: key);

  @override
  _InstamojoScreenState createState() => _InstamojoScreenState();
}

class _InstamojoScreenState extends State<InstamojoScreen>
    implements InstamojoPaymentStatusListener {
  @override
  Widget build(BuildContext context) {
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
    ]);
    return Scaffold(
        appBar: AppBar(
          title: const Text('Instamojo Flutter'),
        ),
        body: SafeArea(
            child: FlutterInstamojo(
          isConvenienceFeesApplied: false,
          listener: this,
          environment: widget.isLive! ? Environment.PRODUCTION : Environment.TEST,
          apiCallType: ApiCallType.createOrder(
              createOrderBody: widget.body,
              orderCreationUrl: widget.orderCreationUrl),
          stylingDetails: StylingDetails(
              buttonStyle: ButtonStyling(
                  buttonColor: Colors.amber,
                  buttonTextStyle: const TextStyle(
                    color: Colors.black,
                  )),
              listItemStyle: ListItemStyle(
                  borderColor: Colors.grey,
                  textStyle: const TextStyle(color: Colors.black, fontSize: 18),
                  subTextStyle: const TextStyle(color: Colors.grey, fontSize: 14)),
              loaderColor: Colors.amber,
              inputFieldTextStyle: InputFieldTextStyle(
                  textStyle: const TextStyle(color: Colors.black, fontSize: 18),
                  hintTextStyle: const TextStyle(color: Colors.grey, fontSize: 14),
                  labelTextStyle: const TextStyle(color: Colors.grey, fontSize: 14)),
              alertStyle: AlertStyle(
                headingTextStyle: const TextStyle(color: Colors.black, fontSize: 14),
                messageTextStyle: const TextStyle(color: Colors.black, fontSize: 12),
                positiveButtonTextStyle:
                    const TextStyle(color: Colors.redAccent, fontSize: 10),
                negativeButtonTextStyle:
                    const TextStyle(color: Colors.amber, fontSize: 10),
              )),
        )));
  }

  @override
  void paymentStatus({Map<String, String>? status}) {
    Navigator.pop(context, status);
  }
}

Instamojo屏幕用于生产模式下的支付初始化

对于生产模式,需要调用ApiCallType.startPayment(orderId: "")

class InstamojoScreen extends StatefulWidget {
  final CreateOrderBody? body;
  final String? orderCreationUrl;
  final bool? isLive;
  final String name;
  final String orderId;

  const InstamojoScreen(
      {Key? key, this.body, this.orderCreationUrl, this.isLive = false, this.name, this.orderId})
      : super(key: key);

  @override
  _InstamojoScreenState createState() => _InstamojoScreenState();
}

class _InstamojoScreenState extends State<InstamojoScreen>
    implements InstamojoPaymentStatusListener {
  @override
  Widget build(BuildContext context) {
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
    ]);
    return Scaffold(
        appBar: AppBar(
          title: const Text('Instamojo Flutter'),
        ),
        body: SafeArea(
            child: FlutterInstamojo(
          isConvenienceFeesApplied: false,
          listener: this,
          environment: Environment.PRODUCTION,
          apiCallType: ApiCallType.startPayment(orderId: widget.orderId), 
          stylingDetails: StylingDetails(
              buttonStyle: ButtonStyling(
                  buttonColor: Colors.amber,
                  buttonTextStyle: const TextStyle(
                    color: Colors.black,
                  )),
              listItemStyle: ListItemStyle(
                  borderColor: Colors.grey,
                  textStyle: const TextStyle(color: Colors.black, fontSize: 18),
                  subTextStyle: const TextStyle(color: Colors.grey, fontSize: 14)),
              loaderColor: Colors.amber,
              inputFieldTextStyle: InputFieldTextStyle(
                  textStyle: const TextStyle(color: Colors.black, fontSize: 18),
                  hintTextStyle: const TextStyle(color: Colors.grey, fontSize: 14),
                  labelTextStyle: const TextStyle(color: Colors.grey, fontSize: 14)),
              alertStyle: AlertStyle(
                headingTextStyle: const TextStyle(color: Colors.black, fontSize: 14),
                messageTextStyle: const TextStyle(color: Colors.black, fontSize: 12),
                positiveButtonTextStyle:
                    const TextStyle(color: Colors.redAccent, fontSize: 10),
                negativeButtonTextStyle:
                    const TextStyle(color: Colors.amber, fontSize: 10),
              )),
        )));
  }

  @override
  void paymentStatus({Map<String, String>? status}) {
    print(status);
  }
}

仅用于测试目的的支付调用

startInstamojo() async {
  dynamic result = await Navigator.push(
      context,
      MaterialPageRoute(
          builder: (ctx) => InstamojoScreen(
                isLive: false,
                body: CreateOrderBody(
                    buyerName: "EvilRAT Technologies",
                    buyerEmail: "ceo@evilrattechnologies.com",
                    buyerPhone: "+91 7004491831",
                    amount: "300",
                    description: "Test Payment"),
                orderCreationUrl:
                    "https://sample-sdk-server.instamojo.com/order", // The sample server of instamojo to create order id.
              )));

  setState(() {
    _paymentResponse = result.toString();
  });
}

用于生产模式的支付调用

startInstamojo() async {
  dynamic result = await Navigator.push(
      context,
      MaterialPageRoute(
          builder: (ctx) => InstamojoScreen(
                isLive: true,
                name: widget.package["name"].toString(),
                orderId: orderId,
                isLive: true,
              )
          )
      );

  setState(() {
    _paymentResponse = result.toString();
  });
}

更多关于Flutter在线支付插件instamojo的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter在线支付插件instamojo的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


在Flutter中使用Instamojo插件进行在线支付,你可以按照以下步骤进行集成和使用。Instamojo是一个流行的支付网关,支持多种支付方式,包括信用卡、借记卡、网上银行和UPI等。

1. 安装插件

首先,你需要在pubspec.yaml文件中添加instamojo_flutter插件的依赖。

dependencies:
  flutter:
    sdk: flutter
  instamojo_flutter: ^2.0.0  # 请检查最新版本

然后运行flutter pub get来安装依赖。

2. 配置Instamojo

在使用Instamojo之前,你需要在Instamojo官网上注册一个账户,并创建一个API密钥和认证令牌(Auth Token)。

3. 初始化Instamojo

在你的Flutter应用中,初始化Instamojo插件。通常你可以在main.dart或某个初始化文件中进行初始化。

import 'package:instamojo_flutter/instamojo_flutter.dart';

void main() {
  InstamojoFlutter.init(
    apiKey: 'YOUR_API_KEY',
    authToken: 'YOUR_AUTH_TOKEN',
    isLive: true,  // 设置为false使用沙盒环境
  );
  runApp(MyApp());
}

4. 创建支付请求

接下来,你可以创建一个支付请求并启动支付流程。

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

class PaymentPage extends StatelessWidget {
  void startPayment() async {
    try {
      InstamojoResponse response = await InstamojoFlutter.startPayment(
        amount: '100',  // 支付金额
        purpose: 'Test Payment',  // 支付目的
        buyerName: 'John Doe',  // 买家姓名
        email: 'john.doe@example.com',  // 买家邮箱
        phone: '9876543210',  // 买家电话
        redirectUrl: 'https://your-redirect-url.com',  // 支付成功后的重定向URL
      );

      if (response.status == 'success') {
        // 支付成功
        print('Payment Successful: ${response.paymentId}');
      } else {
        // 支付失败
        print('Payment Failed: ${response.message}');
      }
    } catch (e) {
      // 处理异常
      print('Error: $e');
    }
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Instamojo Payment'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: startPayment,
          child: Text('Pay with Instamojo'),
        ),
      ),
    );
  }
}
回到顶部