Skip to content

Commit

Permalink
global integration
Browse files Browse the repository at this point in the history
  • Loading branch information
quanquan1 committed Apr 19, 2024
1 parent 1070c8d commit f693201
Show file tree
Hide file tree
Showing 55 changed files with 362 additions and 485 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ Before you can start the initialization of Affirm SDK, you must first set the Af
```java
Affirm.initialize(new Affirm.Configuration.Builder("public key")
.setEnvironment(Affirm.Environment.SANDBOX)
.setCountryCode(Locale.US.getISO3Country()) // Default USA
.setLocale(Locale.US.toString()) // Default en_US
.setName("merchant name")
.setReceiveReasonCodes("true")
.setLogLevel(Affirm.LOG_LEVEL_DEBUG)
.setCheckoutRequestCode(8001)
.setVcnCheckoutRequestCode(8002)
.setPrequalRequestCode(8003)
.setLocation(Affirm.Location.US) // "CA" for Canadian, "US" for American (If not set, default use US)
.build())
```
- `environment` can be set to `Affirm.Environment.SANDBOX` for test.
Expand Down
118 changes: 82 additions & 36 deletions affirm/src/main/java/com/affirm/android/Affirm.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
import static com.affirm.android.AffirmColor.AFFIRM_COLOR_TYPE_BLUE;
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;
import static com.affirm.android.AffirmConstants.COUNTY_CODE_USA;
import static com.affirm.android.AffirmConstants.CREDIT_DETAILS;
import static com.affirm.android.AffirmConstants.LOCALE_USA;
import static com.affirm.android.AffirmConstants.VCN_REASON;
import static com.affirm.android.AffirmLogoType.AFFIRM_DISPLAY_TYPE_LOGO;
import static com.affirm.android.AffirmTracker.TrackingEvent.CHECKOUT_WEBVIEW_CLICK;
Expand Down Expand Up @@ -118,51 +121,66 @@ public interface VcnCheckoutCallbacks {
void onAffirmVcnCheckoutSuccess(@NonNull CardDetails cardDetails);
}

public enum Location {
US, CA
}

public enum Environment {
SANDBOX,
PRODUCTION;

String baseUrl() {
String checkoutUrl() {
switch (this) {
case SANDBOX:
return AffirmConstants.getSandboxUrl();
return AffirmConstants.SANDBOX_CHECKOUT_URL;
default:
return AffirmConstants.getProductionUrl();
return AffirmConstants.PRODUCTION_CHECKOUT_URL;
}
}

String baseJsUrl() {
String jsUrl() {
switch (this) {
case SANDBOX:
return AffirmConstants.getSandboxJsUrl();
return AffirmConstants.SANDBOX_JS_URL;
default:
return AffirmConstants.getProductionJsUrl();
return AffirmConstants.PRODUCTION_JS_URL;
}
}

String trackerBaseUrl() {
return AffirmConstants.getTrackerUrl();
String trackerUrl() {
return AffirmConstants.TRACKER_URL;
}

String promoUrl(String countryCode) {
switch (countryCode) {
case COUNTY_CODE_CAN:
switch (this) {
case SANDBOX:
return AffirmConstants.SANDBOX_PROMO_CA_URL;
default:
return AffirmConstants.PRODUCTION_PROMO_CA_URL;
}
default:
switch (this) {
case SANDBOX:
return AffirmConstants.SANDBOX_PROMO_URL;
default:
return AffirmConstants.PRODUCTION_PROMO_URL;
}
}
}

String basePromoUrl() {
String promoCAUrl() {
switch (this) {
case SANDBOX:
return AffirmConstants.getStagingPromoUrl();
return AffirmConstants.SANDBOX_PROMO_CA_URL;
default:
return AffirmConstants.getProductionPromoUrl();
return AffirmConstants.PRODUCTION_PROMO_CA_URL;
}
}

String baseInvalidCheckoutRedirectUrl() {
String invalidCheckoutRedirectUrl() {
switch (this) {
case SANDBOX:
return AffirmConstants.getStagingInvalidCheckoutRedirectUrl();
return AffirmConstants.SANDBOX_INVALID_CHECKOUT_REDIRECT_URL;
default:
return AffirmConstants.getProductionInvalidCheckoutRedirectUrl();
return AffirmConstants.PRODUCTION_INVALID_CHECKOUT_REDIRECT_URL;
}
}
}
Expand All @@ -172,11 +190,15 @@ public static final class Configuration {
final Environment environment;
final String merchantName;
final String cardTip;
final String locale;
final String countryCode;

Configuration(Builder builder) {
this.publicKey = builder.publicKey;
this.merchantName = builder.merchantName;
this.cardTip = builder.cardTip;
this.locale = builder.locale;
this.countryCode = builder.countryCode;

if (builder.environment != null) {
this.environment = builder.environment;
Expand All @@ -190,6 +212,10 @@ public static final class Builder {
private Environment environment;
private String merchantName;
private String cardTip;
// When a locale is not provided, the locale will default to en_US
private String locale = LOCALE_USA;
// When a country-code is not provided, the country-code will default to USA
private String countryCode = COUNTY_CODE_USA;

/**
* @param configuration Set the configuration to be used by Affirm.
Expand All @@ -198,6 +224,9 @@ public Builder(@NonNull Configuration configuration) {
this.publicKey = configuration.publicKey;
this.environment = configuration.environment;
this.merchantName = configuration.merchantName;
this.cardTip = configuration.cardTip;
this.locale = configuration.locale;
this.countryCode = configuration.countryCode;
}

/**
Expand Down Expand Up @@ -315,8 +344,25 @@ public Builder setPrequalRequestCode(int prequalRequestCode) {
return this;
}

public Builder setLocation(Location location) {
AffirmConstants.setLocation(location);
/**
* Set the locale to be used by Affirm, it's optional
*
* @param locale your locale info to be used by Affirm
* @return The same builder, for easy chaining.
*/
public Builder setLocale(String locale) {
this.locale = locale;
return this;
}

/**
* Set the country code to be used by Affirm, it's optional
*
* @param countryCode your country code to be used by Affirm
* @return The same builder, for easy chaining.
*/
public Builder setCountryCode(String countryCode) {
this.countryCode = countryCode;
return this;
}

Expand Down Expand Up @@ -518,7 +564,7 @@ public static void initialize(@NonNull Configuration configuration) {
* @param merchantName Set the merchant name to be used by Affirm.
*/
public static void setPublicKeyAndMerchantName(@NonNull String publicKey,
@Nullable String merchantName) {
@Nullable String merchantName) {
if (!isInitialized()) {
AffirmLog.w("Affirm has not been initialized");
return;
Expand Down Expand Up @@ -970,13 +1016,13 @@ public static AffirmFragment startCheckout(@NonNull Fragment fragment,
* @return a `AffirmFragment`
*/
protected static AffirmFragment startCheckout(@NonNull AppCompatActivity activity,
@IdRes int containerViewId,
@NonNull Checkout checkout,
@Nullable String caas,
@Nullable Money money,
int cardAuthWindow,
boolean newFlow,
boolean useVCN) {
@IdRes int containerViewId,
@NonNull Checkout checkout,
@Nullable String caas,
@Nullable Money money,
int cardAuthWindow,
boolean newFlow,
boolean useVCN) {
if (useVCN) {
return VcnCheckoutFragment.newInstance(activity,
containerViewId, checkout, receiveReasonCodes, caas, money, cardAuthWindow,
Expand All @@ -1002,16 +1048,16 @@ protected static AffirmFragment startCheckout(@NonNull AppCompatActivity activit
* @return a `AffirmFragment`
*/
protected static AffirmFragment startCheckout(@NonNull Fragment fragment,
@IdRes int containerViewId,
@NonNull Checkout checkout,
@Nullable String caas,
@Nullable Money money,
int cardAuthWindow,
boolean newFlow,
boolean useVCN) {
@IdRes int containerViewId,
@NonNull Checkout checkout,
@Nullable String caas,
@Nullable Money money,
int cardAuthWindow,
boolean newFlow,
boolean useVCN) {
if (useVCN) {
return VcnCheckoutFragment.newInstance(fragment,
containerViewId, checkout, receiveReasonCodes, caas, money, cardAuthWindow,
containerViewId, checkout, receiveReasonCodes, caas, money, cardAuthWindow,
newFlow);
} else {
return CheckoutFragment.newInstance(fragment,
Expand Down
8 changes: 8 additions & 0 deletions affirm/src/main/java/com/affirm/android/AffirmClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
Expand Down Expand Up @@ -46,6 +47,9 @@ public interface AffirmApiRequest {

@Nullable
JsonObject body();

@Nullable
Map<String, String> headers();
}

public interface AffirmListener<T> {
Expand All @@ -66,6 +70,10 @@ public static <T> Call send(@Nullable OkHttpClient okHttpClient,
AffirmHttpRequest.Builder builder = new AffirmHttpRequest.Builder()
.setUrl(request.url())
.setMethod(request.method());
Map<String, String> headers = request.headers();
if (headers != null && !headers.isEmpty()) {
builder.setHeaders(headers);
}
JsonObject requestBody = request.body();
if (requestBody != null) {
builder.setBody(new AffirmHttpBody(CONTENT_TYPE, requestBody.toString()));
Expand Down
Loading

0 comments on commit f693201

Please sign in to comment.