Commit f1aebcaf by Brent Gulanowski

Update all models to derive from generated classes.

parent eab091af
...@@ -28,11 +28,8 @@ ...@@ -28,11 +28,8 @@
@import PassKit; @import PassKit;
@import UIKit; @import UIKit;
@import XCTest; @import XCTest;
#import <Buy/Buy.h> #import <Buy/Buy.h>
#import "BUYAddress+Additions.h"
#import "BUYCheckout_Private.h"
#import "NSDecimalNumber+BUYAdditions.h"
#import "NSDateFormatter+BUYAdditions.h"
#import "BUYPKContact.h" #import "BUYPKContact.h"
#import "BUYNSPersonNameComponents.h" #import "BUYNSPersonNameComponents.h"
...@@ -44,13 +41,20 @@ ...@@ -44,13 +41,20 @@
@implementation BUYApplePayAdditionsTest { @implementation BUYApplePayAdditionsTest {
BUYCheckout *_checkout; BUYCheckout *_checkout;
BUYModelManager *_modelManager;
} }
- (void)setUp - (void)setUp
{ {
_modelManager = [BUYModelManager modelManager];
_checkout = [[BUYCheckout alloc] initWithCart:nil]; _checkout = [[BUYCheckout alloc] initWithCart:nil];
} }
- (void)tearDown
{
_modelManager = nil;
}
#pragma mark - BUYCheckout Apple Pay additions #pragma mark - BUYCheckout Apple Pay additions
- (void)testSummaryItemsWithEmptyCheckout - (void)testSummaryItemsWithEmptyCheckout
...@@ -67,7 +71,7 @@ ...@@ -67,7 +71,7 @@
- (void)testFullSummaryItems - (void)testFullSummaryItems
{ {
_checkout.subtotalPrice = [NSDecimalNumber one]; _checkout.subtotalPrice = [NSDecimalNumber one];
_checkout.shippingRate = [[BUYShippingRate alloc] initWithDictionary:@{ @"price" : @"2.00" }]; _checkout.shippingRate = [[BUYShippingRate alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"price" : @"2.00" }];
_checkout.totalTax = [NSDecimalNumber decimalNumberWithString:@"1.00"]; _checkout.totalTax = [NSDecimalNumber decimalNumberWithString:@"1.00"];
_checkout.paymentDue = [NSDecimalNumber decimalNumberWithString:@"4.00"]; _checkout.paymentDue = [NSDecimalNumber decimalNumberWithString:@"4.00"];
...@@ -87,7 +91,7 @@ ...@@ -87,7 +91,7 @@
- (void)testSummaryItemsWithShippingRate - (void)testSummaryItemsWithShippingRate
{ {
_checkout.subtotalPrice = [NSDecimalNumber one]; _checkout.subtotalPrice = [NSDecimalNumber one];
_checkout.shippingRate = [[BUYShippingRate alloc] initWithDictionary:@{ @"price" : @"2.00" }]; _checkout.shippingRate = [[BUYShippingRate alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"price" : @"2.00" }];
_checkout.paymentDue = [NSDecimalNumber decimalNumberWithString:@"3.00"]; _checkout.paymentDue = [NSDecimalNumber decimalNumberWithString:@"3.00"];
NSArray *summaryItems = [_checkout buy_summaryItems]; NSArray *summaryItems = [_checkout buy_summaryItems];
...@@ -104,7 +108,7 @@ ...@@ -104,7 +108,7 @@
- (void)testSummaryItemsWithFreeShippingAndTaxesShouldNotShowShippingOrTaxes - (void)testSummaryItemsWithFreeShippingAndTaxesShouldNotShowShippingOrTaxes
{ {
_checkout.subtotalPrice = [NSDecimalNumber one]; _checkout.subtotalPrice = [NSDecimalNumber one];
_checkout.shippingRate = [[BUYShippingRate alloc] initWithDictionary:@{ @"price" : @"0.00" }]; _checkout.shippingRate = [[BUYShippingRate alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"price" : @"0.00" }];
_checkout.totalTax = [NSDecimalNumber zero]; _checkout.totalTax = [NSDecimalNumber zero];
_checkout.paymentDue = [NSDecimalNumber decimalNumberWithString:@"3.00"]; _checkout.paymentDue = [NSDecimalNumber decimalNumberWithString:@"3.00"];
...@@ -120,13 +124,12 @@ ...@@ -120,13 +124,12 @@
- (void)testSummaryItemsWithZeroDiscount - (void)testSummaryItemsWithZeroDiscount
{ {
_checkout.subtotalPrice = [NSDecimalNumber one]; _checkout.subtotalPrice = [NSDecimalNumber one];
_checkout.shippingRate = [[BUYShippingRate alloc] initWithDictionary:@{ @"price" : @"0.00" }]; _checkout.shippingRate = [[BUYShippingRate alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"price" : @"0.00" }];
_checkout.totalTax = [NSDecimalNumber zero]; _checkout.totalTax = [NSDecimalNumber zero];
_checkout.paymentDue = [NSDecimalNumber decimalNumberWithString:@"3.00"]; _checkout.paymentDue = [NSDecimalNumber decimalNumberWithString:@"3.00"];
BUYDiscount *discount = [[BUYDiscount alloc] init]; BUYDiscount *discount = [_modelManager discountWithCode:@"BANANA"];
discount.code = @"BANANA";
discount.amount = [NSDecimalNumber zero]; discount.amount = [NSDecimalNumber zero];
discount.applicable = YES; discount.applicableValue = YES;
_checkout.discount = discount; _checkout.discount = discount;
NSArray *summaryItems = [_checkout buy_summaryItems]; NSArray *summaryItems = [_checkout buy_summaryItems];
...@@ -141,13 +144,12 @@ ...@@ -141,13 +144,12 @@
- (void)testSummaryItemsWithNonZeroDiscount - (void)testSummaryItemsWithNonZeroDiscount
{ {
_checkout.subtotalPrice = [NSDecimalNumber one]; _checkout.subtotalPrice = [NSDecimalNumber one];
_checkout.shippingRate = [[BUYShippingRate alloc] initWithDictionary:@{ @"price" : @"0.00" }]; _checkout.shippingRate = [[BUYShippingRate alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"price" : @"0.00" }];
_checkout.totalTax = [NSDecimalNumber zero]; _checkout.totalTax = [NSDecimalNumber zero];
_checkout.paymentDue = [NSDecimalNumber decimalNumberWithString:@"2.00"]; _checkout.paymentDue = [NSDecimalNumber decimalNumberWithString:@"2.00"];
BUYDiscount *discount = [[BUYDiscount alloc] init]; BUYDiscount *discount = [_modelManager discountWithCode:@"BANANA"];
discount.code = @"BANANA";
discount.amount = [NSDecimalNumber one]; discount.amount = [NSDecimalNumber one];
discount.applicable = YES; discount.applicableValue = YES;
_checkout.discount = discount; _checkout.discount = discount;
NSArray *summaryItems = [_checkout buy_summaryItems]; NSArray *summaryItems = [_checkout buy_summaryItems];
...@@ -165,13 +167,12 @@ ...@@ -165,13 +167,12 @@
- (void)testSummaryItemsWithNonZeroCodelessDiscount - (void)testSummaryItemsWithNonZeroCodelessDiscount
{ {
_checkout.subtotalPrice = [NSDecimalNumber one]; _checkout.subtotalPrice = [NSDecimalNumber one];
_checkout.shippingRate = [[BUYShippingRate alloc] initWithDictionary:@{ @"price" : @"0.00" }]; _checkout.shippingRate = [[BUYShippingRate alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"price" : @"0.00" }];
_checkout.totalTax = [NSDecimalNumber zero]; _checkout.totalTax = [NSDecimalNumber zero];
_checkout.paymentDue = [NSDecimalNumber decimalNumberWithString:@"2.00"]; _checkout.paymentDue = [NSDecimalNumber decimalNumberWithString:@"2.00"];
BUYDiscount *discount = [[BUYDiscount alloc] init]; BUYDiscount *discount = [_modelManager discountWithCode:@""];
discount.code = @"";
discount.amount = [NSDecimalNumber one]; discount.amount = [NSDecimalNumber one];
discount.applicable = YES; discount.applicableValue = YES;
_checkout.discount = discount; _checkout.discount = discount;
NSArray *summaryItems = [_checkout buy_summaryItems]; NSArray *summaryItems = [_checkout buy_summaryItems];
...@@ -189,17 +190,16 @@ ...@@ -189,17 +190,16 @@
- (void)testSummaryItemsWithGiftCard - (void)testSummaryItemsWithGiftCard
{ {
_checkout.subtotalPrice = [NSDecimalNumber decimalNumberWithString:@"12.00"]; _checkout.subtotalPrice = [NSDecimalNumber decimalNumberWithString:@"12.00"];
_checkout.shippingRate = [[BUYShippingRate alloc] initWithDictionary:@{ @"price" : @"0.00" }]; _checkout.shippingRate = [[BUYShippingRate alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"price" : @"0.00" }];
_checkout.totalTax = [NSDecimalNumber zero]; _checkout.totalTax = [NSDecimalNumber zero];
_checkout.paymentDue = [NSDecimalNumber decimalNumberWithString:@"2.00"]; _checkout.paymentDue = [NSDecimalNumber decimalNumberWithString:@"2.00"];
BUYDiscount *discount = [[BUYDiscount alloc] init]; BUYDiscount *discount = [_modelManager discountWithCode:@""];
discount.code = @"";
discount.amount = [NSDecimalNumber one]; discount.amount = [NSDecimalNumber one];
discount.applicable = YES; discount.applicableValue = YES;
_checkout.discount = discount; _checkout.discount = discount;
BUYGiftCard *giftCard = [[BUYGiftCard alloc] initWithDictionary:@{ @"amount_used" : [NSDecimalNumber decimalNumberWithString:@"10.00"], @"balance" : [NSDecimalNumber decimalNumberWithString:@"10.00"], @"last_characters" : @"1234" }]; BUYGiftCard *giftCard = [[BUYGiftCard alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"amount_used" : [NSDecimalNumber decimalNumberWithString:@"10.00"], @"balance" : [NSDecimalNumber decimalNumberWithString:@"10.00"], @"last_characters" : @"1234" }];
_checkout.giftCards = @[giftCard]; [[_checkout giftCardsSet] addObject:giftCard];
NSArray *summaryItems = [_checkout buy_summaryItems]; NSArray *summaryItems = [_checkout buy_summaryItems];
XCTAssertEqual(5, [summaryItems count]); XCTAssertEqual(5, [summaryItems count]);
...@@ -227,15 +227,15 @@ ...@@ -227,15 +227,15 @@
NSDate *lastDate = [self dateWithoutTime:[NSDate dateWithTimeIntervalSinceNow:day]]; NSDate *lastDate = [self dateWithoutTime:[NSDate dateWithTimeIntervalSinceNow:day]];
NSDateFormatter *dateFormatter = [NSDateFormatter dateFormatterForShippingRates]; NSDateFormatter *dateFormatter = [NSDateFormatter dateFormatterForShippingRates];
BUYShippingRate *rate1 = [[BUYShippingRate alloc] initWithDictionary:@{@"price" : @"5.00", @"id" : @"1234", @"title" : @"Banana", @"delivery_range" : @[[dateFormatter stringFromDate:firstDate], [dateFormatter stringFromDate:lastDate]]}]; BUYShippingRate *rate1 = [[BUYShippingRate alloc] initWithModelManager:_modelManager JSONDictionary:@{@"price" : @"5.00", @"id" : @"1234", @"title" : @"Banana", @"delivery_range" : @[[dateFormatter stringFromDate:firstDate], [dateFormatter stringFromDate:lastDate]]}];
firstDate = [self dateWithoutTime:[NSDate dateWithTimeIntervalSinceNow:day * 3]]; firstDate = [self dateWithoutTime:[NSDate dateWithTimeIntervalSinceNow:day * 3]];
lastDate = [self dateWithoutTime:[NSDate dateWithTimeIntervalSinceNow:day * 5]]; lastDate = [self dateWithoutTime:[NSDate dateWithTimeIntervalSinceNow:day * 5]];
BUYShippingRate *rate2 = [[BUYShippingRate alloc] initWithDictionary:@{@"price" : @"3.00", @"id" : @"5678", @"title" : @"Dinosaur", @"delivery_range" : @[[dateFormatter stringFromDate:firstDate], [dateFormatter stringFromDate:lastDate]]}]; BUYShippingRate *rate2 = [[BUYShippingRate alloc] initWithModelManager:_modelManager JSONDictionary:@{@"price" : @"3.00", @"id" : @"5678", @"title" : @"Dinosaur", @"delivery_range" : @[[dateFormatter stringFromDate:firstDate], [dateFormatter stringFromDate:lastDate]]}];
firstDate = [self dateWithoutTime:[NSDate dateWithTimeIntervalSinceNow:day * 10]]; firstDate = [self dateWithoutTime:[NSDate dateWithTimeIntervalSinceNow:day * 10]];
lastDate = [self dateWithoutTime:[NSDate dateWithTimeIntervalSinceNow:day * 12]]; lastDate = [self dateWithoutTime:[NSDate dateWithTimeIntervalSinceNow:day * 12]];
BUYShippingRate *rate3 = [[BUYShippingRate alloc] initWithDictionary:@{@"price" : @"19.00", @"id" : @"1357", @"title" : @"Bulldozer", @"delivery_range" : @[[dateFormatter stringFromDate:firstDate], [dateFormatter stringFromDate:lastDate]]}]; BUYShippingRate *rate3 = [[BUYShippingRate alloc] initWithModelManager:_modelManager JSONDictionary:@{@"price" : @"19.00", @"id" : @"1357", @"title" : @"Bulldozer", @"delivery_range" : @[[dateFormatter stringFromDate:firstDate], [dateFormatter stringFromDate:lastDate]]}];
NSArray *shippingMethods = [BUYShippingRate buy_convertShippingRatesToShippingMethods:@[rate1, rate2, rate3]]; NSArray *shippingMethods = [BUYShippingRate buy_convertShippingRatesToShippingMethods:@[rate1, rate2, rate3]];
XCTAssertEqual(3, [shippingMethods count]); XCTAssertEqual(3, [shippingMethods count]);
...@@ -353,7 +353,7 @@ ...@@ -353,7 +353,7 @@
ABRecordSetValue(person, kABPersonAddressProperty, addresses, &error); ABRecordSetValue(person, kABPersonAddressProperty, addresses, &error);
CFRelease(addresses); CFRelease(addresses);
BUYAddress *newAddress = [BUYAddress buy_addressFromRecord:person]; BUYAddress *newAddress = [_modelManager buyAddressWithABRecord:person];
CFRelease(person); CFRelease(person);
...@@ -410,7 +410,7 @@ ...@@ -410,7 +410,7 @@
[postalAddress setISOCountryCode:@"CA"]; [postalAddress setISOCountryCode:@"CA"];
[contact setPostalAddress:postalAddress]; [contact setPostalAddress:postalAddress];
return [BUYAddress buy_addressFromContact:contact]; return [_modelManager buyAddressWithContact:contact];
} }
- (void)testCompareAddressWithContactWithNameOrStreetOrPhone - (void)testCompareAddressWithContactWithNameOrStreetOrPhone
......
...@@ -33,33 +33,32 @@ ...@@ -33,33 +33,32 @@
@implementation BUYCartTest { @implementation BUYCartTest {
BUYCart *_cart; BUYCart *_cart;
BUYModelManager *_modelManager;
} }
- (void)setUp - (void)setUp
{ {
[super setUp]; [super setUp];
_modelManager = [BUYModelManager modelManager];
_cart = [[BUYCart alloc] init]; _cart = [_modelManager insertCartWithJSONDictionary:nil];
} }
#pragma mark - Serialization Tests - (void)tearDown
- (void)testJsonDictionaryShouldBeEmptyWhenNothingIsSet
{ {
NSDictionary *json = [_cart jsonDictionaryForCheckout]; _cart = nil;
XCTAssertNotNil(json); _modelManager = nil;
XCTAssertEqual(0, [json count]);
} }
#pragma mark - Serialization Tests
- (void)testCartShouldBeInvalidWhenEmpty - (void)testCartShouldBeInvalidWhenEmpty
{ {
BUYCart *cart = [[BUYCart alloc] init]; XCTAssertFalse([_cart isValid]);
XCTAssertFalse([cart isValid]);
} }
- (void)testAddVariantWillAddALineItem - (void)testAddVariantWillAddALineItem
{ {
BUYProductVariant *variant = [[BUYProductVariant alloc] initWithDictionary:@{ @"id" : @1 }]; BUYProductVariant *variant = [[BUYProductVariant alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"id" : @1 }];
[_cart addVariant:variant]; [_cart addVariant:variant];
XCTAssertEqual([[_cart lineItems] count], 1); XCTAssertEqual([[_cart lineItems] count], 1);
XCTAssertEqualObjects([[_cart lineItems][0] variantId], variant.identifier); XCTAssertEqualObjects([[_cart lineItems][0] variantId], variant.identifier);
...@@ -67,10 +66,10 @@ ...@@ -67,10 +66,10 @@
- (void)testAddingTwoDifferentVariantsWillAddDifferentLineItems - (void)testAddingTwoDifferentVariantsWillAddDifferentLineItems
{ {
BUYProductVariant *variant = [[BUYProductVariant alloc] initWithDictionary:@{ @"id" : @1 }]; BUYProductVariant *variant = [[BUYProductVariant alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"id" : @1 }];
[_cart addVariant:variant]; [_cart addVariant:variant];
BUYProductVariant *variant2 = [[BUYProductVariant alloc] initWithDictionary:@{ @"id" : @2 }]; BUYProductVariant *variant2 = [[BUYProductVariant alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"id" : @2 }];
[_cart addVariant:variant2]; [_cart addVariant:variant2];
XCTAssertEqual([[_cart lineItems] count], 2); XCTAssertEqual([[_cart lineItems] count], 2);
...@@ -78,7 +77,7 @@ ...@@ -78,7 +77,7 @@
- (void)testAddingAVariantOfTheSameTypeWillNotAddAnotherLineItem - (void)testAddingAVariantOfTheSameTypeWillNotAddAnotherLineItem
{ {
BUYProductVariant *variant = [[BUYProductVariant alloc] initWithDictionary:@{ @"id" : @1 }]; BUYProductVariant *variant = [[BUYProductVariant alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"id" : @1 }];
[_cart addVariant:variant]; [_cart addVariant:variant];
[_cart addVariant:variant]; [_cart addVariant:variant];
XCTAssertEqual([[_cart lineItems] count], 1); XCTAssertEqual([[_cart lineItems] count], 1);
...@@ -88,7 +87,7 @@ ...@@ -88,7 +87,7 @@
- (void)testRemovingAVariantDecrementsQuantity - (void)testRemovingAVariantDecrementsQuantity
{ {
BUYProductVariant *variant = [[BUYProductVariant alloc] initWithDictionary:@{ @"id" : @1 }]; BUYProductVariant *variant = [[BUYProductVariant alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"id" : @1 }];
[_cart addVariant:variant]; [_cart addVariant:variant];
[_cart addVariant:variant]; [_cart addVariant:variant];
[_cart removeVariant:variant]; [_cart removeVariant:variant];
...@@ -97,7 +96,7 @@ ...@@ -97,7 +96,7 @@
- (void)testRemovingAllVariantsOfASingleTypeRemovesItsLineItem - (void)testRemovingAllVariantsOfASingleTypeRemovesItsLineItem
{ {
BUYProductVariant *variant = [[BUYProductVariant alloc] initWithDictionary:@{ @"id" : @1 }]; BUYProductVariant *variant = [[BUYProductVariant alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"id" : @1 }];
[_cart addVariant:variant]; [_cart addVariant:variant];
[_cart removeVariant:variant]; [_cart removeVariant:variant];
XCTAssertEqual([[_cart lineItems] count], 0); XCTAssertEqual([[_cart lineItems] count], 0);
...@@ -105,8 +104,8 @@ ...@@ -105,8 +104,8 @@
- (void)testSetVariantWithQuantity - (void)testSetVariantWithQuantity
{ {
BUYProductVariant *variant = [[BUYProductVariant alloc] initWithDictionary:@{ @"id" : @1 }]; BUYProductVariant *variant = [[BUYProductVariant alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"id" : @1 }];
BUYProductVariant *variantTwo = [[BUYProductVariant alloc] initWithDictionary:@{ @"id" : @2 }]; BUYProductVariant *variantTwo = [[BUYProductVariant alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"id" : @2 }];
[_cart setVariant:variant withTotalQuantity:2]; [_cart setVariant:variant withTotalQuantity:2];
XCTAssertEqual([[_cart lineItems] count], 1); XCTAssertEqual([[_cart lineItems] count], 1);
......
...@@ -26,12 +26,15 @@ ...@@ -26,12 +26,15 @@
@import UIKit; @import UIKit;
@import XCTest; @import XCTest;
#import <Buy/Buy.h> #import <Buy/Buy.h>
#import "BUYCheckout_Private.h" #import "BUYCheckout.h"
@interface BUYCheckoutTest : XCTestCase @interface BUYCheckoutTest : XCTestCase
@end @end
@implementation BUYCheckoutTest { @implementation BUYCheckoutTest {
BUYModelManager *_modelManager;
BUYCheckout *_checkout; BUYCheckout *_checkout;
BUYCart *_cart; BUYCart *_cart;
BUYProductVariant *_variant; BUYProductVariant *_variant;
...@@ -41,27 +44,28 @@ ...@@ -41,27 +44,28 @@
- (void)setUp - (void)setUp
{ {
[super setUp]; [super setUp];
_cart = [[BUYCart alloc] init]; _modelManager = [BUYModelManager modelManager];
_checkout = [[BUYCheckout alloc] initWithCart:_cart]; _cart = [_modelManager insertCartWithJSONDictionary:nil];
_variant = [[BUYProductVariant alloc] initWithDictionary:@{ @"id" : @1 }]; _checkout = [_modelManager checkoutWithCart:_cart];
_variant = [[BUYProductVariant alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"id" : @1 }];
_discountDictionary = @{ @"code" : @"abcd1234", @"amount" : @"5.00", @"applicable" : @true }; _discountDictionary = @{ @"code" : @"abcd1234", @"amount" : @"5.00", @"applicable" : @true };
} }
- (void)testOrderStatusDeserializationWithInvalidURL - (void)testOrderStatusDeserializationWithInvalidURL
{ {
BUYCheckout *checkout = [[BUYCheckout alloc] initWithDictionary:@{ @"order" : @{ @"status_url" : @"NOT REAL" } }]; BUYCheckout *checkout = [[BUYCheckout alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"order" : @{ @"status_url" : @"NOT REAL" } }];
XCTAssertNil(checkout.order.statusURL); XCTAssertNil(checkout.order.statusURL);
} }
- (void)testOrderStatusDeserializationWithValidURL - (void)testOrderStatusDeserializationWithValidURL
{ {
BUYCheckout *checkout = [[BUYCheckout alloc] initWithDictionary:@{ @"order" : @{ @"status_url" : @"http://www.shopify.com/" } }]; BUYCheckout *checkout = [[BUYCheckout alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"order" : @{ @"status_url" : @"http://www.shopify.com/" } }];
XCTAssertNotNil(checkout.order.statusURL); XCTAssertNotNil(checkout.order.statusURL);
} }
- (void)testOrderStatusDeserializationWithNoURL - (void)testOrderStatusDeserializationWithNoURL
{ {
BUYCheckout *checkout = [[BUYCheckout alloc] initWithDictionary:@{}]; BUYCheckout *checkout = [[BUYCheckout alloc] initWithModelManager:_modelManager JSONDictionary:@{}];
XCTAssertNil(checkout.order.statusURL); XCTAssertNil(checkout.order.statusURL);
} }
...@@ -73,25 +77,47 @@ ...@@ -73,25 +77,47 @@
XCTAssertTrue([checkout isDirty]); XCTAssertTrue([checkout isDirty]);
} }
- (void)testCheckoutWithVariant
{
BUYCheckout *checkout = [_modelManager checkoutWithVariant:_variant];
XCTAssertNotNil(checkout);
XCTAssertGreaterThanOrEqual([checkout.lineItems count], 1);
BUYLineItem *lineItem = checkout.lineItems[0];
XCTAssertEqual(_variant.identifier, lineItem.variantId);
}
- (void)testSettingAShippingRateMarksShippingRateIdAsDirty
{
BUYShippingRate *shippingRate = [[BUYShippingRate alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"id" : @"banana" }];
XCTAssertNil(_checkout.shippingRate);
XCTAssertNil(_checkout.shippingRateId);
_checkout.shippingRate = shippingRate;
XCTAssertEqualObjects(@"banana", _checkout.shippingRateId);
XCTAssertTrue([[_checkout dirtyProperties] containsObject:@"shippingRateId"]);
}
- (void)testDirtyPropertiesAreReturnedInJSON - (void)testDirtyPropertiesAreReturnedInJSON
{ {
BUYShippingRate *shippingRate = [[BUYShippingRate alloc] initWithDictionary:@{ @"id" : @"banana" }]; BUYShippingRate *shippingRate = [[BUYShippingRate alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"id" : @"banana" }];
[_checkout markAsClean]; [_checkout markAsClean];
_checkout.shippingRate = shippingRate; _checkout.shippingRate = shippingRate;
_checkout.currency = @"BANANA"; _checkout.currency = @"BANANA";
NSSet *dirtyProperties = [_checkout dirtyProperties]; NSSet *dirtyProperties = [_checkout dirtyProperties];
XCTAssertTrue([dirtyProperties containsObject:@"currency"]); XCTAssertTrue([dirtyProperties containsObject:@"currency"]);
XCTAssertTrue([dirtyProperties containsObject:@"shippingRateId"]);
XCTAssertTrue([dirtyProperties containsObject:@"shippingRate"]); XCTAssertTrue([dirtyProperties containsObject:@"shippingRate"]);
NSDictionary *json = [_checkout jsonDictionaryForCheckout]; NSDictionary *json = [_checkout jsonDictionaryForCheckout];
XCTAssertEqualObjects(json[@"checkout"][@"currency"], @"BANANA"); XCTAssertEqualObjects(json[@"checkout"][@"currency"], @"BANANA");
XCTAssertEqualObjects(json[@"checkout"][@"shipping_rate_id"], @"banana");
} }
- (void)testRequiresShippingAndIncludesTaxesSerialization - (void)testRequiresShippingAndIncludesTaxesSerialization
{ {
_checkout.requiresShipping = YES; _checkout.requiresShippingValue = YES;
_checkout.taxesIncluded = YES; _checkout.includesTaxesValue = YES;
NSDictionary *jsonDictionary = [_checkout jsonDictionaryForCheckout][@"checkout"]; NSDictionary *jsonDictionary = [_checkout jsonDictionaryForCheckout][@"checkout"];
XCTAssertEqualObjects(@YES, jsonDictionary[@"requires_shipping"]); XCTAssertEqualObjects(@YES, jsonDictionary[@"requires_shipping"]);
XCTAssertEqualObjects(@YES, jsonDictionary[@"taxes_included"]); XCTAssertEqualObjects(@YES, jsonDictionary[@"taxes_included"]);
...@@ -99,16 +125,16 @@ ...@@ -99,16 +125,16 @@
- (void)testDiscountDeserialization - (void)testDiscountDeserialization
{ {
BUYDiscount *discount = [[BUYDiscount alloc] initWithDictionary: _discountDictionary]; BUYDiscount *discount = [[BUYDiscount alloc] initWithModelManager:_modelManager JSONDictionary: _discountDictionary];
XCTAssertEqualObjects(@"abcd1234", discount.code); XCTAssertEqualObjects(@"abcd1234", discount.code);
XCTAssertEqualObjects(@5.00, discount.amount); XCTAssertEqualObjects(@5.00, discount.amount);
XCTAssertEqual(true, discount.applicable); XCTAssertEqual(true, discount.applicableValue);
} }
- (void)testDiscountSerialization - (void)testDiscountSerialization
{ {
NSDictionary *jsonDict = @{ @"code": @"abcd1234" }; NSDictionary *jsonDict = @{ @"code": @"abcd1234" };
BUYDiscount *discount = [[BUYDiscount alloc] initWithDictionary:_discountDictionary]; BUYDiscount *discount = [[BUYDiscount alloc] initWithModelManager:_modelManager JSONDictionary:_discountDictionary];
XCTAssertEqualObjects(jsonDict, [discount jsonDictionaryForCheckout]); XCTAssertEqualObjects(jsonDict, [discount jsonDictionaryForCheckout]);
} }
...@@ -125,19 +151,19 @@ ...@@ -125,19 +151,19 @@
- (void)testEmptyCheckoutsDoNotRequireShipping - (void)testEmptyCheckoutsDoNotRequireShipping
{ {
_checkout = [[BUYCheckout alloc] initWithDictionary:@{}]; _checkout = [[BUYCheckout alloc] initWithModelManager:_modelManager JSONDictionary:@{}];
XCTAssertFalse([_checkout requiresShipping]); XCTAssertFalse([_checkout requiresShipping]);
} }
- (void)testCheckoutsWithoutItemsThatRequireShipping - (void)testCheckoutsWithoutItemsThatRequireShipping
{ {
_checkout = [[BUYCheckout alloc] initWithDictionary:@{ @"requires_shipping" : @1 }]; _checkout = [[BUYCheckout alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"requires_shipping" : @1 }];
XCTAssertTrue([_checkout requiresShipping]); XCTAssertTrue([_checkout requiresShipping]);
} }
- (void)testTaxLineDeserialization - (void)testTaxLineDeserialization
{ {
BUYTaxLine *taxLine = [[BUYTaxLine alloc] initWithDictionary:@{@"price": @"0.29", BUYTaxLine *taxLine = [[BUYTaxLine alloc] initWithModelManager:_modelManager JSONDictionary:@{@"price": @"0.29",
@"rate": @"0.13", @"rate": @"0.13",
@"title": @"HST"}]; @"title": @"HST"}];
XCTAssertEqualObjects(@0.29, taxLine.price); XCTAssertEqualObjects(@0.29, taxLine.price);
......
...@@ -29,9 +29,7 @@ ...@@ -29,9 +29,7 @@
#import <Buy/Buy.h> #import <Buy/Buy.h>
#import "BUYTestConstants.h" #import "BUYTestConstants.h"
#import "BUYAddress+Additions.h"
#import "BUYClientTestBase.h" #import "BUYClientTestBase.h"
#import "BUYCollection+Additions.h"
#import "NSURLComponents+BUYAdditions.h" #import "NSURLComponents+BUYAdditions.h"
@interface BUYClient () @interface BUYClient ()
...@@ -69,7 +67,7 @@ ...@@ -69,7 +67,7 @@
- (NSData *)dataForCartFromClient:(BUYClient *)client - (NSData *)dataForCartFromClient:(BUYClient *)client
{ {
BUYCart *cart = [[BUYCart alloc] init]; BUYCart *cart = [self cart];
BUYCheckout *checkout = [[BUYCheckout alloc] initWithCart:cart]; BUYCheckout *checkout = [[BUYCheckout alloc] initWithCart:cart];
NSURLSessionDataTask *task = [self.client createCheckout:checkout completion:nil]; NSURLSessionDataTask *task = [self.client createCheckout:checkout completion:nil];
XCTAssertNotNil(task); XCTAssertNotNil(task);
...@@ -98,10 +96,10 @@ ...@@ -98,10 +96,10 @@
- (void)testPartialAddressesFlag - (void)testPartialAddressesFlag
{ {
BUYCart *cart = [[BUYCart alloc] init]; BUYCart *cart = [self cart];
BUYCheckout *checkout = [[BUYCheckout alloc] initWithCart:cart]; BUYCheckout *checkout = [[BUYCheckout alloc] initWithCart:cart];
XCTAssertThrows([checkout setPartialAddresses:NO]); XCTAssertThrows([checkout setPartialAddressesValue:NO]);
NSURLSessionDataTask *task = [self.client createCheckout:checkout completion:nil]; NSURLSessionDataTask *task = [self.client createCheckout:checkout completion:nil];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:task.originalRequest.HTTPBody options:0 error:nil]; NSDictionary *json = [NSJSONSerialization JSONObjectWithData:task.originalRequest.HTTPBody options:0 error:nil];
...@@ -109,11 +107,11 @@ ...@@ -109,11 +107,11 @@
checkout = [[BUYCheckout alloc] initWithCart:cart]; checkout = [[BUYCheckout alloc] initWithCart:cart];
BUYAddress *partialAddress = [[BUYAddress alloc] init]; BUYAddress *partialAddress = [self.client.modelManager insertAddressWithJSONDictionary:nil];
partialAddress.address1 = nil; partialAddress.address1 = nil;
if ([partialAddress isPartialAddress]) { if ([partialAddress isPartialAddress]) {
checkout.partialAddresses = YES; checkout.partialAddressesValue = YES;
} }
checkout.shippingAddress = partialAddress; checkout.shippingAddress = partialAddress;
...@@ -125,7 +123,7 @@ ...@@ -125,7 +123,7 @@
- (void)testCheckoutPaymentWithOnlyGiftCard - (void)testCheckoutPaymentWithOnlyGiftCard
{ {
BUYCheckout *checkout = [[BUYCheckout alloc] initWithDictionary:@{@"token": @"abcdef", @"payment_due": @0}]; BUYCheckout *checkout = [[BUYCheckout alloc] initWithModelManager:self.client.modelManager JSONDictionary:@{@"token": @"abcdef", @"payment_due": @0}];
NSURLSessionDataTask *task = [self.client completeCheckout:checkout completion:nil]; NSURLSessionDataTask *task = [self.client completeCheckout:checkout completion:nil];
XCTAssertNotNil(task); XCTAssertNotNil(task);
...@@ -200,7 +198,7 @@ ...@@ -200,7 +198,7 @@
XCTAssertEqual(error.code, BUYShopifyError_InvalidCheckoutObject); XCTAssertEqual(error.code, BUYShopifyError_InvalidCheckoutObject);
}]; }];
BUYCheckout *checkout = [[BUYCheckout alloc] initWithDictionary:@{@"token": @"abcdef", @"payment_due": @0}]; BUYCheckout *checkout = [[BUYCheckout alloc] initWithModelManager:self.client.modelManager JSONDictionary:@{@"token": @"abcdef", @"payment_due": @0}];
[self.client completeCheckout:checkout withApplePayToken:nil completion:^(BUYCheckout *checkout, NSError *error) { [self.client completeCheckout:checkout withApplePayToken:nil completion:^(BUYCheckout *checkout, NSError *error) {
callbackCount++; callbackCount++;
...@@ -318,4 +316,9 @@ ...@@ -318,4 +316,9 @@
XCTAssertEqualObjects(requestQueryItems, queryItems); XCTAssertEqualObjects(requestQueryItems, queryItems);
} }
- (BUYCart *)cart
{
return [self.client.modelManager insertCartWithJSONDictionary:nil];
}
@end @end
...@@ -29,7 +29,6 @@ ...@@ -29,7 +29,6 @@
#import <Buy/Buy.h> #import <Buy/Buy.h>
#import "BUYTestConstants.h" #import "BUYTestConstants.h"
#import "BUYCollection.h" #import "BUYCollection.h"
#import "NSDateFormatter+BUYAdditions.h"
#import "BUYClientTestBase.h" #import "BUYClientTestBase.h"
#import <OHHTTPStubs/OHHTTPStubs.h> #import <OHHTTPStubs/OHHTTPStubs.h>
#import "OHHTTPStubsResponse+Helpers.h" #import "OHHTTPStubsResponse+Helpers.h"
......
...@@ -29,8 +29,7 @@ ...@@ -29,8 +29,7 @@
#import <Buy/Buy.h> #import <Buy/Buy.h>
#import "BUYTestConstants.h" #import "BUYTestConstants.h"
#import "BUYAddress+Additions.h" #import "BUYCheckout.h"
#import "BUYCheckout_Private.h"
#import "BUYClientTestBase.h" #import "BUYClientTestBase.h"
#import <OHHTTPStubs/OHHTTPStubs.h> #import <OHHTTPStubs/OHHTTPStubs.h>
#import "OHHTTPStubsResponse+Helpers.h" #import "OHHTTPStubsResponse+Helpers.h"
...@@ -40,6 +39,8 @@ ...@@ -40,6 +39,8 @@
@implementation BUYIntegrationTest { @implementation BUYIntegrationTest {
BUYModelManager *_modelManager;
NSMutableArray *_products; NSMutableArray *_products;
BUYCart *_cart; BUYCart *_cart;
...@@ -52,6 +53,7 @@ ...@@ -52,6 +53,7 @@
{ {
[super setUp]; [super setUp];
_modelManager = [BUYModelManager modelManager];
_products = [[NSMutableArray alloc] init]; _products = [[NSMutableArray alloc] init];
[self fetchProducts]; [self fetchProducts];
...@@ -87,12 +89,12 @@ ...@@ -87,12 +89,12 @@
XCTAssertNil(error); XCTAssertNil(error);
}]; }];
NSLog(@"Fetched products (count: %ld", _products.count); NSLog(@"Fetched products (count: %tu", _products.count);
} }
- (void)createCart - (void)createCart
{ {
_cart = [[BUYCart alloc] init]; _cart = [_modelManager insertCartWithJSONDictionary:nil];
for (BUYProduct *product in _products) { for (BUYProduct *product in _products) {
[_cart addVariant:product.variants[0]]; [_cart addVariant:product.variants[0]];
} }
...@@ -132,12 +134,14 @@ ...@@ -132,12 +134,14 @@
return [OHHTTPStubsResponse responseWithKey:@"testCheckoutFlowUsingCreditCard_1"]; return [OHHTTPStubsResponse responseWithKey:@"testCheckoutFlowUsingCreditCard_1"];
}]; }];
[self createCart];
_checkout = [[BUYCheckout alloc] initWithCart:_cart]; _checkout = [[BUYCheckout alloc] initWithCart:_cart];
NSString *note = @"Order note"; NSString *note = @"Order note";
_checkout.note = note; _checkout.note = note;
NSArray *attributes = @[ [[BUYCheckoutAttribute alloc] initWithDictionary:@{ @"name" : @"attribute1", @"value" : @"value1" }], [[BUYCheckoutAttribute alloc] initWithDictionary:@{ @"name" : @"attribute2", @"value" : @"value2" }] ]; NSArray *attributes = @[ [[BUYCheckoutAttribute alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"name" : @"attribute1", @"value" : @"value1" }],
_checkout.attributes = attributes; [[BUYCheckoutAttribute alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"name" : @"attribute2", @"value" : @"value2" }] ];
_checkout.attributes = [NSSet setWithArray:attributes];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client createCheckout:_checkout completion:^(BUYCheckout *returnedCheckout, NSError *error) { [self.client createCheckout:_checkout completion:^(BUYCheckout *returnedCheckout, NSError *error) {
...@@ -145,7 +149,7 @@ ...@@ -145,7 +149,7 @@
XCTAssertNotNil(returnedCheckout); XCTAssertNotNil(returnedCheckout);
XCTAssertEqualObjects(returnedCheckout.note, note); XCTAssertEqualObjects(returnedCheckout.note, note);
XCTAssertEqualObjects(returnedCheckout.attributes, attributes); XCTAssertEqualObjects(returnedCheckout.attributes, _checkout.attributes);
_checkout = returnedCheckout; _checkout = returnedCheckout;
[expectation fulfill]; [expectation fulfill];
...@@ -509,7 +513,7 @@ ...@@ -509,7 +513,7 @@
return [OHHTTPStubsResponse responseWithKey:@"testRemovingInvalidGiftCardFromCheckout_2"]; return [OHHTTPStubsResponse responseWithKey:@"testRemovingInvalidGiftCardFromCheckout_2"];
}]; }];
BUYGiftCard *giftCard = [[BUYGiftCard alloc] initWithDictionary:@{ @"id" : @"000" }]; BUYGiftCard *giftCard = [[BUYGiftCard alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"id" : @"000" }];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client removeGiftCard:giftCard fromCheckout:_checkout completion:^(BUYCheckout *checkout, NSError *error) { [self.client removeGiftCard:giftCard fromCheckout:_checkout completion:^(BUYCheckout *checkout, NSError *error) {
XCTAssertNotNil(error); XCTAssertNotNil(error);
...@@ -537,7 +541,7 @@ ...@@ -537,7 +541,7 @@
return [OHHTTPStubsResponse responseWithKey:@"testRemovingExpiredGiftCardFromCheckout_2"]; return [OHHTTPStubsResponse responseWithKey:@"testRemovingExpiredGiftCardFromCheckout_2"];
}]; }];
BUYGiftCard *giftCard = [[BUYGiftCard alloc] initWithDictionary:@{ @"id" : self.giftCardIdExpired }]; BUYGiftCard *giftCard = [[BUYGiftCard alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"id" : self.giftCardIdExpired }];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client removeGiftCard:giftCard fromCheckout:_checkout completion:^(BUYCheckout *checkout, NSError *error) { [self.client removeGiftCard:giftCard fromCheckout:_checkout completion:^(BUYCheckout *checkout, NSError *error) {
XCTAssertNotNil(error); XCTAssertNotNil(error);
...@@ -998,7 +1002,7 @@ ...@@ -998,7 +1002,7 @@
_checkout.shippingAddress = [self partialShippingAddress]; _checkout.shippingAddress = [self partialShippingAddress];
if ([_checkout.shippingAddress isPartialAddress]) { if ([_checkout.shippingAddress isPartialAddress]) {
_checkout.partialAddresses = YES; _checkout.partialAddressesValue = YES;
} }
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
...@@ -1181,7 +1185,7 @@ ...@@ -1181,7 +1185,7 @@
- (BUYAddress *)billingAddress - (BUYAddress *)billingAddress
{ {
BUYAddress *address = [[BUYAddress alloc] init]; BUYAddress *address = [_modelManager insertAddressWithJSONDictionary:nil];
address.address1 = @"150 Elgin Street"; address.address1 = @"150 Elgin Street";
address.address2 = @"8th Floor"; address.address2 = @"8th Floor";
address.city = @"Ottawa"; address.city = @"Ottawa";
...@@ -1197,7 +1201,7 @@ ...@@ -1197,7 +1201,7 @@
- (BUYAddress *)shippingAddress - (BUYAddress *)shippingAddress
{ {
BUYAddress *address = [[BUYAddress alloc] init]; BUYAddress *address = [_modelManager insertAddressWithJSONDictionary:nil];
address.address1 = @"150 Elgin Street"; address.address1 = @"150 Elgin Street";
address.address2 = @"8th Floor"; address.address2 = @"8th Floor";
address.city = @"Ottawa"; address.city = @"Ottawa";
...@@ -1213,7 +1217,7 @@ ...@@ -1213,7 +1217,7 @@
- (BUYAddress *)partialShippingAddress - (BUYAddress *)partialShippingAddress
{ {
BUYAddress *address = [[BUYAddress alloc] init]; BUYAddress *address = [_modelManager insertAddressWithJSONDictionary:nil];
address.address1 = nil; address.address1 = nil;
address.city = @"Ottawa"; address.city = @"Ottawa";
address.firstName = nil; address.firstName = nil;
...@@ -1227,20 +1231,17 @@ ...@@ -1227,20 +1231,17 @@
- (BUYDiscount *)applicableDiscount - (BUYDiscount *)applicableDiscount
{ {
BUYDiscount *discount = [[BUYDiscount alloc] initWithCode:self.discountCodeValid]; return [_modelManager discountWithCode:self.discountCodeValid];
return discount;
} }
- (BUYDiscount *)inapplicableDiscount - (BUYDiscount *)inapplicableDiscount
{ {
BUYDiscount *discount = [[BUYDiscount alloc] initWithCode:self.discountCodeExpired]; return [_modelManager discountWithCode:self.discountCodeExpired];
return discount;
} }
- (BUYDiscount *)nonExistentDiscount - (BUYDiscount *)nonExistentDiscount
{ {
BUYDiscount *discount = [[BUYDiscount alloc] initWithCode:@"asdfasdfasdfasdf"]; return [_modelManager discountWithCode:@"asdfasdfasdfasdf"];
return discount;
} }
- (void)testExpiringCheckout - (void)testExpiringCheckout
...@@ -1248,6 +1249,8 @@ ...@@ -1248,6 +1249,8 @@
[self createCart]; [self createCart];
[self createCheckout]; [self createCheckout];
XCTAssertGreaterThanOrEqual([_checkout.lineItems count], 1); XCTAssertGreaterThanOrEqual([_checkout.lineItems count], 1);
XCTAssertNotNil(_checkout.reservationTime);
XCTAssertTrue([_checkout.reservationTime isKindOfClass:[NSNumber class]]);
XCTAssertEqual(300, _checkout.reservationTime.intValue); XCTAssertEqual(300, _checkout.reservationTime.intValue);
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
......
...@@ -32,25 +32,30 @@ ...@@ -32,25 +32,30 @@
@end @end
@implementation BUYLineItemTest { @implementation BUYLineItemTest {
BUYProductVariant *_variant;
BUYLineItem *_lineItem; BUYLineItem *_lineItem;
BUYModelManager *_modelManager;
} }
- (void)setUp - (void)setUp
{ {
[super setUp]; [super setUp];
_lineItem = [[BUYLineItem alloc] init];
_modelManager = [BUYModelManager modelManager];
_variant = [[BUYProductVariant alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"id" : @1, @"requires_shipping" : @YES }];
_lineItem = [[BUYLineItem alloc] initWithVariant:_variant];
} }
- (void)testInitRespectsVariantShippingFlag - (void)tearDown
{ {
XCTAssertFalse([[_lineItem requiresShipping] boolValue]); _modelManager = nil;
}
BUYProductVariant *variant = [[BUYProductVariant alloc] initWithDictionary:@{ @"id" : @1, @"requires_shipping" : @YES }]; - (void)testInitRespectsVariantShippingFlag
_lineItem = [[BUYLineItem alloc] initWithVariant:variant]; {
XCTAssertTrue([[_lineItem requiresShipping] boolValue]);
_variant.requiresShipping = @NO;
XCTAssertTrue([[_lineItem requiresShipping] boolValue]); XCTAssertTrue([[_lineItem requiresShipping] boolValue]);
BUYLineItem *newLineItem = [[BUYLineItem alloc] initWithVariant:variant];
XCTAssertTrue([[newLineItem requiresShipping] boolValue]);
} }
#pragma mark - Serialization Tests #pragma mark - Serialization Tests
...@@ -59,13 +64,13 @@ ...@@ -59,13 +64,13 @@
{ {
NSDictionary *json = [_lineItem jsonDictionaryForCheckout]; NSDictionary *json = [_lineItem jsonDictionaryForCheckout];
XCTAssertNotNil(json); XCTAssertNotNil(json);
XCTAssertEqualObjects([NSDecimalNumber zero], json[@"price"]); XCTAssertEqualObjects(@"0", json[@"price"]);
XCTAssertEqualObjects([NSDecimalNumber zero], json[@"quantity"]); XCTAssertEqualObjects(@"1", json[@"quantity"]);
} }
- (void)testJsonDictionaryDoesntIncludeVariantsWithoutIds - (void)testJsonDictionaryDoesntIncludeVariantsWithoutIds
{ {
_lineItem = [[BUYLineItem alloc] initWithVariant:nil]; _lineItem = [_modelManager lineItemWithVariant:nil];
NSDictionary *json = [_lineItem jsonDictionaryForCheckout]; NSDictionary *json = [_lineItem jsonDictionaryForCheckout];
XCTAssertNotNil(json); XCTAssertNotNil(json);
XCTAssertNil(json[@"variant_id"]); XCTAssertNil(json[@"variant_id"]);
...@@ -73,7 +78,7 @@ ...@@ -73,7 +78,7 @@
- (void)testJsonDictionaryShouldShowAllProperties - (void)testJsonDictionaryShouldShowAllProperties
{ {
BUYProductVariant *variant = [[BUYProductVariant alloc] initWithDictionary:@{ @"id" : @5 }]; BUYProductVariant *variant = [[BUYProductVariant alloc] initWithModelManager:[BUYModelManager modelManager] JSONDictionary:@{ @"id" : @5 }];
_lineItem = [[BUYLineItem alloc] initWithVariant:variant]; _lineItem = [[BUYLineItem alloc] initWithVariant:variant];
_lineItem.quantity = [NSDecimalNumber decimalNumberWithString:@"3"]; _lineItem.quantity = [NSDecimalNumber decimalNumberWithString:@"3"];
_lineItem.price = [NSDecimalNumber decimalNumberWithString:@"5.55"]; _lineItem.price = [NSDecimalNumber decimalNumberWithString:@"5.55"];
...@@ -81,17 +86,15 @@ ...@@ -81,17 +86,15 @@
NSDictionary *json = [_lineItem jsonDictionaryForCheckout]; NSDictionary *json = [_lineItem jsonDictionaryForCheckout];
XCTAssertEqualObjects(@5, json[@"variant_id"]); XCTAssertEqualObjects(@5, json[@"variant_id"]);
XCTAssertEqualObjects([NSDecimalNumber decimalNumberWithString:@"3"], json[@"quantity"]); XCTAssertEqualObjects(@"3", json[@"quantity"]);
XCTAssertEqualObjects([NSDecimalNumber decimalNumberWithString:@"5.55"], json[@"price"]); XCTAssertEqualObjects(@"5.55", json[@"price"]);
XCTAssertEqualObjects(@"banana", json[@"title"]); XCTAssertEqualObjects(@"banana", json[@"title"]);
} }
- (void)testUpdatingFromJsonShouldUpdateAllValues - (void)testUpdatingFromJsonShouldUpdateAllValues
{ {
XCTAssertFalse([[_lineItem requiresShipping] boolValue]); BUYLineItem *lineItem = [[BUYLineItem alloc] initWithModelManager:[BUYModelManager modelManager] JSONDictionary:@{ @"id" : @"5", @"price" : @"5.99", @"quantity" : @5, @"requires_shipping" : @YES, @"title" : @"banana" }];
XCTAssertEqualObjects(@"5", lineItem.identifier);
BUYLineItem *lineItem = [[BUYLineItem alloc] initWithDictionary:@{ @"id" : @5, @"price" : @"5.99", @"quantity" : @5, @"requires_shipping" : @YES, @"title" : @"banana" }];
XCTAssertEqualObjects(@5, lineItem.lineItemIdentifier);
XCTAssertEqualObjects([NSDecimalNumber decimalNumberWithString:@"5.99"], lineItem.price); XCTAssertEqualObjects([NSDecimalNumber decimalNumberWithString:@"5.99"], lineItem.price);
XCTAssertEqualObjects([NSDecimalNumber decimalNumberWithString:@"5"], lineItem.quantity); XCTAssertEqualObjects([NSDecimalNumber decimalNumberWithString:@"5"], lineItem.quantity);
XCTAssertEqualObjects(@"banana", lineItem.title); XCTAssertEqualObjects(@"banana", lineItem.title);
......
...@@ -50,77 +50,33 @@ ...@@ -50,77 +50,33 @@
@property (nonatomic, readonly) NSArray *array; @property (nonatomic, readonly) NSArray *array;
@property (nonatomic, readonly) NSMutableArray *mutableArray; @property (nonatomic, readonly) NSMutableArray *mutableArray;
- (NSArray *)propertyNames;
@end @end
@interface BUYObjectSubclass : BUYObject @interface BUYObjectSubclass : BUYObject
@property (nonatomic, strong) NSNumber *identifier;
@end @end
@interface BUYObjectTests : XCTestCase @interface BUYObjectTests : XCTestCase
@property (nonatomic, strong) BUYModelManager *modelManager;
@end @end
@implementation BUYObjectTests @interface BUYModelManager (BUYDirtyTracked)
+ (BUYModelManager *)testModelManager;
- (void)testInitWithDictionaryParsesIdentifier @end
{
BUYObject *object = [[BUYObject alloc] initWithDictionary:@{ @"id" : @5 }];
XCTAssert([object isDirty] == NO);
XCTAssertEqual(@5, [object identifier]);
}
- (void)testInitWithDictionaryWithoutIdentifier
{
BUYObject *object = [[BUYObject alloc] initWithDictionary:@{}];
XCTAssertNil([object identifier]);
}
- (void)testConvertObject
{
BUYObject *object = [BUYObject convertObject:@{ @"id" : @10 }];
XCTAssertNotNil(object);
XCTAssertEqual(@10, [object identifier]);
}
- (void)testConvertObjectWorksWithSubclasses
{
BUYObject *object = [BUYObjectSubclass convertObject:@{ @"id" : @10 }];
XCTAssertNotNil(object);
XCTAssertTrue([object isKindOfClass:[BUYObjectSubclass class]]);
XCTAssertEqual(@10, [object identifier]);
}
- (void)testConvertJSONArrayWithEmptyArray
{
NSArray *json = @[];
XCTAssertEqual(0, [[BUYObject convertJSONArray:json] count]);
}
- (void)testConvertJSONArrayCreatesObjectOfClass @implementation BUYObjectTests
{
NSArray *json = @[@{ @"id" : @5 }, @{ @"id" : @7 }];
NSArray *convertedArray = [BUYObject convertJSONArray:json];
XCTAssertEqual(2, [convertedArray count]);
XCTAssertTrue([convertedArray[0] isKindOfClass:[BUYObject class]]);
XCTAssertTrue([convertedArray[1] isKindOfClass:[BUYObject class]]);
XCTAssertEqualObjects(@5, [convertedArray[0] identifier]);
XCTAssertEqualObjects(@7, [convertedArray[1] identifier]);
}
- (void)testConvertJSONArrayWithCreatedBlock - (void)setUp
{ {
NSArray *json = @[@{ @"id" : @5 }, @{ @"id" : @6 }, @{ @"id" : @7 }]; [super setUp];
__block NSUInteger numberOfInvokes = 0; self.modelManager = [BUYModelManager testModelManager];
NSArray *convertedArray = [BUYObject convertJSONArray:json block:^(id obj) {
XCTAssertTrue([obj isKindOfClass:[BUYObject class]]);
++numberOfInvokes;
}];
XCTAssertNotNil(convertedArray);
XCTAssertEqual([json count], [convertedArray count]);
XCTAssertEqual([json count], numberOfInvokes);
} }
- (void)testDirtyTracking - (void)testDirtyTracking
{ {
BUYDirtyTracked *object = [[BUYDirtyTracked alloc] init]; BUYDirtyTracked *object = [self dirtyTrackedObject];
object.s = @"short property name test"; object.s = @"short property name test";
object.dirtyObjectValue = @"Banana"; object.dirtyObjectValue = @"Banana";
object.dirtyBooleanValue = true; object.dirtyBooleanValue = true;
...@@ -136,19 +92,14 @@ ...@@ -136,19 +92,14 @@
object.dirtyUnsignedLongLongValue = 1234123412341234; object.dirtyUnsignedLongLongValue = 1234123412341234;
object.dirtyFloatValue = 0.5f; object.dirtyFloatValue = 0.5f;
object.dirtyDoubleValue = 0.5; object.dirtyDoubleValue = 0.5;
NSSet *expected = [NSSet setWithArray:@[@"s", @"dirtyObjectValue", @"dirtyBooleanValue", @"dirtyCharacterValue", NSSet *expected = [NSSet setWithArray:[object propertyNames]];
@"dirtyUnsignedCharValue", @"dirtyIntegerValue", @"dirtyUnsignedIntegerValue",
@"dirtyShortValue", @"dirtyUnsignedShortValue", @"dirtyLongValue",
@"dirtyUnsignedLongValue", @"dirtyLongLongValue", @"dirtyUnsignedLongLongValue",
@"dirtyFloatValue", @"dirtyDoubleValue"]];
NSSet *actual = [object dirtyProperties]; NSSet *actual = [object dirtyProperties];
XCTAssert([expected isEqual:actual]); XCTAssertEqualObjects(actual, expected);
XCTAssertEqual([actual count], [expected count]);
} }
- (void)testIsDirtyReturnsFalseWhenClean - (void)testIsDirtyReturnsFalseWhenClean
{ {
BUYDirtyTracked *object = [[BUYDirtyTracked alloc] init]; BUYDirtyTracked *object = [self dirtyTrackedObject];
XCTAssert([object isDirty] == NO); XCTAssert([object isDirty] == NO);
object.dirtyObjectValue = @"Banana"; object.dirtyObjectValue = @"Banana";
[object markAsClean]; [object markAsClean];
...@@ -157,18 +108,23 @@ ...@@ -157,18 +108,23 @@
- (void)testIsDirtyReturnsTrueWhenDirty - (void)testIsDirtyReturnsTrueWhenDirty
{ {
BUYDirtyTracked *object = [[BUYDirtyTracked alloc] init]; BUYDirtyTracked *object = [self dirtyTrackedObject];
object.dirtyObjectValue = @"Banana"; object.dirtyObjectValue = @"Banana";
XCTAssert([object isDirty]); XCTAssert([object isDirty]);
} }
- (void)testMarkAsClean - (void)testMarkAsClean
{ {
BUYDirtyTracked *object = [[BUYDirtyTracked alloc] init]; BUYDirtyTracked *object = [self dirtyTrackedObject];
object.dirtyObjectValue = @"Banana"; object.dirtyObjectValue = @"Banana";
XCTAssert([object dirtyProperties]); XCTAssert([object dirtyProperties]);
} }
- (BUYDirtyTracked *)dirtyTrackedObject
{
return [[BUYDirtyTracked alloc] initWithModelManager:nil JSONDictionary:nil];
}
@end @end
#pragma mark - Helper Impls #pragma mark - Helper Impls
...@@ -180,7 +136,44 @@ ...@@ -180,7 +136,44 @@
return YES; return YES;
} }
+ (NSString *)entityName
{
return nil;
}
// Overriding private BUYObject class method to avoid need for entity
- (NSArray *)propertyNames
{
return @[@"s", @"dirtyObjectValue", @"dirtyBooleanValue", @"dirtyCharacterValue",
@"dirtyUnsignedCharValue", @"dirtyIntegerValue", @"dirtyUnsignedIntegerValue",
@"dirtyShortValue", @"dirtyUnsignedShortValue", @"dirtyLongValue",
@"dirtyUnsignedLongValue", @"dirtyLongLongValue", @"dirtyUnsignedLongLongValue",
@"dirtyFloatValue", @"dirtyDoubleValue"];
}
@end @end
@implementation BUYObjectSubclass @implementation BUYObjectSubclass
+ (NSString *)entityName
{
return @"ObjectSubclass";
}
@end
@implementation BUYModelManager (BUYDirtyTracked)
+ (BUYModelManager *)testModelManager
{
static dispatch_once_t onceToken;
static BUYModelManager *modelManager;
dispatch_once(&onceToken, ^{
NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:@[[NSBundle bundleForClass:self]]];
modelManager = [[BUYModelManager alloc] initWithModel:model];
});
return modelManager;
}
@end @end
...@@ -39,7 +39,6 @@ extern NSString * const BUYJSONPropertyKeyUserInfoKey; // = @"JSONPropertyK ...@@ -39,7 +39,6 @@ extern NSString * const BUYJSONPropertyKeyUserInfoKey; // = @"JSONPropertyK
* The name of a value transformer used to convert to JSON values and back. * The name of a value transformer used to convert to JSON values and back.
* Uses the value specified in the property's user info dictionary under the "JSONValueTransformer" key. * Uses the value specified in the property's user info dictionary under the "JSONValueTransformer" key.
* *
* Currently only two transformers are supported: "BUYPublicationsDate" and "BUYShippingRateDate".
* Currently, only attributes (instances of NSAttributeDescription) use value transformers. * Currently, only attributes (instances of NSAttributeDescription) use value transformers.
*/ */
@property (nonatomic, readonly, getter=buy_JSONValueTransformerName) NSString *JSONValueTransformerName; @property (nonatomic, readonly, getter=buy_JSONValueTransformerName) NSString *JSONValueTransformerName;
......
...@@ -46,6 +46,8 @@ NSString * const BUYJSONPropertyKeyUserInfoKey = @"JSONPropertyKey"; ...@@ -46,6 +46,8 @@ NSString * const BUYJSONPropertyKeyUserInfoKey = @"JSONPropertyKey";
// This is defined by mogenerator // This is defined by mogenerator
static NSString * const BUYAttributeValueClassNameKey = @"attributeValueClassName"; static NSString * const BUYAttributeValueClassNameKey = @"attributeValueClassName";
static NSString * const BUYDateFormat = @"yyyy-MM-dd'T'HH:mm:ssZ";
#pragma mark - #pragma mark -
@interface NSObject (BUYValueTransforming) @interface NSObject (BUYValueTransforming)
...@@ -69,6 +71,7 @@ static NSString * const BUYAttributeValueClassNameKey = @"attributeValueClassNam ...@@ -69,6 +71,7 @@ static NSString * const BUYAttributeValueClassNameKey = @"attributeValueClassNam
// value type transformers // value type transformers
[NSValueTransformer setValueTransformer:[[BUYURLTransformer alloc] init] forName:BUYURLTransformerName]; [NSValueTransformer setValueTransformer:[[BUYURLTransformer alloc] init] forName:BUYURLTransformerName];
[NSValueTransformer setValueTransformer:[BUYDateTransformer dateTransformerWithFormat:BUYDateFormat] forName:BUYDateTransformerName];
}); });
return self.userInfo[BUYJSONValueTransformerUserInfoKey]; return self.userInfo[BUYJSONValueTransformerUserInfoKey];
} }
......
...@@ -68,3 +68,16 @@ FOUNDATION_EXPORT const unsigned char BuyVersionString[]; ...@@ -68,3 +68,16 @@ FOUNDATION_EXPORT const unsigned char BuyVersionString[];
#import <Buy/BUYStoreViewController.h> #import <Buy/BUYStoreViewController.h>
#import <Buy/BUYTheme.h> #import <Buy/BUYTheme.h>
#import <Buy/BUYViewController.h> #import <Buy/BUYViewController.h>
#import <Buy/NSArray+BUYAdditions.h>
#import <Buy/NSDateFormatter+BUYAdditions.h>
#import <Buy/NSDecimalNumber+BUYAdditions.h>
#import <Buy/NSDictionary+BUYAdditions.h>
#import <Buy/NSDictionary+BUYAdditions.h>
#import <Buy/NSEntityDescription+BUYAdditions.h>
#import <Buy/NSException+BUYAdditions.h>
#import <Buy/NSPropertyDescription+BUYAdditions.h>
#import <Buy/NSRegularExpression+BUYAdditions.h>
#import <Buy/NSString+BUYAdditions.h>
#import <Buy/NSURL+BUYAdditions.h>
#import <Buy/NSURLComponents+BUYAdditions.h>
...@@ -25,10 +25,10 @@ ...@@ -25,10 +25,10 @@
// //
@import Foundation; @import Foundation;
#import "BUYSerializable.h"
@class PKPaymentToken; @class PKPaymentToken;
@class BUYAccountCredentials;
@class BUYCart; @class BUYCart;
@class BUYCheckout; @class BUYCheckout;
@class BUYCreditCard; @class BUYCreditCard;
...@@ -37,6 +37,8 @@ ...@@ -37,6 +37,8 @@
@class BUYProductVariant; @class BUYProductVariant;
@class BUYShop; @class BUYShop;
@class BUYCollection; @class BUYCollection;
@class BUYOrder;
@class BUYModelManager;
/** /**
* The sort order for products in a collection * The sort order for products in a collection
...@@ -176,12 +178,10 @@ typedef void (^BUYDataProductBlock)(BUYProduct *product, NSError *error); ...@@ -176,12 +178,10 @@ typedef void (^BUYDataProductBlock)(BUYProduct *product, NSError *error);
typedef void (^BUYDataProductsBlock)(NSArray<BUYProduct *> *products, NSError *error); typedef void (^BUYDataProductsBlock)(NSArray<BUYProduct *> *products, NSError *error);
/** /**
* Return block containing a list of BUYCollection objects * Return block containing list of collections
* *
* @param collections An array of BUYCollection objects * @param collections An array of BUYCollection objects
* @param page Index of the page requested * @param error Optional NSError
* @param reachedEnd Boolean indicating whether additional pages exist
* @param error An optional NSError
*/ */
typedef void (^BUYDataCollectionsListBlock)(NSArray<BUYCollection *> *collections, NSUInteger page, BOOL reachedEnd, NSError *error); typedef void (^BUYDataCollectionsListBlock)(NSArray<BUYCollection *> *collections, NSUInteger page, BOOL reachedEnd, NSError *error);
...@@ -232,6 +232,11 @@ typedef void (^BUYDataGiftCardBlock)(BUYGiftCard *giftCard, NSError *error); ...@@ -232,6 +232,11 @@ typedef void (^BUYDataGiftCardBlock)(BUYGiftCard *giftCard, NSError *error);
- (instancetype)initWithShopDomain:(NSString *)shopDomain apiKey:(NSString *)apiKey appId:(NSString *)appId NS_DESIGNATED_INITIALIZER; - (instancetype)initWithShopDomain:(NSString *)shopDomain apiKey:(NSString *)apiKey appId:(NSString *)appId NS_DESIGNATED_INITIALIZER;
/** /**
*
*/
@property (nonatomic, strong) BUYModelManager *modelManager;
/**
* Queue where callbacks will be called * Queue where callbacks will be called
* defaults to main queue * defaults to main queue
*/ */
...@@ -260,7 +265,7 @@ typedef void (^BUYDataGiftCardBlock)(BUYGiftCard *giftCard, NSError *error); ...@@ -260,7 +265,7 @@ typedef void (^BUYDataGiftCardBlock)(BUYGiftCard *giftCard, NSError *error);
/** /**
* The Merchant ID is used for Apple Pay and set using `enableApplePayWithMerchantId:` * The Merchant ID is used for Apple Pay and set using `enableApplePayWithMerchantId:`
*/ */
@property (nonatomic, strong, readonly) NSString *merchantId DEPRECATED_MSG_ATTRIBUTE("Set the `merchantId` on a BUYViewController subclass"); @property (nonatomic, strong, readonly) NSString *merchantId NS_DEPRECATED_IOS(8_0, 9_0, "Set the `merchantId` on a BUYViewController subclass instead");
/** /**
* Application name to attribute orders to. Defaults to app bundle name (CFBundleName) * Application name to attribute orders to. Defaults to app bundle name (CFBundleName)
...@@ -515,7 +520,7 @@ typedef void (^BUYDataGiftCardBlock)(BUYGiftCard *giftCard, NSError *error); ...@@ -515,7 +520,7 @@ typedef void (^BUYDataGiftCardBlock)(BUYGiftCard *giftCard, NSError *error);
* *
* @return The associated NSURLSessionDataTask * @return The associated NSURLSessionDataTask
*/ */
- (NSURLSessionDataTask *)storeCreditCard:(id <BUYSerializable>)creditCard checkout:(BUYCheckout *)checkout completion:(BUYDataCreditCardBlock)block; - (NSURLSessionDataTask *)storeCreditCard:(BUYCreditCard *)creditCard checkout:(BUYCheckout *)checkout completion:(BUYDataCreditCardBlock)block;
/** /**
* Convenience method to release all product inventory reservations by setting its * Convenience method to release all product inventory reservations by setting its
...@@ -536,6 +541,6 @@ typedef void (^BUYDataGiftCardBlock)(BUYGiftCard *giftCard, NSError *error); ...@@ -536,6 +541,6 @@ typedef void (^BUYDataGiftCardBlock)(BUYGiftCard *giftCard, NSError *error);
* *
* @param merchantId The Merchant ID generated on Shopify Admin * @param merchantId The Merchant ID generated on Shopify Admin
*/ */
- (void)enableApplePayWithMerchantId:(NSString *)merchantId DEPRECATED_MSG_ATTRIBUTE("Set the merchantId on a BUYViewController subclass instead"); - (void)enableApplePayWithMerchantId:(NSString *)merchantId NS_DEPRECATED_IOS(8_0, 9_0, "Set the merchantId on a `BUYViewController` subclass instead");
@end @end
...@@ -24,20 +24,22 @@ ...@@ -24,20 +24,22 @@
// THE SOFTWARE. // THE SOFTWARE.
// //
#import "BUYClient_Internal.h"
#import "BUYAddress.h" #import "BUYAddress.h"
#import "BUYCart.h" #import "BUYCart.h"
#import "BUYCheckout.h" #import "BUYCheckout.h"
#import "BUYCheckout_Private.h"
#import "BUYCreditCard.h" #import "BUYCreditCard.h"
#import "BUYClient.h"
#import "BUYCollection.h" #import "BUYCollection.h"
#import "BUYCollection+Additions.h"
#import "BUYError.h" #import "BUYError.h"
#import "BUYGiftCard.h" #import "BUYGiftCard.h"
#import "BUYModelManager.h"
#import "BUYOrder.h"
#import "BUYProduct.h" #import "BUYProduct.h"
#import "BUYShippingRate.h" #import "BUYShippingRate.h"
#import "BUYShop.h" #import "BUYShop.h"
#import "NSDecimalNumber+BUYAdditions.h" #import "NSDecimalNumber+BUYAdditions.h"
#import "NSDictionary+BUYAdditions.h"
#import "NSURLComponents+BUYAdditions.h" #import "NSURLComponents+BUYAdditions.h"
#if __has_include(<PassKit/PassKit.h>) #if __has_include(<PassKit/PassKit.h>)
...@@ -47,18 +49,22 @@ ...@@ -47,18 +49,22 @@
#define kGET @"GET" #define kGET @"GET"
#define kPOST @"POST" #define kPOST @"POST"
#define kPATCH @"PATCH" #define kPATCH @"PATCH"
#define kPUT @"PUT"
#define kDELETE @"DELETE" #define kDELETE @"DELETE"
#define kJSONType @"application/json" #define kJSONType @"application/json"
#define kShopifyError @"shopify"
#define kMinSuccessfulStatusCode 200 #define kMinSuccessfulStatusCode 200
#define kMaxSuccessfulStatusCode 299 #define kMaxSuccessfulStatusCode 299
NSString * const BUYVersionString = @"1.2.6"; NSString * const BUYVersionString = @"1.3";
NSString *const kShopifyError = @"shopify";
static NSString *const kBUYClientPathProductPublications = @"product_listings"; static NSString *const kBUYClientPathProductPublications = @"product_listings";
static NSString *const kBUYClientPathCollectionPublications = @"collection_listings"; static NSString *const kBUYClientPathCollectionPublications = @"collection_listings";
NSString *const BUYClientCustomerAccessToken = @"X-Shopify-Customer-Access-Token";
@interface BUYClient () <NSURLSessionDelegate> @interface BUYClient () <NSURLSessionDelegate>
@property (nonatomic, strong) NSString *shopDomain; @property (nonatomic, strong) NSString *shopDomain;
...@@ -83,18 +89,19 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi ...@@ -83,18 +89,19 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi
self = [super init]; self = [super init];
if (self) { if (self) {
self.modelManager = [BUYModelManager modelManager];
self.shopDomain = shopDomain; self.shopDomain = shopDomain;
self.apiKey = apiKey; self.apiKey = apiKey;
self.appId = appId; self.appId = appId;
self.applicationName = [[NSBundle mainBundle] infoDictionary][@"CFBundleName"] ?: @""; self.applicationName = [[NSBundle mainBundle] infoDictionary][@"CFBundleName"] ?: @"";
self.queue = dispatch_get_main_queue(); self.queue = dispatch_get_main_queue();
self.session = [self createUrlSession]; self.session = [self urlSession];
self.pageSize = 25; self.pageSize = 25;
} }
return self; return self;
} }
- (NSURLSession *)createUrlSession - (NSURLSession *)urlSession
{ {
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
...@@ -124,7 +131,7 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi ...@@ -124,7 +131,7 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi
return [self getRequestForURL:shopComponents.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) { return [self getRequestForURL:shopComponents.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
BUYShop *shop = nil; BUYShop *shop = nil;
if (json && error == nil) { if (json && error == nil) {
shop = [[BUYShop alloc] initWithDictionary:json]; shop = [self.modelManager insertShopWithJSONDictionary:json];
} }
block(shop, error); block(shop, error);
}]; }];
...@@ -140,7 +147,7 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi ...@@ -140,7 +147,7 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi
NSArray *products = nil; NSArray *products = nil;
if (json && error == nil) { if (json && error == nil) {
products = [BUYProduct convertJSONArray:json[kBUYClientPathProductPublications]]; products = [self.modelManager insertProductsWithJSONArray:json[kBUYClientPathProductPublications]];
} }
block(products, page, [self hasReachedEndOfPage:products] || error, error); block(products, page, [self hasReachedEndOfPage:products] || error, error);
}]; }];
...@@ -169,7 +176,7 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi ...@@ -169,7 +176,7 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi
NSArray *products = nil; NSArray *products = nil;
if (json && error == nil) { if (json && error == nil) {
products = [BUYProduct convertJSONArray:json[kBUYClientPathProductPublications]]; products = [self.modelManager insertProductsWithJSONArray:json[kBUYClientPathProductPublications]];
} }
if (error == nil && [products count] == 0) { if (error == nil && [products count] == 0) {
error = [NSError errorWithDomain:kShopifyError code:BUYShopifyError_InvalidProductID userInfo:@{ NSLocalizedDescriptionKey : @"Product IDs are not valid. Confirm the product IDs on your shop's admin and also ensure that the visibility is on for the Mobile App channel." }]; error = [NSError errorWithDomain:kShopifyError code:BUYShopifyError_InvalidProductID userInfo:@{ NSLocalizedDescriptionKey : @"Product IDs are not valid. Confirm the product IDs on your shop's admin and also ensure that the visibility is on for the Mobile App channel." }];
...@@ -194,7 +201,7 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi ...@@ -194,7 +201,7 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi
NSArray *collections = nil; NSArray *collections = nil;
if (json && error == nil) { if (json && error == nil) {
collections = [BUYCollection convertJSONArray:json[kBUYClientPathCollectionPublications]]; collections = [self.modelManager buy_objectsWithEntityName:[BUYCollection entityName] JSONArray:json[kBUYClientPathCollectionPublications]];
} }
block(collections, page, [self hasReachedEndOfPage:collections], error); block(collections, page, [self hasReachedEndOfPage:collections], error);
}]; }];
...@@ -219,7 +226,7 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi ...@@ -219,7 +226,7 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi
NSArray *products = nil; NSArray *products = nil;
if (json && error == nil) { if (json && error == nil) {
products = [BUYProduct convertJSONArray:json[kBUYClientPathProductPublications]]; products = [self.modelManager buy_objectsWithEntityName:[BUYProduct entityName] JSONArray:json[kBUYClientPathProductPublications]];
} }
block(products, page, [self hasReachedEndOfPage:products] || error, error); block(products, page, [self hasReachedEndOfPage:products] || error, error);
}]; }];
...@@ -291,7 +298,7 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi ...@@ -291,7 +298,7 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi
{ {
BUYCheckout *checkout = nil; BUYCheckout *checkout = nil;
if (error == nil) { if (error == nil) {
checkout = [[BUYCheckout alloc] initWithDictionary:json[@"checkout"]]; checkout = [self.modelManager insertCheckoutWithJSONDictionary:json[@"checkout"]];
} }
block(checkout, error); block(checkout, error);
} }
...@@ -301,7 +308,7 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi ...@@ -301,7 +308,7 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi
checkout.marketingAttribution = @{@"medium": @"iOS", @"source": self.applicationName}; checkout.marketingAttribution = @{@"medium": @"iOS", @"source": self.applicationName};
checkout.sourceName = @"mobile_app"; checkout.sourceName = @"mobile_app";
if (self.urlScheme || checkout.webReturnToURL) { if (self.urlScheme || checkout.webReturnToURL) {
checkout.webReturnToURL = checkout.webReturnToURL ?: self.urlScheme; checkout.webReturnToURL = checkout.webReturnToURL ?: [NSURL URLWithString:self.urlScheme];
checkout.webReturnToLabel = checkout.webReturnToLabel ?: [@"Return to " stringByAppendingString:self.applicationName]; checkout.webReturnToLabel = checkout.webReturnToLabel ?: [@"Return to " stringByAppendingString:self.applicationName];
} }
} }
...@@ -317,7 +324,7 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi ...@@ -317,7 +324,7 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi
- (NSURLSessionDataTask *)createCheckoutWithCartToken:(NSString *)cartToken completion:(BUYDataCheckoutBlock)block - (NSURLSessionDataTask *)createCheckoutWithCartToken:(NSString *)cartToken completion:(BUYDataCheckoutBlock)block
{ {
BUYCheckout *checkout = [[BUYCheckout alloc] initWithCartToken:cartToken]; BUYCheckout *checkout = [self.modelManager checkoutwithCartToken:cartToken];
[self configureCheckout:checkout]; [self configureCheckout:checkout];
NSDictionary *json = [checkout jsonDictionaryForCheckout]; NSDictionary *json = [checkout jsonDictionaryForCheckout];
...@@ -348,7 +355,7 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi ...@@ -348,7 +355,7 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi
block(nil, [NSError errorWithDomain:kShopifyError code:BUYShopifyError_NoGiftCardSpecified userInfo:nil]); block(nil, [NSError errorWithDomain:kShopifyError code:BUYShopifyError_NoGiftCardSpecified userInfo:nil]);
} }
else { else {
BUYGiftCard *giftCard = [[BUYGiftCard alloc] initWithDictionary:@{ @"code" : giftCardCode }]; BUYGiftCard *giftCard = [self.modelManager giftCardWithCode:giftCardCode];
NSURLComponents *components = [self URLComponentsForCheckoutsAppendingPath:@"gift_cards" NSURLComponents *components = [self URLComponentsForCheckoutsAppendingPath:@"gift_cards"
checkoutToken:checkout.token checkoutToken:checkout.token
queryItems:nil]; queryItems:nil];
...@@ -389,14 +396,13 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi ...@@ -389,14 +396,13 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi
- (void)updateCheckout:(BUYCheckout *)checkout withGiftCardDictionary:(NSDictionary *)giftCardDictionary addingGiftCard:(BOOL)addingGiftCard - (void)updateCheckout:(BUYCheckout *)checkout withGiftCardDictionary:(NSDictionary *)giftCardDictionary addingGiftCard:(BOOL)addingGiftCard
{ {
NSMutableArray *giftCardArray = [NSMutableArray arrayWithArray:checkout.giftCards];
BUYGiftCard *giftCard = [[BUYGiftCard alloc] initWithDictionary:giftCardDictionary];
if (addingGiftCard) { if (addingGiftCard) {
[giftCardArray addObject:giftCard]; BUYGiftCard *giftCard = [self.modelManager insertGiftCardWithJSONDictionary:giftCardDictionary];
[checkout.giftCardsSet addObject:giftCard];
} else { } else {
[giftCardArray removeObject:giftCard]; [checkout removeGiftCardWithIdentifier:giftCardDictionary[@"id"]];
} }
checkout.giftCards = [giftCardArray copy];
checkout.paymentDue = [NSDecimalNumber buy_decimalNumberFromJSON:giftCardDictionary[@"checkout"][@"payment_due"]]; 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. // Marking the checkout as clean. The properties we have updated above we don't need to re-sync with Shopify.
...@@ -552,7 +558,7 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi ...@@ -552,7 +558,7 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi
task = [self getRequestForURL:components.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) { task = [self getRequestForURL:components.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
NSArray *shippingRates = nil; NSArray *shippingRates = nil;
if (error == nil && json) { if (error == nil && json) {
shippingRates = [BUYShippingRate convertJSONArray:json[@"shipping_rates"]]; shippingRates = [self.modelManager insertShippingRatesWithJSONArray:json[@"shipping_rates"]];
} }
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode]; NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
...@@ -567,7 +573,7 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi ...@@ -567,7 +573,7 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi
#pragma mark - Payments #pragma mark - Payments
- (NSURLSessionDataTask *)storeCreditCard:(id <BUYSerializable>)creditCard checkout:(BUYCheckout *)checkout completion:(BUYDataCreditCardBlock)block - (NSURLSessionDataTask *)storeCreditCard:(BUYCreditCard *)creditCard checkout:(BUYCheckout *)checkout completion:(BUYDataCreditCardBlock)block
{ {
NSURLSessionDataTask *task = nil; NSURLSessionDataTask *task = nil;
......
//
// BUYClient_Internal.h
// Mobile Buy SDK
//
// Created by Gabriel O'Flaherty-Chan on 2016-04-04.
// Copyright © 2016 Shopify Inc. All rights reserved.
//
#import "BUYClient.h"
#import "BUYSerializable.h"
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 body:(NSData *)body completionHandler:(void (^)(NSDictionary *json, NSURLResponse *response, NSError *error))completionHandler;
- (NSURLSessionDataTask *)getRequestForURL:(NSURL *)url completionHandler:(void (^)(NSDictionary *json, NSURLResponse *response, NSError *error))completionHandler;
- (NSURLSessionDataTask *)requestForURL:(NSURL *)url method:(NSString *)method body:(NSData *)body additionalHeaders:(NSDictionary *)headers completionHandler:(void (^)(NSDictionary *json, NSURLResponse *response, NSError *error))completionHandler;
- (NSURLComponents *)URLComponentsForAPIPath:(NSString *)apiPath appendingPath:(NSString *)appendingPath queryItems:(NSDictionary*)queryItems;
- (NSError *)extractErrorFromResponse:(NSURLResponse *)response json:(NSDictionary *)json;
@end
...@@ -34,16 +34,11 @@ ...@@ -34,16 +34,11 @@
#import "NSEntityDescription+BUYAdditions.h" #import "NSEntityDescription+BUYAdditions.h"
#import "NSPropertyDescription+BUYAdditions.h" #import "NSPropertyDescription+BUYAdditions.h"
// Custom value transformer names
NSString * const BUYPublicationsDateTransformerName = @"BUYPublicationsDate";
// Structured value transformer names // Structured value transformer names
NSString * const BUYDeliveryRangeTransformerName = @"BUYDeliveryRange"; NSString * const BUYDeliveryRangeTransformerName = @"BUYDeliveryRange";
NSString * const BUYFlatArrayTransformerName = @"BUYFlatArray"; NSString * const BUYFlatArrayTransformerName = @"BUYFlatArray";
NSString * const BUYProductTagsTransformerName = @"BUYProductTags"; NSString * const BUYProductTagsTransformerName = @"BUYProductTags";
NSString * const BUYPublicationsDateFormat = @"yyyy-MM-dd'T'HH:mm:ssZ";
@interface BUYModelManager () @interface BUYModelManager ()
@property (nonatomic, strong) NSManagedObjectModel *model; @property (nonatomic, strong) NSManagedObjectModel *model;
@end @end
...@@ -54,10 +49,6 @@ NSString * const BUYPublicationsDateFormat = @"yyyy-MM-dd'T'HH:mm:ssZ"; ...@@ -54,10 +49,6 @@ NSString * const BUYPublicationsDateFormat = @"yyyy-MM-dd'T'HH:mm:ssZ";
{ {
static dispatch_once_t onceToken; static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ dispatch_once(&onceToken, ^{
// specialty type transformers
[NSValueTransformer setValueTransformer:[BUYDateTransformer dateTransformerWithFormat:BUYPublicationsDateFormat] forName:BUYPublicationsDateTransformerName];
// specialty collection transformers
[NSValueTransformer setValueTransformer:[[BUYDeliveryRangeTransformer alloc] init] forName:BUYDeliveryRangeTransformerName]; [NSValueTransformer setValueTransformer:[[BUYDeliveryRangeTransformer alloc] init] forName:BUYDeliveryRangeTransformerName];
[NSValueTransformer setValueTransformer:[BUYFlatCollectionTransformer arrayTransformer] forName:BUYFlatArrayTransformerName]; [NSValueTransformer setValueTransformer:[BUYFlatCollectionTransformer arrayTransformer] forName:BUYFlatArrayTransformerName];
[NSValueTransformer setValueTransformer:[BUYFlatCollectionTransformer setTransformerWithSeparator:@", "] forName:BUYProductTagsTransformerName]; [NSValueTransformer setValueTransformer:[BUYFlatCollectionTransformer setTransformerWithSeparator:@", "] forName:BUYProductTagsTransformerName];
......
...@@ -27,37 +27,16 @@ ...@@ -27,37 +27,16 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import <Buy/BUYObjectProtocol.h> #import <Buy/BUYObjectProtocol.h>
#import <Buy/BUYModelManagerProtocol.h>
/**
* This is the base class for all Shopify model objects.
* This class takes care of convertion .json responses into
* the associated subclass.
*
* You will generally not need to interact with this class directly.
*/
@interface BUYObject : NSObject<BUYObject> @interface BUYObject : NSObject<BUYObject>
/** /**
* The identifier of any Shopify model object.
*/
@property (nonatomic, strong, readonly) NSNumber *identifier;
/**
* Objects marked as "dirty" are unsynced with Shopify and will * Objects marked as "dirty" are unsynced with Shopify and will
* sync on any calls to Shopify when updating a checkout. * sync on any calls to Shopify when updating a checkout.
*/ */
@property (nonatomic, readonly, getter=isDirty) BOOL dirty; @property (nonatomic, readonly, getter=isDirty) BOOL dirty;
- (instancetype)initWithDictionary:(NSDictionary *)dictionary;
- (void)updateWithDictionary:(NSDictionary *)dictionary;
+ (NSArray *)convertJSONArray:(NSArray *)json block:(void (^)(id obj))createdBlock;
+ (NSArray *)convertJSONArray:(NSArray *)json;
+ (instancetype)convertObject:(id)object;
#pragma mark - Dirty Property Tracking
- (NSSet *)dirtyProperties; - (NSSet *)dirtyProperties;
- (void)markPropertyAsDirty:(NSString *)property; - (void)markPropertyAsDirty:(NSString *)property;
- (void)markAsClean; - (void)markAsClean;
......
...@@ -43,7 +43,7 @@ ...@@ -43,7 +43,7 @@
- (instancetype)init - (instancetype)init
{ {
return [self initWithDictionary:nil]; return [self initWithModelManager:nil JSONDictionary:nil];
} }
- (instancetype)initWithDictionary:(NSDictionary *)dictionary - (instancetype)initWithDictionary:(NSDictionary *)dictionary
...@@ -100,23 +100,6 @@ ...@@ -100,23 +100,6 @@
[self.dirtyObserver reset]; [self.dirtyObserver reset];
} }
- (BOOL)isEqual:(id)object
{
if (self == object) return YES;
if (![object isKindOfClass:self.class]) return NO;
BOOL same = ([self.identifier isEqual:((BUYObject*)object).identifier]);
return same;
}
- (NSUInteger)hash
{
NSUInteger hash = [self.identifier hash];
return hash;
}
- (void)trackDirtyProperties:(NSArray *)properties - (void)trackDirtyProperties:(NSArray *)properties
{ {
self.dirtyObserver = [BUYObserver observeProperties:properties ofObject:self]; self.dirtyObserver = [BUYObserver observeProperties:properties ofObject:self];
...@@ -124,29 +107,14 @@ ...@@ -124,29 +107,14 @@
#pragma mark - Dynamic JSON Serialization #pragma mark - Dynamic JSON Serialization
+ (NSArray *)propertyNames - (NSArray *)propertyNames
{ {
static NSMutableDictionary *namesCache; return [self.entity.JSONEncodedProperties allKeys];
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
namesCache = [NSMutableDictionary dictionary];
});
NSString *className = NSStringFromClass(self);
NSArray *names = namesCache[className];
if (names == nil) {
NSMutableSet *allNames = [class_getBUYProperties(self) mutableCopy];
[allNames removeObject:NSStringFromSelector(@selector(dirtyObserver))];
names = [allNames allObjects];
namesCache[className] = names;
}
return names;
} }
+ (NSEntityDescription *)entity - (NSEntityDescription *)entity
{ {
@throw BUYAbstractMethod(); return [self.modelManager buy_entityWithName:[[self class] entityName]];
} }
+ (NSString *)entityName + (NSString *)entityName
...@@ -161,23 +129,17 @@ ...@@ -161,23 +129,17 @@
- (instancetype)initWithModelManager:(id<BUYModelManager>)modelManager JSONDictionary:(NSDictionary *)dictionary - (instancetype)initWithModelManager:(id<BUYModelManager>)modelManager JSONDictionary:(NSDictionary *)dictionary
{ {
self = [super init]; self = [self init];
if (self) { if (self) {
self.modelManager = modelManager; self.modelManager = modelManager;
[self updateWithDictionary:dictionary]; self.JSONDictionary = dictionary;
if ([[self class] tracksDirtyProperties]) { if ([[self class] tracksDirtyProperties]) {
[self trackDirtyProperties:[[self class] propertyNames]]; [self trackDirtyProperties:[self propertyNames]];
} }
} }
return self; return self;
} }
- (void)updateWithDictionary:(NSDictionary *)dictionary
{
_identifier = dictionary[@"id"];
[self markAsClean];
}
- (NSDictionary *)jsonDictionaryForCheckout - (NSDictionary *)jsonDictionaryForCheckout
{ {
return self.JSONDictionary; return self.JSONDictionary;
...@@ -198,11 +160,6 @@ ...@@ -198,11 +160,6 @@
return NO; return NO;
} }
- (NSEntityDescription *)entity
{
return [self.modelManager buy_entityWithName:[[self class] entityName]];
}
- (NSDictionary *)JSONDictionary - (NSDictionary *)JSONDictionary
{ {
// JSON generation starts in `-buy_JSONForObject`. // JSON generation starts in `-buy_JSONForObject`.
......
...@@ -74,11 +74,6 @@ ...@@ -74,11 +74,6 @@
*/ */
+ (BOOL)tracksDirtyProperties; + (BOOL)tracksDirtyProperties;
/**
* Use the values in the given dictionary to update properties.
*/
- (void)updateWithDictionary:(NSDictionary *)dictionary;
@optional @optional
- (instancetype)initWithModelManager:(id<BUYModelManager>)modelManager JSONDictionary:(NSDictionary *)dictionary; - (instancetype)initWithModelManager:(id<BUYModelManager>)modelManager JSONDictionary:(NSDictionary *)dictionary;
......
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="10158" systemVersion="15D21" minimumToolsVersion="Xcode 7.0"> <model userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="10171" systemVersion="15E65" minimumToolsVersion="Xcode 7.0">
<entity name="Address" representedClassName="BUYAddress" syncable="YES"> <entity name="Address" representedClassName="BUYAddress" syncable="YES">
<attribute name="address1" optional="YES" attributeType="String" syncable="YES"> <attribute name="address1" optional="YES" attributeType="String" syncable="YES">
<userInfo> <userInfo>
...@@ -255,6 +255,7 @@ ...@@ -255,6 +255,7 @@
<entry key="documentation" value="The URL Scheme of the host app."/> <entry key="documentation" value="The URL Scheme of the host app."/>
</userInfo> </userInfo>
</attribute> </attribute>
<relationship name="attributes" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="CheckoutAttribute" inverseName="checkout" inverseEntity="CheckoutAttribute" syncable="YES"/>
<relationship name="billingAddress" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Address" syncable="YES"/> <relationship name="billingAddress" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Address" syncable="YES"/>
<relationship name="creditCard" optional="YES" maxCount="1" deletionRule="Cascade" destinationEntity="MaskedCreditCard" inverseName="checkout" inverseEntity="MaskedCreditCard" syncable="YES"> <relationship name="creditCard" optional="YES" maxCount="1" deletionRule="Cascade" destinationEntity="MaskedCreditCard" inverseName="checkout" inverseEntity="MaskedCreditCard" syncable="YES">
<userInfo> <userInfo>
...@@ -299,6 +300,18 @@ ...@@ -299,6 +300,18 @@
<entry key="documentation" value="The checkout object. This is the main object that you will interact with when creating orders on Shopify."/> <entry key="documentation" value="The checkout object. This is the main object that you will interact with when creating orders on Shopify."/>
</userInfo> </userInfo>
</entity> </entity>
<entity name="CheckoutAttribute" representedClassName="BUYCheckoutAttribute" syncable="YES">
<attribute name="name" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="value" optional="YES" attributeType="String" syncable="YES">
<userInfo>
<entry key="documentation" value="The attribute value."/>
</userInfo>
</attribute>
<relationship name="checkout" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Checkout" inverseName="attributes" inverseEntity="Checkout" syncable="YES"/>
<userInfo>
<entry key="documentation" value="The attribute name."/>
</userInfo>
</entity>
<entity name="Collection" representedClassName="BUYCollection" syncable="YES"> <entity name="Collection" representedClassName="BUYCollection" syncable="YES">
<attribute name="collectionId" optional="YES" attributeType="Integer 64" defaultValueString="0" syncable="YES"> <attribute name="collectionId" optional="YES" attributeType="Integer 64" defaultValueString="0" syncable="YES">
<userInfo> <userInfo>
...@@ -327,7 +340,6 @@ ...@@ -327,7 +340,6 @@
<attribute name="publishedAt" optional="YES" attributeType="Date" syncable="YES"> <attribute name="publishedAt" optional="YES" attributeType="Date" syncable="YES">
<userInfo> <userInfo>
<entry key="documentation" value="The publish date for the collection."/> <entry key="documentation" value="The publish date for the collection."/>
<entry key="JSONValueTransformer" value="BUYPublicationsDate"/>
</userInfo> </userInfo>
</attribute> </attribute>
<attribute name="title" optional="YES" attributeType="String" syncable="YES"> <attribute name="title" optional="YES" attributeType="String" syncable="YES">
...@@ -704,7 +716,6 @@ ...@@ -704,7 +716,6 @@
<attribute name="publishedAt" optional="YES" attributeType="Date" syncable="YES"> <attribute name="publishedAt" optional="YES" attributeType="Date" syncable="YES">
<userInfo> <userInfo>
<entry key="documentation" value="The publish date for a product."/> <entry key="documentation" value="The publish date for a product."/>
<entry key="JSONValueTransformer" value="BUYPublicationsDate"/>
</userInfo> </userInfo>
</attribute> </attribute>
<attribute name="tags" optional="YES" attributeType="Transformable" syncable="YES"> <attribute name="tags" optional="YES" attributeType="Transformable" syncable="YES">
...@@ -972,12 +983,14 @@ ...@@ -972,12 +983,14 @@
<memberEntity name="TaxLine"/> <memberEntity name="TaxLine"/>
<memberEntity name="ShippingRate"/> <memberEntity name="ShippingRate"/>
<memberEntity name="Address"/> <memberEntity name="Address"/>
<memberEntity name="CheckoutAttribute"/>
</configuration> </configuration>
<elements> <elements>
<element name="Address" positionX="126" positionY="521" width="128" height="225"/> <element name="Address" positionX="126" positionY="521" width="128" height="225"/>
<element name="Cart" positionX="-576" positionY="558" width="128" height="60"/> <element name="Cart" positionX="-576" positionY="558" width="128" height="60"/>
<element name="CartLineItem" positionX="-380" positionY="558" width="128" height="90"/> <element name="CartLineItem" positionX="-380" positionY="558" width="128" height="90"/>
<element name="Checkout" positionX="333" positionY="442" width="128" height="630"/> <element name="Checkout" positionX="333" positionY="442" width="128" height="645"/>
<element name="CheckoutAttribute" positionX="-954" positionY="594" width="128" height="90"/>
<element name="Collection" positionX="-963" positionY="772" width="128" height="210"/> <element name="Collection" positionX="-963" positionY="772" width="128" height="210"/>
<element name="Customer" positionX="106" positionY="1263" width="128" height="60"/> <element name="Customer" positionX="106" positionY="1263" width="128" height="60"/>
<element name="Discount" positionX="126" positionY="877" width="128" height="105"/> <element name="Discount" positionX="126" positionY="877" width="128" height="105"/>
......
// //
// BUYCart.h // _BUYCart.h
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -24,25 +24,11 @@ ...@@ -24,25 +24,11 @@
// THE SOFTWARE. // THE SOFTWARE.
// //
@import Foundation; #import "_BUYCart.h"
#import "BUYSerializable.h"
@class BUYLineItem;
@class BUYCartLineItem;
@class BUYProductVariant; @class BUYProductVariant;
/** @interface BUYCart : _BUYCart {}
* The BUYCart is the starting point for the Checkout API. You are responsible for building a cart, then transforming it
* into a BUYCheckout using the BUYDataClient.
*/
@interface BUYCart : NSObject <BUYSerializable>
/**
* Array of BUYCartLineItem objects in the cart
* Note: These are different from BUYLineItem objects in that
* the line item objects do include the BUYProductVariant.
*/
@property (nonatomic, strong, readonly, nonnull) NSArray<BUYCartLineItem *> *lineItems;
/** /**
* Returns true if the cart is acceptable to send to Shopify. * Returns true if the cart is acceptable to send to Shopify.
......
// //
// BUYCart.m // _BUYCart.m
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -25,94 +25,91 @@ ...@@ -25,94 +25,91 @@
// //
#import "BUYCart.h" #import "BUYCart.h"
#import "BUYCartLineItem.h"
#import "BUYProductVariant.h"
@interface BUYCart () #import "./BUYCartLineItem.h"
#import "BUYModelManager.h"
@property (nonatomic, strong, nonnull) NSMutableSet<BUYCartLineItem *> *lineItemsSet;
@end
@implementation BUYCart @implementation BUYCart
#if !defined CORE_DATA_PERSISTENCE
- (instancetype)init - (instancetype)init
{ {
self = [super init]; self = [super init];
if (self) { if (self) {
self.lineItemsSet = [[NSMutableSet alloc] init]; self.lineItems = [NSOrderedSet orderedSet];
} }
return self; return self;
} }
- (nonnull NSArray<BUYCartLineItem *> *)lineItems #endif
{
return [self.lineItemsSet allObjects];
}
- (BOOL)isValid - (BOOL)isValid
{ {
return [self.lineItemsSet count] > 0; return [self.lineItems count] > 0;
} }
- (void)clearCart - (void)clearCart
{ {
[self.lineItemsSet removeAllObjects]; self.lineItems = [NSOrderedSet orderedSet];
} }
#pragma mark - Simple Cart Editing - (void)addVariant:(BUYProductVariant *)variant
- (void)addVariant:(nonnull BUYProductVariant *)variant
{ {
BUYCartLineItem *lineItem = [[BUYCartLineItem alloc] initWithVariant:variant]; [self willChangeValueForKey:BUYCartRelationships.lineItems];
BUYCartLineItem *existingLineItem = [self.lineItemsSet member:lineItem];
if (existingLineItem) { BUYCartLineItem *lineItem = [self linetItemForVariant:variant];
existingLineItem.quantity = [existingLineItem.quantity decimalNumberByAdding:[NSDecimalNumber one]]; if (lineItem) {
} else { [lineItem incrementQuantity];
[self.lineItemsSet addObject:lineItem];
} }
else {
// quantity is 1 by default
[self.lineItemsSet addObject:[self newCartLineItemWithVariant:variant]];
}
[self didChangeValueForKey:BUYCartRelationships.lineItems];
} }
- (void)removeVariant:(nonnull BUYProductVariant *)variant - (void)removeVariant:(BUYProductVariant *)variant
{ {
BUYCartLineItem *lineItem = [[BUYCartLineItem alloc] initWithVariant:variant]; [self willChangeValueForKey:BUYCartRelationships.lineItems];
BUYCartLineItem *existingLineItem = [self.lineItemsSet member:lineItem];
if (existingLineItem) { BUYCartLineItem *lineItem = [self linetItemForVariant:variant];
existingLineItem.quantity = [existingLineItem.quantity decimalNumberBySubtracting:[NSDecimalNumber one]]; if (lineItem && [lineItem decrementQuantity].integerValue <= 0) {
if ([[existingLineItem quantity] isEqual:[NSDecimalNumber zero]]) { [self.lineItemsSet removeObject:lineItem];
[self.lineItemsSet removeObject:existingLineItem];
}
} }
[self didChangeValueForKey:BUYCartRelationships.lineItems];
} }
- (void)setVariant:(nonnull BUYProductVariant *)variant withTotalQuantity:(NSInteger)quantity - (void)setVariant:(BUYProductVariant *)variant withTotalQuantity:(NSInteger)quantity
{ {
BUYCartLineItem *lineItem = [[BUYCartLineItem alloc] initWithVariant:variant]; [self willChangeValueForKey:BUYCartRelationships.lineItems];
BUYCartLineItem *existingLineItem = [self.lineItemsSet member:lineItem];
if (existingLineItem && quantity > 0) { BUYCartLineItem *lineItem = [self linetItemForVariant:variant];
existingLineItem.quantity = (NSDecimalNumber*)[NSDecimalNumber numberWithInteger:quantity];
} else if (existingLineItem && quantity == 0) { if (quantity == 0 && lineItem != nil) {
[self.lineItemsSet removeObject:existingLineItem]; [self.lineItemsSet removeObject:lineItem];
} else { }
lineItem.quantity = (NSDecimalNumber*)[NSDecimalNumber numberWithInteger:quantity]; else if (quantity > 0) {
if (lineItem == nil) {
lineItem = [self newCartLineItemWithVariant:variant];
[self.lineItemsSet addObject:lineItem]; [self.lineItemsSet addObject:lineItem];
} }
lineItem.quantity = [NSDecimalNumber decimalNumberWithMantissa:quantity exponent:0 isNegative:NO];
}
[self didChangeValueForKey:BUYCartRelationships.lineItems];
} }
#pragma mark - Helpers - (BUYCartLineItem *)newCartLineItemWithVariant:(BUYProductVariant *)variant
{
BUYCartLineItem *lineItem = [self.modelManager buy_objectWithEntityName:[BUYCartLineItem entityName] JSONDictionary:nil];
lineItem.variant = variant;
return lineItem;
}
- (NSDictionary *)jsonDictionaryForCheckout - (BUYCartLineItem *)linetItemForVariant:(BUYProductVariant *)variant
{ {
NSMutableDictionary *cart = [[NSMutableDictionary alloc] init]; return [[self.lineItems filteredOrderedSetUsingPredicate:[NSPredicate predicateWithFormat:@"variant = %@", variant]] lastObject];
NSArray *lineItems = [self lineItems];
if ([lineItems count] > 0) {
NSMutableArray *lineItemsJson = [[NSMutableArray alloc] init];
for (BUYCartLineItem *lineItem in lineItems) {
[lineItemsJson addObject:[lineItem jsonDictionaryForCheckout]];
}
cart[@"line_items"] = lineItemsJson;
}
return cart;
} }
@end @end
// //
// BUYCartLineItem.h // _BUYCartLineItem.h
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -24,23 +24,48 @@ ...@@ -24,23 +24,48 @@
// THE SOFTWARE. // THE SOFTWARE.
// //
#import "BUYLineItem.h" #import <Buy/_BUYCartLineItem.h>
/** /**
* BUYCartLineItem is a subclass of BUYLineItem that extends the object * Newly inserted `CartLineItem`s have an initial quantity of 1.
* by exposing the BUYProductVariant that the line item was initialized with
* using `initWithVariant:`.
*
* Note that this object is only used for a BUYCart and line item objects on
* BUYCheckout are represented by BUYLineItem objects that only contain the
* variant ID (if created from a BUYProductVariant).
*/ */
@interface BUYCartLineItem : BUYLineItem
@interface BUYCartLineItem : _BUYCartLineItem {}
/**
* Convenience method for access the identifier of the underlying variant.
*/
- (NSNumber *)variantId;
/**
* The variant price times the quantity.
*/
@property (nonatomic, readonly) NSDecimalNumber *linePrice;
/**
* Add the amount to the current quantity.
*/
- (NSDecimalNumber *)addQuantity:(NSDecimalNumber *)amount;
/** /**
* The BUYProductVariant object associated with the line item * Subtract the amount from the current quantity.
* when created using the preferred `initWithVariant:` initializer.
*/ */
@property (nonatomic, strong, readonly) BUYProductVariant *variant; - (NSDecimalNumber *)subtractQuantity:(NSDecimalNumber *)amount;
/**
* Add 1 to the existing quantity;
*/
- (NSDecimalNumber *)incrementQuantity;
/**
* Subtract 1 from the existing quantity.
*/
- (NSDecimalNumber *)decrementQuantity;
@end
@interface BUYModelManager (BUYCartLineItemCreation)
- (BUYCartLineItem *)cartLineItemWithVariant:(BUYProductVariant *)variant;
@end @end
\ No newline at end of file
// //
// BUYCartLineItem.m // _BUYCartLineItem.m
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -26,39 +26,81 @@ ...@@ -26,39 +26,81 @@
#import "BUYCartLineItem.h" #import "BUYCartLineItem.h"
#import "BUYProductVariant.h" #import "BUYProductVariant.h"
#import "BUYModelManager.h"
@interface BUYCartLineItem ()
@property (nonatomic, strong) BUYProductVariant *variant;
@end
@implementation BUYCartLineItem @implementation BUYCartLineItem
- (instancetype)initWithVariant:(BUYProductVariant *)variant #if defined CORE_DATA_PERSISTENCE
- (void)awakeFromInsert
{ {
self = [super initWithVariant:variant]; self.quantity = [NSDecimalNumber one];
}
#else
- (instancetype)init
{
self = [super init];
if (self) { if (self) {
self.variant = variant; self.quantity = [NSDecimalNumber one];
} }
return self; return self;
} }
#endif
+ (NSSet *)keyPathsForValuesAffectingLinePrice
{
NSString *variantPriceKeyPath = [@[BUYCartLineItemRelationships.variant, BUYProductVariantAttributes.price] componentsJoinedByString:@"."];
return [NSSet setWithObjects:BUYCartLineItemAttributes.quantity, variantPriceKeyPath, nil];
}
- (NSNumber *)variantId
{
return self.variant.identifier;
}
- (NSDecimalNumber *)linePrice
{
return [self.quantity decimalNumberByMultiplyingBy:self.variant.price];
}
- (BOOL)isEqual:(id)object - (NSDecimalNumber *)addQuantity:(NSDecimalNumber *)amount
{ {
if (self == object) return YES; NSDecimalNumber *quantity = [self.quantity decimalNumberByAdding:amount];
self.quantity = quantity;
return quantity;
}
if (![object isKindOfClass:self.class]) return NO; - (NSDecimalNumber *)subtractQuantity:(NSDecimalNumber *)amount
{
NSDecimalNumber *quantity = self.quantity ?: [NSDecimalNumber zero];
if ([quantity compare:amount] == NSOrderedDescending) {
quantity = [quantity decimalNumberBySubtracting:amount];
}
else {
quantity = [NSDecimalNumber zero];
}
self.quantity = quantity;
return quantity;
}
BOOL same = ([self.identifier isEqual:((BUYObject*)object).identifier]) || [self.variantId isEqual:((BUYCartLineItem*)object).variant.identifier]; - (NSDecimalNumber *)incrementQuantity
{
return [self addQuantity:[NSDecimalNumber one]];
}
return same; - (NSDecimalNumber *)decrementQuantity
{
return [self subtractQuantity:[NSDecimalNumber one]];
} }
- (NSUInteger)hash @end
@implementation BUYModelManager (BUYCartLineItemCreation)
- (BUYCartLineItem *)cartLineItemWithVariant:(BUYProductVariant *)variant
{ {
NSUInteger hash = [self.identifier hash]; BUYCartLineItem *lineItem = [self insertCartLineItemWithJSONDictionary:nil];
return hash; lineItem.variant = variant;
return lineItem;
} }
@end @end
// //
// BUYCollection.h // _BUYCollection.h
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -24,57 +24,26 @@ ...@@ -24,57 +24,26 @@
// THE SOFTWARE. // THE SOFTWARE.
// //
@import Foundation; #import <Buy/_BUYCollection.h>
#import "BUYObject.h" #import "BUYClient.h"
/** @interface BUYCollection : _BUYCollection {}
* Represents a collection of products on the shop
*/
@interface BUYCollection : BUYObject
/**
* The title of the collection
*/
@property (nonatomic, strong, readonly) NSString *title;
/**
* The unique collection ID
*/
@property (nonatomic, strong, readonly) NSNumber *collectionId;
/** @property (nonatomic, readonly) NSDate *createdAtDate NS_DEPRECATED_IOS(8_0, 9_0);
* The html description @property (nonatomic, readonly) NSDate *updatedAtDate NS_DEPRECATED_IOS(8_0, 9_0);
*/ @property (nonatomic, readonly) NSDate *publishedAtDate NS_DEPRECATED_IOS(8_0, 9_0);
@property (nonatomic, strong, readonly) NSString *htmlDescription;
/** @property (nonatomic, readonly) NSURL *imageURL NS_DEPRECATED_IOS(8_0, 9_0);
* The collection's image URL
*/
@property (nonatomic, strong, readonly) NSURL *imageURL;
/** @property (nonatomic, readonly) NSString *stringDescription;
* The handle of the collection
*/
@property (nonatomic, strong, readonly) NSString *handle;
/**
* The state of whether the collection is currently published or not
*/
@property (nonatomic, assign, readonly) BOOL published;
/**
* The creation date for the collection
*/
@property (nonatomic, readonly, copy) NSDate *createdAtDate;
/**
* The updated date for the collection
*/
@property (nonatomic, readonly, copy) NSDate *updatedAtDate;
/** /**
* The publish date for the collection * Converts the BUYCollectionSort enum to an API-compatible string for the collection sort parameter
*
* @param sort BUYCollectionSort enum
*
* @return API-compatible string for the collection sort parameter
*/ */
@property (nonatomic, readonly, copy) NSDate *publishedAtDate; + (NSString *)sortOrderParameterForCollectionSort:(BUYCollectionSort)sort;
@end @end
// //
// BUYCollection.m // _BUYCollection.m
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -25,35 +25,64 @@ ...@@ -25,35 +25,64 @@
// //
#import "BUYCollection.h" #import "BUYCollection.h"
#import "NSDateFormatter+BUYAdditions.h" #import "BUYImageLink.h"
#import "NSURL+BUYAdditions.h" #import "NSString+BUYAdditions.h"
#import "NSDictionary+BUYAdditions.h"
@interface BUYCollection ()
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *htmlDescription;
@property (nonatomic, strong) NSString *handle;
@property (nonatomic, assign) BOOL published;
@property (nonatomic, strong) NSNumber *collectionId;
@end
@implementation BUYCollection @implementation BUYCollection
@synthesize stringDescription=_stringDescription;
- (NSDate *)createdAtDate
{
return self.createdAt;
}
- (NSDate *)updatedAtDate
{
return self.updatedAt;
}
- (NSDate *)publishedAtDate
{
return self.publishedAt;
}
- (NSURL *)imageURL
{
return self.image.sourceURL;
}
- (void)updateStringDescription
{
// Force early cache of this value to prevent spooky behaviour
_stringDescription = [[self.htmlDescription buy_stringByStrippingHTML] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
- (void)setJSONDictionary:(NSDictionary *)JSONDictionary
{
[super setJSONDictionary:JSONDictionary];
[self updateStringDescription];
}
- (void)updateWithDictionary:(NSDictionary *)dictionary + (NSString *)sortOrderParameterForCollectionSort:(BUYCollectionSort)sort
{ {
[super updateWithDictionary:dictionary]; switch (sort) {
case BUYCollectionSortBestSelling:
_title = dictionary[@"title"]; return @"best-selling";
_htmlDescription = dictionary[@"body_html"]; case BUYCollectionSortCreatedAscending:
_imageURL = [NSURL buy_urlWithString:[dictionary buy_objectForKey:@"image"][@"src"]]; return @"created-ascending";
_handle = dictionary[@"handle"]; case BUYCollectionSortCreatedDescending:
_published = [dictionary[@"published"] boolValue]; return @"created-descending";
_collectionId = dictionary[@"collection_id"]; case BUYCollectionSortPriceAscending:
return @"price-ascending";
NSDateFormatter *dateFormatter = [NSDateFormatter dateFormatterForPublications]; case BUYCollectionSortPriceDescending:
_createdAtDate = [dateFormatter dateFromString:dictionary[@"created_at"]]; return @"price-descending";
_updatedAtDate = [dateFormatter dateFromString:dictionary[@"updated_at"]]; case BUYCollectionSortTitleAscending:
_publishedAtDate = [dateFormatter dateFromString:dictionary[@"published_at"]]; return @"title-ascending";
case BUYCollectionSortTitleDescending:
return @"title-descending";
default:
return @"collection-default";
}
} }
@end @end
// //
// BUYImageLink.h // _BUYImageLink.h
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -24,41 +24,64 @@ ...@@ -24,41 +24,64 @@
// THE SOFTWARE. // THE SOFTWARE.
// //
#import "BUYObject.h" #import <Buy/_BUYImageLink.h>
/** @import UIKit;
* Products are easier to sell if customers can see pictures of them, which is why there are product images.
*/
@interface BUYImageLink : BUYObject
/** // Defines for common maximum image sizes
* Specifies the location of the product image. typedef NS_ENUM(NSUInteger, BUYImageURLSize) {
*/ BUYImageURLSize100x100,
@property (nonatomic, readonly, copy) NSString *src; BUYImageURLSize160x160,
BUYImageURLSize240x240,
BUYImageURLSize480x480,
BUYImageURLSize600x600,
BUYImageURLSize1024x1024,
BUYImageURLSize2048x2048
};
/** @interface BUYImageLink : _BUYImageLink {}
* An array of variant ids associated with the image.
*/
@property (nonatomic, readonly, copy) NSArray<NSNumber *> *variantIds;
/**
* Creation date of the image
*/
@property (nonatomic, readonly, copy) NSDate *createdAtDate; @property (nonatomic, readonly, copy) NSDate *createdAtDate;
@property (nonatomic, readonly, copy) NSDate *updatedAtDate;
@property (nonatomic, readonly) NSString *src NS_DEPRECATED_IOS(8_0, 9_0);
@end
@interface BUYImageLink (BUYImageSizing)
/** /**
* The date the image was last updated * Generates a link to the image with the specified size
*
* @param size desired maximum size of the image
*
* @return NSURL to the image resourse
*/ */
@property (nonatomic, readonly, copy) NSDate *updatedAtDate; - (NSURL *)imageURLWithSize:(BUYImageURLSize)size;
@end
@interface NSURL (BUYImageSizing)
/** /**
* The position of the image for the product * Generates a link to the image with the specified size
*
* @param size desired maximum size of the image
*
* @return NSURL to the image resourse
*/ */
@property (nonatomic, readonly, copy) NSNumber *position; - (instancetype)buy_imageURLWithSize:(BUYImageURLSize)size;
@end
@interface UIView (BUYImageSizing)
/** /**
* The associated product ID for the image * Determines the optimal size for the image for the given view
*
* @return the size enum value
*/ */
@property (nonatomic, readonly, copy) NSNumber *productId; - (BUYImageURLSize)buy_imageSize;
@end @end
// //
// BUYImageLink.m // _BUYImageLink.m
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -25,22 +25,104 @@ ...@@ -25,22 +25,104 @@
// //
#import "BUYImageLink.h" #import "BUYImageLink.h"
#import "NSDateFormatter+BUYAdditions.h" #import "NSString+BUYAdditions.h"
#import "NSURL+BUYAdditions.h"
@implementation BUYImageLink @implementation BUYImageLink
- (void)updateWithDictionary:(NSDictionary *)dictionary - (NSDate *)createdAtDate
{ {
[super updateWithDictionary:dictionary]; return self.createdAt;
}
- (NSDate *)updatedAtDate
{
return self.updatedAt;
}
- (NSString *)src
{
return self.sourceURL.absoluteString;
}
@end
@implementation BUYImageLink (BUYImageSizing)
- (NSURL *)imageURLWithSize:(BUYImageURLSize)size
{
return [self.sourceURL buy_imageURLWithSize:size];
}
+ (NSString *)keyForImageSize:(BUYImageURLSize)size
{
NSString *sizeKey = nil;
switch (size) {
case BUYImageURLSize100x100:
sizeKey = @"_small";
break;
case BUYImageURLSize160x160:
sizeKey = @"_compact";
break;
case BUYImageURLSize240x240:
sizeKey = @"_medium";
break;
case BUYImageURLSize480x480:
sizeKey = @"_large";
break;
case BUYImageURLSize600x600:
sizeKey = @"_grande";
break;
case BUYImageURLSize1024x1024:
sizeKey = @"_1024x1024";
break;
case BUYImageURLSize2048x2048:
sizeKey = @"_2048x2048";
break;
}
return sizeKey;
}
@end
#pragma mark -
@implementation NSURL (BUYImageSizing)
- (instancetype)buy_imageURLWithSize:(BUYImageURLSize)size
{
return [self buy_URLByAppendingFileBaseNameSuffix:[BUYImageLink keyForImageSize:size]];
}
@end
#pragma mark -
@implementation UIView (BUYImageSizing)
- (BUYImageURLSize)buy_imageSize
{
CGFloat scale = [[UIScreen mainScreen] scale];
CGFloat maxDimension = MAX(CGRectGetHeight(self.bounds), CGRectGetWidth(self.bounds));
CGFloat pixelSize = scale * maxDimension;
_src = [dictionary[@"src"] copy]; if (pixelSize <= 100.0) return BUYImageURLSize100x100;
_variantIds = [dictionary[@"variant_ids"] copy]; if (pixelSize <= 160.0) return BUYImageURLSize160x160;
_productId = [dictionary[@"product_id"] copy]; if (pixelSize <= 240.0) return BUYImageURLSize240x240;
_position = [dictionary[@"position"] copy]; if (pixelSize <= 480.0) return BUYImageURLSize480x480;
if (pixelSize <= 600.0) return BUYImageURLSize600x600;
if (pixelSize <= 1024.0) return BUYImageURLSize1024x1024;
NSDateFormatter *dateFormatter = [NSDateFormatter dateFormatterForPublications]; return BUYImageURLSize2048x2048;
_createdAtDate = [dateFormatter dateFromString:dictionary[@"created_at"]];
_updatedAtDate = [dateFormatter dateFromString:dictionary[@"updated_at"]];
} }
@end @end
// //
// BUYLineItem.h // _BUYLineItem.h
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -24,104 +24,25 @@ ...@@ -24,104 +24,25 @@
// THE SOFTWARE. // THE SOFTWARE.
// //
#import "BUYSerializable.h" #import <Buy/_BUYLineItem.h>
#import "BUYObject.h" #import <Buy/BUYModelManager.h>
@class BUYProductVariant; @class BUYCartLineItem, BUYProductVariant;
/** @interface BUYLineItem : _BUYLineItem {}
* This represents a BUYLineItem on a BUYCart or on a BUYCheckout.
*/
@interface BUYLineItem : BUYObject <BUYSerializable>
/** - (instancetype)initWithVariant:(BUYProductVariant *)variant NS_DEPRECATED_IOS(8_0, 9_0, "Use `BUYModelManager` to create new instances of model objects instead");
* The unique line item identifier - (instancetype)initWithCartLineItem:(BUYCartLineItem *)cartLineItem NS_DEPRECATED_IOS(8_0, 9_0, "Use `BUYModelManager` to create new instances of model objects instead");
*/
@property (nonatomic, strong, readonly) NSString *lineItemIdentifier;
/** @property (readonly) NSString *lineItemIdentifier NS_DEPRECATED_IOS(8_0, 9_0);
* BUYProductVariant identifer. Keep a reference to a cart or products if you wish to
* display information for product variants in a BUYCheckout
*/
@property (nonatomic, strong, readonly) NSNumber *variantId;
/** @end
* The `BUYProduct` product ID for the product in the line item
*/
@property (nonatomic, strong, readonly) NSNumber *productId;
/**
* The quantity of the BUYLineItem.
*/
@property (nonatomic, strong) NSDecimalNumber *quantity;
/**
* The weight of the BUYProductVariant in grams.
*/
@property (nonatomic, readonly, strong) NSDecimalNumber *grams;
/**
* The price of the BUYLineItem.
* Note: This price does not need to match the product variant.
*/
@property (nonatomic, strong) NSDecimalNumber *price;
/**
* The line price of the item (price * quantity)
*/
@property (nonatomic, strong) NSDecimalNumber *linePrice;
/**
* The competitor's prices for the same item.
*/
@property (nonatomic, readonly, strong) NSDecimalNumber *compareAtPrice;
/**
* The title of the BUYLineItem.
* Note: The title does not need to match the product variant.
*/
@property (nonatomic, copy) NSString *title;
/**
* The title for the variant in the line item
*/
@property (nonatomic, copy) NSString *variantTitle;
/**
* YES if this BUYLineItem requires shipping.
* Note: This needs to match the product variant.
*/
@property (nonatomic, strong) NSNumber *requiresShipping;
/**
* The unique SKU for the line item
*/
@property (nonatomic, readonly, copy) NSString *sku;
/**
* If the line item is taxable
*/
@property (nonatomic, readonly, assign) BOOL taxable;
/** @class BUYCartLineItem;
* Custom properties set on the line item
*/
@property (nonatomic, copy) NSDictionary *properties;
/** @interface BUYModelManager (BUYLineItemCreation)
* Service provider who is doing the fulfillment
*/
@property (nonatomic, readonly, copy) NSString *fulfillmentService;
/** - (BUYLineItem *)lineItemWithVariant:(BUYProductVariant *)variant;
* Initialize a BUYLineItem with an optional variant. - (BUYLineItem *)lineItemWithCartLineItem:(BUYCartLineItem *)cartLineItem;
* Note: We recommend setting up a BUYCart and using `addVariant:`, which handles incrementing
* existing variants for line items in a cart
*
* @param variant A BUYProductVariant to initialize the BUYLineItem with
*
* @return Returns an instance of BUYLineItem
*/
- (instancetype)initWithVariant:(BUYProductVariant *)variant;
@end @end
// //
// BUYLineItem.m // _BUYLineItem.m
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -20,96 +20,75 @@ ...@@ -20,96 +20,75 @@
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. // THE SOFTWARE.
// //
#import "BUYLineItem.h" #import "BUYLineItem.h"
#import "BUYProductVariant.h"
#import "NSDecimalNumber+BUYAdditions.h"
#import "NSString+BUYAdditions.h"
#import "BUYProduct.h"
@interface BUYLineItem ()
@property (nonatomic, strong) NSString *lineItemIdentifier;
@property (nonatomic, strong) NSNumber *variantId;
@property (nonatomic, strong) NSNumber *productId;
@property (nonatomic, copy) NSString *sku;
@property (nonatomic, readwrite) BOOL taxable;
@property (nonatomic, strong) NSDecimalNumber *compareAtPrice;
@property (nonatomic, strong) NSDecimalNumber *grams;
@property (nonatomic, copy) NSString *fulfillmentService;
@end #import "BUYCartLineItem.h"
#import "BUYProductVariant.h"
@implementation BUYLineItem @implementation BUYLineItem
- (instancetype)init /**
* Have model manager responsible for instantiation, and allow deprecated
* initializers for backwards compatability
*/
- (instancetype)initWithCartLineItem:(BUYCartLineItem *)cartLineItem
{ {
return [self initWithVariant:nil]; BUYLineItem *lineItem = [[BUYLineItem alloc] initWithModelManager:cartLineItem.modelManager JSONDictionary:nil];
[lineItem updateWithLineItem:cartLineItem];
return lineItem;
} }
- (instancetype)initWithVariant:(BUYProductVariant *)variant - (instancetype)initWithVariant:(BUYProductVariant *)variant
{ {
self = [super init]; BUYLineItem *lineItem = [[BUYLineItem alloc] initWithModelManager:variant.modelManager JSONDictionary:nil];
if (self) { [lineItem updateWithVariant:variant];
return lineItem;
}
- (void)updateWithVariant:(BUYProductVariant *)variant
{
self.variantId = variant.identifier; self.variantId = variant.identifier;
self.productId = variant.product.productId;
self.quantity = variant ? [NSDecimalNumber one] : [NSDecimalNumber zero]; self.quantity = variant ? [NSDecimalNumber one] : [NSDecimalNumber zero];
self.price = variant ? [variant price] : [NSDecimalNumber zero]; self.price = variant.price ?: [NSDecimalNumber zero];
self.title = variant ? [variant title] : @""; self.title = variant.title ?: @"";
self.requiresShipping = variant.requiresShipping; self.requiresShipping = variant.requiresShipping;
self.compareAtPrice = variant.compareAtPrice; self.compareAtPrice = variant.compareAtPrice;
self.grams = variant.grams; self.grams = variant.grams;
}
return self;
} }
- (void)updateWithDictionary:(NSDictionary *)dictionary - (void)updateWithLineItem:(BUYCartLineItem *)lineItem
{ {
self.lineItemIdentifier = dictionary[@"id"]; [self updateWithVariant:lineItem.variant];
self.variantId = dictionary[@"variant_id"]; self.quantity = lineItem.quantity;
self.productId = dictionary[@"product_id"];
self.title = dictionary[@"title"];
self.variantTitle = dictionary[@"variant_title"];
self.quantity = [NSDecimalNumber buy_decimalNumberFromJSON:dictionary[@"quantity"]];
self.price = [NSDecimalNumber buy_decimalNumberFromJSON:dictionary[@"price"]];
self.linePrice = [NSDecimalNumber buy_decimalNumberFromJSON:dictionary[@"line_price"]];
self.compareAtPrice = [NSDecimalNumber buy_decimalNumberFromJSON:dictionary[@"compare_at_price"]];
self.requiresShipping = dictionary[@"requires_shipping"];
self.sku = dictionary[@"sku"];
self.taxable = [dictionary[@"taxable"] boolValue];
self.properties = dictionary[@"properties"];
self.grams = [NSDecimalNumber buy_decimalNumberFromJSON:dictionary[@"grams"]];
self.fulfillmentService = [dictionary[@"fulfillment_service"] copy];
} }
- (NSDictionary *)jsonDictionaryForCheckout - (NSString *)lineItemIdentifier
{ {
NSMutableDictionary *lineItem = [[NSMutableDictionary alloc] init]; return self.identifier;
if (self.variantId) { }
lineItem[@"variant_id"] = self.variantId;
}
if ([self.title length] > 0) {
lineItem[@"title"] = [self.title buy_trim];
}
if (self.quantity) {
lineItem[@"quantity"] = self.quantity;
}
if (self.price) { @end
lineItem[@"price"] = self.price;
}
if (self.properties) { @implementation BUYModelManager (BUYLineItemCreation)
lineItem[@"properties"] = self.properties;
}
lineItem[@"requires_shipping"] = self.requiresShipping ?: @NO; - (BUYLineItem *)lineItemWithVariant:(BUYProductVariant *)variant
{
BUYLineItem *lineItem = [self insertLineItemWithJSONDictionary:nil];
[lineItem updateWithVariant:variant];
return lineItem;
}
- (BUYLineItem *)lineItemWithCartLineItem:(BUYCartLineItem *)cartLineItem
{
BUYLineItem *lineItem = [self insertLineItemWithJSONDictionary:nil];
[lineItem updateWithLineItem:cartLineItem];
return lineItem; return lineItem;
} }
......
// //
// BUYOption.h // _BUYOption.h
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -24,27 +24,8 @@ ...@@ -24,27 +24,8 @@
// THE SOFTWARE. // THE SOFTWARE.
// //
#import "BUYObject.h" #import <Buy/_BUYOption.h>
/** @interface BUYOption : _BUYOption {}
* This represent a BUYOption on a BUYProduct
*/
@interface BUYOption : BUYObject
/**
* Custom product property names like "Size", "Color", and "Material".
* 255 characters limit each.
*/
@property (nonatomic, readonly, copy) NSString *name;
/**
* The order in which the option should optionally appear
*/
@property (nonatomic, readonly, strong) NSNumber *position;
/**
* The associated product ID for this option
*/
@property (nonatomic, readonly, copy) NSNumber *productId;
@end @end
// //
// BUYOption.m // _BUYOption.m
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -28,13 +28,4 @@ ...@@ -28,13 +28,4 @@
@implementation BUYOption @implementation BUYOption
- (void)updateWithDictionary:(NSDictionary *)dictionary
{
[super updateWithDictionary:dictionary];
_name = [dictionary[@"name"] copy];
_position = dictionary[@"position"];
_productId = [dictionary[@"product_id"] copy];
}
@end @end
// //
// BUYOptionValue.h // _BUYOptionValue.h
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -24,24 +24,10 @@ ...@@ -24,24 +24,10 @@
// THE SOFTWARE. // THE SOFTWARE.
// //
#import "BUYObject.h" #import <Buy/_BUYOptionValue.h>
@interface BUYOptionValue : BUYObject @interface BUYOptionValue : _BUYOptionValue {}
/** - (BOOL)isEqualToOptionValue:(BUYOptionValue *)other;
* Custom product property names like "Size", "Color", and "Material".
* 255 characters limit each.
*/
@property (nonatomic, readonly, copy) NSString *name;
/**
* The value of the option
*/
@property (nonatomic, readonly, strong) NSString *value;
/**
* the option identifier
*/
@property (nonatomic, readonly, strong) NSNumber *optionId;
@end @end
// //
// BUYOptionValue.m // _BUYOptionValue.m
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -28,37 +28,22 @@ ...@@ -28,37 +28,22 @@
@implementation BUYOptionValue @implementation BUYOptionValue
- (void)updateWithDictionary:(NSDictionary *)dictionary - (BOOL)isEqualToOptionValue:(BUYOptionValue *)other
{ {
[super updateWithDictionary:dictionary]; return [other isKindOfClass:[self class]] && [self.name isEqual:other.name] && [self.optionId isEqual:other.optionId];
_name = [dictionary[@"name"] copy];
_value = [dictionary[@"value"] copy];
_optionId = [dictionary[@"option_id"] copy];
} }
#if !defined CORE_DATA_PERSISTENCE
- (BOOL)isEqual:(id)object - (BOOL)isEqual:(id)object
{ {
BOOL same = NO; return [super isEqual:object] || [self isEqualToOptionValue:object];
if (self == object) {
same = YES;
}
else if ([object isKindOfClass:self.class]) {
BUYOptionValue *optionValue = (BUYOptionValue *)object;
same = ([self.optionId isEqualToNumber:optionValue.optionId] &&
[self.value isEqualToString:optionValue.value]);
}
return same;
} }
- (NSUInteger)hash - (NSUInteger)hash
{ {
NSUInteger hash = [self.value hash]; NSUInteger hash = self.name.hash;
hash ^= [self.optionId hash]; return ((hash << 5) + hash) + self.optionId.hash;
return hash;
} }
#endif
@end @end
// //
// BUYOrder.h // _BUYOrder.h
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -24,18 +24,15 @@ ...@@ -24,18 +24,15 @@
// THE SOFTWARE. // THE SOFTWARE.
// //
#import "BUYObject.h" #import <Buy/_BUYOrder.h>
@interface BUYOrder : BUYObject @interface BUYOrder : _BUYOrder {}
/** @end
* URL for the website showing the order status
*/ @interface BUYModelManager (BUYOrder)
@property (nonatomic, strong, readonly) NSURL *statusURL;
/** - (NSArray<BUYOrder *> *)ordersWithJSONDictionary:(NSDictionary *)json;
* The customer's order name as represented by a number.
*/
@property (nonatomic, strong, readonly) NSString *name;
@end @end
// //
// BUYOrder.m // _BUYOrder.m
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -25,24 +25,53 @@ ...@@ -25,24 +25,53 @@
// //
#import "BUYOrder.h" #import "BUYOrder.h"
#import "NSURL+BUYAdditions.h" #import "BUYLineItem.h"
#import "NSDictionary+BUYAdditions.h"
@interface BUYOrder () @implementation BUYOrder
- (NSArray *)formatIDsForLineItemsJSON:(NSArray<NSDictionary *> *)lineItems
{
__block NSMutableArray<NSDictionary *> *mutableLineItems = [NSMutableArray array];
[lineItems enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull lineItem, NSUInteger idx, BOOL * _Nonnull stop) {
NSNumber *identifier = lineItem[@"id"];
NSMutableDictionary *mutableLineItem = [lineItem mutableCopy];
if ([identifier isKindOfClass:[NSNumber class]]) {
mutableLineItem[@"id"] = identifier.stringValue;
}
[mutableLineItems addObject:mutableLineItem];
}];
return mutableLineItems;
}
- (void)setJSONDictionary:(NSDictionary *)JSONDictionary
{
// TODO: Have API return string IDs for line items instead of numbers
NSArray *fulfilledLineItemsJSON = [self formatIDsForLineItemsJSON:JSONDictionary[@"fulfilled_line_items"]];
NSArray *unFulfilledLineItemsJSON = [self formatIDsForLineItemsJSON:JSONDictionary[@"unfulfilled_line_items"]];
@property (nonatomic, strong) NSURL *statusURL; [super setJSONDictionary:JSONDictionary];
@property (nonatomic, strong) NSString *name;
// Required if core data is not being used
if (!self.lineItems) {
self.lineItems = [NSOrderedSet orderedSet];
}
NSArray *fulfilledLineItems = [self.modelManager buy_objectsWithEntityName:[BUYLineItem entityName] JSONArray:fulfilledLineItemsJSON];
[fulfilledLineItems makeObjectsPerformSelector:@selector(setFulfilled:) withObject:@YES];
NSArray *unfulfilledLineItems = [self.modelManager buy_objectsWithEntityName:[BUYLineItem entityName] JSONArray:unFulfilledLineItemsJSON];
[self.lineItemsSet addObjectsFromArray:fulfilledLineItems];
[self.lineItemsSet addObjectsFromArray:unfulfilledLineItems];
}
@end @end
@implementation BUYOrder @implementation BUYModelManager (BUYOrder)
- (void)updateWithDictionary:(NSDictionary *)dictionary - (NSArray<BUYOrder *> *)ordersWithJSONDictionary:(NSDictionary *)json
{ {
[super updateWithDictionary:dictionary]; NSArray *orders = [json objectForKey:@"orders"];
NSString *statusURLString = dictionary[@"status_url"]; return (id)[self buy_objectsWithEntityName:[BUYOrder entityName] JSONArray:orders];
self.statusURL = [NSURL buy_urlWithString:statusURLString];
self.name = [dictionary buy_objectForKey:@"name"];
} }
@end @end
...@@ -24,94 +24,42 @@ ...@@ -24,94 +24,42 @@
// THE SOFTWARE. // THE SOFTWARE.
// //
#import "BUYObject.h" #import <Buy/_BUYProduct.h>
@class BUYProductVariant; @interface BUYProduct : _BUYProduct {}
@class BUYImageLink;
@class BUYOption;
/** @property (nonatomic, readonly, copy) NSDate *createdAtDate;
* A BUYProduct is an individual item for sale in a Shopify shop. @property (nonatomic, readonly, copy) NSDate *updatedAtDate;
*/ @property (nonatomic, readonly, copy) NSDate *publishedAtDate;
@interface BUYProduct : BUYObject @property (nonatomic, readonly, copy) NSString *stringDescription;
/**
* The product ID
*/
@property (nonatomic, readonly, copy) NSNumber *productId;
/**
* The name of the product. In a shop's catalog, clicking on a product's title takes you to that product's page.
* On a product's page, the product's title typically appears in a large font.
*/
@property (nonatomic, readonly, copy) NSString *title;
/**
* The handle of the product. Can be used to construct links to the web page for the product
*/
@property (nonatomic, readonly, copy) NSString *handle;
/**
* The name of the vendor of the product.
*/
@property (nonatomic, readonly, copy) NSString *vendor;
/**
* A categorization that a product can be tagged with, commonly used for filtering and searching.
*/
@property (nonatomic, readonly, copy) NSString *productType;
/**
* A list of BUYProductVariant objects, each one representing a slightly different version of the product.
*/
@property (nonatomic, readonly, copy) NSArray<BUYProductVariant *> *variants;
/**
* A list of BUYImageLink objects, each one representing an image associated with the product.
*/
@property (nonatomic, readonly, copy) NSArray<BUYImageLink *> *images;
/**
* Custom product property names like "Size", "Color", and "Material".
* Products are based on permutations of these options.
* A product may have a maximum of 3 options. 255 characters limit each.
*/
@property (nonatomic, readonly, copy) NSArray<BUYOption *> *options;
/**
* The description of the product, complete with HTML formatting.
*/
@property (nonatomic, readonly, copy) NSString *htmlDescription;
/**
* If the product is in stock (see each variant for their specific availability)
*/
@property (nonatomic, readonly, assign) BOOL available;
/** @end
* A categorization that a product can be tagged with, commonly used for filtering and searching.
* Each tag has a character limit of 255.
*/
@property (nonatomic, readonly, copy) NSSet<NSString *> *tags;
/** @interface BUYProduct (Options)
* The product is published on the current sales channel
*/
@property (nonatomic, readonly, assign) BOOL published;
/** /**
* The creation date for a product * Get the option values available for the given option
*
* @param option the option
*
* @return array of BUYOptionValues
*/ */
@property (nonatomic, readonly, copy) NSDate *createdAtDate; - (NSArray *)valuesForOption:(BUYOption *)option variants:(NSArray *)variants;
/** /**
* The updated date for a product * Determine the variant given an array of options
*
* @param options array of option values
*
* @return the product variant matching the set of options
*/ */
@property (nonatomic, readonly, copy) NSDate *updatedAtDate; - (BUYProductVariant *)variantWithOptions:(NSArray *)options;
/** /**
* The publish date for a product * Determine if the variant is a default variant automatically created by Shopify
*
* @return YES if its a default variant
*/ */
@property (nonatomic, readonly, copy) NSDate *publishedAtDate; - (BOOL)isDefaultVariant;
@end @end
// //
// BUYProduct.m // _BUYProduct.m
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -24,39 +24,94 @@ ...@@ -24,39 +24,94 @@
// THE SOFTWARE. // THE SOFTWARE.
// //
#import "BUYImageLink.h"
#import "BUYOption.h"
#import "BUYProduct.h" #import "BUYProduct.h"
#import "BUYModelManager.h"
#import "BUYOption.h"
#import "BUYOptionValue.h"
#import "BUYProductVariant.h" #import "BUYProductVariant.h"
#import "NSDateFormatter+BUYAdditions.h" #import "NSString+BUYAdditions.h"
#import "NSDictionary+BUYAdditions.h"
@implementation BUYProduct @implementation BUYProduct
- (void)updateWithDictionary:(NSDictionary *)dictionary @synthesize stringDescription=_stringDescription;
- (NSDate *)createdAtDate
{
return self.createdAt;
}
- (NSDate *)updatedAtDate
{
return self.updatedAt;
}
- (NSDate *)publishedAtDate
{
return self.publishedAt;
}
- (NSString *)stringDescription
{
if (nil == _stringDescription) {
_stringDescription = [self.htmlDescription buy_stringByStrippingHTML];
}
return _stringDescription;
}
@end
@implementation BUYProduct (Options)
- (NSArray *)valuesForOption:(BUYOption *)option variants:(NSArray *)variants
{
NSMutableOrderedSet *set = [NSMutableOrderedSet new];
for (BUYProductVariant *variant in variants) {
BUYOptionValue *optionValue = [variant optionValueForName:option.name];
[set addObject:optionValue];
}
return [set array];
}
- (BUYProductVariant *)variantWithOptions:(NSArray *)options
{
BUYProductVariant *variant = nil;
for (BUYProductVariant *aVariant in self.variants) {
BOOL match = YES;
for (BUYOptionValue *value in options) {
BUYOptionValue *optionValue = [aVariant optionValueForName:value.name];
if (![optionValue isEqual:value]) {
match = NO;
break;
}
}
if (match) {
variant = aVariant;
}
}
return variant;
}
- (BOOL)isDefaultVariant
{ {
[super updateWithDictionary:dictionary]; if ([self.variants count] == 1) {
BUYProductVariant *productVariant = [self.variants firstObject];
_title = [dictionary[@"title"] copy]; BUYOptionValue *optionValue = [productVariant.options anyObject];
_handle = [dictionary[@"handle"] copy]; NSString *defaultTitleString = @"Default Title";
_productId = [dictionary[@"product_id"] copy]; NSString *defaultString = @"Default";
_vendor = [dictionary[@"vendor"] copy]; if ([productVariant.title isEqualToString:defaultTitleString] &&
_productType = [dictionary[@"product_type"] copy]; ([optionValue.value isEqualToString:defaultTitleString] || [optionValue.value isEqualToString:defaultString])) {
_variants = [BUYProductVariant convertJSONArray:dictionary[@"variants"] block:^(BUYProductVariant *variant) { return YES;
variant.product = self; }
}]; }
_images = [BUYImageLink convertJSONArray:dictionary[@"images"]]; return NO;
_options = [BUYOption convertJSONArray:dictionary[@"options"]];
_htmlDescription = [dictionary buy_objectForKey:@"body_html"];
_available = [dictionary[@"available"] boolValue];
_published = [dictionary[@"published"] boolValue];
NSDateFormatter *dateFormatter = [NSDateFormatter dateFormatterForPublications];
_createdAtDate = [dateFormatter dateFromString:dictionary[@"created_at"]];
_updatedAtDate = [dateFormatter dateFromString:dictionary[@"updated_at"]];
_publishedAtDate = [dateFormatter dateFromString:dictionary[@"published_at"]];
NSArray *tagsArray = [dictionary[@"tags"] componentsSeparatedByString:@", "];
NSSet *tagsSet = [NSSet setWithArray:tagsArray];
_tags = [tagsSet copy];
} }
@end @end
// //
// BUYProductVariant.h // _BUYProductVariant.h
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -24,72 +24,29 @@ ...@@ -24,72 +24,29 @@
// THE SOFTWARE. // THE SOFTWARE.
// //
#import "BUYObject.h" #import <Buy/_BUYProductVariant.h>
@class BUYProduct;
@class BUYOptionValue; @class BUYOptionValue;
/** @interface BUYProductVariant : _BUYProductVariant {}
* A BUYProductVariant is a different version of a product, such as differing sizes or differing colours.
*/
@interface BUYProductVariant : BUYObject
/**
* The BUYProduct associated this BUYProductVariant
*/
@property (nonatomic, strong) BUYProduct *product;
/**
* The title of the BUYProductVariant.
*/
@property (nonatomic, readonly, copy) NSString *title;
/**
* Custom properties that a shop owner can use to define BUYProductVariants.
*/
@property (nonatomic, readonly, copy) NSArray<BUYOptionValue *> *options;
/**
* The price of the BUYProductVariant.
*/
@property (nonatomic, readonly, strong) NSDecimalNumber *price;
/**
* The competitor's prices for the same item.
*/
@property (nonatomic, readonly, strong) NSDecimalNumber *compareAtPrice;
/**
* The weight of the BUYProductVariant in grams.
*/
@property (nonatomic, readonly, strong) NSDecimalNumber *grams;
/**
* Specifies whether or not a customer needs to provide a shipping address when placing an order for this BUYProductVariant.
* Valid values are:
* true: Customer needs to supply a shipping address.
* false: Customer does not need to supply a shipping address.
*/
@property (nonatomic, readonly, strong) NSNumber *requiresShipping;
/**
* A unique identifier for the product in the shop.
*/
@property (nonatomic, readonly, strong) NSString *sku;
/**
* Specifies whether or not a tax is charged when the BUYProductVariant is sold.
*/
@property (nonatomic, readonly, strong) NSNumber *taxable;
/** /**
* The order of the BUYProductVariant in the list of product variants. 1 is the first position. * Returns the option value for the given name
*
* @param optionName name of the option
*
* @return the option value
*/ */
@property (nonatomic, readonly, strong) NSNumber *position; - (BUYOptionValue *)optionValueForName:(NSString *)optionName;
/** /**
* If the variant is in stock * Filters array of product variants filtered based on a selected option value
*
* @param productVariants BUYProductVariant objects to filter
* @param optionValue The option value to filter with
*
* @return A filtered copy of the original array
*/ */
@property (nonatomic, readonly, assign) BOOL available; + (NSArray *)filterProductVariants:(NSArray *)productVariants forOptionValue:(BUYOptionValue *)optionValue;
@end @end
// //
// BUYProductVariant.m // _BUYProductVariant.m
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -25,29 +25,32 @@ ...@@ -25,29 +25,32 @@
// //
#import "BUYProductVariant.h" #import "BUYProductVariant.h"
#import "NSDecimalNumber+BUYAdditions.h"
#import "BUYOptionValue.h" #import "BUYOptionValue.h"
@implementation BUYProductVariant @implementation BUYProductVariant
- (void)updateWithDictionary:(NSDictionary *)dictionary - (BUYOptionValue *)optionValueForName:(NSString *)optionName
{ {
[super updateWithDictionary:dictionary]; for (BUYOptionValue *value in self.options) {
if ([value.name isEqualToString:optionName]) {
return value;
}
}
_title = [dictionary[@"title"] copy]; return nil;
}
_options = [BUYOptionValue convertJSONArray:dictionary[@"option_values"]];
_price = [NSDecimalNumber buy_decimalNumberFromJSON:dictionary[@"price"]];
_compareAtPrice = [NSDecimalNumber buy_decimalNumberFromJSON:dictionary[@"compare_at_price"]];
_grams = [NSDecimalNumber buy_decimalNumberFromJSON:dictionary[@"grams"]];
_requiresShipping = dictionary[@"requires_shipping"];
_sku = dictionary[@"sku"];
_taxable = dictionary[@"taxable"];
_position = dictionary[@"position"];
_available = [dictionary[@"available"] boolValue]; + (NSArray *)filterProductVariants:(NSArray *)productVariants forOptionValue:(BUYOptionValue *)optionValue
{
NSMutableArray *filteredArray = [NSMutableArray new];
for (BUYProductVariant *variant in productVariants) {
for (BUYOptionValue *opValue in variant.options) {
if ([opValue isEqual:optionValue]) {
[filteredArray addObject:variant];
}
}
}
return [filteredArray copy];
} }
@end @end
// //
// BUYShop.h // _BUYShop.h
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -24,66 +24,8 @@ ...@@ -24,66 +24,8 @@
// THE SOFTWARE. // THE SOFTWARE.
// //
#import "BUYObject.h" #import <Buy/_BUYShop.h>
/** @interface BUYShop : _BUYShop {}
* The BUYShop object is a collection of the general settings and information about the shop.
*/
@interface BUYShop : BUYObject
/**
* The name of the shop.
*/
@property (nonatomic, readonly, copy) NSString *name;
/**
* The city in which the shop is located.
*/
@property (nonatomic, readonly, copy) NSString *city;
/**
* The shop's normalized province or state name.
*/
@property (nonatomic, readonly, copy) NSString *province;
/**
* The country in which the shop is located
*/
@property (nonatomic, readonly, copy) NSString *country;
/**
* The three-letter code for the currency that the shop accepts.
*/
@property (nonatomic, readonly, copy) NSString *currency;
/**
* A string representing the way currency is formatted when the currency isn't specified.
*/
@property (nonatomic, readonly, copy) NSString *moneyFormat;
/**
* The shop's domain.
*/
@property (nonatomic, readonly, copy) NSString *domain;
/**
* The shop's description.
*/
@property (nonatomic, readonly, copy) NSString *shopDescription;
/**
* A list of two-letter country codes identifying the countries that the shop ships to.
*/
@property (nonatomic, readonly, copy) NSArray<NSString *> *shipsToCountries;
/**
* The URL for the web storefront
*/
@property (nonatomic, readonly) NSURL *shopURL;
/**
* The shop's 'myshopify.com' domain.
*/
@property (nonatomic, readonly) NSURL *myShopifyURL;
@end @end
// //
// BUYShop.m // _BUYShop.m
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -28,21 +28,4 @@ ...@@ -28,21 +28,4 @@
@implementation BUYShop @implementation BUYShop
- (void)updateWithDictionary:(NSDictionary *)dictionary
{
[super updateWithDictionary:dictionary];
_name = [dictionary[@"name"] copy];
_city = [dictionary[@"city"] copy];
_country = [dictionary[@"country"] copy];
_province = [dictionary[@"province"] copy];
_currency = [dictionary[@"currency"] copy];
_moneyFormat = [dictionary[@"money_format"] copy];
_domain = [dictionary[@"domain"] copy];
_shopDescription = [dictionary[@"description"] copy];
_shipsToCountries = [dictionary[@"ships_to_countries"] copy];
_shopURL = [NSURL URLWithString:dictionary[@"url"]];
_myShopifyURL = [NSURL URLWithString:dictionary[@"myshopify_domain"]];
}
@end @end
// //
// BUYAddress.h // _BUYAddress.h
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -24,72 +24,26 @@ ...@@ -24,72 +24,26 @@
// THE SOFTWARE. // THE SOFTWARE.
// //
#import "BUYObject.h" #import <Buy/_BUYAddress.h>
#import "BUYSerializable.h"
/** @interface BUYAddress : _BUYAddress {}
* A BUYAddress represents a shipping or billing address on an order. This will be associated with the customer upon completion.
*/
@interface BUYAddress : BUYObject <BUYSerializable>
/**
* The street address of the address.
*/
@property (nonatomic, copy) NSString *address1;
/**
* An optional additional field for the street address of the address.
*/
@property (nonatomic, copy) NSString *address2;
/**
* The city of the address.
*/
@property (nonatomic, copy) NSString *city;
/**
* The company of the person associated with the address (optional).
*/
@property (nonatomic, copy) NSString *company;
/**
* The first name of the person associated with the payment method.
*/
@property (nonatomic, copy) NSString *firstName;
/**
* The last name of the person associated with the payment method.
*/
@property (nonatomic, copy) NSString *lastName;
/**
* The phone number at the address.
*/
@property (nonatomic, copy) NSString *phone;
/**
* The name of the country of the address.
*/
@property (nonatomic, copy) NSString *country;
/**
* The two-letter code (ISO 3166-1 alpha-2 two-letter country code) for the country of the address.
*/
@property (nonatomic, copy) NSString *countryCode;
/**
* The name of the state or province of the address
*/
@property (nonatomic, copy) NSString *province;
/** /**
* The two-letter abbreviation of the state or province of the address. * Check if the address does not include first and last name
* and address1 field. This is used to determine whether a
* placeholder was set for shipping rates calculations in Apple Pay.
*
* @return True if first name, last name or address1 contain placeholders
*/ */
@property (nonatomic, copy) NSString *provinceCode; - (BOOL)isPartialAddress;
/** /**
* The zip or postal code of the address. * Local validation to check that the minimum set of properties required
* to calculate shipping rates are available.
*
* @return True if city, zip/postal code, province/state and country or
* country code are set.
*/ */
@property (nonatomic, copy) NSString *zip; - (BOOL)isValidAddressForShippingRates;
@end @end
// //
// BUYAddress.m // _BUYAddress.m
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -25,65 +25,38 @@ ...@@ -25,65 +25,38 @@
// //
#import "BUYAddress.h" #import "BUYAddress.h"
#import "NSString+BUYAdditions.h"
#import "NSDictionary+BUYAdditions.h"
@implementation BUYAddress @implementation BUYAddress
- (void)updateWithDictionary:(NSDictionary *)dictionary -(NSString *)countryCode
{ {
self.address1 = dictionary[@"address1"]; return [[super countryCode] uppercaseString];
self.address2 = dictionary[@"address2"];
self.city = dictionary[@"city"];
self.company = dictionary[@"company"];
self.firstName = dictionary[@"first_name"];
self.lastName = dictionary[@"last_name"];
self.phone = dictionary[@"phone"];
self.country = dictionary[@"country"];
self.countryCode = dictionary[@"country_code"];
self.province = [dictionary buy_objectForKey:@"province"];
self.provinceCode = [dictionary buy_objectForKey:@"province_code"];
self.zip = dictionary[@"zip"];
} }
- (NSDictionary *)jsonDictionaryForCheckout - (BOOL)isPartialAddress
{ {
NSMutableDictionary *json = [[NSMutableDictionary alloc] init]; if (self.address1.length == 0 ||
json[@"address1"] = [self.address1 buy_trim] ?: @""; self.firstName.length == 0 ||
json[@"address2"] = [self.address2 buy_trim] ?: @""; self.lastName.length == 0) {
json[@"city"] = [self.city buy_trim] ?: @""; return YES;
json[@"company"] = [self.company buy_trim] ?: @"";
json[@"first_name"] = [self.firstName buy_trim] ?: @"";
json[@"last_name"] = [self.lastName buy_trim] ?: @"";
json[@"phone"] = [self.phone buy_trim] ?: @"";
json[@"zip"] = [self.zip buy_trim] ?: @"";
NSString *country = [self.country buy_trim];
if ([country length] > 0) {
json[@"country"] = country;
} }
NSString *countryCode = [self.countryCode buy_trim]; return NO;
if ([countryCode length] > 0) { }
json[@"country_code"] = countryCode;
}
NSString *province = [self.province buy_trim]; - (BOOL)isValidAddressForShippingRates
if ([province length] > 0) { {
json[@"province"] = province; BOOL valid = NO;
}
if (self.city.length > 0 &&
self.zip.length > 0 &&
self.province.length > 0 &&
(self.country.length > 0 || self.countryCode.length == 2)) {
NSString *provinceCode = [self.provinceCode buy_trim]; valid = YES;
if ([provinceCode length] > 0) {
json[@"province_code"] = provinceCode;
} }
return json;
}
-(NSString *)countryCode return valid;
{
return [_countryCode uppercaseString];
} }
@end @end
// //
// BUYCheckout.h // _BUYCheckout.h
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -24,275 +24,36 @@ ...@@ -24,275 +24,36 @@
// THE SOFTWARE. // THE SOFTWARE.
// //
#import "BUYObject.h" #import <Buy/_BUYCheckout.h>
#import "BUYSerializable.h" #import <Buy/_BUYProductVariant.h>
#import <Buy/BUYModelManager.h>
@class BUYAddress; @class BUYCart, BUYCartLineItem, BUYAddress, BUYGiftCard;
@class BUYCart;
@class BUYCreditCard;
@class BUYDiscount;
@class BUYMaskedCreditCard;
@class BUYOrder;
@class BUYShippingRate;
@class BUYTaxLine;
@class BUYLineItem;
@class BUYGiftCard;
@class BUYCheckoutAttribute;
/** @interface BUYCheckout : _BUYCheckout {}
* The checkout object. This is the main object that you will interact with when creating orders on Shopify.
*
* Note: Do not create a BUYCheckout object directly. Use initWithCart: to transform a BUYCart into a BUYCheckout.
*/
@interface BUYCheckout : BUYObject <BUYSerializable>
/** @property (nonatomic, copy) NSNumber *taxesIncluded;
* The customer's email address @property (nonatomic, readonly, copy) NSDate *createdAtDate;
*/ @property (nonatomic, readonly, copy) NSDate *updatedAtDate;
@property (nonatomic, copy) NSString *email; @property (nonatomic, readonly, copy) NSDictionary *attributesDictionary;
/** @property (nonatomic) BOOL hasToken;
* Unique token for the checkout on Shopify
*/
@property (nonatomic, copy, readonly) NSString *token;
/** - (instancetype)initWithCart:(BUYCart *)cart NS_DEPRECATED_IOS(8_0, 9_0, "Use `BUYModelManager` to create new instances of model objects instead");
* Unique token for a cart which can be used to convert to a checkout - (instancetype)initWithCartToken:(NSString *)token NS_DEPRECATED_IOS(8_0, 9_0, "Use `BUYModelManager` to create new instances of model objects instead");;
*/
@property (nonatomic, copy, readonly) NSString *cartToken;
/** - (void)updateWithCart:(BUYCart *)cart;
* States whether or not the fulfillment requires shipping
*/
@property (nonatomic, assign, readonly) BOOL requiresShipping;
/** - (BUYGiftCard *)giftCardWithIdentifier:(NSNumber *)identifier;
* States whether or not the taxes are included in the price - (void)removeGiftCardWithIdentifier:(NSNumber *)identifier;
*/
@property (nonatomic, assign, readonly) BOOL taxesIncluded;
/** @end
* The three letter code (ISO 4217) for the currency used for the payment
*/
@property (nonatomic, copy, readonly) NSString *currency;
/**
* Price of the order before shipping and taxes
*/
@property (nonatomic, strong, readonly) NSDecimalNumber *subtotalPrice;
/**
* The sum of all the taxes applied to the line items in the order
*/
@property (nonatomic, strong, readonly) NSDecimalNumber *totalTax;
/**
* The sum of all the prices of all the items in the order, taxes and discounts included
*/
@property (nonatomic, strong, readonly) NSDecimalNumber *totalPrice;
/**
* The Payment Session ID associated with a credit card transaction
*/
@property (nonatomic, strong, readonly) NSString *paymentSessionId;
/**
* URL to the payment gateway
*/
@property (nonatomic, strong, readonly) NSURL *paymentURL;
/**
* Reservation time on the checkout in seconds. Setting to @0 and updating the checkout
* will release inventory reserved by the checkout (when product inventory is not infinite).
*
* 300 seconds is default and maximum. `reservationTime` is reset to @300 on every
* `updateCheckout:completion:` call.
*
* Note: This can also be done with `removeProductReservationsFromCheckout:completion`
* found in the BUYClient.
*/
@property (nonatomic, strong) NSNumber *reservationTime;
/**
* Reservation time remaining on the checkout in seconds
*/
@property (nonatomic, strong, readonly) NSNumber *reservationTimeLeft;
/**
* Amount of payment due on the checkout
*/
@property (nonatomic, strong, readonly) NSDecimalNumber *paymentDue;
/**
* Array of BUYLineItem objects in the checkout
* Note: These are different from BUYCartLineItems in that the line item
* objects do not include the BUYProductVariant
*/
@property (nonatomic, readonly, copy) NSArray<__kindof BUYLineItem *> *lineItems;
/**
* Array of tax line objects on the checkout
*/
@property (nonatomic, readonly, copy) NSArray<BUYTaxLine *> *taxLines;
/**
* The mailing address associated with the payment method
*/
@property (nonatomic, strong) BUYAddress *billingAddress;
/**
* The mailing address to where the order will be shipped
*/
@property (nonatomic, strong) BUYAddress *shippingAddress;
/**
* The shipping rate chosen for the checkout
*/
@property (nonatomic, strong) BUYShippingRate *shippingRate;
/**
* Shipping rate identifier
*/
@property (nonatomic, readonly) NSString *shippingRateId DEPRECATED_MSG_ATTRIBUTE("Use shippingRate.shippingRateIdentifier");
/**
* A discount added to the checkout
* Only one discount can be added to a checkout. Call `updateCheckout:completion:`
* after adding a discount to apply the discount code to the checkout.
*/
@property (nonatomic, strong) BUYDiscount *discount;
/**
* An array of BUYGiftCard objects applied to the checkout
*/
@property (nonatomic, strong, readonly) NSArray<BUYGiftCard *> *giftCards;
/**
* Attributions for the checkout, containing the application name and platform (defaults to applicationName set
* on the BUYClient, and "iOS" respectively
*/
@property (nonatomic, strong) NSDictionary *marketingAttribution;
/**
* URL which is used for completing checkout. It is recommended to open the URL in Safari to take
* advantage of its autocompletion and credit card capture capabilities
*/
@property (nonatomic, strong, readonly) NSURL *webCheckoutURL;
/**
* The URL Scheme of the host app. Used to return to the app from the web checkout
*/
@property (nonatomic, strong) NSString *webReturnToURL;
/**
* The button title that will appear after checkout to return to the host app. Defaults to "Return to 'application'",
* where 'application' is the `applicationName` set on the BUYClient
*/
@property (nonatomic, strong) NSString *webReturnToLabel;
/**
* Creation date of the checkout
*/
@property (nonatomic, copy, readonly) NSDate *createdAtDate;
/**
* Last updated date for the checkout
*/
@property (nonatomic, copy, readonly) NSDate *updatedAtDate;
/**
* The website URL for the privacy policy for the checkout
*/
@property (nonatomic, strong, readonly) NSURL *privacyPolicyURL;
/**
* The website URL for the refund policy for the checkout
*/
@property (nonatomic, strong, readonly) NSURL *refundPolicyURL;
/**
* The website URL for the terms of service for the checkout
*/
@property (nonatomic, strong, readonly) NSURL *termsOfServiceURL;
/**
* The name of the source of the checkout: "mobile_app"
*/
@property (nonatomic, copy, readonly) NSString *sourceName;
/**
* Credit card stored on the checkout
*/
@property (nonatomic, strong, readonly) BUYMaskedCreditCard *creditCard;
/**
* Customer ID associated with the checkout
*/
@property (nonatomic, copy, readonly) NSString *customerId;
/**
* An optional note attached to the order
*/
@property (nonatomic, copy) NSString *note;
/**
* Extra information that is added to the order
*/
@property (nonatomic, copy) NSArray <BUYCheckoutAttribute *> *attributes;
/**
* The BUYOrder for a completed checkout
*/
@property (nonatomic, strong, readonly) BUYOrder *order;
/**
* Flag used to inform server that the shipping address is partially filled, suitable to retrieve shipping rates
* with partial shipping addresses provided by PKPaymentAuthorizationViewController.
* Note: This should only ever be set to YES. Setting it to NO throws an exception.
*/
@property (nonatomic, assign) BOOL partialAddresses;
/**
* It is recommended to instantiate a checkout with a cart, or cart token
*
* @return Checkout
*/
- (instancetype)init NS_UNAVAILABLE;
/**
* Creates a new checkout
*
* @param cart a Cart with line items on it
*
* @return a checkout object
*/
- (instancetype)initWithCart:(BUYCart *)cart;
/**
* Creates a new checkout
*
* @param cartToken a token for a previously created cart
*
* @return a checkout object
*/
- (instancetype)initWithCartToken:(NSString *)cartToken;
/**
* Helper method to determine if there is a valid token on the checkout
*
* @return YES if the token is valid
*/
- (BOOL)hasToken;
#pragma mark - Deprecated properties
/** @interface BUYModelManager (BUYCheckoutCreating)
* The unique order ID
*/
@property (nonatomic, copy, readonly) NSNumber *orderId DEPRECATED_MSG_ATTRIBUTE("Available on the BUYOrder object");
/** - (BUYCheckout *)checkout;
* URL for the website showing the order status - (BUYCheckout *)checkoutWithCart:(BUYCart *)cart;
*/ - (BUYCheckout *)checkoutWithVariant:(BUYProductVariant *)productVariant;
@property (nonatomic, strong, readonly) NSURL *orderStatusURL DEPRECATED_MSG_ATTRIBUTE("Available on the BUYOrder object"); - (BUYCheckout *)checkoutwithCartToken:(NSString *)token;
@end @end
...@@ -24,22 +24,10 @@ ...@@ -24,22 +24,10 @@
// THE SOFTWARE. // THE SOFTWARE.
// //
#import "BUYObject.h" #import <Buy/_BUYCheckoutAttribute.h>
#import "BUYSerializable.h"
/** /**
* A BUYCheckoutAttribute represents a checkout attributes key and value * A BUYCheckoutAttribute represents a checkout attributes key and value
*/ */
@interface BUYCheckoutAttribute : BUYObject <BUYSerializable> @interface BUYCheckoutAttribute : _BUYCheckoutAttribute
/**
* The attribute name
*/
@property (nonatomic, strong, nonnull) NSString *name;
/**
* The attribute value
*/
@property (nonatomic, strong, nonnull) NSString *value;
@end @end
...@@ -28,17 +28,6 @@ ...@@ -28,17 +28,6 @@
@implementation BUYCheckoutAttribute @implementation BUYCheckoutAttribute
- (void)updateWithDictionary:(NSDictionary *)dictionary
{
self.name = dictionary[@"name"];
self.value = dictionary[@"value"];
}
- (NSDictionary *)jsonDictionaryForCheckout
{
return @{ self.name : self.value };
}
- (BOOL)isEqual:(id)object - (BOOL)isEqual:(id)object
{ {
BOOL same = NO; BOOL same = NO;
......
// //
// BUYDiscount.h // _BUYDiscount.h
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -24,36 +24,12 @@ ...@@ -24,36 +24,12 @@
// THE SOFTWARE. // THE SOFTWARE.
// //
#import "BUYObject.h" #import <Buy/_BUYDiscount.h>
#import "BUYSerializable.h" #import <Buy/BUYModelManager.h>
/** @interface BUYDiscount : _BUYDiscount {}
* BUYDiscount represents a discount that is applied to the BUYCheckout. @end
*/
@interface BUYDiscount : BUYObject <BUYSerializable>
/**
* The unique identifier for the discount code.
*/
@property (nonatomic, copy) NSString *code;
/**
* The amount that is deducted from `paymentDue` on BUYCheckout.
*/
@property (nonatomic, strong) NSDecimalNumber *amount;
/**
* Whether this discount code can be applied to the checkout.
*/
@property (nonatomic, assign) BOOL applicable;
/**
* Created a BUYDiscount with a code
*
* @param code The discount code
*
* @return BUYDiscount object
*/
- (instancetype)initWithCode:(NSString *)code;
@interface BUYModelManager (BUYDiscountCreating)
- (BUYDiscount *)discountWithCode:(NSString *)code;
@end @end
// //
// BUYDiscount.m // _BUYDiscount.m
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -25,29 +25,24 @@ ...@@ -25,29 +25,24 @@
// //
#import "BUYDiscount.h" #import "BUYDiscount.h"
#import "NSDecimalNumber+BUYAdditions.h" #import "NSEntityDescription+BUYAdditions.h"
#import "NSString+BUYAdditions.h"
@implementation BUYDiscount @implementation BUYDiscount
- (instancetype)initWithCode:(NSString *)code - (NSDictionary *)JSONEncodedProperties
{ {
return [super initWithDictionary:@{@"code": code ?: @""}]; return [self.entity.JSONEncodedProperties dictionaryWithValuesForKeys:@[@"code"]];
} }
- (void)updateWithDictionary:(NSDictionary *)dictionary @end
{
[super updateWithDictionary:dictionary]; @implementation BUYModelManager (BUYDiscountCreating)
self.code = dictionary[@"code"];
self.amount = [NSDecimalNumber buy_decimalNumberFromJSON:dictionary[@"amount"]];
self.applicable = [dictionary[@"applicable"] boolValue];
}
- (NSDictionary *)jsonDictionaryForCheckout - (BUYDiscount *)discountWithCode:(NSString *)code
{ {
NSMutableDictionary *json = [[NSMutableDictionary alloc] init]; BUYDiscount *discount = [self buy_objectWithEntityName:[BUYDiscount entityName] JSONDictionary:nil];
json[@"code"] = [self.code buy_trim] ?: @""; discount.code = code ?: @"";
return json; return discount;
} }
@end @end
\ No newline at end of file
// //
// BUYGiftCard.h // _BUYGiftCard.h
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -24,30 +24,15 @@ ...@@ -24,30 +24,15 @@
// THE SOFTWARE. // THE SOFTWARE.
// //
#import "BUYObject.h" #import <Buy/_BUYGiftCard.h>
#import "BUYSerializable.h" #import <Buy/BUYModelManager.h>
@interface BUYGiftCard : BUYObject <BUYSerializable> @interface BUYGiftCard : _BUYGiftCard {}
/** @end
* The gift card code. This is only used when applying a gift card and
* is not visible on a BUYCheckout object synced with Shopify.
*/
@property (nonatomic, readonly, copy) NSString *code;
/**
* The last characters of the applied gift card code.
*/
@property (nonatomic, readonly, copy) NSString *lastCharacters;
/** @interface BUYModelManager (BUYGiftCardCreation)
* The amount left on the gift card after being applied to this checkout.
*/
@property (nonatomic, readonly, strong) NSDecimalNumber *balance;
/** - (BUYGiftCard *)giftCardWithCode:(NSString *)code;
* The amount of the gift card used by this checkout.
*/
@property (nonatomic, readonly, strong) NSDecimalNumber *amountUsed;
@end @end
// //
// BUYGiftCard.m // _BUYGiftCard.m
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -25,41 +25,23 @@ ...@@ -25,41 +25,23 @@
// //
#import "BUYGiftCard.h" #import "BUYGiftCard.h"
#import "NSDecimalNumber+BUYAdditions.h"
@implementation BUYGiftCard @implementation BUYGiftCard
- (void)updateWithDictionary:(NSDictionary *)dictionary
{
[super updateWithDictionary:dictionary];
_code = dictionary[@"code"];
_lastCharacters = dictionary[@"last_characters"];
_balance = [NSDecimalNumber buy_decimalNumberFromJSON:dictionary[@"balance"]];
_amountUsed = [NSDecimalNumber buy_decimalNumberFromJSON:dictionary[@"amount_used"]];
}
- (NSDictionary *)jsonDictionaryForCheckout - (NSDictionary *)jsonDictionaryForCheckout
{ {
NSMutableDictionary *json = [[NSMutableDictionary alloc] init]; return @{ @"gift_card" : self.JSONDictionary };
}
if (_code) {
json[@"code"] = _code;
}
if (_lastCharacters) {
json[@"last_characters"] = _lastCharacters;
}
if (_balance) { @end
json[@"balance"] = _balance;
}
if (_amountUsed) { @implementation BUYModelManager (BUYGiftCardCreation)
json[@"amount_used"] = _amountUsed;
}
return @{ @"gift_card" : json }; - (BUYGiftCard *)giftCardWithCode:(NSString *)code
{
BUYGiftCard *giftCard = [self buy_objectWithEntityName:[BUYGiftCard entityName] JSONDictionary:nil];
giftCard.code = code;
return giftCard;
} }
@end @end
// //
// BUYMaskedCreditCard.h // _BUYMaskedCreditCard.h
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -24,41 +24,8 @@ ...@@ -24,41 +24,8 @@
// THE SOFTWARE. // THE SOFTWARE.
// //
#import "BUYObject.h" #import <Buy/_BUYMaskedCreditCard.h>
/** @interface BUYMaskedCreditCard : _BUYMaskedCreditCard {}
* This represents a masked credit card that has been applied to a checkout.
*/
@interface BUYMaskedCreditCard : BUYObject
/**
* The first name on the credit card
*/
@property (nonatomic, copy) NSString *firstName;
/**
* The last name on the credit card
*/
@property (nonatomic, copy) NSString *lastName;
/**
* The first digits of credit card number.
*/
@property (nonatomic, copy) NSString *firstDigits;
/**
* The last digits of credit card number.
*/
@property (nonatomic, copy) NSString *lastDigits;
/**
* The year the card expires
*/
@property (nonatomic, copy) NSNumber *expiryYear;
/**
* The two digits representing the month the card expires
*/
@property (nonatomic, copy) NSNumber *expiryMonth;
@end @end
// //
// BUYMaskedCreditCard.m // _BUYMaskedCreditCard.m
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -28,18 +28,4 @@ ...@@ -28,18 +28,4 @@
@implementation BUYMaskedCreditCard @implementation BUYMaskedCreditCard
- (void)updateWithDictionary:(NSDictionary *)dictionary
{
[super updateWithDictionary:dictionary];
_firstName = [dictionary[@"first_name"] copy];
_lastName = [dictionary[@"last_name"] copy];
_firstDigits = [dictionary[@"first_digits"] copy];
_lastDigits = [dictionary[@"last_digits"] copy];
_expiryMonth = [dictionary[@"expiry_month"] copy];
_expiryYear = [dictionary[@"expiry_year"] copy];
}
@end @end
// //
// BUYShippingRate.h // _BUYShippingRate.h
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -24,32 +24,8 @@ ...@@ -24,32 +24,8 @@
// THE SOFTWARE. // THE SOFTWARE.
// //
#import "BUYObject.h" #import <Buy/_BUYShippingRate.h>
#import "BUYSerializable.h"
/** @interface BUYShippingRate : _BUYShippingRate {}
* BUYShippingRate represents the amount that the merchant is charging a customer for shipping to the specified address.
*/
@interface BUYShippingRate : BUYObject <BUYSerializable>
/**
* A reference to the shipping method.
*/
@property (nonatomic, strong, readonly) NSString *shippingRateIdentifier;
/**
* The shipping method name.
*/
@property (nonatomic, strong, readonly) NSString *title;
/**
* The price of this shipping method.
*/
@property (nonatomic, strong, readonly) NSDecimalNumber *price;
/**
* One or two NSDate objects of the potential delivery dates.
*/
@property (nonatomic, strong, readonly) NSArray *deliveryRange;
@end @end
// //
// BUYShippingRate.m // _BUYShippingRate.m
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -25,52 +25,7 @@ ...@@ -25,52 +25,7 @@
// //
#import "BUYShippingRate.h" #import "BUYShippingRate.h"
#import "NSDecimalNumber+BUYAdditions.h"
#import "NSString+BUYAdditions.h"
#import "NSDateFormatter+BUYAdditions.h"
@interface BUYShippingRate ()
@property (nonatomic, strong) NSString *shippingRateIdentifier;
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSDecimalNumber *price;
@property (nonatomic, strong) NSArray *deliveryRange;
@end
@implementation BUYShippingRate @implementation BUYShippingRate
- (void)updateWithDictionary:(NSDictionary *)dictionary
{
self.shippingRateIdentifier = dictionary[@"id"];
self.price = [NSDecimalNumber buy_decimalNumberFromJSON:dictionary[@"price"]];
self.title = dictionary[@"title"];
if ([dictionary[@"delivery_range"] isKindOfClass:[NSNull class]] == NO && [dictionary[@"delivery_range"] count]) {
NSDateFormatter *dateFormatter = [NSDateFormatter dateFormatterForShippingRates];
NSMutableArray *shippingRangeDates = [NSMutableArray new];
for (NSString *dateString in dictionary[@"delivery_range"]) {
[shippingRangeDates addObject:[dateFormatter dateFromString:dateString]];
}
self.deliveryRange = [shippingRangeDates copy];
}
}
- (NSDictionary *)jsonDictionaryForCheckout
{
NSMutableDictionary *json = [[NSMutableDictionary alloc] init];
json[@"id"] = [self.shippingRateIdentifier buy_trim] ?: @"";
json[@"title"] = [self.title buy_trim] ?: @"";
json[@"price"] = self.price ?: [NSDecimalNumber zero];
if (self.deliveryRange) {
NSDateFormatter *dateFormatter = [NSDateFormatter dateFormatterForShippingRates];
NSMutableArray *shippingRangeStrings = [NSMutableArray new];
for (NSDate *date in self.deliveryRange) {
[shippingRangeStrings addObject:[dateFormatter stringFromDate:date]];
}
json[@"delivery_range"] = [shippingRangeStrings copy];
}
return json;
}
@end @end
// //
// BUYTaxLine.h // _BUYTaxLine.h
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -24,27 +24,8 @@ ...@@ -24,27 +24,8 @@
// THE SOFTWARE. // THE SOFTWARE.
// //
#import "BUYObject.h" #import <Buy/_BUYTaxLine.h>
#import "BUYSerializable.h"
/** @interface BUYTaxLine : _BUYTaxLine {}
* BUYTaxLine represents a single tax line on a checkout. Use this to display an itemized list of taxes that a customer is being charged for.
*/
@interface BUYTaxLine : BUYObject
/**
* The amount of tax to be charged.
*/
@property (nonatomic, strong) NSDecimalNumber *price;
/**
* The rate of tax to be applied.
*/
@property (nonatomic, strong) NSDecimalNumber *rate;
/**
* The name of the tax.
*/
@property (nonatomic, copy) NSString *title;
@end @end
// //
// BUYTaxLine.m // _BUYTaxLine.m
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -25,17 +25,7 @@ ...@@ -25,17 +25,7 @@
// //
#import "BUYTaxLine.h" #import "BUYTaxLine.h"
#import "NSDecimalNumber+BUYAdditions.h"
@implementation BUYTaxLine @implementation BUYTaxLine
- (void)updateWithDictionary:(NSDictionary *)dictionary
{
[super updateWithDictionary:dictionary];
_price = [NSDecimalNumber buy_decimalNumberFromJSON:dictionary[@"price"]];
_rate = [NSDecimalNumber buy_decimalNumberFromJSON:dictionary[@"rate"]];
_title = dictionary[@"title"];
}
@end @end
...@@ -64,6 +64,7 @@ extern const struct BUYCheckoutAttributes { ...@@ -64,6 +64,7 @@ extern const struct BUYCheckoutAttributes {
} BUYCheckoutAttributes; } BUYCheckoutAttributes;
extern const struct BUYCheckoutRelationships { extern const struct BUYCheckoutRelationships {
__unsafe_unretained NSString *attributes;
__unsafe_unretained NSString *billingAddress; __unsafe_unretained NSString *billingAddress;
__unsafe_unretained NSString *creditCard; __unsafe_unretained NSString *creditCard;
__unsafe_unretained NSString *discount; __unsafe_unretained NSString *discount;
...@@ -80,6 +81,7 @@ extern const struct BUYCheckoutUserInfo { ...@@ -80,6 +81,7 @@ extern const struct BUYCheckoutUserInfo {
__unsafe_unretained NSString *documentation; __unsafe_unretained NSString *documentation;
} BUYCheckoutUserInfo; } BUYCheckoutUserInfo;
@class BUYCheckoutAttribute;
@class BUYAddress; @class BUYAddress;
@class BUYMaskedCreditCard; @class BUYMaskedCreditCard;
@class BUYDiscount; @class BUYDiscount;
...@@ -300,6 +302,10 @@ extern const struct BUYCheckoutUserInfo { ...@@ -300,6 +302,10 @@ extern const struct BUYCheckoutUserInfo {
*/ */
@property (nonatomic, strong) NSURL* webReturnToURL; @property (nonatomic, strong) NSURL* webReturnToURL;
@property (nonatomic, strong) NSSet *attributes;
- (NSMutableSet*)attributesSet;
@property (nonatomic, strong) BUYAddress *billingAddress; @property (nonatomic, strong) BUYAddress *billingAddress;
/** /**
...@@ -351,6 +357,10 @@ extern const struct BUYCheckoutUserInfo { ...@@ -351,6 +357,10 @@ extern const struct BUYCheckoutUserInfo {
@end @end
@interface _BUYCheckout (AttributesCoreDataGeneratedAccessors)
@end
@interface _BUYCheckout (GiftCardsCoreDataGeneratedAccessors) @interface _BUYCheckout (GiftCardsCoreDataGeneratedAccessors)
- (void)insertObject:(BUYGiftCard*)value inGiftCardsAtIndex:(NSUInteger)idx; - (void)insertObject:(BUYGiftCard*)value inGiftCardsAtIndex:(NSUInteger)idx;
......
...@@ -62,6 +62,7 @@ const struct BUYCheckoutAttributes BUYCheckoutAttributes = { ...@@ -62,6 +62,7 @@ const struct BUYCheckoutAttributes BUYCheckoutAttributes = {
}; };
const struct BUYCheckoutRelationships BUYCheckoutRelationships = { const struct BUYCheckoutRelationships BUYCheckoutRelationships = {
.attributes = @"attributes",
.billingAddress = @"billingAddress", .billingAddress = @"billingAddress",
.creditCard = @"creditCard", .creditCard = @"creditCard",
.discount = @"discount", .discount = @"discount",
...@@ -175,6 +176,12 @@ const struct BUYCheckoutUserInfo BUYCheckoutUserInfo = { ...@@ -175,6 +176,12 @@ const struct BUYCheckoutUserInfo BUYCheckoutUserInfo = {
[self setReservationTimeLeft:@(value_)]; [self setReservationTimeLeft:@(value_)];
} }
- (NSMutableSet*)attributesSet {
return (NSMutableSet*)[self mutableSetValueForKey:@"attributes"];
}
- (NSMutableOrderedSet*)giftCardsSet { - (NSMutableOrderedSet*)giftCardsSet {
return (NSMutableOrderedSet*)[self mutableOrderedSetValueForKey:@"giftCards"]; return (NSMutableOrderedSet*)[self mutableOrderedSetValueForKey:@"giftCards"];
......
// //
// BUYAddress+Additions.h // _BUYCheckoutAttribute.h
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -23,28 +23,51 @@ ...@@ -23,28 +23,51 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. // THE SOFTWARE.
// //
// DO NOT EDIT. This file is machine-generated and constantly overwritten.
// Make changes to BUYCheckoutAttribute.h instead.
@import Foundation; #import "BUYObject.h"
#import "BUYAddress.h"
@interface BUYAddress (Additions) #import <Buy/BUYModelManager.h>
extern const struct BUYCheckoutAttributeAttributes {
__unsafe_unretained NSString *name;
__unsafe_unretained NSString *value;
} BUYCheckoutAttributeAttributes;
extern const struct BUYCheckoutAttributeRelationships {
__unsafe_unretained NSString *checkout;
} BUYCheckoutAttributeRelationships;
extern const struct BUYCheckoutAttributeUserInfo {
__unsafe_unretained NSString *documentation;
} BUYCheckoutAttributeUserInfo;
@class BUYCheckout;
@class BUYCheckoutAttribute;
@interface BUYModelManager (BUYCheckoutAttributeInserting)
- (NSArray<BUYCheckoutAttribute *> *)allCheckoutAttributeObjects;
- (BUYCheckoutAttribute *)fetchCheckoutAttributeWithIdentifierValue:(int64_t)identifier;
- (BUYCheckoutAttribute *)insertCheckoutAttributeWithJSONDictionary:(NSDictionary *)dictionary;
- (NSArray<BUYCheckoutAttribute *> *)insertCheckoutAttributesWithJSONArray:(NSArray <NSDictionary *> *)array;
@end
/** /**
* Check if the address does not include first and last name * The attribute name.
* and address1 field. This is used to determine whether a
* placeholder was set for shipping rates calculations in Apple Pay.
*
* @return True if first name, last name or address1 contain placeholders
*/ */
- (BOOL)isPartialAddress; @interface _BUYCheckoutAttribute : BUYObject
+ (NSString *)entityName;
@property (nonatomic, strong) NSString* name;
/** /**
* Local validation to check that the minimum set of properties required * The attribute value.
* to calculate shipping rates are available.
*
* @return True if city, zip/postal code, province/state and country or
* country code are set.
*/ */
- (BOOL)isValidAddressForShippingRates; @property (nonatomic, strong) NSString* value;
@property (nonatomic, strong) BUYCheckout *checkout;
@end @end
// //
// BUYProductVariant+Options.m // _BUYCheckoutAttribute.h
// Mobile Buy SDK // Mobile Buy SDK
// //
// Created by Shopify. // Created by Shopify.
...@@ -23,33 +23,60 @@ ...@@ -23,33 +23,60 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. // THE SOFTWARE.
// //
// DO NOT EDIT. This file is machine-generated and constantly overwritten.
// Make changes to BUYCheckoutAttribute.m instead.
#import "BUYProductVariant+Options.h" #import "_BUYCheckoutAttribute.h"
@implementation BUYProductVariant (Options) const struct BUYCheckoutAttributeAttributes BUYCheckoutAttributeAttributes = {
.name = @"name",
.value = @"value",
};
- (BUYOptionValue *)optionValueForName:(NSString *)optionName const struct BUYCheckoutAttributeRelationships BUYCheckoutAttributeRelationships = {
.checkout = @"checkout",
};
const struct BUYCheckoutAttributeUserInfo BUYCheckoutAttributeUserInfo = {
.documentation = @"The attribute name.",
};
@implementation _BUYCheckoutAttribute
+ (NSString *)entityName {
return @"CheckoutAttribute";
}
+ (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key {
NSSet *keyPaths = [super keyPathsForValuesAffectingValueForKey:key];
return keyPaths;
}
@end
#pragma mark -
@implementation BUYModelManager (BUYCheckoutAttributeInserting)
- (BUYCheckoutAttribute *)insertCheckoutAttributeWithJSONDictionary:(NSDictionary *)dictionary
{
return (BUYCheckoutAttribute *)[self buy_objectWithEntityName:@"CheckoutAttribute" JSONDictionary:dictionary];
}
- (NSArray<BUYCheckoutAttribute *> *)insertCheckoutAttributesWithJSONArray:(NSArray <NSDictionary *> *)array
{ {
for (BUYOptionValue *value in self.options) { return (NSArray<BUYCheckoutAttribute *> *)[self buy_objectsWithEntityName:@"CheckoutAttribute" JSONArray:array];
if ([value.name isEqualToString:optionName]) { }
return value;
}
}
return nil; - (NSArray<BUYCheckoutAttribute *> *)allCheckoutAttributeObjects
{
return (NSArray<BUYCheckoutAttribute *> *)[self buy_objectsWithEntityName:@"CheckoutAttribute" identifiers:nil];
} }
+ (NSArray *)filterProductVariants:(NSArray *)productVariants forOptionValue:(BUYOptionValue *)optionValue - (BUYCheckoutAttribute *)fetchCheckoutAttributeWithIdentifierValue:(int64_t)identifier
{ {
NSMutableArray *filteredArray = [NSMutableArray new]; return (BUYCheckoutAttribute *)[self buy_objectWithEntityName:@"CheckoutAttribute" identifier:@(identifier)];
for (BUYProductVariant *variant in productVariants) {
for (BUYOptionValue *opValue in variant.options) {
if ([opValue isEqual:optionValue]) {
[filteredArray addObject:variant];
}
}
}
return [filteredArray copy];
} }
@end @end
...@@ -105,7 +105,7 @@ ...@@ -105,7 +105,7 @@
self.priceLabel.text = [currencyFormatter stringFromNumber:productVariant.price]; self.priceLabel.text = [currencyFormatter stringFromNumber:productVariant.price];
} }
if (productVariant.available == YES && productVariant.compareAtPrice) { if (productVariant.available.boolValue && productVariant.compareAtPrice) {
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:[currencyFormatter stringFromNumber:productVariant.compareAtPrice] NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:[currencyFormatter stringFromNumber:productVariant.compareAtPrice]
attributes:@{NSStrikethroughStyleAttributeName: @(NSUnderlineStyleSingle)}]; attributes:@{NSStrikethroughStyleAttributeName: @(NSUnderlineStyleSingle)}];
self.comparePriceLabel.attributedText = attributedString; self.comparePriceLabel.attributedText = attributedString;
......
...@@ -97,7 +97,7 @@ CGFloat const buttonWidth = 10.0f; ...@@ -97,7 +97,7 @@ CGFloat const buttonWidth = 10.0f;
- (void)setOptionsForProductVariant:(BUYProductVariant *)productVariant - (void)setOptionsForProductVariant:(BUYProductVariant *)productVariant
{ {
NSArray *productOptions = productVariant.options; NSArray *productOptions = productVariant.options.allObjects;
switch (productVariant.options.count) { switch (productVariant.options.count) {
case 3: case 3:
......
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
#import "BUYImageView.h" #import "BUYImageView.h"
#import "BUYOptionSelectionNavigationController.h" #import "BUYOptionSelectionNavigationController.h"
#import "BUYPresentationControllerWithNavigationController.h" #import "BUYPresentationControllerWithNavigationController.h"
#import "BUYProduct+Options.h" #import "BUYProduct.h"
#import "BUYProductViewController.h" #import "BUYProductViewController.h"
#import "BUYImageKit.h" #import "BUYImageKit.h"
#import "BUYProductView.h" #import "BUYProductView.h"
...@@ -397,8 +397,9 @@ CGFloat const BUYMaxProductViewHeight = 640.0; ...@@ -397,8 +397,9 @@ CGFloat const BUYMaxProductViewHeight = 640.0;
[self.variantCell setOptionsForProductVariant:self.selectedProductVariant]; [self.variantCell setOptionsForProductVariant:self.selectedProductVariant];
} }
if (self.productView.productViewHeader.collectionView) { if (self.productView.productViewHeader.collectionView) {
[self.productView.productViewHeader setImageForSelectedVariant:_selectedProductVariant withImages:self.product.images]; NSArray *images = self.product.images.array;
[self.productView updateBackgroundImage:self.product.images]; [self.productView.productViewHeader setImageForSelectedVariant:_selectedProductVariant withImages:images];
[self.productView updateBackgroundImage:images];
} }
if (self.productView.productViewFooter) { if (self.productView.productViewFooter) {
[self.productView.productViewFooter updateButtonsForProductVariant:selectedProductVariant]; [self.productView.productViewFooter updateButtonsForProductVariant:selectedProductVariant];
...@@ -460,7 +461,7 @@ CGFloat const BUYMaxProductViewHeight = 640.0; ...@@ -460,7 +461,7 @@ CGFloat const BUYMaxProductViewHeight = 640.0;
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{ {
if ([scrollView isKindOfClass:[UICollectionView class]]) { if ([scrollView isKindOfClass:[UICollectionView class]]) {
[self.productView updateBackgroundImage:self.product.images]; [self.productView updateBackgroundImage:self.product.images.array];
} }
} }
...@@ -468,7 +469,7 @@ CGFloat const BUYMaxProductViewHeight = 640.0; ...@@ -468,7 +469,7 @@ CGFloat const BUYMaxProductViewHeight = 640.0;
- (BUYCart *)cart - (BUYCart *)cart
{ {
BUYCart *cart = [[BUYCart alloc] init]; BUYCart *cart = [self.client.modelManager insertCartWithJSONDictionary:nil];
[cart addVariant:self.selectedProductVariant]; [cart addVariant:self.selectedProductVariant];
return cart; return cart;
} }
......
...@@ -25,7 +25,6 @@ ...@@ -25,7 +25,6 @@
// //
#import "BUYImageKit.h" #import "BUYImageKit.h"
#import "BUYProduct+Options.h"
#import "BUYOptionSelectionViewController.h" #import "BUYOptionSelectionViewController.h"
#import "BUYOptionValue.h" #import "BUYOptionValue.h"
#import "BUYOptionValueCell.h" #import "BUYOptionValueCell.h"
......
...@@ -28,13 +28,14 @@ ...@@ -28,13 +28,14 @@
#import "BUYOptionSelectionNavigationController.h" #import "BUYOptionSelectionNavigationController.h"
#import "BUYOptionSelectionViewController.h" #import "BUYOptionSelectionViewController.h"
#import "BUYPresentationControllerForVariantSelection.h" #import "BUYPresentationControllerForVariantSelection.h"
#import "BUYProduct+Options.h" #import "BUYProduct.h"
#import "BUYProductVariant+Options.h" #import "BUYProductVariant.h"
#import "BUYTheme.h" #import "BUYTheme.h"
#import "BUYTheme+Additions.h" #import "BUYTheme+Additions.h"
#import "BUYVariantSelectionViewController.h" #import "BUYVariantSelectionViewController.h"
#import "BUYVariantOptionBreadCrumbsView.h" #import "BUYVariantOptionBreadCrumbsView.h"
#import "BUYOption.h" #import "BUYOption.h"
#import "BUYOptionValue.h"
@interface BUYVariantSelectionViewController () <BUYOptionSelectionDelegate> @interface BUYVariantSelectionViewController () <BUYOptionSelectionDelegate>
......
...@@ -65,3 +65,16 @@ ...@@ -65,3 +65,16 @@
#import "BUYStoreViewController.h" #import "BUYStoreViewController.h"
#import "BUYTheme.h" #import "BUYTheme.h"
#import "BUYViewController.h" #import "BUYViewController.h"
#import "NSArray+BUYAdditions.h"
#import "NSDateFormatter+BUYAdditions.h"
#import "NSDecimalNumber+BUYAdditions.h"
#import "NSDictionary+BUYAdditions.h"
#import "NSDictionary+BUYAdditions.h"
#import "NSEntityDescription+BUYAdditions.h"
#import "NSException+BUYAdditions.h"
#import "NSPropertyDescription+BUYAdditions.h"
#import "NSRegularExpression+BUYAdditions.h"
#import "NSString+BUYAdditions.h"
#import "NSURL+BUYAdditions.h"
#import "NSURLComponents+BUYAdditions.h"
//
// BUYAddress+Additions.m
// Mobile Buy SDK
//
// Created by Shopify.
// Copyright (c) 2015 Shopify Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import "BUYAddress+Additions.h"
@implementation BUYAddress (Additions)
- (BOOL)isPartialAddress
{
if (self.address1.length == 0 ||
self.firstName.length == 0 ||
self.lastName.length == 0) {
return YES;
}
return NO;
}
- (BOOL)isValidAddressForShippingRates
{
BOOL valid = NO;
if (self.city.length > 0 &&
self.zip.length > 0 &&
(self.country.length > 0 || self.countryCode.length == 2)) {
valid = YES;
}
return valid;
}
@end
...@@ -67,22 +67,8 @@ ...@@ -67,22 +67,8 @@
+ (nullable NSString *)buy_emailFromRecord:(nullable ABRecordRef)record; + (nullable NSString *)buy_emailFromRecord:(nullable ABRecordRef)record;
/** - (void)updateWithRecord:(nullable ABRecordRef)record NS_DEPRECATED_IOS(8_0, 9_0, "Use the CNContact backed `updateWithContact:` instead");
* Creates a BUYAddress from an ABRecordRef
*
* @param record ABRecordRef to create a BUYAddress from
*
* @return The BUYAddress created from an ABRecordRef
*/
+ (nonnull BUYAddress *)buy_addressFromRecord:(nullable ABRecordRef)record NS_DEPRECATED_IOS(8_0, 9_0, "Use the CNContact backed `buy_addressFromContact:` instead");
/** - (void)updateWithContact:(nullable PKContact*)contact NS_AVAILABLE_IOS(9_0);
* Creates a BUYAddress from a PKContact
*
* @param contact PKContact to create a BUYAddress from
*
* @return The BUYAddress created from a PKContact
*/
+ (nonnull BUYAddress *)buy_addressFromContact:(nullable PKContact*)contact NS_AVAILABLE_IOS(9_0);
@end @end
...@@ -30,7 +30,6 @@ ...@@ -30,7 +30,6 @@
#import "BUYGiftCard.h" #import "BUYGiftCard.h"
#import "BUYApplePayAdditions.h" #import "BUYApplePayAdditions.h"
#import "BUYDiscount.h" #import "BUYDiscount.h"
#import "BUYAddress+Additions.h"
#import "NSDecimalNumber+BUYAdditions.h" #import "NSDecimalNumber+BUYAdditions.h"
#import "NSDate+BUYAdditions.h" #import "NSDate+BUYAdditions.h"
...@@ -137,13 +136,11 @@ ...@@ -137,13 +136,11 @@
return email; return email;
} }
+ (nonnull BUYAddress *)buy_addressFromRecord:(nullable ABRecordRef)record - (void)updateWithRecord:(nullable ABRecordRef)record
{ {
BUYAddress *address = [[BUYAddress alloc] init];
//Grab the simple information //Grab the simple information
address.firstName = (__bridge_transfer NSString *)ABRecordCopyValue(record, kABPersonFirstNameProperty); self.firstName = (__bridge_transfer NSString *)ABRecordCopyValue(record, kABPersonFirstNameProperty);
address.lastName = (__bridge_transfer NSString *)ABRecordCopyValue(record, kABPersonLastNameProperty); self.lastName = (__bridge_transfer NSString *)ABRecordCopyValue(record, kABPersonLastNameProperty);
//Grab the address information //Grab the address information
ABMultiValueRef addressMultiValue = ABRecordCopyValue(record, kABPersonAddressProperty); ABMultiValueRef addressMultiValue = ABRecordCopyValue(record, kABPersonAddressProperty);
...@@ -152,17 +149,17 @@ ...@@ -152,17 +149,17 @@
CFDictionaryRef firstAddress = CFArrayGetValueAtIndex(allAddresses, 0); CFDictionaryRef firstAddress = CFArrayGetValueAtIndex(allAddresses, 0);
//NOTE: We do not receive an address1 line right now via this partial address, as Apple deemds it unimportant to calculate the shipping rates. We get the actual address later on in a later step. //NOTE: We do not receive an address1 line right now via this partial address, as Apple deemds it unimportant to calculate the shipping rates. We get the actual address later on in a later step.
address.address1 = (__bridge NSString *)CFDictionaryGetValue(firstAddress, kABPersonAddressStreetKey); self.address1 = (__bridge NSString *)CFDictionaryGetValue(firstAddress, kABPersonAddressStreetKey);
address.city = (__bridge NSString *)CFDictionaryGetValue(firstAddress, kABPersonAddressCityKey); self.city = (__bridge NSString *)CFDictionaryGetValue(firstAddress, kABPersonAddressCityKey);
address.province = (__bridge NSString *)CFDictionaryGetValue(firstAddress, kABPersonAddressStateKey); self.province = (__bridge NSString *)CFDictionaryGetValue(firstAddress, kABPersonAddressStateKey);
address.zip = (__bridge NSString *)CFDictionaryGetValue(firstAddress, kABPersonAddressZIPKey); self.zip = (__bridge NSString *)CFDictionaryGetValue(firstAddress, kABPersonAddressZIPKey);
// The Checkout API accepts country OR ISO country code. // The Checkout API accepts country OR ISO country code.
// We default to the ISO country code because it's more // We default to the ISO country code because it's more
// reliable regardless of locale. Fallback to country if // reliable regardless of locale. Fallback to country if
// we do not receive it (iOS 8 sometimes) // we do not receive it (iOS 8 sometimes)
address.countryCode = (__bridge NSString *)CFDictionaryGetValue(firstAddress, kABPersonAddressCountryCodeKey); self.countryCode = (__bridge NSString *)CFDictionaryGetValue(firstAddress, kABPersonAddressCountryCodeKey);
if ([address.countryCode length] == 0) { if ([self.countryCode length] == 0) {
address.country = (__bridge NSString *)CFDictionaryGetValue(firstAddress, kABPersonAddressCountryKey); self.country = (__bridge NSString *)CFDictionaryGetValue(firstAddress, kABPersonAddressCountryKey);
} }
} }
CFSafeRelease(allAddresses); CFSafeRelease(allAddresses);
...@@ -172,43 +169,37 @@ ...@@ -172,43 +169,37 @@
ABMultiValueRef phoneMultiValue = ABRecordCopyValue(record, kABPersonPhoneProperty); ABMultiValueRef phoneMultiValue = ABRecordCopyValue(record, kABPersonPhoneProperty);
CFArrayRef allPhoneNumbers = ABMultiValueCopyArrayOfAllValues(phoneMultiValue); CFArrayRef allPhoneNumbers = ABMultiValueCopyArrayOfAllValues(phoneMultiValue);
if (allPhoneNumbers && CFArrayGetCount(allPhoneNumbers) > 0) { if (allPhoneNumbers && CFArrayGetCount(allPhoneNumbers) > 0) {
address.phone = (__bridge NSString *)CFArrayGetValueAtIndex(allPhoneNumbers, 0); self.phone = (__bridge NSString *)CFArrayGetValueAtIndex(allPhoneNumbers, 0);
} }
CFSafeRelease(phoneMultiValue); CFSafeRelease(phoneMultiValue);
CFSafeRelease(allPhoneNumbers); CFSafeRelease(allPhoneNumbers);
return address;
} }
+ (nonnull BUYAddress *)buy_addressFromContact:(nullable PKContact*)contact - (void)updateWithContact:(nullable PKContact*)contact
{ {
BUYAddress *address = [[BUYAddress alloc] init]; self.firstName = contact.name.givenName;
self.lastName = contact.name.familyName;
address.firstName = contact.name.givenName;
address.lastName = contact.name.familyName;
if (contact.postalAddress) { if (contact.postalAddress) {
// break up the address: // break up the address:
NSArray *addressComponents = [contact.postalAddress.street componentsSeparatedByString:@"\n"]; NSArray *addressComponents = [contact.postalAddress.street componentsSeparatedByString:@"\n"];
address.address1 = addressComponents[0]; self.address1 = addressComponents[0];
address.address2 = (addressComponents.count > 1) ? addressComponents[1] : nil; self.address2 = (addressComponents.count > 1) ? addressComponents[1] : nil;
address.city = contact.postalAddress.city; self.city = contact.postalAddress.city;
address.province = contact.postalAddress.state; self.province = contact.postalAddress.state;
address.zip = contact.postalAddress.postalCode; self.zip = contact.postalAddress.postalCode;
// The Checkout API accepts country OR ISO country code. // The Checkout API accepts country OR ISO country code.
// We default to the ISO country code because it's more // We default to the ISO country code because it's more
// reliable regardless of locale. Fallback to country if // reliable regardless of locale. Fallback to country if
// we do not receive it (iOS 8 sometimes) // we do not receive it (iOS 8 sometimes)
address.countryCode = [contact.postalAddress.ISOCountryCode length] ? contact.postalAddress.ISOCountryCode : nil; self.countryCode = [contact.postalAddress.ISOCountryCode length] ? contact.postalAddress.ISOCountryCode : nil;
if (address.countryCode == nil) { if (self.countryCode == nil) {
address.country = contact.postalAddress.country; self.country = contact.postalAddress.country;
} }
} }
address.phone = contact.phoneNumber.stringValue; self.phone = contact.phoneNumber.stringValue;
return address;
} }
@end @end
...@@ -27,6 +27,9 @@ ...@@ -27,6 +27,9 @@
@import Foundation; @import Foundation;
@import PassKit; @import PassKit;
#import <Buy/BUYModelManager.h>
@class BUYAddress;
@class BUYClient; @class BUYClient;
@class BUYCheckout; @class BUYCheckout;
@class BUYShop; @class BUYShop;
...@@ -60,7 +63,7 @@ ...@@ -60,7 +63,7 @@
* @param payment the authorized payment * @param payment the authorized payment
* @param completion completion block thats called after Shopify authorizes the payment * @param completion completion block thats called after Shopify authorizes the payment
*/ */
- (void)updateAndCompleteCheckoutWithPayment:(PKPayment *)payment completion:(void (^)(PKPaymentAuthorizationStatus status))completion DEPRECATED_MSG_ATTRIBUTE("BUYApplePayHelpers now implements PKPaymentAuthorizationViewControllerDelegate instead"); - (void)updateAndCompleteCheckoutWithPayment:(PKPayment *)payment completion:(void (^)(PKPaymentAuthorizationStatus status))completion NS_DEPRECATED_IOS(8_0, 9_0, "Use `PKPaymentAuthorizationViewControllerDelegate` instead");
/** /**
* Call this method in the PKPaymentAuthorizationViewControllerDelegate `paymentAuthorizationViewController:didSelectShippingMethod:completion` * Call this method in the PKPaymentAuthorizationViewControllerDelegate `paymentAuthorizationViewController:didSelectShippingMethod:completion`
...@@ -68,7 +71,7 @@ ...@@ -68,7 +71,7 @@
* @param shippingMethod The selected shipping method * @param shippingMethod The selected shipping method
* @param completion the completion block called after shipping method is updated on the checkout * @param completion the completion block called after shipping method is updated on the checkout
*/ */
- (void)updateCheckoutWithShippingMethod:(PKShippingMethod *)shippingMethod completion:(void (^)(PKPaymentAuthorizationStatus status, NSArray *methods))completion DEPRECATED_MSG_ATTRIBUTE("BUYApplePayHelpers now implements PKPaymentAuthorizationViewControllerDelegate instead"); - (void)updateCheckoutWithShippingMethod:(PKShippingMethod *)shippingMethod completion:(void (^)(PKPaymentAuthorizationStatus status, NSArray *methods))completion NS_DEPRECATED_IOS(8_0, 9_0, "Use `PKPaymentAuthorizationViewControllerDelegate` instead");
/** /**
* Call this method in the PKPaymentAuthorizationViewControllerDelegate `paymentAuthorizationViewController:didSelectShippingAddress:completion` * Call this method in the PKPaymentAuthorizationViewControllerDelegate `paymentAuthorizationViewController:didSelectShippingAddress:completion`
...@@ -84,7 +87,7 @@ ...@@ -84,7 +87,7 @@
* @param contact The selected contact * @param contact The selected contact
* @param completion the completion block called after the shipping address is updated on the checkout * @param completion the completion block called after the shipping address is updated on the checkout
*/ */
- (void)updateCheckoutWithContact:(PKContact*)contact completion:(void (^)(PKPaymentAuthorizationStatus, NSArray *shippingMethods, NSArray *summaryItems))completion DEPRECATED_MSG_ATTRIBUTE("BUYApplePayHelpers now implements PKPaymentAuthorizationViewControllerDelegate instead"); - (void)updateCheckoutWithContact:(PKContact*)contact completion:(void (^)(PKPaymentAuthorizationStatus, NSArray *shippingMethods, NSArray *summaryItems))completion NS_DEPRECATED_IOS(8_0, 9_0, "Use `PKPaymentAuthorizationViewControllerDelegate` instead");
/** /**
* The current checkout * The current checkout
...@@ -107,3 +110,25 @@ ...@@ -107,3 +110,25 @@
@property (nonatomic, strong, readonly) BUYShop *shop; @property (nonatomic, strong, readonly) BUYShop *shop;
@end @end
@interface BUYModelManager (ApplePay)
/**
* Creates a BUYAddress from an ABRecordRef
*
* @param record ABRecordRef to create a BUYAddress from
*
* @return The BUYAddress created from an ABRecordRef
*/
- (BUYAddress *)buyAddressWithABRecord:(ABRecordRef)addressRecord NS_DEPRECATED_IOS(8_0, 9_0, "Use the CNContact backed `buyAddressWithContact:` instead");
/**
* Creates a BUYAddress from a PKContact
*
* @param contact PKContact to create a BUYAddress from
*
* @return The BUYAddress created from a PKContact
*/
- (BUYAddress *)buyAddressWithContact:(PKContact *)contact NS_AVAILABLE_IOS(9_0);
@end
...@@ -25,11 +25,11 @@ ...@@ -25,11 +25,11 @@
// //
#import "BUYApplePayHelpers.h" #import "BUYApplePayHelpers.h"
#import "BUYApplePayAdditions.h"
#import "BUYClient.h" #import "BUYClient.h"
#import "BUYCheckout.h" #import "BUYCheckout.h"
#import "BUYApplePayAdditions.h"
#import "BUYError.h" #import "BUYError.h"
#import "BUYAddress+Additions.h" #import "BUYModelManager.h"
#import "BUYShop.h" #import "BUYShop.h"
const NSTimeInterval PollDelay = 0.5; const NSTimeInterval PollDelay = 0.5;
...@@ -89,18 +89,22 @@ const NSTimeInterval PollDelay = 0.5; ...@@ -89,18 +89,22 @@ const NSTimeInterval PollDelay = 0.5;
{ {
// Update the checkout with the rest of the information. Apple has now provided us with a FULL billing address and a FULL shipping address. // Update the checkout with the rest of the information. Apple has now provided us with a FULL billing address and a FULL shipping address.
// We now update the checkout with our new found data so that you can ship the products to the right address, and we collect whatever else we need. // We now update the checkout with our new found data so that you can ship the products to the right address, and we collect whatever else we need.
if ([payment respondsToSelector:@selector(shippingContact)]) { if ([payment respondsToSelector:@selector(shippingContact)]) {
self.checkout.email = payment.shippingContact.emailAddress; self.checkout.email = payment.shippingContact.emailAddress;
self.checkout.shippingAddress = self.checkout.requiresShipping ? [BUYAddress buy_addressFromContact:payment.shippingContact] : nil; if (self.checkout.requiresShipping) {
self.checkout.shippingAddress = [self buyAddressWithContact:payment.shippingContact];
}
} else { } else {
self.checkout.email = [BUYAddress buy_emailFromRecord:payment.shippingAddress]; self.checkout.email = [BUYAddress buy_emailFromRecord:payment.shippingAddress];
self.checkout.shippingAddress = self.checkout.requiresShipping ? [BUYAddress buy_addressFromRecord:payment.shippingAddress] : nil; if (self.checkout.requiresShipping) {
self.checkout.shippingAddress = [self buyAddressWithABRecord:payment.shippingAddress];
}
} }
if ([payment respondsToSelector:@selector(billingContact)]) { if ([payment respondsToSelector:@selector(billingContact)]) {
self.checkout.billingAddress = [BUYAddress buy_addressFromContact:payment.billingContact]; self.checkout.billingAddress = [self buyAddressWithContact:payment.billingContact];
} else { } else {
self.checkout.billingAddress = [BUYAddress buy_addressFromRecord:payment.billingAddress]; self.checkout.billingAddress = [self buyAddressWithABRecord:payment.billingAddress];
} }
[self.client updateCheckout:self.checkout completion:^(BUYCheckout *checkout, NSError *error) { [self.client updateCheckout:self.checkout completion:^(BUYCheckout *checkout, NSError *error) {
...@@ -127,6 +131,15 @@ const NSTimeInterval PollDelay = 0.5; ...@@ -127,6 +131,15 @@ const NSTimeInterval PollDelay = 0.5;
}]; }];
} }
- (BUYAddress *)buyAddressWithABRecord:(ABRecordRef)addressRecord
{
return [self.client.modelManager buyAddressWithABRecord:addressRecord];
}
- (BUYAddress *)buyAddressWithContact:(PKContact *)contact
{
return [self.client.modelManager buyAddressWithContact:contact];
}
- (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller - (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller
{ {
...@@ -135,13 +148,13 @@ const NSTimeInterval PollDelay = 0.5; ...@@ -135,13 +148,13 @@ const NSTimeInterval PollDelay = 0.5;
-(void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didSelectShippingAddress:(ABRecordRef)address completion:(void (^)(PKPaymentAuthorizationStatus, NSArray<PKShippingMethod *> * _Nonnull, NSArray<PKPaymentSummaryItem *> * _Nonnull))completion -(void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didSelectShippingAddress:(ABRecordRef)address completion:(void (^)(PKPaymentAuthorizationStatus, NSArray<PKShippingMethod *> * _Nonnull, NSArray<PKPaymentSummaryItem *> * _Nonnull))completion
{ {
self.checkout.shippingAddress = [BUYAddress buy_addressFromRecord:address]; self.checkout.shippingAddress = [self buyAddressWithABRecord:address];
[self updateCheckoutWithAddressCompletion:completion]; [self updateCheckoutWithAddressCompletion:completion];
} }
-(void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didSelectShippingContact:(PKContact *)contact completion:(void (^)(PKPaymentAuthorizationStatus, NSArray<PKShippingMethod *> * _Nonnull, NSArray<PKPaymentSummaryItem *> * _Nonnull))completion -(void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didSelectShippingContact:(PKContact *)contact completion:(void (^)(PKPaymentAuthorizationStatus, NSArray<PKShippingMethod *> * _Nonnull, NSArray<PKPaymentSummaryItem *> * _Nonnull))completion
{ {
self.checkout.shippingAddress = [BUYAddress buy_addressFromContact:contact]; self.checkout.shippingAddress = [self buyAddressWithContact:contact];
[self updateCheckoutWithAddressCompletion:completion]; [self updateCheckoutWithAddressCompletion:completion];
} }
...@@ -169,7 +182,7 @@ const NSTimeInterval PollDelay = 0.5; ...@@ -169,7 +182,7 @@ const NSTimeInterval PollDelay = 0.5;
// However, to ensure we never set partialAddresses to NO, we want to guard the setter. Should PKPaymentAuthorizationViewController ever // However, to ensure we never set partialAddresses to NO, we want to guard the setter. Should PKPaymentAuthorizationViewController ever
// return a full address through it's delegate method, this will still function since a complete address can be used to calculate shipping rates // return a full address through it's delegate method, this will still function since a complete address can be used to calculate shipping rates
if ([self.checkout.shippingAddress isPartialAddress] == YES) { if ([self.checkout.shippingAddress isPartialAddress] == YES) {
self.checkout.partialAddresses = YES; self.checkout.partialAddresses = @YES;
} }
if ([self.checkout.shippingAddress isValidAddressForShippingRates]) { if ([self.checkout.shippingAddress isValidAddressForShippingRates]) {
...@@ -338,3 +351,22 @@ const NSTimeInterval PollDelay = 0.5; ...@@ -338,3 +351,22 @@ const NSTimeInterval PollDelay = 0.5;
} }
@end @end
@implementation BUYModelManager (ApplePay)
- (BUYAddress *)buyAddressWithABRecord:(ABRecordRef)addressRecord
{
BUYAddress *address = [self insertAddressWithJSONDictionary:nil];
[address updateWithRecord:addressRecord];
return address;
}
- (BUYAddress *)buyAddressWithContact:(PKContact *)contact
{
BUYAddress *address = [self insertAddressWithJSONDictionary:nil];
[address updateWithContact:contact];
return address;
}
@end
\ No newline at end of file
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