Commit c502aa4c by Dima Bart

Move BUYClient checkout API into category.

parent be11d176
......@@ -347,6 +347,10 @@
9A0B0C6D1CEB4D300037D68F /* BUYClient+Storefront.h in Headers */ = {isa = PBXBuildFile; fileRef = 9A0B0C6A1CEB4D300037D68F /* BUYClient+Storefront.h */; settings = {ATTRIBUTES = (Public, ); }; };
9A0B0C6E1CEB4D300037D68F /* BUYClient+Storefront.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A0B0C6B1CEB4D300037D68F /* BUYClient+Storefront.m */; };
9A0B0C6F1CEB4D300037D68F /* BUYClient+Storefront.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A0B0C6B1CEB4D300037D68F /* BUYClient+Storefront.m */; };
9A0B0C721CEB52B90037D68F /* BUYClient+Checkout.h in Headers */ = {isa = PBXBuildFile; fileRef = 9A0B0C701CEB52B90037D68F /* BUYClient+Checkout.h */; settings = {ATTRIBUTES = (Public, ); }; };
9A0B0C731CEB52B90037D68F /* BUYClient+Checkout.h in Headers */ = {isa = PBXBuildFile; fileRef = 9A0B0C701CEB52B90037D68F /* BUYClient+Checkout.h */; };
9A0B0C741CEB52B90037D68F /* BUYClient+Checkout.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A0B0C711CEB52B90037D68F /* BUYClient+Checkout.m */; };
9A0B0C751CEB52B90037D68F /* BUYClient+Checkout.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A0B0C711CEB52B90037D68F /* BUYClient+Checkout.m */; };
9A102D1B1CDD1F960026CC43 /* BUYErrorTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A102D1A1CDD1F960026CC43 /* BUYErrorTests.m */; };
9A102D1E1CDD25980026CC43 /* BUYOptionValueTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A102D1D1CDD25980026CC43 /* BUYOptionValueTests.m */; };
9A47CEFD1CE39F6000A6D5BA /* BUYCreditCardToken.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A47CEFB1CE39F5B00A6D5BA /* BUYCreditCardToken.m */; };
......@@ -625,6 +629,8 @@
9A0B0C651CEA703E0037D68F /* BUYClient+Routing.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "BUYClient+Routing.m"; sourceTree = "<group>"; };
9A0B0C6A1CEB4D300037D68F /* BUYClient+Storefront.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "BUYClient+Storefront.h"; sourceTree = "<group>"; };
9A0B0C6B1CEB4D300037D68F /* BUYClient+Storefront.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "BUYClient+Storefront.m"; sourceTree = "<group>"; };
9A0B0C701CEB52B90037D68F /* BUYClient+Checkout.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "BUYClient+Checkout.h"; sourceTree = "<group>"; };
9A0B0C711CEB52B90037D68F /* BUYClient+Checkout.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "BUYClient+Checkout.m"; sourceTree = "<group>"; };
9A102D1A1CDD1F960026CC43 /* BUYErrorTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BUYErrorTests.m; sourceTree = "<group>"; };
9A102D1D1CDD25980026CC43 /* BUYOptionValueTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BUYOptionValueTests.m; sourceTree = "<group>"; };
9A47CEF81CE39EC200A6D5BA /* BUYPaymentToken.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BUYPaymentToken.h; sourceTree = "<group>"; };
......@@ -1118,6 +1124,8 @@
9A0B0C651CEA703E0037D68F /* BUYClient+Routing.m */,
9A0B0C6A1CEB4D300037D68F /* BUYClient+Storefront.h */,
9A0B0C6B1CEB4D300037D68F /* BUYClient+Storefront.m */,
9A0B0C701CEB52B90037D68F /* BUYClient+Checkout.h */,
9A0B0C711CEB52B90037D68F /* BUYClient+Checkout.m */,
8498DCB11CDD1B4A00BD12A8 /* BUYClient+Customers.h */,
8498DCB21CDD1B4A00BD12A8 /* BUYClient+Customers.m */,
);
......@@ -1143,6 +1151,7 @@
9A0B0C671CEA703E0037D68F /* BUYClient+Routing.h in Headers */,
84DD12991CC63FE600A2442D /* _BUYCart.h in Headers */,
84DD129D1CC63FE600A2442D /* _BUYCollection.h in Headers */,
9A0B0C731CEB52B90037D68F /* BUYClient+Checkout.h in Headers */,
84DD12A11CC63FE600A2442D /* _BUYImageLink.h in Headers */,
84DD12A51CC63FE600A2442D /* _BUYOption.h in Headers */,
84B0A7411CE10F8100253EB0 /* BUYClient+Checkout.h in Headers */,
......@@ -1246,6 +1255,7 @@
9A0B0C661CEA703E0037D68F /* BUYClient+Routing.h in Headers */,
84DD12B71CC63FEE00A2442D /* _BUYGiftCard.h in Headers */,
84DD12911CC63FE600A2442D /* _BUYOrder.h in Headers */,
9A0B0C721CEB52B90037D68F /* BUYClient+Checkout.h in Headers */,
84DD12811CC63FE600A2442D /* _BUYCart.h in Headers */,
84DD12851CC63FE600A2442D /* _BUYCollection.h in Headers */,
84B0A7401CE10F8100253EB0 /* BUYClient+Checkout.h in Headers */,
......@@ -1556,6 +1566,10 @@
901930FD1BC5B9BC00D1134E /* BUYTaxLine.m in Sources */,
84980F351CB75C2900CFAB58 /* NSEntityDescription+BUYAdditions.m in Sources */,
84D915461CC0359700D334FB /* BUYObserver.m in Sources */,
901930FF1BC5B9BC00D1134E /* BUYVariantOptionBreadCrumbsView.m in Sources */,
901931011BC5B9BC00D1134E /* BUYTheme+Additions.m in Sources */,
9A0B0C751CEB52B90037D68F /* BUYClient+Checkout.m in Sources */,
901931021BC5B9BC00D1134E /* BUYStoreViewController.m in Sources */,
84D73C091CDD194D000F978A /* BUYAddress.m in Sources */,
901931031BC5B9BC00D1134E /* BUYOptionValue.m in Sources */,
84980F551CB7616900CFAB58 /* BUYDecimalNumberTransformer.m in Sources */,
......@@ -1687,6 +1701,10 @@
BE9A64521B503CB80033E558 /* BUYTaxLine.m in Sources */,
84980F341CB75C2900CFAB58 /* NSEntityDescription+BUYAdditions.m in Sources */,
84D915451CC0359700D334FB /* BUYObserver.m in Sources */,
90DE92711B9897B6002EF4DA /* BUYVariantOptionBreadCrumbsView.m in Sources */,
906EAE441B836DE000976165 /* BUYTheme+Additions.m in Sources */,
9A0B0C741CEB52B90037D68F /* BUYClient+Checkout.m in Sources */,
BE9A647F1B503D960033E558 /* BUYStoreViewController.m in Sources */,
84D73C071CDD194D000F978A /* BUYAddress.m in Sources */,
BE9A64691B503D0C0033E558 /* BUYOptionValue.m in Sources */,
84980F541CB7616900CFAB58 /* BUYDecimalNumberTransformer.m in Sources */,
......
......@@ -66,6 +66,7 @@ FOUNDATION_EXPORT const unsigned char BuyVersionString[];
#import <Buy/BUYClient+Customers.h>
#import <Buy/BUYClient+Checkout.h>
#import <Buy/BUYClient+Storefront.h>
#import <Buy/BUYClient+Checkout.h>
#import <Buy/BUYError.h>
#import <Buy/BUYError+BUYAdditions.h>
#import <Buy/BUYManagedObject.h>
......
......@@ -113,7 +113,7 @@ typedef void (^BUYDataOrdersBlock)(NSArray <BUYOrder*> * _Nullable orders, NSErr
*
* @return the associated NSURLSessionDataTask
*/
- (NSURLSessionDataTask *)recoverPasswordForCustomer:(NSString *)email callback:(BUYDataCheckoutStatusBlock)block;
- (NSURLSessionDataTask *)recoverPasswordForCustomer:(NSString *)email callback:(BUYDataStatusBlock)block;
/**
* PUT /api/customers/:customer_id/customer_token/renew
......
......@@ -113,7 +113,7 @@
return [self createTokenForCustomerWithCredentials:credentials customerJSON:nil callback:block];
}
- (NSURLSessionDataTask *)recoverPasswordForCustomer:(NSString *)email callback:(BUYDataCheckoutStatusBlock)block
- (NSURLSessionDataTask *)recoverPasswordForCustomer:(NSString *)email callback:(BUYDataStatusBlock)block
{
NSURL *route = [self routeForCustomersPasswordRecovery];
return [self postRequestForURL:route object:@{@"email": email} completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
......
......@@ -31,10 +31,14 @@ extern NSString *const kShopifyError;
@interface BUYClient (Internal)
- (NSURLSessionDataTask *)postRequestForURL:(NSURL *)url object:(id <BUYSerializable>)object completionHandler:(void (^)(NSDictionary *json, NSURLResponse *response, NSError *error))completionHandler;
- (NSURLSessionDataTask *)putRequestForURL:(NSURL *)url object:(id<BUYSerializable>)object completionHandler:(void (^)(NSDictionary *json, NSURLResponse *response, NSError *error))completionHandler;
- (NSURLSessionDataTask *)getRequestForURL:(NSURL *)url completionHandler:(void (^)(NSDictionary *json, NSURLResponse *response, NSError *error))completionHandler;
- (NSURLSessionDataTask *)getRequestForURL:(NSURL *)url completionHandler:(void (^)(NSDictionary *json, NSURLResponse *response, NSError *error))completionHandler;
- (NSURLSessionDataTask *)deleteRequestForURL:(NSURL *)url completionHandler:(void (^)(NSDictionary *json, NSURLResponse *response, NSError *error))completionHandler;
- (NSURLSessionDataTask *)postRequestForURL:(NSURL *)url object:(id <BUYSerializable>)object completionHandler:(void (^)(NSDictionary *json, NSURLResponse *response, NSError *error))completionHandler;
- (NSURLSessionDataTask *)putRequestForURL:(NSURL *)url object:(id<BUYSerializable>)object completionHandler:(void (^)(NSDictionary *json, NSURLResponse *response, NSError *error))completionHandler;
- (NSURLSessionDataTask *)patchRequestForURL:(NSURL *)url object:(id <BUYSerializable>)object completionHandler:(void (^)(NSDictionary *json, NSURLResponse *response, NSError *error))completionHandler;
- (BUYStatus)statusForStatusCode:(NSUInteger)statusCode error:(NSError *)error;
- (NSError *)errorFromJSON:(NSDictionary *)json response:(NSURLResponse *)response;
@end
......@@ -114,54 +114,12 @@ typedef NS_ENUM(NSUInteger, BUYStatus) {
};
/**
* Return block containing a BUYCheckout, id<BUYPaymentToken> and/or an NSError
*
* @param checkout The returned BUYCheckout
* @param paymentToken An opaque payment token type that wraps necessary credentials for payment
* @param error Optional NSError
*/
typedef void (^BUYDataCreditCardBlock)(BUYCheckout * _Nullable checkout, id<BUYPaymentToken> _Nullable paymentToken, NSError * _Nullable error);
/**
* Return block containing a BUYCheckout and/or an NSError
*
* @param checkout The returned BUYCheckout
* @param error Optional NSError
*/
typedef void (^BUYDataCheckoutBlock)(BUYCheckout * _Nullable checkout, NSError * _Nullable error);
/**
* Return block containing a BUYCheckout, a BUYStatus and/or an NSError
*
* @param status A BUYStatus specifying the requested job's completion status
* @param error Optional NSError
*/
typedef void (^BUYDataCheckoutStatusBlock)(BUYStatus status, NSError * _Nullable error);
/**
* Return block containing BUYShippingRate objects, a BUYStatus and/or an NSError
*
* @param shippingRates Array of SHKShippingRates
* @param status A BUYStatus specifying the requested job's completion status
* @param error Optional NSError
*/
typedef void (^BUYDataShippingRatesBlock)(NSArray * _Nullable shippingRates, BUYStatus status, NSError * _Nullable error);
/**
* Return block containing a list of BUYProductImage objects and/or an NSError
*
* @param images An array of BUYProductImage objects
* @param error An optional NSError
*/
typedef void (^BUYDataImagesListBlock)(NSArray * _Nullable images, NSError * _Nullable error);
/**
* Return block containing a BUYGiftCard
*
* @param giftCard A BUYGiftCard
* @param error An optional NSError
*/
typedef void (^BUYDataGiftCardBlock)(BUYGiftCard * _Nullable giftCard, NSError * _Nullable error);
typedef void (^BUYDataStatusBlock)(BUYStatus status, NSError * _Nullable error);
/**
The BUYDataClient provides all requests needed to perform request on the Shopify Checkout API.
......@@ -237,161 +195,6 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (strong, nonatomic, nullable) NSString *customerToken;
#pragma mark - Checkout
/**
* Builds a checkout on Shopify. The checkout object is used to prepare an order
*
* @param checkout BUYCheckout to create on Shopify
* @param block (^BUYDataCheckoutBlock)(BUYCheckout *checkout, NSError *error);
*
* @return The associated NSURLSessionDataTask
*/
- (NSURLSessionDataTask *)createCheckout:(BUYCheckout *)checkout completion:(BUYDataCheckoutBlock)block;
/**
* Builds a checkout on Shopify using a Cart Token from an existing cart on your Shopify store's storefront.
* The BUYCheckout object is used to prepare an order.
*
* @param cartToken Cart Token associated with an existing BUYCheckout on Shopify
* @param block (^BUYDataCheckoutBlock)(BUYCheckout *checkout, NSError *error);
*
* @return The associated NSURLSessionDataTask
*/
- (NSURLSessionDataTask *)createCheckoutWithCartToken:(NSString *)cartToken completion:(BUYDataCheckoutBlock)block;
/**
* Applies a gift card code to the checkout.
*
* @param giftCardCode The gift card code to apply on an existing checkout on Shopify. Note: This is not the same as the gift card identifier.
* @param checkout An existing BUYCheckout on Shopify
* @param block (^BUYDataCheckoutBlock)(BUYCheckout *checkout, NSError *error);
*
* @return The associated NSURLSessionDataTask
*/
- (NSURLSessionDataTask *)applyGiftCardWithCode:(NSString *)giftCardCode toCheckout:(BUYCheckout *)checkout completion:(BUYDataCheckoutBlock)block;
/**
* Removes a gift card from the checkout.
*
* @param giftCardCode The BUYGiftCard identifier to remove on an existing checkout on Shopify.
* @param checkout An existing BUYCheckout on Shopify
* @param block (^BUYDataCheckoutBlock)(BUYCheckout *checkout, NSError *error);
*
* @return The associated NSURLSessionDataTask
*/
- (NSURLSessionDataTask *)removeGiftCard:(BUYGiftCard *)giftCard fromCheckout:(BUYCheckout *)checkout completion:(BUYDataCheckoutBlock)block;
/**
* Retrieves an updated version of a BUYCheckout from Shopify.
*
* Note: There's no guarantee that the BUYCheckout returned will be the same as the one that is passed in.
* We recommended using the BUYCheckout returned in the block.
*
* @param checkout The BUYCheckout to retrieve (updated) from Shopify
* @param block (^BUYDataCheckoutBlock)(BUYCheckout *checkout, NSError *error);
*
* @return The associated NSURLSessionDataTask
*/
- (NSURLSessionDataTask *)getCheckout:(BUYCheckout *)checkout completion:(BUYDataCheckoutBlock)block;
/**
* Updates a given BUYCheckout on Shopify.
*
* Note: There's no guarantee that the BUYCheckout returned will be the same as the one that is passed in.
* We recommended using the BUYCheckout returned in the block.
*
* Note: A BUYCheckout object with an `orderId` is a completed checkout.
*
* @param checkout The BUYCheckout to updated on Shopify
* @param block (^BUYDataCheckoutBlock)(BUYCheckout *checkout, NSError *error);
*
* @return The associated NSURLSessionDataTask
*/
- (NSURLSessionDataTask *)updateCheckout:(BUYCheckout *)checkout completion:(BUYDataCheckoutBlock)block;
/**
* Finalizes the BUYCheckout and charges the payment provider (ex: Credit Card, Apple Pay, etc).
* This enqueues a completion job on Shopify and returns immediately.
* You must get the job's status by calling checkCompletionStatusOfCheckout:block
*
* Note: There's no guarantee that the BUYCheckout returned will be the same as the one that is passed in.
* We recommended using the BUYCheckout returned in the block.
*
* @param checkout The BUYCheckout to complete
* @param paymentToken Opaque payment token object. May be nil if the total checkout amount is equal to $0.00
* @param block (^BUYDataCheckoutBlock)(BUYCheckout *checkout, NSError *error);
*
* @return The associated NSURLSessionDataTask
*/
- (NSURLSessionDataTask *)completeCheckout:(BUYCheckout *)checkout paymentToken:(_Nullable id<BUYPaymentToken>)paymentToken completion:(BUYDataCheckoutBlock)block;
/**
* Retrieve the status of a BUYCheckout. This checks the status of the current payment processing job for the provided checkout.
* Once the job is complete (status == BUYStatusComplete), you can retrieve the completed order by calling `getCheckout:completion`
*
* @param checkout The BUYCheckout to retrieve completion status for
* @param block (^BUYDataCheckoutStatusBlock)(BUYCheckout *checkout, BUYStatus status, NSError *error);
*
* @return The associated NSURLSessionDataTask
*/
- (NSURLSessionDataTask *)getCompletionStatusOfCheckout:(BUYCheckout *)checkout completion:(BUYDataCheckoutStatusBlock)block;
/**
* Retrieve the status of a checkout given a URL obtained in the UIApplicationDelegate method `application:sourceApplication:annotation`
*
* @param url The URL resource used to open the application
* @param block (^BUYDataCheckoutStatusBlock)(BUYCheckout *checkout, BUYStatus status, NSError *error);
*
* @return The associated NSURLSessionDataTask
*/
- (NSURLSessionDataTask *)getCompletionStatusOfCheckoutURL:(NSURL *)url completion:(BUYDataCheckoutStatusBlock)block;
#pragma mark - Shipping Rates
/**
* Retrieves a list of applicable shipping rates for a given BUYCheckout.
* Add the preferred/selected BUYShippingRate to BUYCheckout, then update BUYCheckout
*
* @param checkout The BUYCheckout to retrieve shipping rates for
* @param block (^BUYDataShippingRatesBlock)(NSArray *shippingRates, BUYStatus status, NSError *error);
*
* @return The associated NSURLSessionDataTask
*/
- (NSURLSessionDataTask *)getShippingRatesForCheckout:(BUYCheckout *)checkout completion:(BUYDataShippingRatesBlock)block;
#pragma mark - Payment Management
/**
* Prepares a credit card for usage during the checkout process. This sends it to Shopify's secure servers.
* Note: Storing the token does not charge the associated card (credit or otherwise).
* The card will be charged upon finalizing the checkout (`completeCheckout:completion:`)
*
* You MUST call `completeCheckout:completion:` after this call and receiving a `paymentSessionId`.
* The `paymentSessionId` on the `BUYCheckout` object is not persisted on `updateCheckout:completion:` calls.
*
* @param creditCard BUYCreditCard to prepare for usage
* @param checkout The BUYCheckout associated to the purchase.
* The `billingAddress` stored on the BUYCheckout object is optional and recommended and
* used (if provided) to help with fraud checking.
* @param block (^BUYDataCreditCardBlock)(BUYCheckout *checkout, NSString *paymentSessionId, NSError *error);
*
* @return The associated NSURLSessionDataTask
*/
- (NSURLSessionDataTask *)storeCreditCard:(BUYCreditCard *)creditCard checkout:(BUYCheckout *)checkout completion:(BUYDataCreditCardBlock)block;
/**
* Convenience method to release all product inventory reservations by setting its
* `reservationTime` to `@0` and calls `updateCheckout:completion`. We recommend creating
* a new BUYCheckout object from a BUYCart for further API calls.
*
* @param checkout The BUYCheckout to expire
* @param block (^BUYDataCheckoutBlock)(BUYCheckout *checkout, NSError *error);
*
* @return The associated NSURLSessionDataTask
*/
- (NSURLSessionDataTask *)removeProductReservationsFromCheckout:(BUYCheckout *)checkout completion:(BUYDataCheckoutBlock)block;
#pragma mark - Deprecated methods
/**
......
......@@ -39,7 +39,6 @@
#import "BUYModelManager.h"
#import "BUYOrder.h"
#import "BUYProduct.h"
#import "BUYPaymentToken.h"
#import "BUYShippingRate.h"
#import "BUYShop.h"
#import "BUYShopifyErrorCodes.h"
......@@ -57,8 +56,6 @@
#define kMinSuccessfulStatusCode 200
#define kMaxSuccessfulStatusCode 299
#define BUYAssertCheckout(checkout) BUYAssert([(checkout) hasToken], @"Checkout assertion failed. Checkout must have a valid token associated with it.")
NSString * const BUYVersionString = @"1.3";
NSString *const kShopifyError = @"shopify";
......@@ -123,224 +120,9 @@ NSString *const BUYClientCustomerAccessToken = @"X-Shopify-Customer-Access-Token
_pageSize = MAX(MIN(pageSize, 250), 1);
}
#pragma mark - Checkout
- (void)handleCheckoutResponse:(NSDictionary *)json error:(NSError *)error block:(BUYDataCheckoutBlock)block
{
BUYCheckout *checkout = nil;
if (!error) {
checkout = [self.modelManager insertCheckoutWithJSONDictionary:json[@"checkout"]];
}
block(checkout, error);
}
- (void)configureCheckout:(BUYCheckout *)checkout
{
checkout.marketingAttribution = @{@"medium": @"iOS", @"source": self.applicationName};
checkout.sourceName = @"mobile_app";
if (self.urlScheme || checkout.webReturnToURL) {
checkout.webReturnToURL = checkout.webReturnToURL ?: [NSURL URLWithString:self.urlScheme];
checkout.webReturnToLabel = checkout.webReturnToLabel ?: [@"Return to " stringByAppendingString:self.applicationName];
}
}
- (NSURLSessionDataTask *)createCheckout:(BUYCheckout *)checkout completion:(BUYDataCheckoutBlock)block
{
BUYAssert(checkout, @"Failed to create checkout. Invalid checkout object.");
// Inject channel and marketing attributions
[self configureCheckout:checkout];
NSDictionary *json = [checkout jsonDictionaryForCheckout];
return [self postCheckout:json completion:block];
}
- (NSURLSessionDataTask *)createCheckoutWithCartToken:(NSString *)cartToken completion:(BUYDataCheckoutBlock)block
{
BUYAssert(cartToken, @"Failed to create checkout. Invalid cart token");
BUYCheckout *checkout = [self.modelManager checkoutwithCartToken:cartToken];
[self configureCheckout:checkout];
NSDictionary *json = [checkout jsonDictionaryForCheckout];
return [self postCheckout:json completion:block];
}
- (NSURLSessionDataTask *)postCheckout:(NSDictionary *)checkoutJSON completion:(BUYDataCheckoutBlock)block
{
return [self postRequestForURL:[self routeForCheckouts] object:checkoutJSON completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
[self handleCheckoutResponse:json error:error block:block];
}];
}
- (NSURLSessionDataTask *)applyGiftCardWithCode:(NSString *)giftCardCode toCheckout:(BUYCheckout *)checkout completion:(BUYDataCheckoutBlock)block
{
BUYAssertCheckout(checkout);
BUYAssert(giftCardCode.length > 0, @"Failed to apply gift card code. Invalid gift card code.");
BUYGiftCard *giftCard = [self.modelManager giftCardWithCode:giftCardCode];
NSURL *route = [self routeForCheckoutsUsingGiftCardWithToken:checkout.token];
return [self postRequestForURL:route object:giftCard completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
if (json && !error) {
[self updateCheckout:checkout withGiftCardDictionary:json[@"gift_card"] addingGiftCard:YES];
}
block(checkout, error);
}];
}
- (NSURLSessionDataTask *)removeGiftCard:(BUYGiftCard *)giftCard fromCheckout:(BUYCheckout *)checkout completion:(BUYDataCheckoutBlock)block
{
BUYAssertCheckout(checkout);
BUYAssert(giftCard.identifier, @"Failed to remove gift card. Gift card must have a valid identifier.");
NSURL *route = [self routeForCheckoutsUsingGiftCard:giftCard.identifier token:checkout.token];
return [self deleteRequestForURL:route completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
if (!error) {
[self updateCheckout:checkout withGiftCardDictionary:json[@"gift_card"] addingGiftCard:NO];
}
block(checkout, error);
}];
}
- (void)updateCheckout:(BUYCheckout *)checkout withGiftCardDictionary:(NSDictionary *)giftCardDictionary addingGiftCard:(BOOL)addingGiftCard
{
if (addingGiftCard) {
BUYGiftCard *giftCard = [self.modelManager insertGiftCardWithJSONDictionary:giftCardDictionary];
[checkout.giftCardsSet addObject:giftCard];
} else {
[checkout removeGiftCardWithIdentifier:giftCardDictionary[@"id"]];
}
checkout.paymentDue = [NSDecimalNumber buy_decimalNumberFromJSON:giftCardDictionary[@"checkout"][@"payment_due"]];
// Marking the checkout as clean. The properties we have updated above we don't need to re-sync with Shopify.
// There's also an issue with gift cards where syncing the gift card JSON won't work since the update endpoint
// doesn't accept the gift card without a gift card code (which we do not have).
[checkout markAsClean];
}
- (NSURLSessionDataTask *)getCheckout:(BUYCheckout *)checkout completion:(BUYDataCheckoutBlock)block
{
BUYAssertCheckout(checkout);
NSURL *route = [self routeForCheckoutsWithToken:checkout.token];
return [self getRequestForURL:route completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
[self handleCheckoutResponse:json error:error block:block];
}];
}
- (NSURLSessionDataTask *)updateCheckout:(BUYCheckout *)checkout completion:(BUYDataCheckoutBlock)block
{
BUYAssertCheckout(checkout);
NSURL *route = [self routeForCheckoutsWithToken:checkout.token];
return [self patchRequestForURL:route object:checkout completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
[self handleCheckoutResponse:json error:error block:block];
}];
}
- (NSURLSessionDataTask*)completeCheckout:(BUYCheckout *)checkout paymentToken:(id<BUYPaymentToken>)paymentToken completion:(BUYDataCheckoutBlock)block
{
BUYAssertCheckout(checkout);
BOOL isFree = (checkout.paymentDue && checkout.paymentDue.floatValue == 0);
BUYAssert(paymentToken || isFree, @"Failed to complete checkout. Checkout must have a payment token or have a payment value equal to $0.00");
NSURL *route = [self routeForCheckoutsCompletionWithToken:checkout.token];
return [self postRequestForURL:route object:[paymentToken JSONDictionary] completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
[self handleCheckoutResponse:json error:error block:block];
}];
}
- (NSURLSessionDataTask *)getCompletionStatusOfCheckout:(BUYCheckout *)checkout completion:(BUYDataCheckoutStatusBlock)block
{
BUYAssertCheckout(checkout);
return [self getCompletionStatusOfCheckoutToken:checkout.token completion:block];
}
- (NSURLSessionDataTask *)getCompletionStatusOfCheckoutURL:(NSURL *)url completion:(BUYDataCheckoutStatusBlock)block
{
NSURLComponents *components = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
NSString *token = nil;
for (NSURLQueryItem *item in components.queryItems) {
if ([item.name isEqualToString:@"checkout[token]"]) {
token = item.value;
break;
}
}
BUYAssert(token, @"Failed to get completion status of checkout. Checkout URL must have a valid token associated with it.");
return [self getCompletionStatusOfCheckoutToken:token completion:block];
}
- (NSURLSessionDataTask *)getCompletionStatusOfCheckoutToken:(NSString *)token completion:(BUYDataCheckoutStatusBlock)block
{
NSURL *route = [self routeForCheckoutsProcessingWithToken:token];
return [self getRequestForURL:route completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
block([BUYClient statusForStatusCode:statusCode error:error], error);
}];
}
#pragma mark - Shipping Rates
- (NSURLSessionDataTask *)getShippingRatesForCheckout:(BUYCheckout *)checkout completion:(BUYDataShippingRatesBlock)block
{
BUYAssertCheckout(checkout);
NSURL *route = [self routeForCheckoutsShippingRatesWithToken:checkout.token parameters:@{
@"checkout" : @"",
}];
return [self getRequestForURL:route completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
NSArray *shippingRates = nil;
if (json && !error) {
shippingRates = [self.modelManager insertShippingRatesWithJSONArray:json[@"shipping_rates"]];
}
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
block(shippingRates, [BUYClient statusForStatusCode:statusCode error:error], error);
}];
}
#pragma mark - Payments
- (NSURLSessionDataTask *)storeCreditCard:(BUYCreditCard *)creditCard checkout:(BUYCheckout *)checkout completion:(BUYDataCreditCardBlock)completion
{
BUYAssertCheckout(checkout);
BUYAssert(creditCard, @"Failed to store credit card. No credit card provided.");
NSMutableDictionary *json = [[NSMutableDictionary alloc] init];
json[@"token"] = checkout.token;
json[@"credit_card"] = [creditCard jsonDictionaryForCheckout];
if (checkout.billingAddress) {
json[@"billing_address"] = [checkout.billingAddress jsonDictionaryForCheckout];
}
return [self postRequestForURL:checkout.paymentURL object:@{ @"checkout" : json } completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
id<BUYPaymentToken> token = nil;
if (!error) {
token = [[BUYCreditCardToken alloc] initWithPaymentSessionID:json[@"id"]];
}
completion(checkout, token, error);
}];
}
- (NSURLSessionDataTask *)removeProductReservationsFromCheckout:(BUYCheckout *)checkout completion:(BUYDataCheckoutBlock)block
{
BUYAssertCheckout(checkout);
checkout.reservationTime = @0;
return [self updateCheckout:checkout completion:block];
}
#pragma mark - Error
+ (BUYStatus)statusForStatusCode:(NSUInteger)statusCode error:(NSError *)error
- (BUYStatus)statusForStatusCode:(NSUInteger)statusCode error:(NSError *)error
{
BUYStatus status = BUYStatusUnknown;
if (statusCode == BUYStatusPreconditionFailed) {
......
......@@ -40,6 +40,7 @@
#import "BUYClient.h"
#import "BUYClient+Customers.h"
#import "BUYClient+Storefront.h"
#import "BUYClient+Checkout.h"
#import "BUYCollection.h"
#import "BUYCreditCard.h"
#import "BUYCustomer.h"
......
......@@ -27,7 +27,7 @@
#import "BUYApplePayHelpers.h"
#import "BUYAddress.h"
#import "BUYApplePayAdditions.h"
#import "BUYClient.h"
#import "BUYClient+Checkout.h"
#import "BUYClient+Storefront.h"
#import "BUYCheckout.h"
#import "BUYError.h"
......
......@@ -33,6 +33,7 @@
#import "BUYOrder.h"
#import "BUYShopifyErrorCodes.h"
#import "BUYClient+Storefront.h"
#import "BUYClient+Checkout.h"
@interface BUYStoreViewController () <WKNavigationDelegate, WKScriptMessageHandler>
@end
......
......@@ -31,6 +31,7 @@
#import "BUYApplePayAdditions.h"
#import "BUYCart.h"
#import "BUYClient+Storefront.h"
#import "BUYClient+Checkout.h"
#import "BUYViewController.h"
#import "BUYApplePayHelpers.h"
#import "BUYDiscount.h"
......
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