Flutter三星支付集成插件samsungpay的使用
Flutter三星支付集成插件samsungpay的使用
文档
Build.Gradle -> app level
android {
defaultConfig {
minSdkVersion 22
}
}
ANDROID MANIFEST:
需要更新的字段:
- DEBUG_MODE
- SPAY_DEBUG_API_KEY
- SPAY_SDK_API_LEVEL
<queries>
<package android:name="com.samsung.android.spay" />
<package android:name="com.samsung.android.samsungpay.gear" />
</queries>
// For Testing/QA
<meta-data
android:name="debug_mode"
android:value="Y" /> //Y-Debug N-production
<meta-data
android:name="spay_debug_api_key"
android:value="[add your debug key here]"/>
<meta-data
android:name="spay_sdk_api_level"
android:value="2.17"/>
// For Production
<meta-data
android:name="debug_mode"
android:value="N" />
<meta-data
android:name="spay_sdk_api_level"
android:value="2.17"/>
Strings.xml:
可选字段
<string name="amount_control_name_item">Item</string>
<string name="amount_control_name_tax">Tax</string>
<string name="amount_control_name_shipping">Shipping</string>
支持的货币
- USD
- INR
- KRW
- EUR
- AUD
- JPY
- CNY
- GBP
- SGD
- RUB
- BRL
- HKD
- THB
- CAD
- MYR
- CHF
- SEK
- TWD
- AED
检查三星支付是否可用
final SamsungpayStatus sPayAvailable = await Samsungpay.isAvailable(spayServiceID);
使用三星支付付款
String? data = (await Samsungpay.payNow(productAmount,taxAmount,shippingAmount,addedShippingAmount,addedBillingAmount,currency,merchantID,merchantName,orderNumber));
示例代码
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:samsungpay/samsunf_pay_e.dart';
import 'package:samsungpay/samsungpay.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: const Home(),
),
);
}
}
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
[@override](/user/override)
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
String platformVersion = 'Unknown';
final TextEditingController productAmount = TextEditingController();
final TextEditingController taxAmount = TextEditingController();
final TextEditingController shippingAmount = TextEditingController();
final TextEditingController addedShippingAmount = TextEditingController(text: '0');
final TextEditingController addedBillingAmount = TextEditingController(text: '0');
final TextEditingController spayServiceID = TextEditingController();
final TextEditingController merchantID = TextEditingController();
final TextEditingController merchantName = TextEditingController();
final TextEditingController orderNumber = TextEditingController();
final List<String> currency = [
"USD",
"INR",
"KRW",
"EUR",
"AUD",
"JPY",
"CNY",
"GBP",
"SGD",
"RUB",
"BRL",
"HKD",
"THB",
"CAD",
"MYR",
"CHF",
"SEK",
"TWD",
"AED",
];
String dropdownValue = "AED";
[@override](/user/override)
void initState() {
super.initState();
productAmount.text = '10';
taxAmount.text = '0';
shippingAmount.text = '0';
spayServiceID.text = "4e0aed1db4354e86a8b90f";
merchantID.text = "MPGS";
merchantName.text = "MPGS";
orderNumber.text = DateTime.now().millisecondsSinceEpoch.toString();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState(String spayServiceID) async {
try {
platformVersion = (await Samsungpay.isAvailable(spayServiceID)).toString();
} on PlatformException catch (e) {
platformVersion = 'Failed to get platform version. $e';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
platformVersion = platformVersion;
});
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
body: ListView(
children: [
Center(
child: SizedBox(
width: 220,
child: TextField(
controller: productAmount,
keyboardType: TextInputType.number,
decoration: const InputDecoration(hintText: "Product Amount"),
),
),
),
Center(
child: SizedBox(
width: 220,
child: TextField(
controller: taxAmount,
keyboardType: TextInputType.number,
decoration: const InputDecoration(hintText: "Tax Amount"),
),
),
),
Center(
child: SizedBox(
width: 220,
child: TextField(
controller: shippingAmount,
keyboardType: TextInputType.number,
decoration: const InputDecoration(hintText: "Shipping Amount"),
),
),
),
Center(
child: SizedBox(
width: 220,
child: TextField(
controller: addedShippingAmount,
keyboardType: TextInputType.number,
decoration: const InputDecoration(hintText: "Added Shipping Amount"),
),
),
),
Center(
child: SizedBox(
width: 220,
child: TextField(
controller: addedBillingAmount,
keyboardType: TextInputType.number,
decoration: const InputDecoration(hintText: "Added Billing Amount"),
),
),
),
Center(
child: SizedBox(
width: 220,
child: TextField(
controller: merchantID,
keyboardType: TextInputType.text,
decoration: const InputDecoration(hintText: "MerchantId"),
),
),
),
Center(
child: SizedBox(
width: 220,
child: TextField(
controller: merchantName,
keyboardType: TextInputType.text,
decoration: const InputDecoration(hintText: "MerchantName"),
),
),
),
Center(
child: SizedBox(
width: 220,
child: TextField(
controller: orderNumber,
keyboardType: TextInputType.text,
decoration: const InputDecoration(hintText: "OrderNumber"),
),
),
),
Center(
child: SizedBox(
width: 60,
child: DropdownButton<String>(
value: dropdownValue,
iconSize: 24,
elevation: 16,
style: const TextStyle(color: Colors.black87),
underline: Container(
height: 1,
color: Colors.deepPurpleAccent,
),
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
});
},
items: currency.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
),
),
const SizedBox(
height: 80,
),
if (!platformVersion.contains("SPAY_READY"))
Center(
child: SizedBox(
width: 220,
child: TextField(
controller: spayServiceID,
keyboardType: TextInputType.text,
decoration: const InputDecoration(hintText: "SPAY_SERVICE_ID"),
),
),
),
Center(child: Text('Running on: $platformVersion\n')),
if (!platformVersion.contains("SPAY_READY"))
Center(
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.black87,
minimumSize: const Size(220, 50),
),
onPressed: () {
if (spayServiceID.text.isNotEmpty) {
initPlatformState(spayServiceID.text);
} else {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text("Please enter service id")));
}
},
child: const Text(
"Samsung Pay Available",
style: TextStyle(
fontSize: 14,
),
),
),
),
if (platformVersion.contains("SPAY_READY"))
Center(
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.black87,
minimumSize: const Size(220, 50),
),
onPressed: () async {
try {
String? data = (await Samsungpay.payNow(
double.parse(productAmount.text),
double.parse(taxAmount.text),
double.parse(shippingAmount.text),
double.parse(addedShippingAmount.text),
double.parse(addedBillingAmount.text),
dropdownValue,
merchantID.text,
merchantName.text,
orderNumber.text,
));
print("coming from android: $data");
} catch (e) {
platformVersion = 'payment failed $e';
e.toString();
setState(() {});
}
},
child: const Text(
"Samsung Pay",
style: TextStyle(
fontSize: 14,
),
),
),
),
Center(
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.black87,
minimumSize: const Size(220, 50),
),
onPressed: () async {
try {
await Samsungpay.doActivateSamsungPay();
} on PlatformException catch (e) {
platformVersion = 'payment failed $e';
}
},
child: const Text(
"Open Samsung Pay",
style: TextStyle(
fontSize: 14,
),
),
),
),
],
),
);
}
}
更多关于Flutter三星支付集成插件samsungpay的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter三星支付集成插件samsungpay的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
在Flutter应用中集成三星支付(Samsung Pay)通常需要使用由Samsung提供的SDK和插件。然而,截至2023年,Flutter社区并没有一个官方的或广泛使用的三星支付插件。你通常需要通过平台特定的代码(Android端或iOS端)来实现三星支付的集成。
以下是一个大致的步骤指南,帮助你在Flutter应用中集成三星支付:
1. 获取三星支付SDK
首先,你需要前往Samsung Developers网站,注册并获取三星支付的SDK。你需要按照Samsung的开发文档配置你的项目。
2. 创建Flutter项目
如果你还没有一个Flutter项目,可以通过以下命令创建一个新的Flutter项目:
flutter create samsung_pay_integration
3. 配置Android项目
三星支付主要用于Android设备,因此你需要在Android端进行配置。
3.1 在android/app/build.gradle
中添加依赖项
在你的android/app/build.gradle
文件中添加三星支付SDK的依赖项:
dependencies {
implementation 'com.samsung.android.spay:sdk:1.2.0' // 请使用最新版本
}
3.2 在android/app/src/main/AndroidManifest.xml
中添加权限和服务
在你的AndroidManifest.xml
中添加必要的权限和服务:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
4. 创建平台通道(Platform Channel)
由于没有现成的Flutter插件,你需要通过平台通道(Platform Channel)来调用原生代码。
4.1 在lib/main.dart
中定义平台通道
在你的Flutter代码中定义一个平台通道:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Samsung Pay Integration'),
),
body: Center(
child: ElevatedButton(
onPressed: () async {
const platform = MethodChannel('samples.flutter.dev/samsungpay');
try {
final String result = await platform.invokeMethod('startPayment');
print(result);
} on PlatformException catch (e) {
print("Failed: '${e.message}'.");
}
},
child: Text('Start Payment'),
),
),
),
);
}
}
4.2 在Android端实现平台通道
在android/app/src/main/kotlin/com/example/samsung_pay_integration/MainActivity.kt
中实现平台通道:
package com.example.samsung_pay_integration
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import com.samsung.android.spay.SpaySdk
class MainActivity: FlutterActivity() {
private val CHANNEL = "samples.flutter.dev/samsungpay"
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
if (call.method == "startPayment") {
// 初始化三星支付SDK并启动支付流程
startSamsungPayment(result)
} else {
result.notImplemented()
}
}
}
private fun startSamsungPayment(result: MethodChannel.Result) {
// 实现三星支付的逻辑
result.success("Payment started")
}
}