Flutter支付集成插件iyzi_dart的使用

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

Flutter支付集成插件iyzi_dart的使用

开始使用

支持联系邮箱:telli@tahsinemre.com

使用方法

以下是一个完整的示例代码,展示了如何在Flutter应用中使用iyzi_dart插件进行支付集成。

const String callBackUrl = 'https://api.tahsinemre.com/payCheck';
const String baseUrl = 'https://sandbox-api.iyzipay.com/payment';
const String apiKey = 'YOUR-API-KEY';
const String secretKey = 'YOUR-SECRET-KEY';
const String randomKey = 'RANDOMKEY';

final config = IyziConfig(
  baseUrl: baseUrl,
  callBackUrl: callBackUrl,
  apiKey: apiKey,
  secretKey: secretKey,
  randomKey: randomKey,
);

final iyziDart = IyziDart(config);

final card = IyziCard(
  cardHolderName: 'Tahsin Emre Telli',
  cvc: '000',
  expireMonth: '12',
  expireYear: '2030',
  cardNumber: '5400010000000004',
);

final basketItem = IyziBasketItem(
  id: 'itemID',
  price: '360.00',
  name: '土耳其课程',
  category1: '数字服务',
  itemType: ItemTypes.VIRTUAL,
);

final billingAdress = IyziAddress(
  address: 'Nidakule Göztepe, Merdivenköy Mah. Bora Sok. No:1',
  contactName: 'Tahsin Emre Telli',
  city: '伊斯坦布尔',
  country: '土耳其',
  zipCode: '34732',
);

IyziBuyer buyer = IyziBuyer(
  id: 'userIdOnAuthenticationService',
  name: 'Tahsin Emre',
  surname: 'Telli',
  identityNumber: '11111111111',
  email: 'telli@tahsinemre.com',
  registrationAddress: 'Nidakule Göztepe, Merdivenköy Mah. Bora Sok. No:1',
  city: '伊兹密尔',
  country: '土耳其',
  ip: '193.140.25.34',
  gsmNumber: "+905350000000",
  registrationDate: "2013-04-21 15:12:09",
  lastLoginDate: "2024-11-23 12:43:35",
);

final initializeResponse = await iyziDart.initializePayment(
  conversationId: 'testConvID',
  card: card,
  buyer: buyer,
  billingAddress: billingAdress,
  shippingAddress: billingAdress,
  basketItems: [basketItem],
);

if (initializeResponse.status == Status.failure) {
  print(initializeResponse.errorMessage);
  return;
}

if (initializeResponse.status == Status.success) {
  // 您需要将此HTML内容展示给用户
  print(initializeResponse.convertHtml());
}

// 您需要从回调URL中捕获paymentId和conversationData
const paymentId = '0123456';
const conversationData = 'conversationData';
String result = await iyziDart.completePayment(
  conversationId: 'testConvID',
  paymentId: paymentId,
  conversationData: conversationData,
  locale: 'tr',
);
print(result);

其他信息

如需查看更多的使用示例,请参考:


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

1 回复

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


当然,下面是一个关于如何在Flutter项目中集成并使用iyzi_dart插件来实现支付的代码示例。iyzi_dart是一个用于集成iyzico支付网关的Flutter插件。

1. 添加依赖

首先,在你的pubspec.yaml文件中添加iyzi_dart依赖:

dependencies:
  flutter:
    sdk: flutter
  iyzi_dart: ^最新版本号  # 请替换为最新的版本号

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

2. 配置Android和iOS

确保你已经在iyzico后台为你的应用配置了必要的支付信息,并获取了必要的API密钥和商户信息。

Android配置

android/app/src/main/AndroidManifest.xml中添加必要的权限(如果需要):

<uses-permission android:name="android.permission.INTERNET"/>

iOS配置

通常iOS不需要特别的配置,除非有特定的权限要求。

3. 初始化并创建支付请求

在你的Dart代码中,初始化iyzi_dart插件并创建一个支付请求。以下是一个基本的示例:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('IyziPay Integration'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: _initPayment,
            child: Text('Initiate Payment'),
          ),
        ),
      ),
    );
  }

  Future<void> _initPayment() async {
    // 替换为你的实际API密钥和商户信息
    final apiKey = 'your_api_key';
    final secretKey = 'your_secret_key';
    final baseUrl = 'https://sandbox-api.iyzico.com';  // 如果是生产环境,请使用 https://api.iyzico.com

    // 创建IyziPay实例
    final iyziPay = IyziPay(apiKey: apiKey, secretKey: secretKey, baseUrl: baseUrl);

    // 构建支付请求
    final paymentRequest = CreatePaymentRequestModel(
      locale: 'tr',
      conversationId: '123456789',
      price: '1.00',
      paidPrice: '1.00',
      currency: Currency.TRY.toString(),
      installment: 1,
      paymentChannel: PaymentChannel.WEB.toString(),
      basketId: 'basket123',
      paymentGroup: PaymentGroup.PRODUCT.toString(),
      callbackUrl: 'https://yourwebsite.com/callback',
      // 其他必要字段,比如买家信息、商品详情等
      buyer: BuyerModel(
        id: 'buyer123',
        name: 'John Doe',
        surname: 'Smith',
        gsmNumber: '+905555555555',
        email: 'john.doe@example.com',
        identityNumber: '12345678901',
        lastLoginDate: '2023-10-01T10:00:00',
        registrationDate: '2023-01-01T00:00:00',
        ip: '192.168.1.1',
        city: 'Istanbul',
        country: 'Turkey',
        postalCode: '34123',
        address: 'Test Address',
      ),
      shippingAddress: AddressModel(
        contactName: 'John Doe',
        city: 'Istanbul',
        country: 'Turkey',
        postalCode: '34123',
        address: 'Test Address',
      ),
      basketItems: [
        BasketItemModel(
          id: 'item1',
          name: 'Test Item',
          category1: 'Electronics',
          category2: 'Mobile Phones',
          itemType: BasketItemType.PHYSICAL.toString(),
          price: '1.00',
          quantity: 1,
        ),
      ],
    );

    try {
      // 发起支付请求
      final result = await iyziPay.createPayment(paymentRequest);
      print('Payment URL: ${result.data?.checkoutForm?.htmlContent}');
      // 这里你可以使用webview或者其它方式展示支付页面
    } catch (e) {
      print('Error: $e');
    }
  }
}

4. 显示支付页面

上面的代码将生成一个支付页面的HTML内容,你需要在一个WebView中显示这个内容。你可以使用webview_flutter插件来实现。

pubspec.yaml中添加webview_flutter依赖:

dependencies:
  webview_flutter: ^最新版本号  # 请替换为最新的版本号

然后在你的代码中,使用WebView显示支付页面:

import 'package:webview_flutter/webview_flutter.dart';

// ...

Future<void> _showPaymentWebView(String htmlContent) async {
  showDialog(
    context: context,
    builder: (context) => AlertDialog(
      title: Text('Payment'),
      content: SizedBox(
        width: double.infinity,
        height: double.infinity,
        child: WebView(
          initialUrl: Uri.dataFromString(
            htmlContent,
            mimeType: 'text/html',
            encoding: Encoding.getByName('utf-8')
          ).toString(),
          javascriptMode: JavascriptMode.unrestricted,
        ),
      ),
      actions: <Widget>[
        TextButton(
          onPressed: Navigator.of(context).pop,
          child: Text('Close'),
        ),
      ],
    ),
  );
}

// 在_initPayment方法中的try块内调用
try {
  final result = await iyziPay.createPayment(paymentRequest);
  _showPaymentWebView(result.data?.checkoutForm?.htmlContent ?? '');
} catch (e) {
  print('Error: $e');
}

以上代码示例展示了如何在Flutter应用中集成iyzi_dart插件来发起支付请求,并在WebView中显示支付页面。请根据你的实际需求调整代码中的参数和逻辑。

回到顶部