Skip to content

Commit

Permalink
Add product modal methods in the example app (#75)
Browse files Browse the repository at this point in the history
* Add product modal methods in the example app

Co-authored-by: Victor Zhu <[email protected]>
  • Loading branch information
hoyelo and ilabsvictor authored May 28, 2020
1 parent 84da948 commit b607a22
Show file tree
Hide file tree
Showing 12 changed files with 228 additions and 138 deletions.
8 changes: 4 additions & 4 deletions AffirmSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
08FA5921222D08E5007E5E4E /* AffirmSDK.h in Headers */ = {isa = PBXBuildFile; fileRef = 08FA5913222D08E5007E5E4E /* AffirmSDK.h */; settings = {ATTRIBUTES = (Public, ); }; };
08FA592D222D1176007E5E4E /* AffirmUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 08FA592B222D1176007E5E4E /* AffirmUtils.m */; };
08FA592E222D1176007E5E4E /* AffirmUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 08FA592B222D1176007E5E4E /* AffirmUtils.m */; };
79088B4D2238D20E0059D691 /* AffirmPrequalModalViewController.h in Headers */ = {isa = PBXBuildFile; fileRef = 79088B4B2238D20D0059D691 /* AffirmPrequalModalViewController.h */; settings = {ATTRIBUTES = (Private, ); }; };
79088B4D2238D20E0059D691 /* AffirmPrequalModalViewController.h in Headers */ = {isa = PBXBuildFile; fileRef = 79088B4B2238D20D0059D691 /* AffirmPrequalModalViewController.h */; settings = {ATTRIBUTES = (Public, ); }; };
79088B4E2238D20E0059D691 /* AffirmPrequalModalViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 79088B4C2238D20D0059D691 /* AffirmPrequalModalViewController.m */; };
79088B512238DB840059D691 /* AffirmPromoModalViewController.h in Headers */ = {isa = PBXBuildFile; fileRef = 79088B4F2238DB840059D691 /* AffirmPromoModalViewController.h */; settings = {ATTRIBUTES = (Private, ); }; };
79088B512238DB840059D691 /* AffirmPromoModalViewController.h in Headers */ = {isa = PBXBuildFile; fileRef = 79088B4F2238DB840059D691 /* AffirmPromoModalViewController.h */; settings = {ATTRIBUTES = (Public, ); }; };
79088B522238DB840059D691 /* AffirmPromoModalViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 79088B502238DB840059D691 /* AffirmPromoModalViewController.m */; };
7944D9932240BF830040525E /* AffirmActivityIndicatorView.h in Headers */ = {isa = PBXBuildFile; fileRef = 7944D9912240BF830040525E /* AffirmActivityIndicatorView.h */; settings = {ATTRIBUTES = (Private, ); }; };
7944D9942240BF830040525E /* AffirmActivityIndicatorView.m in Sources */ = {isa = PBXBuildFile; fileRef = 7944D9922240BF830040525E /* AffirmActivityIndicatorView.m */; };
Expand Down Expand Up @@ -293,10 +293,10 @@
0827FF8B2325EB9200AEC14C /* AffirmDataHandler.h in Headers */,
7944D9C12248A84D0040525E /* AffirmUtils.h in Headers */,
7944D9B72240E2250040525E /* AffirmLogger.h in Headers */,
7944D9932240BF830040525E /* AffirmActivityIndicatorView.h in Headers */,
79088B4D2238D20E0059D691 /* AffirmPrequalModalViewController.h in Headers */,
79088B512238DB840059D691 /* AffirmPromoModalViewController.h in Headers */,
7944D9932240BF830040525E /* AffirmActivityIndicatorView.h in Headers */,
DA77629D223F959F00FB2DB3 /* AffirmPopupViewController.h in Headers */,
79088B4D2238D20E0059D691 /* AffirmPrequalModalViewController.h in Headers */,
0841E334223A9948006D8B97 /* AffirmConstants.h in Headers */,
08A81F49223F986D00FAA8D2 /* AffirmClient.h in Headers */,
08C4306C223B7C640009E045 /* AffirmRequest.h in Headers */,
Expand Down
44 changes: 25 additions & 19 deletions AffirmSDK/AffirmCheckoutViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ + (AffirmCheckoutViewController *)startCheckout:(AffirmCheckout *)checkout
return [[self alloc] initWithDelegate:delegate
checkout:checkout
useVCN:useVCN
getReasonCodes:NO];
getReasonCodes:NO];
}

+ (AffirmCheckoutViewController *)startCheckout:(AffirmCheckout *)checkout
Expand All @@ -81,7 +81,7 @@ + (AffirmCheckoutViewController *)startCheckout:(AffirmCheckout *)checkout
return [[self alloc] initWithDelegate:delegate
checkout:checkout
useVCN:useVCN
getReasonCodes:getReasonCodes];
getReasonCodes:getReasonCodes];
}

- (void)viewDidLoad
Expand All @@ -90,10 +90,16 @@ - (void)viewDidLoad
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Close"
style:UIBarButtonItemStyleDone
target:self
action:@selector(dismiss)];
action:@selector(cancel:)];
[self prepareForCheckout];
}

- (void)cancel:(id)sender
{
[self.delegate checkoutCancelled:self];
[self dismiss];
}

- (void)prepareForCheckout
{
[[AffirmLogger sharedInstance] trackEvent:@"Checkout initiated"];
Expand Down Expand Up @@ -137,11 +143,11 @@ - (void)loadRedirectURL:(NSURL *)redirectURL
@"{{CONFIRM_CB_URL}}": AFFIRM_CHECKOUT_CONFIRMATION_URL,
@"{{CANCELLED_CB_URL}}": AFFIRM_CHECKOUT_CANCELLATION_URL}
enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSString * _Nonnull obj, BOOL * _Nonnull stop) {
rawContent = [rawContent stringByReplacingOccurrencesOfString:key
withString:obj
options:NSLiteralSearch
range:[rawContent rangeOfString:key]];
}];
rawContent = [rawContent stringByReplacingOccurrencesOfString:key
withString:obj
options:NSLiteralSearch
range:[rawContent rangeOfString:key]];
}];
NSString *baseUrl = [NSString stringWithFormat:@"https://%@", redirectURL.host];
[self.webView loadData:[rawContent dataUsingEncoding:NSUTF8StringEncoding] MIMEType:@"text/html" characterEncodingName:@"utf-8" baseURL:[NSURL URLWithString:baseUrl]];
} else {
Expand Down Expand Up @@ -181,34 +187,34 @@ - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigati
if (_getReasonCodes) {

NSURLComponents *urlComponents = [[NSURLComponents alloc] initWithString:URL.absoluteString];
for(NSURLQueryItem *item in urlComponents.queryItems) {
for (NSURLQueryItem *item in urlComponents.queryItems) {

if([item.name isEqualToString:@"data"]) {
[self.delegate checkoutCancelled:self
checkoutCanceledWithReason:[AffirmReasonCode reasonCodeWithDict:item.value.convertToDictionary]];
[[AffirmLogger sharedInstance] trackEvent:@"Checkout cancelled with reason" parameters:@{@"checkout_ari": self.checkoutARI, @"checkout_canceled_reason": item.value != nil ? item.value : @"false"}];
checkoutCanceledWithReason:[AffirmReasonCode reasonCodeWithDict:item.value.convertToDictionary]];
[[AffirmLogger sharedInstance] trackEvent:@"Checkout cancelled with reason" parameters:@{@"checkout_ari": self.checkoutARI, @"checkout_canceled_reason": item.value != nil ? item.value : @"false"}];
break;
}
}
} else {
[self.delegate checkoutCancelled:self];
[self.delegate checkoutCancelled:self];
[[AffirmLogger sharedInstance] trackEvent:@"Checkout cancelled" parameters:@{@"checkout_ari": self.checkoutARI}];
}
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
[self webView:webView checkIfURL:URL.absoluteString isPopupWithCompletion:^(BOOL isPopup) {

if (isPopup) {

AffirmPopupViewController *viewController = [[AffirmPopupViewController alloc] initWithURL:URL];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
[self presentViewController:navController animated:YES completion:nil];
[[AffirmLogger sharedInstance] trackEvent:@"External link selected from checkout" parameters:@{@"checkout_ari": self.checkoutARI, @"selected_link": URL.absoluteString}];
decisionHandler(WKNavigationActionPolicyCancel);
}
else {

decisionHandler(WKNavigationActionPolicyAllow);
}
}];
Expand Down Expand Up @@ -236,13 +242,13 @@ - (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSStr
[alertController addAction:[UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action) {
completionHandler(NO);
}]];
completionHandler(NO);
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
completionHandler(YES);
}]];
completionHandler(YES);
}]];
[self presentViewController:alertController animated:YES completion:^{}];
}

Expand Down
30 changes: 30 additions & 0 deletions AffirmSDK/AffirmConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,34 @@ typedef NS_ENUM(NSInteger, AffirmLocale) {
AffirmLocaleCA
};

typedef NS_ENUM(NSInteger, AffirmPageType) {
AffirmPageTypeNone,
AffirmPageTypeBanner,
AffirmPageTypeCart,
AffirmPageTypeCategory,
AffirmPageTypeHomepage,
AffirmPageTypeLanding,
AffirmPageTypePayment,
AffirmPageTypeProduct,
AffirmPageTypeSearch
};

typedef NS_ENUM(NSInteger, AffirmLogoType) {
AffirmLogoTypeName,
AffirmLogoTypeText,
AffirmLogoTypeSymbol,
AffirmLogoTypeSymbolHollow
};

typedef NS_ENUM(NSInteger, AffirmColorType) {
AffirmColorTypeDefault,
AffirmColorTypeBlue,
AffirmColorTypeBlack,
AffirmColorTypeWhite,
AffirmColorTypeBlueBlack
};

NSString * FormatAffirmPageTypeString(AffirmPageType type);
NSString * FormatAffirmColorString(AffirmColorType type);

NS_ASSUME_NONNULL_END
39 changes: 39 additions & 0 deletions AffirmSDK/AffirmConstants.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,42 @@
NSString *const AFFIRM_CHECKOUT_CONFIRMATION_URL = @"affirm://checkout/confirmed";
NSString *const AFFIRM_CHECKOUT_CANCELLATION_URL = @"affirm://checkout/cancelled";

NSString * FormatAffirmPageTypeString(AffirmPageType type)
{
switch (type) {
case AffirmPageTypeNone:
return nil;
case AffirmPageTypeBanner:
return @"banner";
case AffirmPageTypeCart:
return @"cart";
case AffirmPageTypeCategory:
return @"category";
case AffirmPageTypeHomepage:
return @"homepage";
case AffirmPageTypeLanding:
return @"landing";
case AffirmPageTypePayment:
return @"payment";
case AffirmPageTypeProduct:
return @"product";
case AffirmPageTypeSearch:
return @"search";
}
}

NSString * FormatAffirmColorString(AffirmColorType type)
{
switch (type) {
case AffirmColorTypeBlue:
return @"blue";
case AffirmColorTypeBlack:
return @"black";
case AffirmColorTypeWhite:
return @"white";
case AffirmColorTypeBlueBlack:
return @"blue-black";
case AffirmColorTypeDefault:
return @"blue";
}
}
1 change: 1 addition & 0 deletions AffirmSDK/AffirmDataHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ + (void)getPromoMessageWithPromoID:(nullable NSString *)promoID
} else {
viewController = [[AffirmPromoModalViewController alloc] initWithPromoId:promoID
amount:decimal
pageType:pageType
delegate:delegate];
}
}
Expand Down
27 changes: 27 additions & 0 deletions AffirmSDK/AffirmPromoModalViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,45 @@

#import "AffirmBaseWebViewController.h"
#import "AffirmPrequalDelegate.h"
#import "AffirmConstants.h"

NS_ASSUME_NONNULL_BEGIN

@interface AffirmPromoModalViewController : AffirmBaseWebViewController

/**
The delegate which handles promo modal events.
*/
@property (nonatomic, weak) id<AffirmPrequalDelegate> delegate;

/**
Initializer. See properties for more details. UseVCN is NO as default
@param promoId Promo ID to use when getting terms (provided by Affirm)
@param amount Amount of the transaction
@param delegate A delegate object which responds to the promo modal events created by the view controller.
@return The newly created promo view controller.
*/
- (instancetype)initWithPromoId:(nullable NSString *)promoId
amount:(NSDecimalNumber *)amount
delegate:(id<AffirmPrequalDelegate>)delegate
NS_SWIFT_NAME(init(promoId:amount:delegate:)) NS_DESIGNATED_INITIALIZER;

/**
Initializer. See properties for more details. UseVCN is NO as default
@param promoId Promo ID to use when getting terms (provided by Affirm)
@param amount Amount of the transaction
@param pageType type of Affirm page to display
@param delegate A delegate object which responds to the promo modal events created by the view controller.
@return The newly created promo view controller.
*/
- (instancetype)initWithPromoId:(nullable NSString *)promoId
amount:(NSDecimalNumber *)amount
pageType:(AffirmPageType)pageType
delegate:(id<AffirmPrequalDelegate>)delegate
NS_SWIFT_NAME(init(promoId:amount:pageType:delegate:)) NS_DESIGNATED_INITIALIZER;

- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_UNAVAILABLE;
Expand Down
40 changes: 33 additions & 7 deletions AffirmSDK/AffirmPromoModalViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "AffirmPromoModalViewController.h"
#import "AffirmPromotionalButton.h"
#import "AffirmConfiguration.h"
#import "AffirmUtils.h"
#import "AffirmClient.h"
Expand All @@ -25,18 +26,43 @@ - (instancetype)initWithPromoId:(nullable NSString *)promoId
{
[AffirmValidationUtils checkNotNil:amount name:@"amount"];
[AffirmValidationUtils checkNotNil:delegate name:@"delegate"];

if (self = [super initWithNibName:nil bundle:nil]) {
NSString *jsURL = [AffirmConfiguration sharedInstance].jsURL;
NSString *filePath = [[NSBundle resourceBundle] pathForResource:@"promo_modal_template"
ofType:@"html"];
NSString *rawContent = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
_htmlString = [NSString stringWithFormat:rawContent, [AffirmConfiguration sharedInstance].publicKey, jsURL, amount, promoId ?: @"", AFFIRM_CHECKOUT_CANCELLATION_URL];
_delegate = delegate;
[self initializeHtmlWithPromoId:promoId amount:amount pageType:AffirmPageTypeNone delegate:delegate];
}
return self;
}

- (instancetype)initWithPromoId:(nullable NSString *)promoId
amount:(NSDecimalNumber *)amount
pageType:(AffirmPageType)pageType
delegate:(id<AffirmPrequalDelegate>)delegate
{
[AffirmValidationUtils checkNotNil:amount name:@"amount"];
[AffirmValidationUtils checkNotNil:delegate name:@"delegate"];

if (self = [super initWithNibName:nil bundle:nil]) {
[self initializeHtmlWithPromoId:promoId amount:amount pageType:pageType delegate:delegate];
}
return self;
}

- (void)initializeHtmlWithPromoId:(nullable NSString *)promoId
amount:(NSDecimalNumber *)amount
pageType:(AffirmPageType)pageType
delegate:(id<AffirmPrequalDelegate>)delegate
{
NSString *jsURL = [AffirmConfiguration sharedInstance].jsURL;
NSString *filePath = [[NSBundle resourceBundle] pathForResource:@"promo_modal_template"
ofType:@"html"];
NSString *rawContent = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

NSString *promoIdString = promoId ?: @"";
NSString *pageTypeString = FormatAffirmPageTypeString(pageType) ?: @"";
_htmlString = [NSString stringWithFormat:rawContent, [AffirmConfiguration sharedInstance].publicKey, jsURL, amount, promoIdString, pageTypeString, promoIdString, AFFIRM_CHECKOUT_CANCELLATION_URL];
_delegate = delegate;
}

- (void)viewDidLoad
{
[super viewDidLoad];
Expand Down
Loading

0 comments on commit b607a22

Please sign in to comment.