Flutter支付集成插件shuftipro_sadad_sdk的使用

Flutter支付集成插件shuftipro_sadad_sdk的使用

简介

Shuftipro 是一个提供身份验证服务的平台。本文将介绍如何在 Flutter 应用程序中集成 Shuftipro 的支付插件 shuftipro_sadad_sdk

示例代码

以下是完整的示例代码,展示了如何在 Flutter 应用程序中集成 Shuftipro 支付插件。

import 'package:flutter/material.dart';
import 'package:shuftipro_sadad_sdk/shuftipro_sadad_sdk.dart';
import 'dart:convert';

// 在此处输入您的客户端ID和密钥
String clientId = ""; 
String secretKey = ""; 

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
          primarySwatch: Colors.blue,
          scaffoldBackgroundColor: Colors.white,
          fontFamily: 'OpenSans'),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  [@override](/user/override)
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var authObject = {
    "auth_type": "basic_auth",
    "client_id": clientId,
    "secret_key": secretKey,
  };
  Map<String, Object> createdPayload = {
    "country": "",
    "language": "EN",
    "email": "",
    "callback_url": "",
    "redirect_url": "",
    "show_consent": 1,
    "show_privacy_policy": 1,
    "verification_mode": "image_only",
    "face": {
      "proof": "",
    },
    "document": {
      "supported_types": [
        "passport",
        "id_card",
        "driving_license",
        "credit_or_debit_card",
      ],
      "name": {
        "first_name": "",
        "last_name": "",
        "middle_name": "",
      },
      "dob": "",
      "document_number": "",
      "expiry_date": "",
      "issue_date": "",
      "gender": "",
      "backside_proof_required": "0",
    },
  };
  Map<String, Object> configObj = {
    "base_url": "api.shuftipro.com",
    "show_requirement_page": false,
    "button_text_color": "#FFFFFF",
    "button_primary_color": "#8e1b3e",
    "button_secondary_color": "#8e1b3e",
    "icon_color": "#8e1b3e",
    "theme_color": "#8e1b3e",
    "loader_color": "#8e1b3e"
  };

  /*
   * 这个函数调用Shuftipro的Flutter插件
   * 并接收响应结果
   */
  Future<void> initPlatformState() async {
    String response = "";
    try {
      response = await ShuftiproSdk.sendRequest(
          authObject: authObject,
          createdPayload: createdPayload,
          configObject: configObj);

      // response = await ShuftiproSdk.registerRequest(clientId: "", customerId: "", configObject: configObj);

      var object = jsonDecode(response);
      ScaffoldMessenger.of(context).showSnackBar(SnackBar(
        content: Text(object.toString()),
      ));

      print(object.toString());
      print(object["event"].toString());
    } catch (e) {
      print(e);
    }
    if (!mounted) return;
  }

  /*
   * 点击事件监听器,开始SDK流程
   */
  void continueFun() {
    var v = DateTime.now();
    var reference = "package_sample_Flutter_$v";
    createdPayload["reference"] = reference;
    initPlatformState();
  }

  /*
   * 演示应用的UI
   */
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        body: Center(
          child: Container(
            margin: const EdgeInsets.all(10.0),
            child: OutlinedButton(
              onPressed: continueFun,
              style: OutlinedButton.styleFrom(backgroundColor: Colors.blueAccent),
              child: Container(
                alignment: Alignment.center,
                child: const Text(
                  "继续",
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    color: Colors.white,
                    fontFamily: 'OpenSans',
                  ),
                ),
              ),
            ),
          ),
        ));
  }
}

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

1 回复

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


shuftipro_sadad_sdk 是一个用于 Flutter 的支付集成插件,主要用于集成 Shufti Pro 的 SADAD 支付网关。SADAD 是一种在沙特阿拉伯广泛使用的电子支付系统。以下是如何在 Flutter 项目中使用 shuftipro_sadad_sdk 的基本步骤。

1. 添加依赖

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

dependencies:
  flutter:
    sdk: flutter
  shuftipro_sadad_sdk: ^1.0.0  # 请使用最新版本

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

2. 导入包

在你的 Dart 文件中导入 shuftipro_sadad_sdk 包。

import 'package:shuftipro_sadad_sdk/shuftipro_sadad_sdk.dart';

3. 初始化 SDK

在使用支付功能之前,你需要初始化 SDK。通常,你需要在应用启动时或在支付流程开始时进行初始化。

void initShuftiProSADAD() {
  ShuftiProSADAD.initialize(
    clientId: 'YOUR_CLIENT_ID',
    clientSecret: 'YOUR_CLIENT_SECRET',
    environment: ShuftiProEnvironment.SANDBOX,  // 或者 ShuftiProEnvironment.PRODUCTION
  );
}

4. 发起支付请求

你可以使用 ShuftiProSADAD 类来发起支付请求。通常,你需要提供一些支付相关的参数,如金额、订单号等。

Future<void> initiatePayment() async {
  try {
    final paymentResponse = await ShuftiProSADAD.initiatePayment(
      amount: '100.00',  // 支付金额
      currency: 'SAR',   // 货币类型
      orderId: 'ORDER_12345',  // 订单号
      customerName: 'John Doe',  // 客户名称
      customerEmail: 'john.doe@example.com',  // 客户邮箱
      customerPhone: '+966555555555',  // 客户电话
      redirectUrl: 'https://yourwebsite.com/redirect',  // 支付完成后重定向的URL
    );

    if (paymentResponse.success) {
      // 支付成功处理逻辑
      print('Payment successful! Transaction ID: ${paymentResponse.transactionId}');
    } else {
      // 支付失败处理逻辑
      print('Payment failed: ${paymentResponse.errorMessage}');
    }
  } catch (e) {
    // 异常处理
    print('Error initiating payment: $e');
  }
}

5. 处理支付回调

支付完成后,用户会被重定向到你提供的 redirectUrl。你需要在服务器端或客户端处理支付回调,以确认支付状态。

6. 检查支付状态

你可以使用 ShuftiProSADAD 提供的 API 来检查支付状态。

Future<void> checkPaymentStatus(String transactionId) async {
  try {
    final statusResponse = await ShuftiProSADAD.checkPaymentStatus(transactionId);

    if (statusResponse.success) {
      // 支付状态查询成功处理逻辑
      print('Payment status: ${statusResponse.status}');
    } else {
      // 支付状态查询失败处理逻辑
      print('Failed to check payment status: ${statusResponse.errorMessage}');
    }
  } catch (e) {
    // 异常处理
    print('Error checking payment status: $e');
  }
}

7. 其他功能

shuftipro_sadad_sdk 可能还提供了其他功能,如取消支付、退款等。你可以参考官方文档或 SDK 的源代码来了解如何使用这些功能。

8. 测试与发布

在开发过程中,你可以使用沙盒环境进行测试。确保在发布应用之前,将环境切换到生产环境。

ShuftiProSADAD.initialize(
  clientId: 'YOUR_CLIENT_ID',
  clientSecret: 'YOUR_CLIENT_SECRET',
  environment: ShuftiProEnvironment.PRODUCTION,
);
回到顶部