Skip to content

Commit

Permalink
Merge pull request #23 from flutter-stripe/add-payment-intent-erorr
Browse files Browse the repository at this point in the history
throw StripeError in case handling paymentintent fails
  • Loading branch information
jonasbark authored Apr 18, 2021
2 parents 5f62cb6 + 2f605c8 commit 6dbbf36
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 312 deletions.
62 changes: 42 additions & 20 deletions stripe_platform_interface/lib/src/method_channel_stripe.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import 'dart:io';

import 'package:flutter/services.dart';
import 'package:stripe_platform_interface/src/models/apple_pay.dart';
import 'package:stripe_platform_interface/src/models/errors.dart';
import 'package:stripe_platform_interface/src/models/payment_intents.dart';
import 'package:stripe_platform_interface/src/models/payment_methods.dart';
import 'package:stripe_platform_interface/src/models/setup_intent.dart';
import 'package:stripe_platform_interface/src/models/three_d_secure.dart';
import 'package:stripe_platform_interface/src/stripe_platform_interface.dart';

import 'stripe_platform_interface.dart';
import 'models/app_info.dart';
import 'stripe_platform_interface.dart';

const _appInfo = AppInfo(
name: "flutter_stripe",
Expand Down Expand Up @@ -79,13 +80,20 @@ class MethodChannelStripe extends StripePlatform {
PaymentMethodParams params, [
Map<String, String> options = const {},
]) async {
final result = await _methodChannel
.invokeMapMethod<String, dynamic>('confirmPaymentMethod', {
'paymentIntentClientSecret': paymentIntentClientSecret,
'params': params.toJson(),
'options': options,
});
return PaymentIntent.fromJson(result.unfoldToNonNull());
try {
final result = await _methodChannel
.invokeMapMethod<String, dynamic>('confirmPaymentMethod', {
'paymentIntentClientSecret': paymentIntentClientSecret,
'params': params.toJson(),
'options': options,
});
return PaymentIntent.fromJson(result.unfoldToNonNull());
} on Exception catch (_) {
throw StripeError<PaymentIntentError>(
code: PaymentIntentError.unknown,
message: "Confirming payment intent failed",
);
}
}

@override
Expand Down Expand Up @@ -117,12 +125,19 @@ class MethodChannelStripe extends StripePlatform {
@override
Future<PaymentIntent> handleCardAction(
String paymentIntentClientSecret) async {
final result = await _methodChannel
.invokeMapMethod<String, dynamic>('handleCardAction', {
'paymentIntentClientSecret': paymentIntentClientSecret,
});

return PaymentIntent.fromJson(result.unfoldToNonNull());
try {
final result = await _methodChannel
.invokeMapMethod<String, dynamic>('handleCardAction', {
'paymentIntentClientSecret': paymentIntentClientSecret,
});

return PaymentIntent.fromJson(result.unfoldToNonNull());
} on Exception catch (_) {
throw StripeError<PaymentIntentError>(
code: PaymentIntentError.unknown,
message: "Handle payment intent for card failed",
);
}
}

@override
Expand All @@ -142,12 +157,19 @@ class MethodChannelStripe extends StripePlatform {

@override
Future<PaymentIntent> retrievePaymentIntent(String clientSecret) async {
final result = await _methodChannel
.invokeMapMethod<String, dynamic>('retrievePaymentIntent', {
'clientSecret': clientSecret,
});

return PaymentIntent.fromJson(result.unfoldToNonNull());
try {
final result = await _methodChannel
.invokeMapMethod<String, dynamic>('retrievePaymentIntent', {
'clientSecret': clientSecret,
});

return PaymentIntent.fromJson(result.unfoldToNonNull());
} on Exception catch (_) {
throw StripeError<PaymentIntentError>(
code: PaymentIntentError.unknown,
message: "Retrieving payment intent failed",
);
}
}
}

Expand Down
31 changes: 15 additions & 16 deletions stripe_platform_interface/lib/src/models/errors.dart
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
import 'package:freezed_annotation/freezed_annotation.dart';

import 'payment_methods.dart';


part 'errors.freezed.dart';
part 'errors.g.dart';

enum ConfirmPaymentError { canceled, failed, unkown }
enum ConfirmPaymentError { canceled, failed, unknown }

enum CardActionError { canceled, failed, unkown }
enum CardActionError { canceled, failed, unknown }

enum ConfirmSetupIntentError { canceled, failed, unkown }
enum ConfirmSetupIntentError { canceled, failed, unknown }

enum CreatePaymentMethodError { failed }
enum RetrievePaymentIntentError { canceled }
enum PaymentIntentError { unknown }

enum ApplePayError { canceled, failed, unkown }
enum ApplePayError { canceled, failed, unknown }

@freezed
class StripeError<T> with _$StripeError<T> {
const factory StripeError.generic({
@JsonSerializable(explicitToJson: true)
const factory StripeError({
required String message,
required T code,
@JsonKey(fromJson: _dataFromJson, toJson: _dataToJson) required T code,
}) = _StripeErrorGeneric;

const factory StripeError.lastPayment({
required String code,
required LastPaymentErrorType type,
required PaymentMethod paymentMethod,
required String message,
}) = _StripeErrorLastPayment;
factory StripeError.fromJson(Map<String, dynamic> json) =>
_$StripeErrorFromJson<T>(json);
}

T _dataFromJson<T>(Map<String, dynamic> input) => input['code'] as T;

Map<String, dynamic> _dataToJson<T>(T input) => {'code': input};

enum LastPaymentErrorType {
ApiConnection,
Api,
Expand Down
Loading

0 comments on commit 6dbbf36

Please sign in to comment.