Skip to content

Commit

Permalink
Merge pull request #146 from ilabsvictor/feature/international
Browse files Browse the repository at this point in the history
Affirm international scoping and production issues
  • Loading branch information
brendamvillareal authored Apr 26, 2024
2 parents d23b334 + cf7d4e9 commit 0ec2dd3
Show file tree
Hide file tree
Showing 29 changed files with 218 additions and 121 deletions.
8 changes: 5 additions & 3 deletions AffirmSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@
08FA5907222D08E5007E5E4E /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 1010;
LastUpgradeCheck = 1340;
ORGANIZATIONNAME = "Affirm, Inc.";
TargetAttributes = {
08FA590F222D08E5007E5E4E = {
Expand Down Expand Up @@ -532,6 +532,7 @@
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
Expand All @@ -558,7 +559,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES;
Expand Down Expand Up @@ -594,6 +595,7 @@
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
Expand All @@ -614,7 +616,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
SDKROOT = iphoneos;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1010"
LastUpgradeVersion = "1340"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
3 changes: 0 additions & 3 deletions AffirmSDK/AffirmBaseWebViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,6 @@ - (void)loadErrorPage:(NSError *)error
{
[[AffirmLogger sharedInstance] trackEvent:@"Web load failed"
parameters:@{@"error_description": error.localizedDescription}];
NSString *errorDescription = [error.localizedDescription stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSString *url = [NSString stringWithFormat:@"https://www.%@/u/#/error?main=Error&sub=%@", [AffirmConfiguration sharedInstance].domain, errorDescription];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
}

- (void)dismiss
Expand Down
13 changes: 6 additions & 7 deletions AffirmSDK/AffirmBillingDetail.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ - (NSDictionary *)toJSONDictionary
if (!self.name && !self.email && !self.phoneNumber && (!self.line1 || !self.line2 || !self.city || !self.state || !self.zipCode || !self.countryCode)) {
return @{};
}

return @{
@"billing": [self getBillingJSONDictionary]
};
Expand All @@ -71,14 +71,13 @@ - (NSDictionary *)getBillingJSONDictionary
{
NSMutableDictionary *jsonDic = [@{} mutableCopy];
if (self.line1 && self.line2 && self.city && self.state && self.zipCode && self.countryCode) {
BOOL usingInternationalRule = [AffirmConfiguration sharedInstance].locale == AffirmLocaleCA;
NSDictionary *address = @{
usingInternationalRule ? @"street1" : @"line1": self.line1,
usingInternationalRule ? @"street2" : @"line2": self.line2,
@"street1": self.line1,
@"street2": self.line2,
@"city": self.city,
usingInternationalRule ? @"region1_code" : @"state": self.state,
usingInternationalRule ? @"postal_code" : @"zipcode": self.zipCode,
usingInternationalRule ? @"country_code" : @"country": self.countryCode
@"region1_code": self.state,
@"postal_code": self.zipCode,
@"country_code": self.countryCode
};
jsonDic[@"address"] = address;
}
Expand Down
23 changes: 12 additions & 11 deletions AffirmSDK/AffirmCheckout.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ - (instancetype)initWithItems:(NSArray <AffirmItem *>*)items
[AffirmValidationUtils checkNotNegative:taxAmount name:@"taxAmount"];
[AffirmValidationUtils checkNotNil:shippingAmount name:@"shippingAmount"];
[AffirmValidationUtils checkNotNegative:shippingAmount name:@"shippingAmount"];

if (self = [super init]) {
_items = [[NSArray alloc] initWithArray:items copyItems:YES];
_shipping = [shipping copy];
Expand Down Expand Up @@ -221,7 +221,7 @@ - (NSDecimalNumber *)calculatedTotalAmount
{
[AffirmValidationUtils checkNotNil:self.taxAmount name:@"taxAmount"];
[AffirmValidationUtils checkNotNil:self.shippingAmount name:@"shippingAmount"];

NSDecimalNumber *totalAmount = [self.taxAmount decimalNumberByAdding:self.shippingAmount];
for (AffirmItem *item in self.items) {
totalAmount = [totalAmount decimalNumberByAdding:[item.unitPrice decimalNumberByMultiplyingBy:[NSDecimalNumber decimalNumberWithDecimal:[[NSNumber numberWithInteger:item.quantity] decimalValue]]]];
Expand Down Expand Up @@ -258,24 +258,25 @@ - (NSDictionary *)toJSONDictionary
}

NSMutableDictionary *dict = [@{
@"items": items,
@"total": [self.totalAmount toIntegerCents],
@"currency": [AffirmConfiguration sharedInstance].currency,
@"api_version" :@"v2"
} mutableCopy];

@"meta": @{@"locale": [AffirmConfiguration sharedInstance].locale},
@"items": items,
@"total": [self.totalAmount toIntegerCents],
@"currency": [AffirmConfiguration sharedInstance].currency,
@"api_version" :@"v2"
} mutableCopy];

if (self.sendShippingAddress) {
if (self.shipping) {
[dict addEntriesFromDictionary:[self.shipping toJSONDictionary]];
} else {
[[AffirmLogger sharedInstance] logException:@"Shipping addresses are required when sendShippingAddress is true."];
}
}

if (self.billing) {
[dict addEntriesFromDictionary:[self.billing toJSONDictionary]];
}

if (self.shippingAmount != nil) {
[dict setValue:self.shippingAmount.toIntegerCents forKey:@"shipping_amount"];
}
Expand All @@ -299,7 +300,7 @@ - (NSDictionary *)toJSONDictionary
if (self.financingProgram != nil) {
[dict setObject:self.financingProgram forKey:@"financing_program"];
}

if (self.orderId != nil) {
[dict setObject:self.orderId forKey:@"order_id"];
}
Expand Down
12 changes: 5 additions & 7 deletions AffirmSDK/AffirmClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ + (void)send:(AffirmRequest *)request handler:(AffirmRequestHandler)handler
}
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
urlRequest.HTTPMethod = method;

NSDictionary *headers = request.headers;
for (NSString *key in headers) {
[urlRequest setValue:headers[key] forHTTPHeaderField:key];
}
if (request.parameters && [NSJSONSerialization isValidJSONObject:request.parameters] && request.method == AffirmHTTPMethodPOST) {
urlRequest.HTTPBody = [NSJSONSerialization dataWithJSONObject:request.parameters options:NSJSONWritingPrettyPrinted error:nil];
}

NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:urlRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (handler) {
NSHTTPURLResponse *result = (NSHTTPURLResponse *)response;
Expand Down Expand Up @@ -138,7 +138,7 @@ @implementation AffirmTrackerClient

+ (NSString *)host
{
return [NSString stringWithFormat:@"https://tracker.%@", [AffirmConfiguration sharedInstance].domain];
return [AffirmConfiguration sharedInstance].trackerURL;
}

@end
Expand All @@ -147,8 +147,7 @@ @implementation AffirmPromoClient

+ (NSString *)host
{
NSString *prefix = [AffirmConfiguration sharedInstance].isProductionEnvironment ? @"www" : @"sandbox";
return [NSString stringWithFormat:@"https://%@.%@", prefix, [AffirmConfiguration sharedInstance].domain];
return [AffirmConfiguration sharedInstance].promosURL;
}

@end
Expand All @@ -157,8 +156,7 @@ @implementation AffirmCheckoutClient

+ (NSString *)host
{
NSString *prefix = [AffirmConfiguration sharedInstance].isProductionEnvironment ? @"api" : @"sandbox";
return [NSString stringWithFormat:@"https://%@.%@", prefix, [AffirmConfiguration sharedInstance].domain];
return [AffirmConfiguration sharedInstance].checkoutURL;
}

@end
Expand Down
43 changes: 30 additions & 13 deletions AffirmSDK/AffirmConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, readonly) AffirmEnvironment environment;

/**
environment User locale. (US, CA)
environment User locale. (en_US, en_CA, fr_CA , etc.)
*/
@property (nonatomic, readonly) AffirmLocale locale;
@property (nonatomic, readonly) NSString *locale;

/**
environment User country code. (USA, CAN , etc.)
*/
@property (nonatomic, readonly) NSString *countryCode;

/**
environment Currency. (USD, CAD)
Expand All @@ -50,7 +55,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, readonly) BOOL isProductionEnvironment;

/**
creditCard Return an existing credit card.
creditCard Return an existing credit card.
*/
@property (nonatomic, readonly, strong, nullable) AffirmCreditCard *creditCard;

Expand Down Expand Up @@ -94,28 +99,40 @@ NS_SWIFT_NAME(configure(publicKey:environment:));
NS_SWIFT_NAME(configure(publicKey:environment:merchantName:));

/**
Convenience constructor. See properties for more details.
*/
Convenience constructor. See properties for more details.
*/
- (void)configureWithPublicKey:(NSString *)publicKey
environment:(AffirmEnvironment)environment
locale:(AffirmLocale)locale
locale:(NSString *)locale
countryCode:(NSString *)countryCode
currency:(NSString *)currency
merchantName:(NSString * _Nullable )merchantName
NS_SWIFT_NAME(configure(publicKey:environment:locale:merchantName:));
NS_SWIFT_NAME(configure(publicKey:environment:locale:countryCode:currency:merchantName:));

/**
affirmSDKVersion Current Affirm SDK version.
*/
+ (NSString *)affirmSDKVersion;

/**
domain Based on currenct locale
*/
- (NSString *)domain;
jsURL Affirm js file url
*/
- (NSString *)jsURL;

/**
jsURL Affirm js file url
*/
- (NSString *)jsURL;
promosURL Promos endpoint url
*/
- (NSString *)promosURL;

/**
checkoutURL Checkout endpoint url
*/
- (NSString *)checkoutURL;

/**
trackerURL Tracker endpoint url
*/
- (NSString *)trackerURL;

/**
environmentDescription Current environment.
Expand Down
66 changes: 42 additions & 24 deletions AffirmSDK/AffirmConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

@interface AffirmConfiguration ()

@property (nonatomic, copy, readwrite) NSString *publicKey;
@property (nonatomic, copy, readwrite) NSString *publicKey, *countryCode, *currency;
@property (nonatomic, readwrite) AffirmEnvironment environment;
@property (nonatomic, readwrite) AffirmLocale locale;
@property (nonatomic, readwrite) NSString *locale;
@property (nonatomic, copy, readwrite, nullable) NSString *merchantName;
@property (nonatomic, strong, readwrite) WKProcessPool *pool;
@property (nonatomic, strong, readwrite, nullable) AffirmCreditCard *creditCard;
Expand Down Expand Up @@ -47,7 +47,9 @@ - (instancetype)init
{
if (self = [super init]) {
_environment = AffirmEnvironmentSandbox;
_locale = AffirmLocaleUS;
_locale = AFFIRM_DEFAULT_LOCALE;
_countryCode = AFFIRM_DEFAULT_COUNTRY_CODE;
_currency = AFFIRM_DEFAULT_CURRENCY;
}
return self;
}
Expand Down Expand Up @@ -79,12 +81,16 @@ - (void)configureWithPublicKey:(NSString *)publicKey

- (void)configureWithPublicKey:(NSString *)publicKey
environment:(AffirmEnvironment)environment
locale:(AffirmLocale)locale
locale:(NSString *)locale
countryCode:(NSString *)countryCode
currency:(NSString *)currency
merchantName:(NSString * _Nullable )merchantName
{
self.publicKey = [publicKey copy];
self.environment = environment;
self.locale = locale;
self.countryCode = countryCode;
self.currency = currency;
self.merchantName = [merchantName copy];
}

Expand All @@ -101,41 +107,50 @@ - (NSString *)publicKey
return _publicKey;
}

- (NSString *)currency
+ (NSString *)affirmSDKVersion
{
switch (self.locale) {
case AffirmLocaleUS:
return @"USD";
case AffirmLocaleCA:
return @"CAD";
}
return [[NSBundle resourceBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
}

+ (NSString *)affirmSDKVersion
- (NSString *)jsURL
{
return [[NSBundle resourceBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
switch (self.environment) {
case AffirmEnvironmentSandbox:
return AFFIRM_JS_SANDBOX_URL;
case AffirmEnvironmentProduction:
return AFFIRM_JS_PRODUCTION_URL;
}
}

- (NSString *)domain
- (NSString *)promosDomain
{
switch (self.locale) {
case AffirmLocaleUS:
return AFFIRM_US_DOMAIN;
case AffirmLocaleCA:
return AFFIRM_CA_DOMAIN;
if ([self.countryCode isEqualToString:@"CAN"]) {
return AFFIRM_PROMOS_CA_DOMAIN;
}
return AFFIRM_PROMOS_US_DOMAIN;
}

- (NSString *)jsURL
- (NSString *)promosURL
{
NSString *prefix = self.isProductionEnvironment ? @"www" : @"sandbox";
return [NSString stringWithFormat:@"https://%@.%@", prefix, self.promosDomain];
}

- (NSString *)checkoutURL
{
switch (self.environment) {
case AffirmEnvironmentSandbox:
return [NSString stringWithFormat:@"https://cdn1-sandbox.%@/js/v2/affirm.js", self.domain];
return AFFIRM_CHECKOUT_SANDBOX_URL;
case AffirmEnvironmentProduction:
return [NSString stringWithFormat:@"https://cdn1.%@/js/v2/affirm.js", self.domain];
return AFFIRM_CHECKOUT_PRODUCTION_URL;
}
}

- (NSString *)trackerURL
{
return [NSString stringWithFormat:@"https://tracker.%@", self.promosDomain];
}

- (NSString *)environmentDescription
{
switch(self.environment) {
Expand All @@ -160,9 +175,12 @@ - (void)updateCreditCard:(nullable AffirmCreditCard *)creditCard
{
NSMutableArray *ownedCookies = [NSMutableArray array];
NSArray *cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage].cookies;
NSArray *urls = @[AFFIRM_PROMOS_US_DOMAIN, AFFIRM_PROMOS_CA_DOMAIN];
for (NSHTTPCookie *cookie in cookies) {
if ([cookie.domain rangeOfString:[AffirmConfiguration sharedInstance].domain].location != NSNotFound) {
[ownedCookies addObject:cookie];
for (NSString *url in urls) {
if ([cookie.domain rangeOfString:url].location != NSNotFound) {
[ownedCookies addObject:cookie];
}
}
}
return ownedCookies;
Expand Down
Loading

0 comments on commit 0ec2dd3

Please sign in to comment.