From 6f10c3239385a4fee6e09f535ec03333a6e7de81 Mon Sep 17 00:00:00 2001 From: Evan Date: Mon, 5 Aug 2024 10:53:51 +0800 Subject: [PATCH] feat: support set country code and locale after initialization --- .../main/java/com/affirm/android/Affirm.java | 51 +++++++++++++++++-- .../com/affirm/android/AffirmConstants.java | 1 + .../java/com/affirm/android/AffirmTest.java | 14 +++++ 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/affirm/src/main/java/com/affirm/android/Affirm.java b/affirm/src/main/java/com/affirm/android/Affirm.java index 4250261..f609acf 100755 --- a/affirm/src/main/java/com/affirm/android/Affirm.java +++ b/affirm/src/main/java/com/affirm/android/Affirm.java @@ -32,6 +32,7 @@ import static android.app.Activity.RESULT_CANCELED; import static android.app.Activity.RESULT_OK; import static com.affirm.android.AffirmColor.AFFIRM_COLOR_TYPE_BLUE; +import static com.affirm.android.AffirmConstants.AFFIRM_NOT_INITIALIZED_MESSAGE; import static com.affirm.android.AffirmConstants.CHECKOUT_ERROR; import static com.affirm.android.AffirmConstants.CHECKOUT_TOKEN; import static com.affirm.android.AffirmConstants.COUNTY_CODE_CAN; @@ -566,7 +567,7 @@ public static void initialize(@NonNull Configuration configuration) { public static void setPublicKeyAndMerchantName(@NonNull String publicKey, @Nullable String merchantName) { if (!isInitialized()) { - AffirmLog.w("Affirm has not been initialized"); + AffirmLog.w(AFFIRM_NOT_INITIALIZED_MESSAGE); return; } @@ -584,7 +585,7 @@ public static void setPublicKeyAndMerchantName(@NonNull String publicKey, */ public static void setPublicKey(@NonNull String publicKey) { if (!isInitialized()) { - AffirmLog.w("Affirm has not been initialized"); + AffirmLog.w(AFFIRM_NOT_INITIALIZED_MESSAGE); return; } @@ -601,7 +602,7 @@ public static void setPublicKey(@NonNull String publicKey) { */ public static void setMerchantName(@Nullable String merchantName) { if (!isInitialized()) { - AffirmLog.w("Affirm has not been initialized"); + AffirmLog.w(AFFIRM_NOT_INITIALIZED_MESSAGE); return; } @@ -611,6 +612,50 @@ public static void setMerchantName(@Nullable String merchantName) { .build()); } + /** + * Updates the country code used by Affirm after initialization. + * + * @param countryCode Set the country code to be used by Affirm. Must not be null or empty. + */ + public static void setCountryCode(@NonNull String countryCode) { + if (!isInitialized()) { + AffirmLog.w(AFFIRM_NOT_INITIALIZED_MESSAGE); + return; + } + + if (countryCode.isEmpty()) { + AffirmLog.w("Country code is empty. Please provide a valid country code."); + return; + } + + AffirmPlugins.get().setConfiguration( + new Affirm.Configuration.Builder(AffirmPlugins.get().getConfiguration()) + .setCountryCode(countryCode) + .build()); + } + + /** + * Updates the locale used by Affirm after initialization. + * + * @param locale Set the locale to be used by Affirm. Must not be null or empty. + */ + public static void setLocale(@NonNull String locale) { + if (!isInitialized()) { + AffirmLog.w(AFFIRM_NOT_INITIALIZED_MESSAGE); + return; + } + + if (locale.isEmpty()) { + AffirmLog.w("Locale is empty. Please provide a valid locale string."); + return; + } + + AffirmPlugins.get().setConfiguration( + new Affirm.Configuration.Builder(AffirmPlugins.get().getConfiguration()) + .setLocale(locale) + .build()); + } + private static boolean isInitialized() { return AffirmPlugins.get() != null; } diff --git a/affirm/src/main/java/com/affirm/android/AffirmConstants.java b/affirm/src/main/java/com/affirm/android/AffirmConstants.java index f8f1ba8..886ab07 100755 --- a/affirm/src/main/java/com/affirm/android/AffirmConstants.java +++ b/affirm/src/main/java/com/affirm/android/AffirmConstants.java @@ -125,6 +125,7 @@ private AffirmConstants() { static final String TOTAL = "total"; static final String INVALID_CHECKOUT_MESSAGE = "Checkout status is in an invalid state."; + static final String AFFIRM_NOT_INITIALIZED_MESSAGE = "Affirm has not been initialized"; static final String PROMO_PATH = "/api/promos/v2/%s"; static final String PROMO_IS_SDK = "is_sdk"; diff --git a/affirm/src/test/java/com/affirm/android/AffirmTest.java b/affirm/src/test/java/com/affirm/android/AffirmTest.java index cdc0981..706e5e8 100755 --- a/affirm/src/test/java/com/affirm/android/AffirmTest.java +++ b/affirm/src/test/java/com/affirm/android/AffirmTest.java @@ -9,6 +9,8 @@ import static org.junit.Assert.assertEquals; +import java.util.Locale; + public class AffirmTest { @Before @@ -39,6 +41,18 @@ public void testSetMerchantName() { assertEquals("aaa", AffirmPlugins.get().merchantName()); } + @Test + public void testSetCountryCode() { + Affirm.setCountryCode(Locale.US.getISO3Country()); + assertEquals(Locale.US.getISO3Country(), AffirmPlugins.get().countryCode()); + } + + @Test + public void testSetLocale() { + Affirm.setLocale(Locale.US.toString()); + assertEquals(Locale.US.toString(), AffirmPlugins.get().locale()); + } + @Test public void onActivityResult_Success() { Affirm.CheckoutCallbacks callbacks = Mockito.mock(Affirm.CheckoutCallbacks.class);