fix(android): ensure the plugin remains the onActivityResult listener

pull/2/merge
codinronan 5 years ago
parent 4eb19c8351
commit 855a1a68f7

@ -183,6 +183,8 @@ public class StripePaymentsPlugin extends CordovaPlugin {
PaymentConfiguration.init(StripePluginConfig.getInstance().publishableKey); PaymentConfiguration.init(StripePluginConfig.getInstance().publishableKey);
stripeInstance.setDefaultPublishableKey(StripePluginConfig.getInstance().publishableKey); stripeInstance.setDefaultPublishableKey(StripePluginConfig.getInstance().publishableKey);
setupCustomerSession(null);
message.put("status", "INIT_SUCCESS"); message.put("status", "INIT_SUCCESS");
message.remove("error"); message.remove("error");
successCallback(callbackContext, StripePluginUtils.mapToJSON(message)); successCallback(callbackContext, StripePluginUtils.mapToJSON(message));
@ -213,20 +215,24 @@ public class StripePaymentsPlugin extends CordovaPlugin {
JSONObject headers = paymentConfig.optJSONObject("extraHTTPHeaders"); JSONObject headers = paymentConfig.optJSONObject("extraHTTPHeaders");
StripePluginConfig.getInstance().extraHTTPHeaders = StripePluginUtils.parseExtraHeaders(headers, new HashMap<>()); StripePluginConfig.getInstance().extraHTTPHeaders = StripePluginUtils.parseExtraHeaders(headers, new HashMap<>());
setupCustomerSession();
setupPaymentSession();
StripePaymentConfig.getInstance().price = paymentConfig.optLong("price", 0L); StripePaymentConfig.getInstance().price = paymentConfig.optLong("price", 0L);
StripePaymentConfig.getInstance().currency = paymentConfig.optString("currency", "USD"); StripePaymentConfig.getInstance().currency = paymentConfig.optString("currency", "USD");
StripePaymentConfig.getInstance().country = paymentConfig.optString("country", "US"); StripePaymentConfig.getInstance().country = paymentConfig.optString("country", "US");
mPaymentSession.setCartTotal(StripePaymentConfig.getInstance().price); setupCustomerSession(new CreateCustomerSessionListener() {
mPaymentSession.presentPaymentMethodSelection(); @Override
public void onKeyRetrieved(String key) {
message.clear(); // For now, we won't bother checking if key is null or non-null (error or not)
message.put("status", "PAYMENT_DIALOG_SHOWN"); setupPaymentSession();
successCallback(callbackContext, StripePluginUtils.mapToJSON(message)); cordova.setActivityResultCallback(StripePaymentsPlugin.this);
mPaymentSession.setCartTotal(StripePaymentConfig.getInstance().price);
mPaymentSession.presentPaymentMethodSelection();
message.clear();
message.put("status", "PAYMENT_DIALOG_SHOWN");
successCallback(callbackContext, StripePluginUtils.mapToJSON(message));
}
});
} }
// Android does in 1 step what requires 2 steps on iOS. Android saves the payment method // Android does in 1 step what requires 2 steps on iOS. Android saves the payment method
@ -311,7 +317,8 @@ public class StripePaymentsPlugin extends CordovaPlugin {
* *
*/ */
private void setupCustomerSession() { private void setupCustomerSession(@Nullable CreateCustomerSessionListener listener) {
CustomerSession.endCustomerSession();
// CustomerSession only needs to be initialized once per app. // CustomerSession only needs to be initialized once per app.
CustomerSession.initCustomerSession( CustomerSession.initCustomerSession(
new StripePluginEphemeralKeyProvider( new StripePluginEphemeralKeyProvider(
@ -322,6 +329,13 @@ public class StripePaymentsPlugin extends CordovaPlugin {
new AlertDialog.Builder(getApplicationContext()) new AlertDialog.Builder(getApplicationContext())
.setMessage(string) .setMessage(string)
.show(); .show();
if (listener != null) {
listener.onKeyRetrieved(null);
}
return;
}
if (listener != null) {
listener.onKeyRetrieved(string);
} }
} }
})); }));
@ -451,6 +465,7 @@ public class StripePaymentsPlugin extends CordovaPlugin {
// Once a 3DS Source is created, that is used // Once a 3DS Source is created, that is used
// to initiate the third-party verification // to initiate the third-party verification
mRedirectSource = source; mRedirectSource = source;
cordova.setActivityResultCallback(StripePaymentsPlugin.this);
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(source.getRedirect().getUrl())); Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(source.getRedirect().getUrl()));
getActivity().startActivity(browserIntent); getActivity().startActivity(browserIntent);
} }
@ -514,4 +529,8 @@ public class StripePaymentsPlugin extends CordovaPlugin {
return result; return result;
} }
private interface CreateCustomerSessionListener {
void onKeyRetrieved(@Nullable String key);
}
} }

Loading…
Cancel
Save