Skip to content

Commit

Permalink
feat: add correct mapping for addressheet
Browse files Browse the repository at this point in the history
  • Loading branch information
Remon committed Dec 15, 2024
1 parent a6dd083 commit 1cd8ad5
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 63 deletions.
57 changes: 57 additions & 0 deletions example/lib/screens/address_sheet/address_sheet.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import 'package:flutter/material.dart';
import 'package:flutter_stripe/flutter_stripe.dart';
import 'package:stripe_example/utils.dart';
import 'package:stripe_example/widgets/example_scaffold.dart';
import 'package:stripe_example/widgets/response_card.dart';

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

@override
State<AddressSheetExample> createState() => _AddressSheetExampleState();
}

class _AddressSheetExampleState extends State<AddressSheetExample> {
bool isCompleted = false;

String? result;

@override
void initState() {
super.initState();
}

@override
void dispose() {
super.dispose();
}

@override
Widget build(BuildContext context) {
return ExampleScaffold(
title: 'Addresssheet',
tags: ['Address sheet'],
padding: EdgeInsets.symmetric(horizontal: 16),
children: [
AddressSheet(
onError: (error) {
setState(() {
result = error.error.localizedMessage;
});
},
onSubmit: (details) {
setState(() {
result = details.toJson().toPrettyString();
});
},
params: AddressSheetParams(),
),
Divider(),
SizedBox(height: 20),
ResponseCard(
response: result ?? '',
),
],
);
}
}
8 changes: 8 additions & 0 deletions example/lib/screens/screens.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:stripe_example/screens/address_sheet/address_sheet.dart';
import 'package:stripe_example/screens/customer_sheet/customer_sheet_screen.dart';
import 'package:stripe_example/screens/others/can_add_to_wallet_screen.dart';
import 'package:stripe_example/screens/payment_sheet/express_checkout/express_checkout_element.dart';
Expand Down Expand Up @@ -143,6 +144,13 @@ class Example extends StatelessWidget {
],
expanded: true,
),
ExampleSection(title: 'Address sheet', children: [
Example(
title: 'Address sheet',
builder: (context) => AddressSheetExample(),
platformsSupported: [DevicePlatform.android, DevicePlatform.ios],
),
]),
ExampleSection(title: 'Customer sheet', children: [
Example(
title: 'Customer sheet',
Expand Down
1 change: 1 addition & 0 deletions packages/stripe/lib/flutter_stripe.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export 'package:stripe_platform_interface/stripe_platform_interface.dart';

export 'src/model/apple_pay_button.dart';
export 'src/stripe.dart';
export 'src/widgets/adresssheet.dart';

Check warning on line 5 in packages/stripe/lib/flutter_stripe.dart

View workflow job for this annotation

GitHub Actions / Typo CI

adresssheet

"adresssheet" is a typo. Did you mean "addressee"?
// export 'src/widgets/apple_pay_button.dart';
export 'src/widgets/aubecs_debit_form.dart';
export 'src/widgets/card_field.dart';
Expand Down
34 changes: 23 additions & 11 deletions packages/stripe/lib/src/widgets/adresssheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class AddressSheet extends StatelessWidget {
final int height;

/// Called when the user submits their information
final OnAddressSheetError onSubmit;
final OnAddressSheetSubmit onSubmit;

/// Called when the user taps the button to close the sheet before submitting their information, or when an error occurs
final OnAddressSheetError onError;
Expand All @@ -31,7 +31,7 @@ class AddressSheet extends StatelessWidget {
return _AddressSheet(
onSubmit: onSubmit,
onError: onError,
params: params,
addressSheetParams: params,
height: height,
);
}
Expand All @@ -42,11 +42,11 @@ class _AddressSheet extends StatefulWidget {
required this.onSubmit,
required this.onError,
required this.height,
required this.params,
required this.addressSheetParams,
});

final AddressSheetParams params;
final OnAddressSheetError onSubmit;
final AddressSheetParams addressSheetParams;
final OnAddressSheetSubmit onSubmit;
final OnAddressSheetError onError;
final int height;

Expand All @@ -61,11 +61,23 @@ class _AddressSheetState extends State<_AddressSheet> {
void onPlatformViewCreated(int viewId) {
_methodChannel = MethodChannel('flutter.stripe/address_sheet/$viewId');
_methodChannel?.setMethodCallHandler((call) async {
if (call.method == 'onSubmit') {
print('blaat details ${call.arguments}');
// widget.onSubmit.call();
} else if (call.method == 'onError') {
print('blaat details ${call.arguments}');
if (call.method == 'onSubmitAction') {
final tmp = Map<String, dynamic>.from(call.arguments as Map);
final tmpAdress = Map<String, dynamic>.from(tmp['address'] as Map);

Check warning on line 66 in packages/stripe/lib/src/widgets/adresssheet.dart

View workflow job for this annotation

GitHub Actions / Typo CI

tmpAdress

"tmpAdress" is a typo. Did you mean "tmpDress"?

widget.onSubmit(
CollectAddressResult(
address: Address.fromJson(tmpAdress),

Check warning on line 70 in packages/stripe/lib/src/widgets/adresssheet.dart

View workflow job for this annotation

GitHub Actions / Typo CI

tmpAdress

"tmpAdress" is a typo. Did you mean "tmpDress"?
name: tmp['name'] as String,
phoneNumber: tmp['phone'] as String?,
),
);
} else if (call.method == 'onErrorAction') {
final tmp = Map<String, dynamic>.from(call.arguments as Map);
final foo = Map<String, dynamic>.from(tmp['error'] as Map);

widget.onError(
StripeException(error: LocalizedErrorMessage.fromJson(foo)));
}
});
}
Expand Down Expand Up @@ -94,7 +106,7 @@ class _AddressSheetState extends State<_AddressSheet> {
id: params.id,
viewType: _viewType,
layoutDirection: TextDirection.ltr,
creationParams: {},
creationParams: widget.addressSheetParams.toJson(),
creationParamsCodec: const StandardMessageCodec(),
)
..addOnPlatformViewCreatedListener(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.flutter.stripe

import android.content.Context
import android.view.View
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.uimanager.ThemedReactContext

Check warning on line 7 in packages/stripe_android/android/src/main/kotlin/com/flutter/stripe/StripeAddressSheetPlatformView.kt

View workflow job for this annotation

GitHub Actions / Typo CI

uimanager

"uimanager" is a typo. Did you mean "manager"?
import com.reactnativestripesdk.StripeSdkModule
import com.reactnativestripesdk.addresssheet.AddressSheetView
import com.reactnativestripesdk.addresssheet.AddressSheetViewManager
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.platform.PlatformView

class StripeAddressSheetPlatformView(
private val context: Context,
private val channel: MethodChannel,
id: Int,
private val creationParams: Map<String?, Any?>?,
private val addressSheetManager: AddressSheetViewManager,
private val sdkAccessor: () -> StripeSdkModule

) : PlatformView, MethodChannel.MethodCallHandler {
private val themedContext = ThemedReactContext(sdkAccessor().reactContext, channel, sdkAccessor)


lateinit var addressSheetView: AddressSheetView

init {

addressSheetView = addressSheetManager.createViewInstance(themedContext)
channel.setMethodCallHandler(this)

if (creationParams?.containsKey("visible") == true) {
addressSheetManager.setVisible(addressSheetView, creationParams?.containsKey("visible") as Boolean)
}

if (creationParams?.containsKey("appearance") == true) {
addressSheetManager.setAppearance(
addressSheetView, ReadableMap(creationParams["appearance"] as Map<String, Any>)
)
}

if (creationParams?.containsKey("defaultValues") == true) {
addressSheetManager.setDefaultValues(
addressSheetView, ReadableMap(creationParams["defaultValues"] as Map<String, Any>)
)
}

if (creationParams?.containsKey("additionalFields") == true) {
addressSheetManager.setAdditionalFields(
addressSheetView, ReadableMap(creationParams["additionalFields"] as Map<String, Any>)
)
}

if (creationParams?.containsKey("allowedCountries") == true) {
addressSheetManager.setAllowedCountries(
addressSheetView, ReadableArray(creationParams["allowedCountries"] as List<String>)
)
}

if (creationParams?.containsKey("autocompleteCountries") == true) {
addressSheetManager.setAllowedCountries(
addressSheetView, ReadableArray(creationParams["autocompleteCountries"] as List<String>)
)
}

if (creationParams?.containsKey("primaryButtonTitle") == true) {
addressSheetManager.setPrimaryButtonTitle(addressSheetView, creationParams?.containsKey("primaryButtonTitle") as String)
}

if (creationParams?.containsKey("sheetTitle") == true) {
addressSheetManager.setPrimaryButtonTitle(addressSheetView, creationParams?.containsKey("sheetTitle") as String)
}

if (creationParams?.containsKey("googlePlacesApiKey") == true) {
addressSheetManager.setPrimaryButtonTitle(addressSheetView, creationParams?.containsKey("googlePlacesApiKey") as String)
}

}

override fun getView(): View? {
return addressSheetView
}

override fun dispose() {
addressSheetManager.onDropViewInstance(addressSheetView)
}

override fun onFlutterViewAttached(flutterView: View) {
addressSheetManager.onAfterUpdateTransaction(addressSheetView)
}

override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ class StripeAddressSheetPlatformViewFactory(
if(context == null){
throw AssertionError("Context is not allowed to be null when launching aubecs view.")
}
return AddressSheetPlatformView(context,channel,viewId, creationParams, addresSheetViewManager, sdkAccessor)
return StripeAddressSheetPlatformView(context,channel,viewId, creationParams, addresSheetViewManager, sdkAccessor)

Check warning on line 24 in packages/stripe_android/android/src/main/kotlin/com/flutter/stripe/StripeAddressSheetPlatformViewFactory.kt

View workflow job for this annotation

GitHub Actions / Typo CI

addresSheetViewManager

"addresSheetViewManager" is a typo. Did you mean "addersSheetViewManager"?
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ class CollectAddressResult with _$CollectAddressResult {
typedef OnAddressSheetSubmit = FutureOr<void> Function(
CollectAddressResult result);

typedef OnAddressSheetError = FutureOr<void> Function(
StripeError<AddressSheetError> error);
typedef OnAddressSheetError = FutureOr<void> Function(StripeException error);

@freezed
class AddressSheetParams with _$AddressSheetParams {
Expand Down

0 comments on commit 1cd8ad5

Please sign in to comment.