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

DRAFT: provide methods for the Pay plugin #3

Merged
merged 4 commits into from
Apr 2, 2021
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
11 changes: 11 additions & 0 deletions stripe/lib/src/stripe.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ class Stripe {
}
}

Future<PaymentMethod> createPaymentMethodFromGooglePay(
Map<String, dynamic> data) async {
try {
final paymentMethod =
await _platform.createPaymentMethodFromGooglePay(data);
return paymentMethod;
} on StripeError catch (error) {
throw StripeError.generic(message: error.message, code: error.message);
}
}

Future<PaymentIntent> retrievePaymentIntent(String clientSecret) async {
try {
final paymentMethod = await _platform.retrievePaymentIntent(clientSecret);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.facebook.react.bridge

import android.content.Intent
import com.stripe.android.Stripe
import io.flutter.plugin.common.PluginRegistry.ActivityResultListener

abstract class BaseActivityEventListener(private val stripeProvider: () -> Stripe?) : ActivityEventListener, ActivityResultListener {
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent): Boolean {
if (stripeProvider()?.isAuthenticateSourceResult(requestCode, data) == true) {
onActivityResult(null, requestCode, resultCode, data)
}
return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import androidx.annotation.NonNull
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReadableMap
import com.reactnativestripesdk.StripeSdkModule
import com.stripe.android.model.GooglePayResult
import com.stripe.android.model.PaymentMethodCreateParams
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import org.json.JSONObject

/** StripeAndroidPlugin */
class StripeAndroidPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
Expand Down Expand Up @@ -63,6 +66,10 @@ class StripeAndroidPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
clientSecret = call.requiredArgument("clientSecret"),
promise = Promise(result)
)
"createPaymentMethodFromGooglePay" -> stripeSdk.createPaymentMethodFromGooglePay(
data = call.requiredArgument("data"),
promise = Promise(result)
)
else -> result.notImplemented()
}
}
Expand Down Expand Up @@ -94,7 +101,8 @@ private inline fun <reified T> MethodCall.optionalArgument(key: String): T? {

private inline fun <reified T> MethodCall.requiredArgument(key: String): T {
if (T::class.java == ReadableMap::class.java) {
return ReadableMap(argument<Map<String, Any>>(key) ?: error("Required parameter $key not set")) as T
return ReadableMap(argument<Map<String, Any>>(key)
?: error("Required parameter $key not set")) as T
}
return argument<T>(key) ?: error("Required parameter $key not set")
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.reactnativestripesdk

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.AsyncTask
import com.facebook.react.bridge.*
import com.stripe.android.*
import com.stripe.android.model.*
import com.stripe.android.model.PaymentMethod
import com.stripe.android.model.PaymentMethodCreateParams
import com.stripe.android.model.StripeIntent
import com.stripe.android.model.Token
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import org.json.JSONObject

class StripeSdkModule(context: ActivityPluginBinding) : ReactContextBaseJavaModule(context) {
override fun getName(): String {
Expand All @@ -20,7 +23,13 @@ class StripeSdkModule(context: ActivityPluginBinding) : ReactContextBaseJavaModu
private var handleCardActionPromise: Promise? = null
private var confirmSetupIntentPromise: Promise? = null

private val mActivityEventListener = object : BaseActivityEventListener() {
private val mActivityEventListener = object : BaseActivityEventListener(
stripeProvider = {
if (this::stripe.isInitialized) {
stripe
} else null
}
) {
override fun onActivityResult(activity: Activity, requestCode: Int, resultCode: Int, data: Intent) {
stripe.onSetupResult(requestCode, data, object : ApiResultCallback<SetupIntentResult> {
override fun onSuccess(result: SetupIntentResult) {
Expand Down Expand Up @@ -223,4 +232,29 @@ class StripeSdkModule(context: ActivityPluginBinding) : ReactContextBaseJavaModu
promise.reject(ConfirmPaymentErrorType.Failed.toString(), error.localizedMessage)
}
}


/// not part of original plugin

@ReactMethod
fun createPaymentMethodFromGooglePay(data: ReadableMap, promise: Promise) {
val paymentDataJson = JSONObject(data)

//val shippingInformation = GooglePayResult.fromJson(paymentDataJson).shippingInformation
// TODO do we need it?
val paymentMethodCreateParams = PaymentMethodCreateParams.createFromGooglePay(paymentDataJson)

stripe.createPaymentMethod(
paymentMethodCreateParams,
callback = object : ApiResultCallback<PaymentMethod> {
override fun onError(e: Exception) {
confirmPromise?.reject("Failed", e.localizedMessage)
}

override fun onSuccess(result: PaymentMethod) {
val paymentMethodMap: WritableMap = mapFromPaymentMethod(result)
promise.resolve(paymentMethodMap)
}
})
}
}
10 changes: 10 additions & 0 deletions stripe_platform_interface/lib/src/method_channel_stripe.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ class MethodChannelStripe extends StripePlatform {
return PaymentMethod.fromJson(result.unfoldToNonNull());
}

@override
Future<PaymentMethod> createPaymentMethodFromGooglePay(
Map<String, dynamic> data) async {
final result =
await _methodChannel.invokeMethod('createPaymentMethodFromGooglePay', {
'data': data,
});
return PaymentMethod.fromJson(result.unfoldToNonNull());
}

@override
Future<void> configure3dSecure(ThreeDSecureConfigurationParams params) {
// TODO: implement configure3dSecure
Expand Down
3 changes: 3 additions & 0 deletions stripe_platform_interface/lib/stripe_platform_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ abstract class StripePlatform extends PlatformInterface {
Map<String, String> options = const {},
]);

Future<PaymentMethod> createPaymentMethodFromGooglePay(
Map<String, dynamic> data);

Future<PaymentIntent> handleCardAction(String paymentIntentClientSecret);
Future<PaymentIntent> confirmPaymentMethod(
String paymentIntentClientSecret, PaymentMethodParams data,
Expand Down