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 #1491

Merged
merged 9 commits into from
Nov 28, 2023
Merged

Sync #1491

Show file tree
Hide file tree
Changes from 4 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
7 changes: 6 additions & 1 deletion packages/stripe/lib/src/widgets/google_pay_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class GooglePayButton extends StatefulWidget {
required this.onTap,
this.type = GooglePayButtonType.pay,
this.buttonType = PlatformButtonType.pay,
this.borderRadius,
this.appearance = PlatformButtonStyle.automatic,
Key? key,
}) : super(key: key);

Expand All @@ -22,6 +24,8 @@ class GooglePayButton extends StatefulWidget {
@Deprecated('Use [buttonType] instead')
final GooglePayButtonType type;

final double? borderRadius;
final PlatformButtonStyle appearance;
final PlatformButtonType buttonType;
final VoidCallback onTap;
}
Expand All @@ -35,7 +39,8 @@ class _GooglePayButtonState extends State<GooglePayButton> {
// ignore: deprecated_member_use_from_same_package
_creationParams['buttonType'] = describeEnum(widget.type);
_creationParams['type'] = widget.buttonType.id;

_creationParams['appearance'] = widget.appearance.id;
_creationParams['borderRadius'] = widget.borderRadius;
super.initState();
}

Expand Down
5 changes: 3 additions & 2 deletions packages/stripe/lib/src/widgets/platform_pay_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ class PlatformPayButton extends StatelessWidget {
/// Defines the displayed text on the button.
final PlatformButtonType type;

/// iOS only, defines the color and border radius of the button
/// Defines the coloring of the button
final PlatformButtonStyle appearance;

/// iOS only, sets the border radius of the corners.
/// Sets the border radius of the corners.
final double borderRadius;

/// ios only, execute a callback when shipping
Expand Down Expand Up @@ -72,6 +72,7 @@ class PlatformPayButton extends StatelessWidget {
return GooglePayButton(
onTap: onPressed,
buttonType: type,
borderRadius: borderRadius,
);
} else if (Platform.isIOS) {
return ApplePayButton(
Expand Down
12 changes: 8 additions & 4 deletions 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.31.+'
ext.stripe_version = '20.34.+'

repositories {
google()
Expand Down Expand Up @@ -48,15 +48,19 @@ dependencies {
implementation 'com.github.bumptech.glide:glide:4.12.0'

implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.4.1"
implementation "com.stripe:stripe-android:$stripe_version"
implementation "com.stripe:financial-connections:$stripe_version"
implementation("com.stripe:stripe-android:$stripe_version") {
exclude group: 'androidx.emoji2', module: 'emoji2'
}
implementation ("com.stripe:financial-connections:$stripe_version") {
exclude group: 'androidx.emoji2', module: 'emoji2'
}
implementation 'com.google.android.material:material:1.6.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4'

// play-services-wallet is already included in stripe-android
compileOnly "com.google.android.gms:play-services-wallet:19.1.0"
compileOnly "com.google.android.gms:play-services-wallet:19.2.0"

// Users need to declare this dependency on their own, otherwise all methods are a no-op
compileOnly 'com.stripe:stripe-android-issuing-push-provisioning:1.1.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ class StripeSdkGooglePayButtonPlatformView(
if (creationParams?.containsKey("type") == true) {
googlePayButtonManager.type(payButton, creationParams["type"] as Int)
}
if (creationParams?.containsKey("appearance") == true) {
googlePayButtonManager.appearance(payButton, creationParams["appearance"] as Int)
}
if (creationParams?.containsKey("borderRadius") == true) {
googlePayButtonManager.borderRadius(payButton, creationParams["borderRadius"] as Int)
}
payButton.initialize()
payButton.getChildAt(0).setOnClickListener {
channel.invokeMethod("onPressed", null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ class GooglePayButtonManager : SimpleViewManager<GooglePayButtonView?>() {
view.setType(buttonType)
}

@ReactProp(name = "appearance")
fun appearance(view: GooglePayButtonView, appearance: Int) {
view.setAppearance(appearance)
}

@ReactProp(name = "borderRadius")
fun borderRadius(view: GooglePayButtonView, borderRadius: Int) {
view.setBorderRadius(borderRadius)
}

override fun createViewInstance(reactContext: ThemedReactContext): GooglePayButtonView {
return GooglePayButtonView(reactContext)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,91 @@
package com.reactnativestripesdk

import android.view.LayoutInflater
import android.util.Log
import android.view.View
import android.widget.FrameLayout
import com.facebook.react.uimanager.PixelUtil
import com.facebook.react.uimanager.ThemedReactContext
import com.flutter.stripe.R
import com.google.android.gms.wallet.button.ButtonConstants.ButtonTheme
import com.google.android.gms.wallet.button.ButtonConstants.ButtonType
import com.google.android.gms.wallet.button.ButtonOptions
import com.google.android.gms.wallet.button.PayButton
import com.stripe.android.GooglePayJsonFactory
import org.json.JSONArray

class GooglePayButtonView(private val context: ThemedReactContext) : FrameLayout(context) {
private var button: View? = null
private var type: Int? = null
private var appearance: Int? = null
private var borderRadius: Int = 4 // Matches the default on iOS's ApplePayButton
private var button: PayButton? = null

fun initialize() {
val resAsset: Int =
when (type) {
0 -> R.layout.plain_googlepay_button
1 -> R.layout.buy_with_googlepay_button
6 -> R.layout.book_with_googlepay_button
5 -> R.layout.checkout_with_googlepay_button
4 -> R.layout.donate_with_googlepay_button
11 -> R.layout.order_with_googlepay_button
1000 -> R.layout.pay_with_googlepay_button
7 -> R.layout.subscribe_with_googlepay_button
1001 -> R.layout.googlepay_mark_button
else -> R.layout.plain_googlepay_button
}
if (button != null) {
removeView(button)
}
button = configureGooglePayButton()
addView(button)
viewTreeObserver.addOnGlobalLayoutListener { requestLayout() }
}

button = LayoutInflater.from(context).inflate(
resAsset, null
private fun configureGooglePayButton(): PayButton {
val googlePayButton = PayButton(
context
)
googlePayButton.initialize(buildButtonOptions())
googlePayButton.setOnClickListener { _ ->
// Call the Javascript TouchableOpacity parent where the onClick handler is set
(this.parent as? View)?.performClick() ?: run {
Log.e("StripeReactNative", "Unable to find parent of GooglePayButtonView.")
}
};
return googlePayButton
}

addView(button)
viewTreeObserver.addOnGlobalLayoutListener { requestLayout() }
private fun buildButtonOptions(): ButtonOptions {
val allowedPaymentMethods = JSONArray().put(
GooglePayJsonFactory(context).createCardPaymentMethod(
billingAddressParameters = null,
allowCreditCards = null
)
).toString()

val options = ButtonOptions.newBuilder()
.setAllowedPaymentMethods(allowedPaymentMethods)

getButtonType()?.let {
options.setButtonType(it)
}

getButtonTheme()?.let {
options.setButtonTheme(it)
}

options.setCornerRadius(PixelUtil.toPixelFromDIP(this.borderRadius.toDouble()).toInt())

return options.build()
}

private fun getButtonType(): Int? {
return when (this.type) {
0,
1 -> ButtonType.BUY
6 -> ButtonType.BOOK
5 -> ButtonType.CHECKOUT
4 -> ButtonType.DONATE
11 -> ButtonType.ORDER
7 -> ButtonType.SUBSCRIBE
1000 -> ButtonType.PAY
1001 -> ButtonType.PLAIN
else -> null
}
}

private fun getButtonTheme(): Int? {
return when (this.appearance) {
0, 1 -> ButtonTheme.LIGHT
2 -> ButtonTheme.DARK
else -> null
}
}

override fun requestLayout() {
Expand All @@ -43,9 +98,18 @@ class GooglePayButtonView(private val context: ThemedReactContext) : FrameLayout
MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY))
button?.layout(left, top, right, bottom)

}

fun setType(type: Int) {
this.type = type
}

fun setAppearance(appearance: Int) {
this.appearance = appearance
}

fun setBorderRadius(borderRadius: Int) {
this.borderRadius = borderRadius
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class GooglePayLauncherFragment : Fragment() {
this.mode = mode
this.callback = callback
this.currencyCode = googlePayParams.getString("currencyCode") ?: "USD"
this.amount = googlePayParams.getInt("amount")
this.amount = getIntOrNull(googlePayParams, "amount")
this.label = googlePayParams.getString("label")
this.configuration = GooglePayLauncher.Config(
environment = if (googlePayParams.getBoolean("testEnv")) GooglePayEnvironment.Test else GooglePayEnvironment.Production,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ class PaymentLauncherFragment(
StripeIntent.NextActionType.WeChatPayRedirect,
StripeIntent.NextActionType.UpiAwaitNotification,
StripeIntent.NextActionType.CashAppRedirect,
StripeIntent.NextActionType.SwishRedirect,
null, -> false
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class PaymentMethodCreateParamsFactory(
PaymentMethod.Type.PayPal -> createPayPalParams()
PaymentMethod.Type.Affirm -> createAffirmParams()
PaymentMethod.Type.CashAppPay -> createCashAppParams()
PaymentMethod.Type.RevolutPay -> createRevolutPayParams()
else -> {
throw Exception("This paymentMethodType is not supported yet")
}
Expand Down Expand Up @@ -208,6 +209,11 @@ class PaymentMethodCreateParamsFactory(
return PaymentMethodCreateParams.createCashAppPay(billingDetailsParams)
}

@Throws(PaymentMethodCreateParamsException::class)
private fun createRevolutPayParams(): PaymentMethodCreateParams {
return PaymentMethodCreateParams.createRevolutPay(billingDetailsParams)
}

@Throws(PaymentMethodCreateParamsException::class)
fun createParams(clientSecret: String, paymentMethodType: PaymentMethod.Type?, isPaymentIntent: Boolean): ConfirmStripeIntentParams {
try {
Expand All @@ -230,7 +236,8 @@ class PaymentMethodCreateParamsFactory(
PaymentMethod.Type.AuBecsDebit,
PaymentMethod.Type.Klarna,
PaymentMethod.Type.PayPal,
PaymentMethod.Type.CashAppPay -> {
PaymentMethod.Type.CashAppPay,
PaymentMethod.Type.RevolutPay -> {
val params = createPaymentMethodParams(paymentMethodType)

return if (isPaymentIntent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ internal fun mapPaymentMethodType(type: PaymentMethod.Type?): String {
PaymentMethod.Type.PayPal -> "PayPal"
PaymentMethod.Type.Affirm -> "Affirm"
PaymentMethod.Type.CashAppPay -> "CashApp"
PaymentMethod.Type.RevolutPay -> "RevolutPay"
else -> "Unknown"
}
}
Expand Down Expand Up @@ -154,6 +155,7 @@ internal fun mapToPaymentMethodType(type: String?): PaymentMethod.Type? {
"PayPal" -> PaymentMethod.Type.PayPal
"Affirm" -> PaymentMethod.Type.Affirm
"CashApp" -> PaymentMethod.Type.CashAppPay
"RevolutPay" -> PaymentMethod.Type.RevolutPay
else -> null
}
}
Expand Down Expand Up @@ -502,6 +504,12 @@ internal fun mapNextAction(type: NextActionType?, data: NextActionData?): Writab
nextActionMap.putString("voucherURL", it.hostedVoucherUrl)
}
}
NextActionType.SwishRedirect -> {
(data as? NextActionData.SwishRedirect)?.let {
nextActionMap.putString("type", "swishRedirect")
nextActionMap.putString("mobileAuthUrl", it.mobileAuthUrl)
}
}
}
return nextActionMap
}
Expand Down Expand Up @@ -898,13 +906,22 @@ internal fun mapFromShippingContact(googlePayResult: GooglePayResult): WritableM
googlePayResult.name
name.putString("givenName", googlePayResult.shippingInformation?.name)
map.putMap("name", name)
map.putString("phoneNumber", googlePayResult.phoneNumber)
googlePayResult.shippingInformation?.phone?.let {
map.putString("phoneNumber", it)
} ?: run {
map.putString("phoneNumber", googlePayResult?.phoneNumber)
}
val postalAddress = WritableNativeMap()
postalAddress.putString("city", googlePayResult.shippingInformation?.address?.city)
postalAddress.putString("country", googlePayResult.shippingInformation?.address?.country)
postalAddress.putString("postalCode", googlePayResult.shippingInformation?.address?.postalCode)
postalAddress.putString("state", googlePayResult.shippingInformation?.address?.state)
postalAddress.putString("street", googlePayResult.shippingInformation?.address?.line1 + "\n" + googlePayResult.shippingInformation?.address?.line2)
val line1: String? = googlePayResult.shippingInformation?.address?.line1
val line2: String? = googlePayResult.shippingInformation?.address?.line2
val street =
(if (line1 != null) "$line1" else "") +
(if (line2 != null) "\n$line2" else "")
postalAddress.putString("street", street)
postalAddress.putString("isoCountryCode", googlePayResult.shippingInformation?.address?.country)
map.putMap("postalAddress", postalAddress)
return map
Expand Down
Loading
Loading