Skip to content

Commit 6f10c32

Browse files
committed
feat: support set country code and locale after initialization
1 parent a2dc655 commit 6f10c32

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

affirm/src/main/java/com/affirm/android/Affirm.java

+48-3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import static android.app.Activity.RESULT_CANCELED;
3333
import static android.app.Activity.RESULT_OK;
3434
import static com.affirm.android.AffirmColor.AFFIRM_COLOR_TYPE_BLUE;
35+
import static com.affirm.android.AffirmConstants.AFFIRM_NOT_INITIALIZED_MESSAGE;
3536
import static com.affirm.android.AffirmConstants.CHECKOUT_ERROR;
3637
import static com.affirm.android.AffirmConstants.CHECKOUT_TOKEN;
3738
import static com.affirm.android.AffirmConstants.COUNTY_CODE_CAN;
@@ -566,7 +567,7 @@ public static void initialize(@NonNull Configuration configuration) {
566567
public static void setPublicKeyAndMerchantName(@NonNull String publicKey,
567568
@Nullable String merchantName) {
568569
if (!isInitialized()) {
569-
AffirmLog.w("Affirm has not been initialized");
570+
AffirmLog.w(AFFIRM_NOT_INITIALIZED_MESSAGE);
570571
return;
571572
}
572573

@@ -584,7 +585,7 @@ public static void setPublicKeyAndMerchantName(@NonNull String publicKey,
584585
*/
585586
public static void setPublicKey(@NonNull String publicKey) {
586587
if (!isInitialized()) {
587-
AffirmLog.w("Affirm has not been initialized");
588+
AffirmLog.w(AFFIRM_NOT_INITIALIZED_MESSAGE);
588589
return;
589590
}
590591

@@ -601,7 +602,7 @@ public static void setPublicKey(@NonNull String publicKey) {
601602
*/
602603
public static void setMerchantName(@Nullable String merchantName) {
603604
if (!isInitialized()) {
604-
AffirmLog.w("Affirm has not been initialized");
605+
AffirmLog.w(AFFIRM_NOT_INITIALIZED_MESSAGE);
605606
return;
606607
}
607608

@@ -611,6 +612,50 @@ public static void setMerchantName(@Nullable String merchantName) {
611612
.build());
612613
}
613614

615+
/**
616+
* Updates the country code used by Affirm after initialization.
617+
*
618+
* @param countryCode Set the country code to be used by Affirm. Must not be null or empty.
619+
*/
620+
public static void setCountryCode(@NonNull String countryCode) {
621+
if (!isInitialized()) {
622+
AffirmLog.w(AFFIRM_NOT_INITIALIZED_MESSAGE);
623+
return;
624+
}
625+
626+
if (countryCode.isEmpty()) {
627+
AffirmLog.w("Country code is empty. Please provide a valid country code.");
628+
return;
629+
}
630+
631+
AffirmPlugins.get().setConfiguration(
632+
new Affirm.Configuration.Builder(AffirmPlugins.get().getConfiguration())
633+
.setCountryCode(countryCode)
634+
.build());
635+
}
636+
637+
/**
638+
* Updates the locale used by Affirm after initialization.
639+
*
640+
* @param locale Set the locale to be used by Affirm. Must not be null or empty.
641+
*/
642+
public static void setLocale(@NonNull String locale) {
643+
if (!isInitialized()) {
644+
AffirmLog.w(AFFIRM_NOT_INITIALIZED_MESSAGE);
645+
return;
646+
}
647+
648+
if (locale.isEmpty()) {
649+
AffirmLog.w("Locale is empty. Please provide a valid locale string.");
650+
return;
651+
}
652+
653+
AffirmPlugins.get().setConfiguration(
654+
new Affirm.Configuration.Builder(AffirmPlugins.get().getConfiguration())
655+
.setLocale(locale)
656+
.build());
657+
}
658+
614659
private static boolean isInitialized() {
615660
return AffirmPlugins.get() != null;
616661
}

affirm/src/main/java/com/affirm/android/AffirmConstants.java

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ private AffirmConstants() {
125125
static final String TOTAL = "total";
126126

127127
static final String INVALID_CHECKOUT_MESSAGE = "Checkout status is in an invalid state.";
128+
static final String AFFIRM_NOT_INITIALIZED_MESSAGE = "Affirm has not been initialized";
128129

129130
static final String PROMO_PATH = "/api/promos/v2/%s";
130131
static final String PROMO_IS_SDK = "is_sdk";

affirm/src/test/java/com/affirm/android/AffirmTest.java

+14
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
import static org.junit.Assert.assertEquals;
1111

12+
import java.util.Locale;
13+
1214
public class AffirmTest {
1315

1416
@Before
@@ -39,6 +41,18 @@ public void testSetMerchantName() {
3941
assertEquals("aaa", AffirmPlugins.get().merchantName());
4042
}
4143

44+
@Test
45+
public void testSetCountryCode() {
46+
Affirm.setCountryCode(Locale.US.getISO3Country());
47+
assertEquals(Locale.US.getISO3Country(), AffirmPlugins.get().countryCode());
48+
}
49+
50+
@Test
51+
public void testSetLocale() {
52+
Affirm.setLocale(Locale.US.toString());
53+
assertEquals(Locale.US.toString(), AffirmPlugins.get().locale());
54+
}
55+
4256
@Test
4357
public void onActivityResult_Success() {
4458
Affirm.CheckoutCallbacks callbacks = Mockito.mock(Affirm.CheckoutCallbacks.class);

0 commit comments

Comments
 (0)