-
Notifications
You must be signed in to change notification settings - Fork 414
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
Update ReactNativePaymentsModule.java #23
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,19 @@ | |
import com.google.android.gms.common.api.ResultCallback; | ||
import com.google.android.gms.common.ConnectionResult; | ||
import com.google.android.gms.identity.intents.model.UserAddress; | ||
import com.google.android.gms.wallet.*; | ||
import com.google.android.gms.wallet.Cart; | ||
import com.google.android.gms.wallet.FullWallet; | ||
import com.google.android.gms.wallet.FullWalletRequest; | ||
import com.google.android.gms.wallet.MaskedWallet; | ||
import com.google.android.gms.wallet.MaskedWalletRequest; | ||
import com.google.android.gms.wallet.PaymentMethodTokenizationParameters; | ||
import com.google.android.gms.wallet.PaymentMethodTokenizationType; | ||
import com.google.android.gms.wallet.Wallet; | ||
import com.google.android.gms.wallet.WalletConstants; | ||
import com.google.android.gms.wallet.IsReadyToPayRequest; | ||
import com.google.android.gms.wallet.LineItem; | ||
|
||
|
||
|
||
import com.facebook.react.bridge.ActivityEventListener; | ||
import com.facebook.react.bridge.BaseActivityEventListener; | ||
|
@@ -54,6 +66,10 @@ public class ReactNativePaymentsModule extends ReactContextBaseJavaModule implem | |
|
||
public static final String REACT_CLASS = "ReactNativePayments"; | ||
|
||
private int theme = WalletConstants.THEME_LIGHT; | ||
|
||
private ArrayList<Integer> cardNetworks = new ArrayList<Integer>(); | ||
|
||
private static ReactApplicationContext reactContext = null; | ||
|
||
private final ActivityEventListener mActivityEventListener = new BaseActivityEventListener() { | ||
|
@@ -141,8 +157,52 @@ public String getName() { | |
return REACT_CLASS; | ||
} | ||
|
||
|
||
|
||
@Override public Map<String, Object> getConstants() { | ||
final Map<String, Object> constants = new HashMap<>(); | ||
constants.put("ENVIRONMENT_TEST", WalletConstants.ENVIRONMENT_TEST); | ||
constants.put("ENVIRONMENT_PRODUCTION", WalletConstants.ENVIRONMENT_PRODUCTION); | ||
|
||
constants.put("THEME_DARK", WalletConstants.THEME_DARK); | ||
constants.put("THEME_LIGHT", WalletConstants.THEME_LIGHT); | ||
|
||
constants.put("CARD_NETWORK_AMEX", WalletConstants.CardNetwork.AMEX); | ||
constants.put("CARD_NETWORK_MASTERCARD", WalletConstants.CardNetwork.MASTERCARD); | ||
constants.put("CARD_NETWORK_VISA", WalletConstants.CardNetwork.VISA); | ||
constants.put("CARD_NETWORK_DISCOVER", WalletConstants.CardNetwork.DISCOVER); | ||
constants.put("CARD_NETWORK_INTERAC", WalletConstants.CardNetwork.INTERAC); | ||
constants.put("CARD_NETWORK_JCB", WalletConstants.CardNetwork.JCB); | ||
constants.put("CARD_NETWORK_OTHER", WalletConstants.CardNetwork.OTHER); | ||
|
||
constants.put("PAYMENT_METHOD_TOKENIZATION_TYPE_PAYMENT_GATEWAY", PaymentMethodTokenizationType.PAYMENT_GATEWAY); // Adyen,Braintree,Stripe,Vantiv | ||
constants.put("PAYMENT_METHOD_TOKENIZATION_TYPE_NETWORK_TOKEN", PaymentMethodTokenizationType.NETWORK_TOKEN); | ||
// constants.put("PAYMENT_METHOD_TOKENIZATION_TYPE_DIRECT", WalletConstants.PAYMENT_METHOD_TOKENIZATION_TYPE_DIRECT); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this still needed? |
||
|
||
return constants; | ||
} | ||
|
||
|
||
// Public Methods | ||
// --------------------------------------------------------------------------------------------- | ||
@ReactMethod | ||
public void setTheme(int theme) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Developers shouldn't have to interactive with the native module to set the theme. Instead, I'd expose this via the configuration |
||
if (theme == WalletConstants.THEME_LIGHT || theme == WalletConstants.THEME_DARK); | ||
this.theme = theme; | ||
} | ||
@ReactMethod | ||
public void setLightTheme(int theme) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's drop the theme parameter here since it isn't being used. |
||
this.theme = WalletConstants.THEME_LIGHT; | ||
} | ||
@ReactMethod | ||
public void setDarkTheme(int theme) { | ||
this.theme = WalletConstants.THEME_DARK; | ||
} | ||
@ReactMethod | ||
public void addAllowedCardNetwork(int network) { | ||
cardNetworks.add(network); | ||
} | ||
|
||
@ReactMethod | ||
public void getSupportedGateways(Callback errorCallback, Callback successCallback) { | ||
WritableNativeArray supportedGateways = new WritableNativeArray(); | ||
|
@@ -153,10 +213,12 @@ public void getSupportedGateways(Callback errorCallback, Callback successCallbac | |
@ReactMethod | ||
public void canMakePayments(ReadableMap paymentMethodData, Callback errorCallback, Callback successCallback) { | ||
final Callback callback = successCallback; | ||
IsReadyToPayRequest req = IsReadyToPayRequest.newBuilder() | ||
.addAllowedCardNetwork(WalletConstants.CardNetwork.MASTERCARD) | ||
.addAllowedCardNetwork(WalletConstants.CardNetwork.VISA) | ||
.build(); | ||
|
||
IsReadyToPayRequest.Builder builder = IsReadyToPayRequest.newBuilder(); | ||
for (int network :cardNetworks) { | ||
builder.addAllowedCardNetwork(network); | ||
} | ||
IsReadyToPayRequest req = builder.build(); | ||
|
||
int environment = getEnvironmentFromPaymentMethodData(paymentMethodData); | ||
if (mGoogleApiClient == null) { | ||
|
@@ -251,33 +313,44 @@ public void getFullWalletAndroid( | |
// --------------------------------------------------------------------------------------------- | ||
private static PaymentMethodTokenizationParameters buildTokenizationParametersFromPaymentMethodData(ReadableMap paymentMethodData) { | ||
ReadableMap tokenizationParameters = paymentMethodData.getMap("paymentMethodTokenizationParameters"); | ||
String tokenizationType = tokenizationParameters.getString("tokenizationType"); | ||
|
||
|
||
if (tokenizationType.equals("GATEWAY_TOKEN")) { | ||
ReadableMap parameters = tokenizationParameters.getMap("parameters"); | ||
PaymentMethodTokenizationParameters.Builder parametersBuilder = PaymentMethodTokenizationParameters.newBuilder() | ||
.setPaymentMethodTokenizationType(PaymentMethodTokenizationType.PAYMENT_GATEWAY) | ||
.addParameter("gateway", parameters.getString("gateway")); | ||
|
||
ReadableMapKeySetIterator iterator = parameters.keySetIterator(); | ||
|
||
while (iterator.hasNextKey()) { | ||
String key = iterator.nextKey(); | ||
|
||
parametersBuilder.addParameter(key, parameters.getString(key)); | ||
} | ||
|
||
return parametersBuilder.build(); | ||
String publicKey = ""; | ||
switch( tokenizationParameters.getString("tokenizationType")) { | ||
/*case WalletConstants.PAYMENT_METHOD_TOKENIZATION_TYPE_DIRECT: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't realize there was a Direct Type. Is it worth uncommenting and adding it to the PR? |
||
publicKey = tokenizationParameters.getMap("parameters").getString("publicKey"); | ||
return PaymentMethodTokenizationParameters.newBuilder() | ||
.setPaymentMethodTokenizationType(PaymentMethodTokenizationType.PAYMENT_DIRECT) | ||
.addParameter("publicKey", publicKey) | ||
.build(); | ||
|
||
break;*/ | ||
case PaymentMethodTokenizationType.PAYMENT_GATEWAY: | ||
ReadableMap parameters = tokenizationParameters.getMap("parameters"); | ||
PaymentMethodTokenizationParameters.Builder parametersBuilder = PaymentMethodTokenizationParameters.newBuilder() | ||
.setPaymentMethodTokenizationType(PaymentMethodTokenizationType.PAYMENT_GATEWAY) | ||
.addParameter("gateway", parameters.getString("gateway")); | ||
|
||
ReadableMapKeySetIterator iterator = parameters.keySetIterator(); | ||
while (iterator.hasNextKey()) { | ||
String key = iterator.nextKey(); | ||
|
||
parametersBuilder.addParameter(key, parameters.getString(key)); | ||
} | ||
|
||
return parametersBuilder.build(); | ||
break; | ||
case PaymentMethodTokenizationType.NETWORK_TOKEN: | ||
publicKey = tokenizationParameters.getMap("parameters").getString("publicKey"); | ||
|
||
return PaymentMethodTokenizationParameters.newBuilder() | ||
.setPaymentMethodTokenizationType(PaymentMethodTokenizationType.NETWORK_TOKEN) | ||
.addParameter("publicKey", publicKey) | ||
.build(); | ||
break; | ||
default: | ||
// TODO warning | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you have planned for a warning? |
||
} | ||
|
||
} else { | ||
String publicKey = tokenizationParameters.getMap("parameters").getString("publicKey"); | ||
|
||
return PaymentMethodTokenizationParameters.newBuilder() | ||
.setPaymentMethodTokenizationType(PaymentMethodTokenizationType.NETWORK_TOKEN) | ||
.addParameter("publicKey", publicKey) | ||
.build(); | ||
} | ||
} | ||
|
||
private static List buildLineItems(ReadableArray displayItems) { | ||
|
@@ -331,7 +404,7 @@ private void sendEvent( | |
} | ||
|
||
private int getEnvironmentFromPaymentMethodData(ReadableMap paymentMethodData) { | ||
return paymentMethodData.hasKey("environment") && paymentMethodData.getString("environment").equals("TEST") | ||
return paymentMethodData.hasKey("environment") && paymentMethodData.getInt("environment") == WalletConstants.ENVIRONMENT_TEST | ||
? WalletConstants.ENVIRONMENT_TEST | ||
: WalletConstants.ENVIRONMENT_PRODUCTION; | ||
} | ||
|
@@ -344,7 +417,7 @@ private void buildGoogleApiClient(Activity currentActivity, int environment) { | |
.addOnConnectionFailedListener(this) | ||
.addApi(Wallet.API, new Wallet.WalletOptions.Builder() | ||
.setEnvironment(environment) | ||
.setTheme(WalletConstants.THEME_LIGHT) | ||
.setTheme(theme) | ||
.build()) | ||
.build(); | ||
mGoogleApiClient.connect(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you confirm that this is the default theme?