Flutter支付集成插件payumoney_pro_unofficial的使用

发布于 1周前 作者 ionicwang 来自 Flutter

Flutter PayUMoney Pro SDK

Flutter Plugin to implement PayUMoney Latest CheckoutPro SDK in Android & iOS App.

Note:

This Plugin is in initial release. Please test it well before using it in production app.

Step 1: Android Implementation

There are a few steps you need to follow to implement PayUMoney SDK:

  1. Open file android/app/build.gradle in your Flutter project and update minSdkVersion under “defaultConfig” to 21 (if greater than leave).
  2. Open AndroidManifest.xml located at android/app/src/main and add the following code:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" <!-- Paste here -->
    package="yourPackageName">

    <application
        android:name="io.flutter.app.FlutterApplication"
        android:label="YourAppName"
        tools:replace="android:label" <!-- Paste here -->
        android:icon="@mipmap/ic_launcher">

Step 2: iOS Implementation

No additional steps required for iOS.

Step 3: Flutter Implementation

Adding Dependency

Run this command in the project terminal to add dependency:

flutter pub add payumoney_pro_unofficial

Or add manually to pubspecs.yaml:

dependencies:
  flutter:
    sdk: flutter
  payumoney_pro_unofficial: ^0.0.9

Get latest packages:

flutter pub get

Import package by adding this line at the top:

import 'package:payumoney_pro_unofficial/payumoney_pro_unofficial.dart';

Example Code

Here is a complete example of how to use the plugin:

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

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  // Example app to test the payumoney plugin
  [@override](/user/override)
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  [@override](/user/override)
  void initState() {
    super.initState();
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('PayUMoney Checkout Pro'),
        ),
        body: Center(
          child: ElevatedButton(
            child: Text("Make Payment"),
            onPressed: () async {
              // Every Transaction should have a unique ID. I am using timestamp as transactionid.
              String orderId = DateTime.now().millisecondsSinceEpoch.toString();
              final String amount = "1"; // Amount is in rs.

              var response = await PayumoneyProUnofficial.payUParams(
                  email: 'test@example.com',
                  firstName: "Orange",
                  merchantName: 'Orange Digitals',
                  isProduction: true,
                  merchantKey: 'merchantKey', // From PayUMoney dashboard
                  merchantSalt: 'merchantSalt',
                  amount: amount,
                  productInfo: 'iPhone 12', // Enter Product Name
                  transactionId: orderId, // Unique ID
                  hashUrl: 'https://example.com/generateHash', // Your hash URL
                  userCredentials: 'merchantKey:test@example.com',
                  showLogs: true,
                  userPhoneNumber: '9999999999');

              if (response['status'] == PayUParams.success)
                handlePaymentSuccess(amount);
              if (response['status'] == PayUParams.failed)
                handlePaymentFailure(amount, response['message']);
            },
          ),
        ),
      ),
    );
  }

  handlePaymentSuccess(String amount) {
    print("Success");
    // Implement your logic here for successful payment.
  }

  handlePaymentFailure(String amount, String error) {
    print("Failed");
    print(error);
    // Implement your logic here for failed payment.
  }
}

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

1 回复

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


当然,下面是一个关于如何在Flutter项目中集成并使用payumoney_pro_unofficial插件的示例代码。这个插件允许你在Flutter应用中集成PayUmoney支付功能。

首先,确保你已经在你的pubspec.yaml文件中添加了payumoney_pro_unofficial依赖:

dependencies:
  flutter:
    sdk: flutter
  payumoney_pro_unofficial: ^最新版本号  # 替换为实际可用的最新版本号

然后,运行flutter pub get来获取依赖。

接下来,你需要进行一些必要的配置,比如设置PayUmoney的密钥和其他参数。在你的Flutter项目的lib目录下,创建一个新的Dart文件(例如payu_config.dart)来存储这些配置:

// payu_config.dart
import 'package:payumoney_pro_unofficial/payumoney_pro_unofficial.dart';

class PayUConfig {
  static final String merchantKey = "你的商家密钥";  // 替换为你的商家密钥
  static final String salt = "你的盐值";  // 替换为你的盐值
  static final String appId = "你的应用ID";  // 替换为你的应用ID
  static final String baseUrl = "https://test.payu.in";  // 测试环境URL,如果是生产环境,请替换为生产环境的URL
}

然后,在你的主页面或需要支付功能的页面中,使用payumoney_pro_unofficial插件来发起支付请求:

// main.dart
import 'package:flutter/material.dart';
import 'package:payumoney_pro_unofficial/payumoney_pro_unofficial.dart';
import 'payu_config.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('PayUmoney Integration'),
        ),
        body: Center(
          child: PayUButton(),
        ),
      ),
    );
  }
}

class PayUButton extends StatefulWidget {
  @override
  _PayUButtonState createState() => _PayUButtonState();
}

class _PayUButtonState extends State<PayUButton> {
  void initiatePayment() async {
    final PayUMoneyResponse response = await PayUMoney.startPayment(
      context: context,
      environment: PayUMoneyEnvironment.SANDBOX,  // 使用测试环境,生产环境请使用PayUMoneyEnvironment.PRODUCTION
      merchantKey: PayUConfig.merchantKey,
      salt: PayUConfig.salt,
      appId: PayUConfig.appId,
      baseUrl: PayUConfig.baseUrl,
      productInfo: "Test Product",
      firstName: "John",
      lastName: "Doe",
      email: "john.doe@example.com",
      phone: "1234567890",
      amount: "100.00",  // 支付金额
      successUrl: "https://yourwebsite.com/success",  // 支付成功后的回调URL
      failureUrl: "https://yourwebsite.com/failure",  // 支付失败后的回调URL
    );

    // 根据支付结果进行相应的处理
    if (response.responseCode == "10000") {
      print("Payment successful");
    } else {
      print("Payment failed: ${response.errorMessage}");
    }
  }

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: initiatePayment,
      child: Text('Pay Now'),
    );
  }
}

在这个示例中,我们创建了一个简单的Flutter应用,包含一个按钮,点击按钮时会调用initiatePayment方法来启动PayUmoney支付流程。你需要根据你的实际信息替换配置中的merchantKeysaltappId等参数。

注意:

  1. 确保你已经注册了PayUmoney账户,并获取了必要的密钥和配置信息。
  2. 在生产环境中,请务必使用生产环境的URL和配置。
  3. 根据PayUmoney的文档,处理支付回调和结果。

这个示例代码应该可以帮助你在Flutter项目中集成并使用payumoney_pro_unofficial插件。如果有任何其他问题,请参考PayUmoney的官方文档或插件的GitHub页面。

回到顶部