Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync with Stripe React Native 0.28.0 #1304

Merged
merged 4 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@
files = (
);
inputPaths = (
"${TARGET_BUILD_DIR}/${INFOPLIST_PATH}",
);
name = "Thin Binary";
outputPaths = (
Expand Down
2 changes: 1 addition & 1 deletion packages/stripe/lib/src/stripe.dart
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ class Stripe {

/// Call this method when the user logs out from your app.
///
/// This will ensur ethat any persisted authentication state in the
/// This will ensure that any persisted authentication state in the
/// paymentsheet, such as authentication cookies are cleared during logout.
Future<void> resetPaymentSheetCustomer() async {
await _awaitForSettings();
Expand Down
10 changes: 10 additions & 0 deletions packages/stripe/lib/src/widgets/card_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class CardField extends StatefulWidget {
this.countryCode,
this.style,
this.autofocus = false,
this.disabled = false,
this.dangerouslyGetFullCardDetails = false,
this.dangerouslyUpdateFullCardDetails = false,
this.cursorColor,
Expand Down Expand Up @@ -77,6 +78,11 @@ class CardField extends StatefulWidget {
/// Default is `false`.
final bool autofocus;

/// When true it applies a state that does not allow the user to interact with
/// the card form field.
/// Default is `false`.
final bool disabled;

/// Controller that can be use to execute several operations on the cardfield
/// e.g (clear).
final CardEditController? controller;
Expand Down Expand Up @@ -178,6 +184,7 @@ class _CardFieldState extends State<CardField> {
delegate: const _NegativeMarginLayout(margin: platformMargin),
child: _MethodChannelCardField(
controller: controller,
disabled: widget.disabled,
height: platformCardHeight,
androidPlatformViewRenderType:
widget.androidPlatformViewRenderType,
Expand Down Expand Up @@ -274,6 +281,7 @@ class _MethodChannelCardField extends StatefulWidget {
this.style,
this.placeholder,
this.enablePostalCode = false,
this.disabled = false,
this.countryCode,
double? width,
double? height = kCardFieldDefaultHeight,
Expand All @@ -298,6 +306,7 @@ class _MethodChannelCardField extends StatefulWidget {
final String? countryCode;
final FocusNode focusNode;
final bool autofocus;
final bool disabled;
final CardEditController controller;
final bool dangerouslyGetFullCardDetails;
final bool dangerouslyUpdateFullCardDetails;
Expand Down Expand Up @@ -395,6 +404,7 @@ class _MethodChannelCardFieldState extends State<_MethodChannelCardField>
controller.initalDetails != null)
'cardDetails': controller.initalDetails?.toJson(),
'autofocus': widget.autofocus,
'disabled': widget.disabled,
};

Widget platform;
Expand Down
34 changes: 22 additions & 12 deletions packages/stripe/lib/src/widgets/card_form_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@ import 'keep_visible_on_focus.dart';
/// ![Sripe Card Form]
/// (https://github.com/flutter-stripe/flutter_stripe/tree/main/docs/assets/card_form.png)
class CardFormField extends StatefulWidget {
const CardFormField(
{this.onCardChanged,
Key? key,
this.onFocus,
this.enablePostalCode = true,
this.countryCode,
this.style,
this.autofocus = false,
this.dangerouslyGetFullCardDetails = false,
this.dangerouslyUpdateFullCardDetails = false,
this.controller})
: super(key: key);
const CardFormField({
this.onCardChanged,
Key? key,
this.onFocus,
this.enablePostalCode = true,
this.countryCode,
this.style,
this.autofocus = false,
this.dangerouslyGetFullCardDetails = false,
this.dangerouslyUpdateFullCardDetails = false,
this.disabled = false,
this.controller,
}) : super(key: key);

/// Callback that will be executed when a specific field gets focus.
final CardFocusCallback? onFocus;
Expand Down Expand Up @@ -80,6 +81,11 @@ class CardFormField extends StatefulWidget {
/// Default is `false`.
final bool dangerouslyUpdateFullCardDetails;

/// When true it applies a state that does not allow the user to interact with
/// the card form field.
/// Default is `false`.
final bool disabled;

@override
// ignore: library_private_types_in_public_api
_CardFormFieldState createState() => _CardFormFieldState();
Expand Down Expand Up @@ -187,6 +193,7 @@ class _CardFormFieldState extends State<CardFormField> {
enablePostalCode: widget.enablePostalCode,
onCardChanged: widget.onCardChanged,
autofocus: widget.autofocus,
disabled: widget.disabled,
onFocus: widget.onFocus,
countryCode: widget.countryCode,
);
Expand All @@ -212,6 +219,7 @@ class _MethodChannelCardFormField extends StatefulWidget {
this.dangerouslyGetFullCardDetails = false,
this.dangerouslyUpdateFullCardDetails = false,
this.autofocus = false,
this.disabled = false,
this.countryCode,
}) : assert(constraints == null || constraints.debugAssertIsValid()),
constraints = (width != null || height != null)
Expand All @@ -227,6 +235,7 @@ class _MethodChannelCardFormField extends StatefulWidget {
final bool enablePostalCode;
final FocusNode focusNode;
final bool autofocus;
final bool disabled;
final CardFormEditController controller;
final bool dangerouslyGetFullCardDetails;
final bool dangerouslyUpdateFullCardDetails;
Expand Down Expand Up @@ -299,6 +308,7 @@ class _MethodChannelCardFormFieldState
controller._initalDetails != null)
'cardDetails': controller._initalDetails?.toJson(),
'autofocus': widget.autofocus,
'disabled': widget.disabled,
'defaultValues': {
'countryCode': widget.countryCode,
}
Expand Down
2 changes: 1 addition & 1 deletion packages/stripe_android/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version '1.0-SNAPSHOT'

buildscript {
ext.kotlin_version = '1.8.0'
ext.stripe_version = '20.23.+'
ext.stripe_version = '20.25.+'

repositories {
google()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import com.facebook.react.uimanager.ThemedReactContext
import com.reactnativestripesdk.*
import com.reactnativestripesdk.utils.getIntOrNull
import com.reactnativestripesdk.utils.getValOr
import com.stripe.android.databinding.CardMultilineWidgetBinding
import com.stripe.android.databinding.StripeCardMultilineWidgetBinding
import com.stripe.android.databinding.StripeCardFormViewBinding
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
Expand Down Expand Up @@ -48,6 +48,9 @@ class StripeSdkCardFormPlatformView(
if (creationParams?.containsKey("autofocus") == true) {
cardFormViewManager.setAutofocus(cardView, creationParams["autofocus"] as Boolean)
}
if (creationParams?.containsKey("disabled") == true) {
cardFormViewManager.setDisabled(cardView, creationParams["disabled"] as Boolean)
}
if (creationParams?.containsKey("cardDetails") == true) {
val value = ReadableMap(creationParams["cardDetails"] as Map<String, Any>)

Expand Down Expand Up @@ -101,8 +104,13 @@ class StripeSdkCardFormPlatformView(
cardFormViewManager.setAutofocus(cardView, arguments.getBoolean("autofocus"))
result.success(null)
}
"disabled" -> {
val arguments = ReadableMap(call.arguments as Map<String, Any>)
cardFormViewManager.setDisabled(cardView, arguments.getBoolean("disabled"))
result.success(null)
}
"requestFocus" -> {
val binding = CardMultilineWidgetBinding.bind(cardView.cardForm)
val binding = StripeCardMultilineWidgetBinding.bind(cardView.cardForm)
binding.etCardNumber.requestFocus()
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.facebook.react.uimanager.ThemedReactContext
import com.reactnativestripesdk.*
import com.reactnativestripesdk.utils.getIntOrNull
import com.reactnativestripesdk.utils.getValOr
import com.stripe.android.databinding.CardInputWidgetBinding
import com.stripe.android.databinding.StripeCardInputWidgetBinding
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.platform.PlatformView
Expand Down Expand Up @@ -43,6 +43,9 @@ class StripeSdkCardPlatformView(
if (creationParams?.containsKey("placeholder") == true) {
stripeSdkCardViewManager.setPlaceHolders(cardView, ReadableMap(creationParams["placeholder"] as Map<String, Any>))
}
if (creationParams?.containsKey("disabled") == true) {
stripeSdkCardViewManager.setDisabled(cardView, creationParams["disabled"] as Boolean)
}
if (creationParams?.containsKey("dangerouslyGetFullCardDetails") == true) {
stripeSdkCardViewManager.setDangerouslyGetFullCardDetails(cardView, creationParams["dangerouslyGetFullCardDetails"] as Boolean)
}
Expand All @@ -53,7 +56,7 @@ class StripeSdkCardPlatformView(
val value = ReadableMap(creationParams["cardDetails"] as Map<String, Any>)
stripeSdkCardViewManager.setCardDetails(value, themedContext)

val binding = CardInputWidgetBinding.bind(cardView.mCardWidget)
val binding = StripeCardInputWidgetBinding.bind(cardView.mCardWidget)
val number = getValOr(value, "number", null)
val expirationYear = getIntOrNull(value, "expiryYear")
val expirationMonth = getIntOrNull(value, "expiryMonth")
Expand Down Expand Up @@ -118,8 +121,13 @@ class StripeSdkCardPlatformView(
stripeSdkCardViewManager.setAutofocus(cardView, arguments.getBoolean("autofocus"))
result.success(null)
}
"disabled" -> {
val arguments = ReadableMap(call.arguments as Map<String, Any>)
stripeSdkCardViewManager.setDisabled(cardView, arguments.getBoolean("disabled"))
result.success(null)
}
"requestFocus" -> {
val binding = CardInputWidgetBinding.bind(cardView.mCardWidget)
val binding = StripeCardInputWidgetBinding.bind(cardView.mCardWidget)
binding.cardNumberEditText.requestFocus()
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.google.android.material.shape.MaterialShapeDrawable
import com.google.android.material.shape.ShapeAppearanceModel
import com.reactnativestripesdk.utils.getIntOrNull
import com.reactnativestripesdk.utils.getValOr
import com.stripe.android.databinding.BecsDebitWidgetBinding
import com.stripe.android.databinding.StripeBecsDebitWidgetBinding
import com.stripe.android.model.PaymentMethodCreateParams
import com.stripe.android.view.BecsDebitWidget
import com.stripe.android.view.StripeEditText
Expand All @@ -35,7 +35,7 @@ class AuBECSDebitFormView(private val context: ThemedReactContext) : FrameLayout
if (!this::becsDebitWidget.isInitialized || value == null) {
return
}
val binding = BecsDebitWidgetBinding.bind(becsDebitWidget)
val binding = StripeBecsDebitWidgetBinding.bind(becsDebitWidget)
val textColor = getValOr(value, "textColor", null)
val textErrorColor = getValOr(value, "textErrorColor", null)
val placeholderColor = getValOr(value, "placeholderColor", null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import com.reactnativestripesdk.utils.*
import com.reactnativestripesdk.utils.mapCardBrand
import com.stripe.android.core.model.CountryCode
import com.stripe.android.core.model.CountryUtils
import com.stripe.android.databinding.CardInputWidgetBinding
import com.stripe.android.databinding.StripeCardInputWidgetBinding
import com.stripe.android.model.Address
import com.stripe.android.model.PaymentMethodCreateParams
import com.stripe.android.view.CardInputListener
Expand All @@ -33,7 +33,7 @@ import java.lang.Exception

class CardFieldView(context: ThemedReactContext) : FrameLayout(context) {
internal var mCardWidget: CardInputWidget = CardInputWidget(context)
private val cardInputWidgetBinding = CardInputWidgetBinding.bind(mCardWidget)
private val cardInputWidgetBinding = StripeCardInputWidgetBinding.bind(mCardWidget)
val cardDetails: MutableMap<String, Any?> = mutableMapOf("brand" to "", "last4" to "", "expiryMonth" to null, "expiryYear" to null, "postalCode" to "", "validNumber" to "Unknown", "validCVC" to "Unknown", "validExpiryDate" to "Unknown")
var cardParams: PaymentMethodCreateParams.Card? = null
var cardAddress: Address? = null
Expand Down Expand Up @@ -210,6 +210,10 @@ class CardFieldView(context: ThemedReactContext) : FrameLayout(context) {
}
}

fun setDisabled(isDisabled: Boolean) {
mCardWidget.isEnabled = !isDisabled
}

/**
* We can reliable assume that setPostalCodeEnabled is called before
* setCountryCode because of the order of the props in CardField.tsx
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ class CardFieldViewManager : SimpleViewManager<CardFieldView>() {
view.setPlaceHolders(placeholders)
}

@ReactProp(name = "disabled")
fun setDisabled(view: CardFieldView, isDisabled: Boolean) {
view.setDisabled(isDisabled)
}

override fun createViewInstance(reactContext: ThemedReactContext): CardFieldView {
val stripeSdkModule: StripeSdkModule? = reactContext.getNativeModule(StripeSdkModule::class.java)
val view = CardFieldView(reactContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import com.google.android.material.shape.ShapeAppearanceModel
import com.reactnativestripesdk.utils.*
import com.reactnativestripesdk.utils.mapCardBrand
import com.stripe.android.core.model.CountryCode
import com.stripe.android.databinding.CardMultilineWidgetBinding
import com.stripe.android.databinding.StripeCardMultilineWidgetBinding
import com.stripe.android.databinding.StripeCardFormViewBinding
import com.stripe.android.model.Address
import com.stripe.android.model.PaymentMethodCreateParams
Expand All @@ -37,7 +37,7 @@ class CardFormView(context: ThemedReactContext) : FrameLayout(context) {
var cardParams: PaymentMethodCreateParams.Card? = null
var cardAddress: Address? = null
private val cardFormViewBinding = StripeCardFormViewBinding.bind(cardForm)
private val multilineWidgetBinding = CardMultilineWidgetBinding.bind(cardFormViewBinding.cardMultilineWidget)
private val multilineWidgetBinding = StripeCardMultilineWidgetBinding.bind(cardFormViewBinding.cardMultilineWidget)

init {
cardFormViewBinding.cardMultilineWidgetContainer.isFocusable = true
Expand All @@ -60,6 +60,10 @@ class CardFormView(context: ThemedReactContext) : FrameLayout(context) {
setCountry(defaults.getString("countryCode"))
}

fun setDisabled(isDisabled: Boolean) {
cardForm.isEnabled = !isDisabled
}

private fun setCountry(countryString: String?) {
if (countryString != null) {
cardFormViewBinding.countryLayout.setSelectedCountryCode(CountryCode(countryString))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ class CardFormViewManager : SimpleViewManager<CardFormView>() {
view.setDefaultValues(defaults)
}

@ReactProp(name = "disabled")
fun setDisabled(view: CardFormView, isDisabled: Boolean) {
view.setDisabled(isDisabled)
}

override fun createViewInstance(reactContext: ThemedReactContext): CardFormView {
val stripeSdkModule: StripeSdkModule? = reactContext.getNativeModule(StripeSdkModule::class.java)
val view = CardFormView(reactContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,18 +180,44 @@ class FinancialConnectionsSheetFragment : Fragment() {
val map = WritableNativeMap()
map.putDouble("asOf", balance.asOf * 1000.0)
map.putString("type", mapFromBalanceType(balance.type))
map.putMap("current", balance.current as ReadableMap)
WritableNativeMap().also {
it.putMap("available", balance.cash?.available as ReadableMap)
map.putMap("cash", it)
}
WritableNativeMap().also {
it.putMap("used", balance.credit?.used as ReadableMap)
map.putMap("credit", it)
for (entry in balance.current.entries) {
it.putInt(entry.key, entry.value)
}
map.putMap("current", it)
}
map.putMap("cash", mapFromCashAvailable(balance))
map.putMap("credit", mapFromCreditUsed(balance))

return map
}

private fun mapFromCashAvailable(balance: Balance): WritableNativeMap {
return WritableNativeMap().also { cashMap ->
WritableNativeMap().also { availableMap ->
balance.cash?.available?.entries?.let { entries ->
for (entry in entries) {
availableMap.putInt(entry.key, entry.value)
}
}
cashMap.putMap("available", availableMap)
}
}
}

private fun mapFromCreditUsed(balance: Balance): WritableNativeMap {
return WritableNativeMap().also { creditMap ->
WritableNativeMap().also { usedMap ->
balance.credit?.used?.entries?.let { entries ->
for (entry in entries) {
usedMap.putInt(entry.key, entry.value)
}
}
creditMap.putMap("used", usedMap)
}
}
}

private fun mapFromAccountBalanceRefresh(balanceRefresh: BalanceRefresh?): WritableMap? {
if (balanceRefresh == null) {
return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class AddToWalletButtonView(private val context: ThemedReactContext, private val
}
})
.centerCrop()
.override((widthOverride * scale.toFloat()).toInt(), (heightOverride * scale.toFloat()).toInt())
.override((widthOverride * scale.toInt()), (heightOverride * scale.toInt()))
.into(this)
}
}
Expand Down
Loading