Commit 2e1154ad by Dima Bart

Add NSOperation subclasses for completing checkout.

parent 432ddba2
......@@ -376,6 +376,14 @@
9A47CF201CE50EBB00A6D5BA /* BUYApplePayTestToken.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A47CF1F1CE50EBB00A6D5BA /* BUYApplePayTestToken.m */; };
9A47CF221CE5112A00A6D5BA /* BUYAssert.h in Headers */ = {isa = PBXBuildFile; fileRef = 9A47CF211CE5112A00A6D5BA /* BUYAssert.h */; };
9A47CF231CE5112A00A6D5BA /* BUYAssert.h in Headers */ = {isa = PBXBuildFile; fileRef = 9A47CF211CE5112A00A6D5BA /* BUYAssert.h */; };
9A585C061CE6440B001F20F0 /* BUYCheckoutOperation.h in Headers */ = {isa = PBXBuildFile; fileRef = 9A585C021CE6440B001F20F0 /* BUYCheckoutOperation.h */; };
9A585C071CE6440B001F20F0 /* BUYCheckoutOperation.h in Headers */ = {isa = PBXBuildFile; fileRef = 9A585C021CE6440B001F20F0 /* BUYCheckoutOperation.h */; };
9A585C081CE6440B001F20F0 /* BUYCheckoutOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A585C031CE6440B001F20F0 /* BUYCheckoutOperation.m */; };
9A585C091CE6440B001F20F0 /* BUYCheckoutOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A585C031CE6440B001F20F0 /* BUYCheckoutOperation.m */; };
9A585C0A1CE6440B001F20F0 /* BUYOperation.h in Headers */ = {isa = PBXBuildFile; fileRef = 9A585C041CE6440B001F20F0 /* BUYOperation.h */; };
9A585C0B1CE6440B001F20F0 /* BUYOperation.h in Headers */ = {isa = PBXBuildFile; fileRef = 9A585C041CE6440B001F20F0 /* BUYOperation.h */; };
9A585C0C1CE6440B001F20F0 /* BUYOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A585C051CE6440B001F20F0 /* BUYOperation.m */; };
9A585C0D1CE6440B001F20F0 /* BUYOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A585C051CE6440B001F20F0 /* BUYOperation.m */; };
9A6B03791CDA5D4F0054C26E /* BUYAccountCredentialsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A6B03781CDA5D4F0054C26E /* BUYAccountCredentialsTests.m */; };
9ABBCCA11CF5C9D10075B0C5 /* BUYFakeSafariController.m in Sources */ = {isa = PBXBuildFile; fileRef = 9ABBCCA01CF5C9D10075B0C5 /* BUYFakeSafariController.m */; };
B2653EC31CEF55CC0012D57D /* BUYModelManager+ApplePay.h in Headers */ = {isa = PBXBuildFile; fileRef = B2653EC11CEF55CC0012D57D /* BUYModelManager+ApplePay.h */; };
......@@ -668,6 +676,10 @@
9A47CF1E1CE50EBB00A6D5BA /* BUYApplePayTestToken.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BUYApplePayTestToken.h; sourceTree = "<group>"; };
9A47CF1F1CE50EBB00A6D5BA /* BUYApplePayTestToken.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BUYApplePayTestToken.m; sourceTree = "<group>"; };
9A47CF211CE5112A00A6D5BA /* BUYAssert.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = BUYAssert.h; path = ../Additions/BUYAssert.h; sourceTree = "<group>"; };
9A585C021CE6440B001F20F0 /* BUYCheckoutOperation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BUYCheckoutOperation.h; sourceTree = "<group>"; };
9A585C031CE6440B001F20F0 /* BUYCheckoutOperation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BUYCheckoutOperation.m; sourceTree = "<group>"; };
9A585C041CE6440B001F20F0 /* BUYOperation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BUYOperation.h; sourceTree = "<group>"; };
9A585C051CE6440B001F20F0 /* BUYOperation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BUYOperation.m; sourceTree = "<group>"; };
9A6B03781CDA5D4F0054C26E /* BUYAccountCredentialsTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BUYAccountCredentialsTests.m; sourceTree = "<group>"; };
9ABBCC9F1CF5C9D10075B0C5 /* BUYFakeSafariController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BUYFakeSafariController.h; sourceTree = "<group>"; };
9ABBCCA01CF5C9D10075B0C5 /* BUYFakeSafariController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BUYFakeSafariController.m; sourceTree = "<group>"; };
......@@ -1041,6 +1053,50 @@
name = "Test Objects";
sourceTree = "<group>";
};
9A585C011CE6440B001F20F0 /* Operations */ = {
isa = PBXGroup;
children = (
9A585C021CE6440B001F20F0 /* BUYCheckoutOperation.h */,
9A585C031CE6440B001F20F0 /* BUYCheckoutOperation.m */,
9A585C041CE6440B001F20F0 /* BUYOperation.h */,
9A585C051CE6440B001F20F0 /* BUYOperation.m */,
);
path = Operations;
sourceTree = "<group>";
};
BE1007981B6165CD0031CEE7 /* Cells */ = {
isa = PBXGroup;
children = (
BE1007991B6165EC0031CEE7 /* BUYOptionValueCell.h */,
BE10079A1B6165EC0031CEE7 /* BUYOptionValueCell.m */,
);
name = Cells;
sourceTree = "<group>";
};
BE1C4DF21AE98F8E00E21624 /* View Controllers */ = {
isa = PBXGroup;
children = (
BE1C4DF31AE98FBB00E21624 /* BUYStoreViewController.h */,
BE1C4DF41AE98FBB00E21624 /* BUYStoreViewController.m */,
BE1C4DF51AE98FBB00E21624 /* BUYViewController.h */,
BE1C4DF61AE98FBB00E21624 /* BUYViewController.m */,
);
path = "View Controllers";
sourceTree = "<group>";
};
BE2E1D371B599039009610DA /* Cells */ = {
isa = PBXGroup;
children = (
90516C951B4D76D900E35E45 /* BUYProductDescriptionCell.h */,
90516C961B4D76D900E35E45 /* BUYProductDescriptionCell.m */,
90516C971B4D76D900E35E45 /* BUYProductHeaderCell.h */,
90516C981B4D76D900E35E45 /* BUYProductHeaderCell.m */,
90516C991B4D76D900E35E45 /* BUYProductVariantCell.h */,
90516C9A1B4D76D900E35E45 /* BUYProductVariantCell.m */,
);
name = Cells;
sourceTree = "<group>";
};
F773741419C770CB0039681C = {
isa = PBXGroup;
children = (
......@@ -1139,6 +1195,8 @@
F773749F19C77E4E0039681C /* Classes */ = {
isa = PBXGroup;
children = (
9A585C011CE6440B001F20F0 /* Operations */,
841ADDEA1CB6C942000004B0 /* Additions */,
90DA8DF11BC6A82D0027BCB7 /* Buy.h */,
841ADDEA1CB6C942000004B0 /* Additions */,
F7FDA16F19C93F6100AF4E93 /* Data */,
......@@ -1241,6 +1299,7 @@
84B0A73B1CE10ED900253EB0 /* BUYWebCheckoutPaymentProvider.h in Headers */,
841ADE081CB6C942000004B0 /* NSDateFormatter+BUYAdditions.h in Headers */,
841ADE001CB6C942000004B0 /* NSArray+BUYAdditions.h in Headers */,
9A585C071CE6440B001F20F0 /* BUYCheckoutOperation.h in Headers */,
841ADE101CB6C942000004B0 /* NSDictionary+BUYAdditions.h in Headers */,
841ADE1C1CB6C942000004B0 /* NSString+BUYAdditions.h in Headers */,
841ADE181CB6C942000004B0 /* NSRegularExpression+BUYAdditions.h in Headers */,
......@@ -1265,6 +1324,7 @@
9019315F1BC5B9BC00D1134E /* BUYProductHeaderCell.h in Headers */,
901931601BC5B9BC00D1134E /* BUYProductViewErrorView.h in Headers */,
901931611BC5B9BC00D1134E /* BUYClient.h in Headers */,
9A585C0B1CE6440B001F20F0 /* BUYOperation.h in Headers */,
901931631BC5B9BC00D1134E /* BUYGradientView.h in Headers */,
9A0B0C791CEB5BBD0037D68F /* BUYAuthenticatedResponse.h in Headers */,
849810971CB7E07900CFAB58 /* BUYFlatCollectionTransformer.h in Headers */,
......@@ -1347,6 +1407,7 @@
84B0A73A1CE10ED900253EB0 /* BUYWebCheckoutPaymentProvider.h in Headers */,
841ADE031CB6C942000004B0 /* NSDate+BUYAdditions.h in Headers */,
841ADE071CB6C942000004B0 /* NSDateFormatter+BUYAdditions.h in Headers */,
9A585C061CE6440B001F20F0 /* BUYCheckoutOperation.h in Headers */,
841ADDFF1CB6C942000004B0 /* NSArray+BUYAdditions.h in Headers */,
841ADE1B1CB6C942000004B0 /* NSString+BUYAdditions.h in Headers */,
841ADE171CB6C942000004B0 /* NSRegularExpression+BUYAdditions.h in Headers */,
......@@ -1371,6 +1432,7 @@
BEB74A6D1B5564200005A300 /* BUYProductHeaderCell.h in Headers */,
903BCC7C1B7D1C2D00C21FEB /* BUYProductViewErrorView.h in Headers */,
BE9A64471B503C8B0033E558 /* BUYClient.h in Headers */,
9A585C0A1CE6440B001F20F0 /* BUYOperation.h in Headers */,
BEB74A651B5563FF0005A300 /* BUYGradientView.h in Headers */,
9A0B0C781CEB5BBD0037D68F /* BUYAuthenticatedResponse.h in Headers */,
849810961CB7E07900CFAB58 /* BUYFlatCollectionTransformer.h in Headers */,
......@@ -1621,6 +1683,8 @@
901931031BC5B9BC00D1134E /* BUYOptionValue.m in Sources */,
84980F551CB7616900CFAB58 /* BUYDecimalNumberTransformer.m in Sources */,
901931041BC5B9BC00D1134E /* BUYApplePayAdditions.m in Sources */,
901931051BC5B9BC00D1134E /* BUYOptionSelectionNavigationController.m in Sources */,
9A585C091CE6440B001F20F0 /* BUYCheckoutOperation.m in Sources */,
901931061BC5B9BC00D1134E /* BUYDiscount.m in Sources */,
84DD129A1CC63FE600A2442D /* _BUYCart.m in Sources */,
841ADE0A1CB6C942000004B0 /* NSDateFormatter+BUYAdditions.m in Sources */,
......@@ -1651,6 +1715,7 @@
8498DCAF1CDD1B2F00BD12A8 /* BUYError+BUYAdditions.m in Sources */,
84DD12D01CC6401400A2442D /* BUYCustomer.m in Sources */,
84980F391CB75C2900CFAB58 /* NSPropertyDescription+BUYAdditions.m in Sources */,
9A585C0D1CE6440B001F20F0 /* BUYOperation.m in Sources */,
841ADE221CB6C942000004B0 /* NSURL+BUYAdditions.m in Sources */,
901931161BC5B9BC00D1134E /* BUYShippingRate.m in Sources */,
841ADE061CB6C942000004B0 /* NSDate+BUYAdditions.m in Sources */,
......@@ -1762,6 +1827,8 @@
BE9A64691B503D0C0033E558 /* BUYOptionValue.m in Sources */,
84980F541CB7616900CFAB58 /* BUYDecimalNumberTransformer.m in Sources */,
BE9A646D1B503D1C0033E558 /* BUYApplePayAdditions.m in Sources */,
BEB74A2A1B554BFB0005A300 /* BUYOptionSelectionNavigationController.m in Sources */,
9A585C081CE6440B001F20F0 /* BUYCheckoutOperation.m in Sources */,
BE9A64501B503CAD0033E558 /* BUYDiscount.m in Sources */,
84DD12821CC63FE600A2442D /* _BUYCart.m in Sources */,
841ADE091CB6C942000004B0 /* NSDateFormatter+BUYAdditions.m in Sources */,
......@@ -1792,6 +1859,7 @@
8498DCAD1CDD1B2F00BD12A8 /* BUYError+BUYAdditions.m in Sources */,
84DD12CE1CC6401400A2442D /* BUYCustomer.m in Sources */,
84980F381CB75C2900CFAB58 /* NSPropertyDescription+BUYAdditions.m in Sources */,
9A585C0C1CE6440B001F20F0 /* BUYOperation.m in Sources */,
841ADE211CB6C942000004B0 /* NSURL+BUYAdditions.m in Sources */,
BE9A644E1B503CA60033E558 /* BUYShippingRate.m in Sources */,
841ADE051CB6C942000004B0 /* NSDate+BUYAdditions.m in Sources */,
......
//
// BUYCheckoutOperation.h
// Mobile Buy SDK
//
// Created by Dima Bart on 2016-05-12.
// Copyright © 2016 Shopify Inc. All rights reserved.
//
#import "BUYOperation.h"
@class BUYClient;
@class BUYCheckout;
@protocol BUYPaymentToken;
typedef void (^BUYOperationCheckoutCompletion)(BUYCheckout *checkout, NSError *error);
@interface BUYCheckoutOperation : BUYOperation
@property (strong, nonatomic, readonly) BUYOperationCheckoutCompletion completion;
- (instancetype)initWithCheckout:(BUYCheckout *)checkout token:(id<BUYPaymentToken>)token client:(BUYClient *)client completion:(BUYOperationCheckoutCompletion)completion;
@end
//
// BUYCheckoutOperation.m
// Mobile Buy SDK
//
// Created by Dima Bart on 2016-05-12.
// Copyright © 2016 Shopify Inc. All rights reserved.
//
#import "BUYCheckoutOperation.h"
#import "BUYCheckout.h"
#import "BUYClient.h"
@interface BUYOperation (Private)
- (void)setExecuting:(BOOL)executing;
- (void)setFinished:(BOOL)finished;
@end
@interface BUYCheckoutOperation ()
@property (strong, nonatomic) BUYCheckout *checkout;
@property (strong, nonatomic) id<BUYPaymentToken> token;
@property (strong, nonatomic) BUYClient *client;
@end
@implementation BUYCheckoutOperation
#pragma mark - Init -
- (instancetype)initWithCheckout:(BUYCheckout *)checkout token:(id<BUYPaymentToken>)token client:(BUYClient *)client completion:(BUYOperationCheckoutCompletion)completion
{
self = [super init];
if (self) {
_checkout = checkout;
_token = token;
_client = client;
_completion = completion;
}
return self;
}
#pragma mark - Actions -
- (void)finishWithCheckout:(BUYCheckout *)checkout
{
[self setExecuting:NO];
self.completion(checkout, nil);
}
- (void)finishWithError:(NSError *)error
{
[self setExecuting:NO];
self.completion(nil, error);
}
- (void)finishByCancellation
{
[self setFinished:YES];
[self setExecuting:NO];
}
#pragma mark - Start -
- (void)start
{
[super start];
[self setExecuting:YES];
if (self.isCancelled) {
[self finishByCancellation];
return;
}
/* ---------------------------------
* Initiate the checkout process by
* sending the checkout object to the
* backend for processing.
*/
[self.client completeCheckout:self.checkout paymentToken:self.token completion:^(BUYCheckout *checkout, NSError *error) {
if (self.isCancelled) {
[self finishByCancellation];
return;
} else if (checkout) {
/* ----------------------------------------
* Recursively poll for checkout completion
* status until we encounter an error or
* get a successful completion status.
*/
[self pollUntilCheckoutCompletion:^(BOOL success, NSError *error) {
if (self.isCancelled ) {
[self finishByCancellation];
return;
} else if (success) {
/* ---------------------------------
* Finally, after the checkout is
* complete, we'll need to fetch the
* completed checkout object.
*/
[self.client getCheckout:self.checkout completion:^(BUYCheckout *checkout, NSError *error) {
if (self.isCancelled) {
[self finishByCancellation];
return;
} else if (checkout) {
[self finishWithCheckout:checkout];
} else {
[self finishWithError:error];
}
}];
} else {
[self finishWithError:error];
}
}];
} else {
[self finishWithError:error];
}
}];
}
#pragma mark - Requests -
- (void)pollUntilCheckoutCompletion:(void(^)(BOOL success, NSError *error))completion {
[self.client getCompletionStatusOfCheckout:self.checkout completion:^(BUYStatus status, NSError *error) {
if (self.isCancelled) {
[self finishByCancellation];
return;
} else if (status == BUYStatusComplete) {
completion(YES, nil);
} else if (error) {
completion(NO, error);
} else {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self pollUntilCheckoutCompletion:completion];
});
}
}];
}
@end
//
// BUYOperation.h
// Mobile Buy SDK
//
// Created by Dima Bart on 2016-05-12.
// Copyright © 2016 Shopify Inc. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface BUYOperation : NSOperation
@end
//
// BUYOperation.m
// Mobile Buy SDK
//
// Created by Dima Bart on 2016-05-12.
// Copyright © 2016 Shopify Inc. All rights reserved.
//
#import "BUYOperation.h"
@interface BUYOperation ()
@property (atomic, assign) BOOL isExecuting;
@property (atomic, assign) BOOL isFinished;
@end
@implementation BUYOperation
#pragma mark - Init -
- (instancetype)init
{
self = [super init];
if (self) {
}
return self;
}
#pragma mark - Setters -
- (void)setExecuting:(BOOL)executing
{
[self willChangeValueForKey:@"isExecuting"];
self.isExecuting = executing;
[self didChangeValueForKey:@"isExecuting"];
}
- (void)setFinished:(BOOL)finished
{
[self willChangeValueForKey:@"isFinished"];
self.isFinished = finished;
[self didChangeValueForKey:@"isFinished"];
}
@end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment