parent
9f25d105c6
commit
fd66b06c7e
@ -0,0 +1,69 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
|
||||
part 'pay_model.g.dart';
|
||||
|
||||
@JsonSerializable()
|
||||
class PayModel extends Equatable {
|
||||
@JsonKey(name: 'alipay_trade_app_pay_response')
|
||||
final AliPayTradeAppPayResponse aliPayTradeAppPayResponse;
|
||||
final String sign;
|
||||
@JsonKey(name: 'sign_type')
|
||||
final String signType;
|
||||
PayModel({
|
||||
required this.aliPayTradeAppPayResponse,
|
||||
required this.sign,
|
||||
required this.signType,
|
||||
});
|
||||
factory PayModel.fromJson(Map<String, dynamic> json) =>
|
||||
_$PayModelFromJson(json);
|
||||
@override
|
||||
List<Object> get props => [aliPayTradeAppPayResponse, sign, signType];
|
||||
}
|
||||
|
||||
@JsonSerializable()
|
||||
class AliPayTradeAppPayResponse extends Equatable {
|
||||
final String code;
|
||||
final String msg;
|
||||
@JsonKey(name: 'app_id')
|
||||
final String appId;
|
||||
@JsonKey(name: 'out_trade_no')
|
||||
final String outTradeNo;
|
||||
@JsonKey(name: 'trade_no')
|
||||
final String tradeNo;
|
||||
@JsonKey(name: 'total_amount')
|
||||
final String totalAmount;
|
||||
@JsonKey(name: 'seller_id')
|
||||
final String sellerId;
|
||||
final String charset;
|
||||
final String timestamp;
|
||||
AliPayTradeAppPayResponse({
|
||||
required this.code,
|
||||
required this.msg,
|
||||
required this.appId,
|
||||
required this.outTradeNo,
|
||||
required this.tradeNo,
|
||||
required this.totalAmount,
|
||||
required this.sellerId,
|
||||
required this.charset,
|
||||
required this.timestamp,
|
||||
});
|
||||
|
||||
factory AliPayTradeAppPayResponse.fromJson(Map<String, dynamic> json) =>
|
||||
_$AliPayTradeAppPayResponseFromJson(json);
|
||||
|
||||
@override
|
||||
List<Object> get props {
|
||||
return [
|
||||
code,
|
||||
msg,
|
||||
appId,
|
||||
outTradeNo,
|
||||
tradeNo,
|
||||
totalAmount,
|
||||
sellerId,
|
||||
charset,
|
||||
timestamp,
|
||||
];
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'pay_model.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
PayModel _$PayModelFromJson(Map<String, dynamic> json) {
|
||||
return PayModel(
|
||||
aliPayTradeAppPayResponse: AliPayTradeAppPayResponse.fromJson(
|
||||
json['alipay_trade_app_pay_response'] as Map<String, dynamic>),
|
||||
sign: json['sign'] as String,
|
||||
signType: json['sign_type'] as String,
|
||||
);
|
||||
}
|
||||
|
||||
AliPayTradeAppPayResponse _$AliPayTradeAppPayResponseFromJson(
|
||||
Map<String, dynamic> json) {
|
||||
return AliPayTradeAppPayResponse(
|
||||
code: json['code'] as String,
|
||||
msg: json['msg'] as String,
|
||||
appId: json['app_id'] as String,
|
||||
outTradeNo: json['out_trade_no'] as String,
|
||||
tradeNo: json['trade_no'] as String,
|
||||
totalAmount: json['total_amount'] as String,
|
||||
sellerId: json['seller_id'] as String,
|
||||
charset: json['charset'] as String,
|
||||
timestamp: json['timestamp'] as String,
|
||||
);
|
||||
}
|
@ -0,0 +1,96 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:aku_community/models/pay/pay_model.dart';
|
||||
import 'package:aku_community/utils/network/net_util.dart';
|
||||
import 'package:bot_toast/bot_toast.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:power_logger/power_logger.dart';
|
||||
import 'package:tobias/tobias.dart';
|
||||
|
||||
enum PAYTYPE {
|
||||
///支付宝
|
||||
ALI,
|
||||
|
||||
///微信
|
||||
WX,
|
||||
|
||||
///现金
|
||||
CASH,
|
||||
|
||||
///pos
|
||||
POS
|
||||
}
|
||||
|
||||
class PayUtil {
|
||||
void resultSatus(String status) {
|
||||
switch (status) {
|
||||
case '8000':
|
||||
BotToast.showText(text: '正在处理中');
|
||||
break;
|
||||
case '4000':
|
||||
BotToast.showText(text: '订单支付失败');
|
||||
break;
|
||||
case '5000':
|
||||
BotToast.showText(text: '重复请求');
|
||||
break;
|
||||
case '6001':
|
||||
BotToast.showText(text: ' 用户中途取消');
|
||||
break;
|
||||
case '6002':
|
||||
BotToast.showText(text: '网络连接出错');
|
||||
break;
|
||||
case '6004':
|
||||
BotToast.showText(text: '支付结果未知,请查询商户订单列表中订单的支付状态');
|
||||
break;
|
||||
default:
|
||||
BotToast.showText(text: '其他支付错误');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
String _resultStatus = '';
|
||||
|
||||
///传入订单信息和确认订单请求地址
|
||||
Future<bool> callAliPay(String order, String path) async {
|
||||
Map<dynamic, dynamic> result = await aliPay(order);
|
||||
_resultStatus = result['resultStatus'];
|
||||
if (_resultStatus == '9000') {
|
||||
String _res = result['result'];
|
||||
PayModel _model = PayModel.fromJson(jsonDecode(_res));
|
||||
bool _confirmResult = await _confirmPayResult(
|
||||
path, _model.aliPayTradeAppPayResponse.outTradeNo);
|
||||
return _confirmResult;
|
||||
} else {
|
||||
resultSatus(_resultStatus);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> _confirmPayResult(String path, String code) async {
|
||||
try {
|
||||
int status = 0;
|
||||
for (var i = 0; i < 3; i++) {
|
||||
await Future.delayed(Duration(milliseconds: 1000), () async {
|
||||
Response response = await NetUtil().dio!.get(path, queryParameters: {
|
||||
"code": code,
|
||||
});
|
||||
status = response.data['status'] as int;
|
||||
});
|
||||
if (status == 2) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (status == 2) {
|
||||
BotToast.showText(text: '交易成功');
|
||||
return true;
|
||||
} else {
|
||||
BotToast.showText(text: '交易失败 错误码${status}');
|
||||
return false;
|
||||
}
|
||||
} catch (e) {
|
||||
BotToast.showText(text: '网络请求错误');
|
||||
LoggerData.addData(e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue