Support disconnecting Stripe

This commit is contained in:
Hillel Coren 2021-10-29 16:15:00 +03:00
parent 5bff0a0336
commit 8c7a059f30
5 changed files with 88 additions and 2 deletions

View File

@ -57,6 +57,17 @@ class CompanyGatewayRepository {
return companyGatewayResponse.data.toList();
}
Future<void> disconnect(Credentials credentials, String id, String password,
String idToken) async {
final url = credentials.url + '/stripe/disconnect/$id';
await webClient.post(
url,
credentials.token,
password: password,
idToken: idToken,
);
}
Future<CompanyGatewayEntity> saveData(
Credentials credentials, CompanyGatewayEntity companyGateway) async {
final data = serializers.serializeWith(

View File

@ -9,6 +9,7 @@ import 'package:invoiceninja_flutter/redux/app/app_actions.dart';
import 'package:invoiceninja_flutter/redux/app/app_state.dart';
import 'package:invoiceninja_flutter/ui/app/entities/entity_actions_dialog.dart';
import 'package:invoiceninja_flutter/utils/completers.dart';
import 'package:invoiceninja_flutter/utils/dialogs.dart';
import 'package:invoiceninja_flutter/utils/localization.dart';
class ViewCompanyGatewayList implements StopLoading {
@ -173,6 +174,28 @@ class DeleteCompanyGatewayFailure implements StopSaving {
final List<CompanyGatewayEntity> companyGateways;
}
class DisconnectCompanyGatewayRequest implements StartSaving {
DisconnectCompanyGatewayRequest({
@required this.completer,
@required this.companyGatewayId,
@required this.password,
@required this.idToken,
});
final Completer completer;
final String companyGatewayId;
final String password;
final String idToken;
}
class DisconnectCompanyGatewaySuccess implements StopSaving, PersistData {}
class DisconnectCompanyGatewayFailure implements StopSaving {
DisconnectCompanyGatewayFailure(this.error);
final Object error;
}
class RestoreCompanyGatewayRequest implements StartSaving {
RestoreCompanyGatewayRequest(this.completer, this.companyGatewayIds);
@ -274,6 +297,26 @@ void handleCompanyGatewayAction(BuildContext context,
store.dispatch(DeleteCompanyGatewayRequest(
snackBarCompleter<Null>(context, message), companyGatewayIds));
break;
case EntityAction.disconnect:
final completer =
snackBarCompleter<Null>(context, localization.disconnectedGateway);
completer.future.then((value) {
store.dispatch(RefreshData());
});
passwordCallback(
context: context,
callback: (password, idToken) {
store.dispatch(
DisconnectCompanyGatewayRequest(
completer: completer,
companyGatewayId: companyGateway.id,
password: password,
idToken: idToken,
),
);
},
);
break;
case EntityAction.toggleMultiselect:
if (!store.state.companyGatewayListState.isInMultiselect()) {
store.dispatch(StartCompanyGatewayMultiselect());

View File

@ -27,6 +27,7 @@ List<Middleware<AppState>> createStoreCompanyGatewaysMiddleware([
final archiveCompanyGateway = _archiveCompanyGateway(repository);
final deleteCompanyGateway = _deleteCompanyGateway(repository);
final restoreCompanyGateway = _restoreCompanyGateway(repository);
final disconnectCompanyGateway = _disconnectCompanyGateway(repository);
return [
TypedMiddleware<AppState, ViewCompanyGatewayList>(viewCompanyGatewayList),
@ -41,6 +42,8 @@ List<Middleware<AppState>> createStoreCompanyGatewaysMiddleware([
deleteCompanyGateway),
TypedMiddleware<AppState, RestoreCompanyGatewayRequest>(
restoreCompanyGateway),
TypedMiddleware<AppState, DisconnectCompanyGatewayRequest>(
disconnectCompanyGateway),
];
}
@ -175,6 +178,30 @@ Middleware<AppState> _restoreCompanyGateway(
};
}
Middleware<AppState> _disconnectCompanyGateway(
CompanyGatewayRepository repository) {
return (Store<AppState> store, dynamic dynamicAction, NextDispatcher next) {
final action = dynamicAction as DisconnectCompanyGatewayRequest;
repository
.disconnect(store.state.credentials, action.companyGatewayId,
action.password, action.idToken)
.then((_) {
store.dispatch(DisconnectCompanyGatewaySuccess());
if (action.completer != null) {
action.completer.complete(null);
}
}).catchError((Object error) {
print(error);
store.dispatch(DisconnectCompanyGatewayFailure(error));
if (action.completer != null) {
action.completer.completeError(error);
}
});
next(action);
};
}
Middleware<AppState> _saveCompanyGateway(CompanyGatewayRepository repository) {
return (Store<AppState> store, dynamic dynamicAction, NextDispatcher next) {
final action = dynamicAction as SaveCompanyGatewayRequest;

View File

@ -161,8 +161,8 @@ void confirmCallback({
}
void passwordCallback({
BuildContext context,
Function(String, String) callback,
@required BuildContext context,
@required Function(String, String) callback,
bool alwaysRequire = false,
bool skipOAuth = false,
}) {

View File

@ -15,6 +15,7 @@ mixin LocalizationsProvider on LocaleCodeAware {
static final Map<String, Map<String, String>> _localizedValues = {
'en': {
// STARTER: lang key - do not remove comment
'disconnected_gateway': 'Successfully disconnected gateway',
'disconnect': 'Disconnect',
'add_to_invoices': 'Add to Invoices',
'acss': 'ACSS',
@ -62761,6 +62762,10 @@ mixin LocalizationsProvider on LocaleCodeAware {
_localizedValues[localeCode]['disconnect'] ??
_localizedValues['en']['disconnect'];
String get disconnectedGateway =>
_localizedValues[localeCode]['disconnected_gateway'] ??
_localizedValues['en']['disconnected_gateway'];
// STARTER: lang field - do not remove comment
String lookup(String key) {