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

feat: support set country code and locale after initialization #134

Merged
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
51 changes: 48 additions & 3 deletions affirm/src/main/java/com/affirm/android/Affirm.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
14 changes: 14 additions & 0 deletions affirm/src/test/java/com/affirm/android/AffirmTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import static org.junit.Assert.assertEquals;

import java.util.Locale;

public class AffirmTest {

@Before
Expand Down Expand Up @@ -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);
Expand Down
Loading