Commit f1aebcaf by Brent Gulanowski

Update all models to derive from generated classes.

parent eab091af
......@@ -28,11 +28,8 @@
@import PassKit;
@import UIKit;
@import XCTest;
#import <Buy/Buy.h>
#import "BUYAddress+Additions.h"
#import "BUYCheckout_Private.h"
#import "NSDecimalNumber+BUYAdditions.h"
#import "NSDateFormatter+BUYAdditions.h"
#import "BUYPKContact.h"
#import "BUYNSPersonNameComponents.h"
......@@ -44,13 +41,20 @@
@implementation BUYApplePayAdditionsTest {
BUYCheckout *_checkout;
BUYModelManager *_modelManager;
}
- (void)setUp
{
_modelManager = [BUYModelManager modelManager];
_checkout = [[BUYCheckout alloc] initWithCart:nil];
}
- (void)tearDown
{
_modelManager = nil;
}
#pragma mark - BUYCheckout Apple Pay additions
- (void)testSummaryItemsWithEmptyCheckout
......@@ -67,7 +71,7 @@
- (void)testFullSummaryItems
{
_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.paymentDue = [NSDecimalNumber decimalNumberWithString:@"4.00"];
......@@ -87,7 +91,7 @@
- (void)testSummaryItemsWithShippingRate
{
_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"];
NSArray *summaryItems = [_checkout buy_summaryItems];
......@@ -104,7 +108,7 @@
- (void)testSummaryItemsWithFreeShippingAndTaxesShouldNotShowShippingOrTaxes
{
_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.paymentDue = [NSDecimalNumber decimalNumberWithString:@"3.00"];
......@@ -120,13 +124,12 @@
- (void)testSummaryItemsWithZeroDiscount
{
_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.paymentDue = [NSDecimalNumber decimalNumberWithString:@"3.00"];
BUYDiscount *discount = [[BUYDiscount alloc] init];
discount.code = @"BANANA";
BUYDiscount *discount = [_modelManager discountWithCode:@"BANANA"];
discount.amount = [NSDecimalNumber zero];
discount.applicable = YES;
discount.applicableValue = YES;
_checkout.discount = discount;
NSArray *summaryItems = [_checkout buy_summaryItems];
......@@ -141,13 +144,12 @@
- (void)testSummaryItemsWithNonZeroDiscount
{
_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.paymentDue = [NSDecimalNumber decimalNumberWithString:@"2.00"];
BUYDiscount *discount = [[BUYDiscount alloc] init];
discount.code = @"BANANA";
BUYDiscount *discount = [_modelManager discountWithCode:@"BANANA"];
discount.amount = [NSDecimalNumber one];
discount.applicable = YES;
discount.applicableValue = YES;
_checkout.discount = discount;
NSArray *summaryItems = [_checkout buy_summaryItems];
......@@ -165,13 +167,12 @@
- (void)testSummaryItemsWithNonZeroCodelessDiscount
{
_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.paymentDue = [NSDecimalNumber decimalNumberWithString:@"2.00"];
BUYDiscount *discount = [[BUYDiscount alloc] init];
discount.code = @"";
BUYDiscount *discount = [_modelManager discountWithCode:@""];
discount.amount = [NSDecimalNumber one];
discount.applicable = YES;
discount.applicableValue = YES;
_checkout.discount = discount;
NSArray *summaryItems = [_checkout buy_summaryItems];
......@@ -189,17 +190,16 @@
- (void)testSummaryItemsWithGiftCard
{
_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.paymentDue = [NSDecimalNumber decimalNumberWithString:@"2.00"];
BUYDiscount *discount = [[BUYDiscount alloc] init];
discount.code = @"";
BUYDiscount *discount = [_modelManager discountWithCode:@""];
discount.amount = [NSDecimalNumber one];
discount.applicable = YES;
discount.applicableValue = YES;
_checkout.discount = discount;
BUYGiftCard *giftCard = [[BUYGiftCard alloc] initWithDictionary:@{ @"amount_used" : [NSDecimalNumber decimalNumberWithString:@"10.00"], @"balance" : [NSDecimalNumber decimalNumberWithString:@"10.00"], @"last_characters" : @"1234" }];
_checkout.giftCards = @[giftCard];
BUYGiftCard *giftCard = [[BUYGiftCard alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"amount_used" : [NSDecimalNumber decimalNumberWithString:@"10.00"], @"balance" : [NSDecimalNumber decimalNumberWithString:@"10.00"], @"last_characters" : @"1234" }];
[[_checkout giftCardsSet] addObject:giftCard];
NSArray *summaryItems = [_checkout buy_summaryItems];
XCTAssertEqual(5, [summaryItems count]);
......@@ -227,15 +227,15 @@
NSDate *lastDate = [self dateWithoutTime:[NSDate dateWithTimeIntervalSinceNow:day]];
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]];
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]];
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]];
XCTAssertEqual(3, [shippingMethods count]);
......@@ -353,7 +353,7 @@
ABRecordSetValue(person, kABPersonAddressProperty, addresses, &error);
CFRelease(addresses);
BUYAddress *newAddress = [BUYAddress buy_addressFromRecord:person];
BUYAddress *newAddress = [_modelManager buyAddressWithABRecord:person];
CFRelease(person);
......@@ -410,7 +410,7 @@
[postalAddress setISOCountryCode:@"CA"];
[contact setPostalAddress:postalAddress];
return [BUYAddress buy_addressFromContact:contact];
return [_modelManager buyAddressWithContact:contact];
}
- (void)testCompareAddressWithContactWithNameOrStreetOrPhone
......
......@@ -33,33 +33,32 @@
@implementation BUYCartTest {
BUYCart *_cart;
BUYModelManager *_modelManager;
}
- (void)setUp
{
[super setUp];
_cart = [[BUYCart alloc] init];
_modelManager = [BUYModelManager modelManager];
_cart = [_modelManager insertCartWithJSONDictionary:nil];
}
#pragma mark - Serialization Tests
- (void)testJsonDictionaryShouldBeEmptyWhenNothingIsSet
- (void)tearDown
{
NSDictionary *json = [_cart jsonDictionaryForCheckout];
XCTAssertNotNil(json);
XCTAssertEqual(0, [json count]);
_cart = nil;
_modelManager = nil;
}
#pragma mark - Serialization Tests
- (void)testCartShouldBeInvalidWhenEmpty
{
BUYCart *cart = [[BUYCart alloc] init];
XCTAssertFalse([cart isValid]);
XCTAssertFalse([_cart isValid]);
}
- (void)testAddVariantWillAddALineItem
{
BUYProductVariant *variant = [[BUYProductVariant alloc] initWithDictionary:@{ @"id" : @1 }];
BUYProductVariant *variant = [[BUYProductVariant alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"id" : @1 }];
[_cart addVariant:variant];
XCTAssertEqual([[_cart lineItems] count], 1);
XCTAssertEqualObjects([[_cart lineItems][0] variantId], variant.identifier);
......@@ -67,10 +66,10 @@
- (void)testAddingTwoDifferentVariantsWillAddDifferentLineItems
{
BUYProductVariant *variant = [[BUYProductVariant alloc] initWithDictionary:@{ @"id" : @1 }];
BUYProductVariant *variant = [[BUYProductVariant alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"id" : @1 }];
[_cart addVariant:variant];
BUYProductVariant *variant2 = [[BUYProductVariant alloc] initWithDictionary:@{ @"id" : @2 }];
BUYProductVariant *variant2 = [[BUYProductVariant alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"id" : @2 }];
[_cart addVariant:variant2];
XCTAssertEqual([[_cart lineItems] count], 2);
......@@ -78,7 +77,7 @@
- (void)testAddingAVariantOfTheSameTypeWillNotAddAnotherLineItem
{
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);
......@@ -88,7 +87,7 @@
- (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 removeVariant:variant];
......@@ -97,7 +96,7 @@
- (void)testRemovingAllVariantsOfASingleTypeRemovesItsLineItem
{
BUYProductVariant *variant = [[BUYProductVariant alloc] initWithDictionary:@{ @"id" : @1 }];
BUYProductVariant *variant = [[BUYProductVariant alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"id" : @1 }];
[_cart addVariant:variant];
[_cart removeVariant:variant];
XCTAssertEqual([[_cart lineItems] count], 0);
......@@ -105,8 +104,8 @@
- (void)testSetVariantWithQuantity
{
BUYProductVariant *variant = [[BUYProductVariant alloc] initWithDictionary:@{ @"id" : @1 }];
BUYProductVariant *variantTwo = [[BUYProductVariant alloc] initWithDictionary:@{ @"id" : @2 }];
BUYProductVariant *variant = [[BUYProductVariant alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"id" : @1 }];
BUYProductVariant *variantTwo = [[BUYProductVariant alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"id" : @2 }];
[_cart setVariant:variant withTotalQuantity:2];
XCTAssertEqual([[_cart lineItems] count], 1);
......
......@@ -26,12 +26,15 @@
@import UIKit;
@import XCTest;
#import <Buy/Buy.h>
#import "BUYCheckout_Private.h"
#import "BUYCheckout.h"
@interface BUYCheckoutTest : XCTestCase
@end
@implementation BUYCheckoutTest {
BUYModelManager *_modelManager;
BUYCheckout *_checkout;
BUYCart *_cart;
BUYProductVariant *_variant;
......@@ -41,27 +44,28 @@
- (void)setUp
{
[super setUp];
_cart = [[BUYCart alloc] init];
_checkout = [[BUYCheckout alloc] initWithCart:_cart];
_variant = [[BUYProductVariant alloc] initWithDictionary:@{ @"id" : @1 }];
_modelManager = [BUYModelManager modelManager];
_cart = [_modelManager insertCartWithJSONDictionary:nil];
_checkout = [_modelManager checkoutWithCart:_cart];
_variant = [[BUYProductVariant alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"id" : @1 }];
_discountDictionary = @{ @"code" : @"abcd1234", @"amount" : @"5.00", @"applicable" : @true };
}
- (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);
}
- (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);
}
- (void)testOrderStatusDeserializationWithNoURL
{
BUYCheckout *checkout = [[BUYCheckout alloc] initWithDictionary:@{}];
BUYCheckout *checkout = [[BUYCheckout alloc] initWithModelManager:_modelManager JSONDictionary:@{}];
XCTAssertNil(checkout.order.statusURL);
}
......@@ -73,25 +77,47 @@
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
{
BUYShippingRate *shippingRate = [[BUYShippingRate alloc] initWithDictionary:@{ @"id" : @"banana" }];
BUYShippingRate *shippingRate = [[BUYShippingRate alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"id" : @"banana" }];
[_checkout markAsClean];
_checkout.shippingRate = shippingRate;
_checkout.currency = @"BANANA";
NSSet *dirtyProperties = [_checkout dirtyProperties];
XCTAssertTrue([dirtyProperties containsObject:@"currency"]);
XCTAssertTrue([dirtyProperties containsObject:@"shippingRateId"]);
XCTAssertTrue([dirtyProperties containsObject:@"shippingRate"]);
NSDictionary *json = [_checkout jsonDictionaryForCheckout];
XCTAssertEqualObjects(json[@"checkout"][@"currency"], @"BANANA");
XCTAssertEqualObjects(json[@"checkout"][@"shipping_rate_id"], @"banana");
}
- (void)testRequiresShippingAndIncludesTaxesSerialization
{
_checkout.requiresShipping = YES;
_checkout.taxesIncluded = YES;
_checkout.requiresShippingValue = YES;
_checkout.includesTaxesValue = YES;
NSDictionary *jsonDictionary = [_checkout jsonDictionaryForCheckout][@"checkout"];
XCTAssertEqualObjects(@YES, jsonDictionary[@"requires_shipping"]);
XCTAssertEqualObjects(@YES, jsonDictionary[@"taxes_included"]);
......@@ -99,16 +125,16 @@
- (void)testDiscountDeserialization
{
BUYDiscount *discount = [[BUYDiscount alloc] initWithDictionary: _discountDictionary];
BUYDiscount *discount = [[BUYDiscount alloc] initWithModelManager:_modelManager JSONDictionary: _discountDictionary];
XCTAssertEqualObjects(@"abcd1234", discount.code);
XCTAssertEqualObjects(@5.00, discount.amount);
XCTAssertEqual(true, discount.applicable);
XCTAssertEqual(true, discount.applicableValue);
}
- (void)testDiscountSerialization
{
NSDictionary *jsonDict = @{ @"code": @"abcd1234" };
BUYDiscount *discount = [[BUYDiscount alloc] initWithDictionary:_discountDictionary];
BUYDiscount *discount = [[BUYDiscount alloc] initWithModelManager:_modelManager JSONDictionary:_discountDictionary];
XCTAssertEqualObjects(jsonDict, [discount jsonDictionaryForCheckout]);
}
......@@ -125,21 +151,21 @@
- (void)testEmptyCheckoutsDoNotRequireShipping
{
_checkout = [[BUYCheckout alloc] initWithDictionary:@{}];
_checkout = [[BUYCheckout alloc] initWithModelManager:_modelManager JSONDictionary:@{}];
XCTAssertFalse([_checkout requiresShipping]);
}
- (void)testCheckoutsWithoutItemsThatRequireShipping
{
_checkout = [[BUYCheckout alloc] initWithDictionary:@{ @"requires_shipping" : @1 }];
_checkout = [[BUYCheckout alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"requires_shipping" : @1 }];
XCTAssertTrue([_checkout requiresShipping]);
}
- (void)testTaxLineDeserialization
{
BUYTaxLine *taxLine = [[BUYTaxLine alloc] initWithDictionary:@{@"price": @"0.29",
@"rate": @"0.13",
@"title": @"HST"}];
BUYTaxLine *taxLine = [[BUYTaxLine alloc] initWithModelManager:_modelManager JSONDictionary:@{@"price": @"0.29",
@"rate": @"0.13",
@"title": @"HST"}];
XCTAssertEqualObjects(@0.29, taxLine.price);
XCTAssertEqualObjects(@0.13, taxLine.rate);
XCTAssertEqualObjects(@"HST", taxLine.title);
......
......@@ -29,9 +29,7 @@
#import <Buy/Buy.h>
#import "BUYTestConstants.h"
#import "BUYAddress+Additions.h"
#import "BUYClientTestBase.h"
#import "BUYCollection+Additions.h"
#import "NSURLComponents+BUYAdditions.h"
@interface BUYClient ()
......@@ -69,7 +67,7 @@
- (NSData *)dataForCartFromClient:(BUYClient *)client
{
BUYCart *cart = [[BUYCart alloc] init];
BUYCart *cart = [self cart];
BUYCheckout *checkout = [[BUYCheckout alloc] initWithCart:cart];
NSURLSessionDataTask *task = [self.client createCheckout:checkout completion:nil];
XCTAssertNotNil(task);
......@@ -98,10 +96,10 @@
- (void)testPartialAddressesFlag
{
BUYCart *cart = [[BUYCart alloc] init];
BUYCart *cart = [self cart];
BUYCheckout *checkout = [[BUYCheckout alloc] initWithCart:cart];
XCTAssertThrows([checkout setPartialAddresses:NO]);
XCTAssertThrows([checkout setPartialAddressesValue:NO]);
NSURLSessionDataTask *task = [self.client createCheckout:checkout completion:nil];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:task.originalRequest.HTTPBody options:0 error:nil];
......@@ -109,11 +107,11 @@
checkout = [[BUYCheckout alloc] initWithCart:cart];
BUYAddress *partialAddress = [[BUYAddress alloc] init];
BUYAddress *partialAddress = [self.client.modelManager insertAddressWithJSONDictionary:nil];
partialAddress.address1 = nil;
if ([partialAddress isPartialAddress]) {
checkout.partialAddresses = YES;
checkout.partialAddressesValue = YES;
}
checkout.shippingAddress = partialAddress;
......@@ -125,7 +123,7 @@
- (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];
XCTAssertNotNil(task);
......@@ -200,7 +198,7 @@
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) {
callbackCount++;
......@@ -318,4 +316,9 @@
XCTAssertEqualObjects(requestQueryItems, queryItems);
}
- (BUYCart *)cart
{
return [self.client.modelManager insertCartWithJSONDictionary:nil];
}
@end
......@@ -29,7 +29,6 @@
#import <Buy/Buy.h>
#import "BUYTestConstants.h"
#import "BUYCollection.h"
#import "NSDateFormatter+BUYAdditions.h"
#import "BUYClientTestBase.h"
#import <OHHTTPStubs/OHHTTPStubs.h>
#import "OHHTTPStubsResponse+Helpers.h"
......
......@@ -29,8 +29,7 @@
#import <Buy/Buy.h>
#import "BUYTestConstants.h"
#import "BUYAddress+Additions.h"
#import "BUYCheckout_Private.h"
#import "BUYCheckout.h"
#import "BUYClientTestBase.h"
#import <OHHTTPStubs/OHHTTPStubs.h>
#import "OHHTTPStubsResponse+Helpers.h"
......@@ -40,6 +39,8 @@
@implementation BUYIntegrationTest {
BUYModelManager *_modelManager;
NSMutableArray *_products;
BUYCart *_cart;
......@@ -52,6 +53,7 @@
{
[super setUp];
_modelManager = [BUYModelManager modelManager];
_products = [[NSMutableArray alloc] init];
[self fetchProducts];
......@@ -87,12 +89,12 @@
XCTAssertNil(error);
}];
NSLog(@"Fetched products (count: %ld", _products.count);
NSLog(@"Fetched products (count: %tu", _products.count);
}
- (void)createCart
{
_cart = [[BUYCart alloc] init];
_cart = [_modelManager insertCartWithJSONDictionary:nil];
for (BUYProduct *product in _products) {
[_cart addVariant:product.variants[0]];
}
......@@ -132,20 +134,22 @@
return [OHHTTPStubsResponse responseWithKey:@"testCheckoutFlowUsingCreditCard_1"];
}];
[self createCart];
_checkout = [[BUYCheckout alloc] initWithCart:_cart];
NSString *note = @"Order note";
_checkout.note = note;
NSArray *attributes = @[ [[BUYCheckoutAttribute alloc] initWithDictionary:@{ @"name" : @"attribute1", @"value" : @"value1" }], [[BUYCheckoutAttribute alloc] initWithDictionary:@{ @"name" : @"attribute2", @"value" : @"value2" }] ];
_checkout.attributes = attributes;
NSArray *attributes = @[ [[BUYCheckoutAttribute alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"name" : @"attribute1", @"value" : @"value1" }],
[[BUYCheckoutAttribute alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"name" : @"attribute2", @"value" : @"value2" }] ];
_checkout.attributes = [NSSet setWithArray:attributes];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client createCheckout:_checkout completion:^(BUYCheckout *returnedCheckout, NSError *error) {
XCTAssertNil(error);
XCTAssertNotNil(returnedCheckout);
XCTAssertEqualObjects(returnedCheckout.note, note);
XCTAssertEqualObjects(returnedCheckout.attributes, attributes);
XCTAssertEqualObjects(returnedCheckout.attributes, _checkout.attributes);
_checkout = returnedCheckout;
[expectation fulfill];
......@@ -509,7 +513,7 @@
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)];
[self.client removeGiftCard:giftCard fromCheckout:_checkout completion:^(BUYCheckout *checkout, NSError *error) {
XCTAssertNotNil(error);
......@@ -537,7 +541,7 @@
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)];
[self.client removeGiftCard:giftCard fromCheckout:_checkout completion:^(BUYCheckout *checkout, NSError *error) {
XCTAssertNotNil(error);
......@@ -998,7 +1002,7 @@
_checkout.shippingAddress = [self partialShippingAddress];
if ([_checkout.shippingAddress isPartialAddress]) {
_checkout.partialAddresses = YES;
_checkout.partialAddressesValue = YES;
}
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
......@@ -1181,7 +1185,7 @@
- (BUYAddress *)billingAddress
{
BUYAddress *address = [[BUYAddress alloc] init];
BUYAddress *address = [_modelManager insertAddressWithJSONDictionary:nil];
address.address1 = @"150 Elgin Street";
address.address2 = @"8th Floor";
address.city = @"Ottawa";
......@@ -1197,7 +1201,7 @@
- (BUYAddress *)shippingAddress
{
BUYAddress *address = [[BUYAddress alloc] init];
BUYAddress *address = [_modelManager insertAddressWithJSONDictionary:nil];
address.address1 = @"150 Elgin Street";
address.address2 = @"8th Floor";
address.city = @"Ottawa";
......@@ -1213,7 +1217,7 @@
- (BUYAddress *)partialShippingAddress
{
BUYAddress *address = [[BUYAddress alloc] init];
BUYAddress *address = [_modelManager insertAddressWithJSONDictionary:nil];
address.address1 = nil;
address.city = @"Ottawa";
address.firstName = nil;
......@@ -1227,20 +1231,17 @@
- (BUYDiscount *)applicableDiscount
{
BUYDiscount *discount = [[BUYDiscount alloc] initWithCode:self.discountCodeValid];
return discount;
return [_modelManager discountWithCode:self.discountCodeValid];
}
- (BUYDiscount *)inapplicableDiscount
{
BUYDiscount *discount = [[BUYDiscount alloc] initWithCode:self.discountCodeExpired];
return discount;
return [_modelManager discountWithCode:self.discountCodeExpired];
}
- (BUYDiscount *)nonExistentDiscount
{
BUYDiscount *discount = [[BUYDiscount alloc] initWithCode:@"asdfasdfasdfasdf"];
return discount;
return [_modelManager discountWithCode:@"asdfasdfasdfasdf"];
}
- (void)testExpiringCheckout
......@@ -1248,6 +1249,8 @@
[self createCart];
[self createCheckout];
XCTAssertGreaterThanOrEqual([_checkout.lineItems count], 1);
XCTAssertNotNil(_checkout.reservationTime);
XCTAssertTrue([_checkout.reservationTime isKindOfClass:[NSNumber class]]);
XCTAssertEqual(300, _checkout.reservationTime.intValue);
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
......
......@@ -32,25 +32,30 @@
@end
@implementation BUYLineItemTest {
BUYProductVariant *_variant;
BUYLineItem *_lineItem;
BUYModelManager *_modelManager;
}
- (void)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)tearDown
{
_modelManager = nil;
}
- (void)testInitRespectsVariantShippingFlag
{
XCTAssertFalse([[_lineItem requiresShipping] boolValue]);
BUYProductVariant *variant = [[BUYProductVariant alloc] initWithDictionary:@{ @"id" : @1, @"requires_shipping" : @YES }];
_lineItem = [[BUYLineItem alloc] initWithVariant:variant];
XCTAssertTrue([[_lineItem requiresShipping] boolValue]);
BUYLineItem *newLineItem = [[BUYLineItem alloc] initWithVariant:variant];
XCTAssertTrue([[newLineItem requiresShipping] boolValue]);
_variant.requiresShipping = @NO;
XCTAssertTrue([[_lineItem requiresShipping] boolValue]);
}
#pragma mark - Serialization Tests
......@@ -59,13 +64,13 @@
{
NSDictionary *json = [_lineItem jsonDictionaryForCheckout];
XCTAssertNotNil(json);
XCTAssertEqualObjects([NSDecimalNumber zero], json[@"price"]);
XCTAssertEqualObjects([NSDecimalNumber zero], json[@"quantity"]);
XCTAssertEqualObjects(@"0", json[@"price"]);
XCTAssertEqualObjects(@"1", json[@"quantity"]);
}
- (void)testJsonDictionaryDoesntIncludeVariantsWithoutIds
{
_lineItem = [[BUYLineItem alloc] initWithVariant:nil];
_lineItem = [_modelManager lineItemWithVariant:nil];
NSDictionary *json = [_lineItem jsonDictionaryForCheckout];
XCTAssertNotNil(json);
XCTAssertNil(json[@"variant_id"]);
......@@ -73,7 +78,7 @@
- (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.quantity = [NSDecimalNumber decimalNumberWithString:@"3"];
_lineItem.price = [NSDecimalNumber decimalNumberWithString:@"5.55"];
......@@ -81,17 +86,15 @@
NSDictionary *json = [_lineItem jsonDictionaryForCheckout];
XCTAssertEqualObjects(@5, json[@"variant_id"]);
XCTAssertEqualObjects([NSDecimalNumber decimalNumberWithString:@"3"], json[@"quantity"]);
XCTAssertEqualObjects([NSDecimalNumber decimalNumberWithString:@"5.55"], json[@"price"]);
XCTAssertEqualObjects(@"3", json[@"quantity"]);
XCTAssertEqualObjects(@"5.55", json[@"price"]);
XCTAssertEqualObjects(@"banana", json[@"title"]);
}
- (void)testUpdatingFromJsonShouldUpdateAllValues
{
XCTAssertFalse([[_lineItem requiresShipping] boolValue]);
BUYLineItem *lineItem = [[BUYLineItem alloc] initWithDictionary:@{ @"id" : @5, @"price" : @"5.99", @"quantity" : @5, @"requires_shipping" : @YES, @"title" : @"banana" }];
XCTAssertEqualObjects(@5, lineItem.lineItemIdentifier);
BUYLineItem *lineItem = [[BUYLineItem alloc] initWithModelManager:[BUYModelManager modelManager] JSONDictionary:@{ @"id" : @"5", @"price" : @"5.99", @"quantity" : @5, @"requires_shipping" : @YES, @"title" : @"banana" }];
XCTAssertEqualObjects(@"5", lineItem.identifier);
XCTAssertEqualObjects([NSDecimalNumber decimalNumberWithString:@"5.99"], lineItem.price);
XCTAssertEqualObjects([NSDecimalNumber decimalNumberWithString:@"5"], lineItem.quantity);
XCTAssertEqualObjects(@"banana", lineItem.title);
......
......@@ -50,77 +50,33 @@
@property (nonatomic, readonly) NSArray *array;
@property (nonatomic, readonly) NSMutableArray *mutableArray;
- (NSArray *)propertyNames;
@end
@interface BUYObjectSubclass : BUYObject
@property (nonatomic, strong) NSNumber *identifier;
@end
@interface BUYObjectTests : XCTestCase
@property (nonatomic, strong) BUYModelManager *modelManager;
@end
@implementation BUYObjectTests
- (void)testInitWithDictionaryParsesIdentifier
{
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]);
}
@interface BUYModelManager (BUYDirtyTracked)
+ (BUYModelManager *)testModelManager;
@end
- (void)testConvertJSONArrayCreatesObjectOfClass
{
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]);
}
@implementation BUYObjectTests
- (void)testConvertJSONArrayWithCreatedBlock
- (void)setUp
{
NSArray *json = @[@{ @"id" : @5 }, @{ @"id" : @6 }, @{ @"id" : @7 }];
__block NSUInteger numberOfInvokes = 0;
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);
[super setUp];
self.modelManager = [BUYModelManager testModelManager];
}
- (void)testDirtyTracking
{
BUYDirtyTracked *object = [[BUYDirtyTracked alloc] init];
BUYDirtyTracked *object = [self dirtyTrackedObject];
object.s = @"short property name test";
object.dirtyObjectValue = @"Banana";
object.dirtyBooleanValue = true;
......@@ -136,19 +92,14 @@
object.dirtyUnsignedLongLongValue = 1234123412341234;
object.dirtyFloatValue = 0.5f;
object.dirtyDoubleValue = 0.5;
NSSet *expected = [NSSet setWithArray:@[@"s", @"dirtyObjectValue", @"dirtyBooleanValue", @"dirtyCharacterValue",
@"dirtyUnsignedCharValue", @"dirtyIntegerValue", @"dirtyUnsignedIntegerValue",
@"dirtyShortValue", @"dirtyUnsignedShortValue", @"dirtyLongValue",
@"dirtyUnsignedLongValue", @"dirtyLongLongValue", @"dirtyUnsignedLongLongValue",
@"dirtyFloatValue", @"dirtyDoubleValue"]];
NSSet *expected = [NSSet setWithArray:[object propertyNames]];
NSSet *actual = [object dirtyProperties];
XCTAssert([expected isEqual:actual]);
XCTAssertEqual([actual count], [expected count]);
XCTAssertEqualObjects(actual, expected);
}
- (void)testIsDirtyReturnsFalseWhenClean
{
BUYDirtyTracked *object = [[BUYDirtyTracked alloc] init];
BUYDirtyTracked *object = [self dirtyTrackedObject];
XCTAssert([object isDirty] == NO);
object.dirtyObjectValue = @"Banana";
[object markAsClean];
......@@ -157,18 +108,23 @@
- (void)testIsDirtyReturnsTrueWhenDirty
{
BUYDirtyTracked *object = [[BUYDirtyTracked alloc] init];
BUYDirtyTracked *object = [self dirtyTrackedObject];
object.dirtyObjectValue = @"Banana";
XCTAssert([object isDirty]);
}
- (void)testMarkAsClean
{
BUYDirtyTracked *object = [[BUYDirtyTracked alloc] init];
BUYDirtyTracked *object = [self dirtyTrackedObject];
object.dirtyObjectValue = @"Banana";
XCTAssert([object dirtyProperties]);
}
- (BUYDirtyTracked *)dirtyTrackedObject
{
return [[BUYDirtyTracked alloc] initWithModelManager:nil JSONDictionary:nil];
}
@end
#pragma mark - Helper Impls
......@@ -180,7 +136,44 @@
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
@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
......@@ -39,7 +39,6 @@ extern NSString * const BUYJSONPropertyKeyUserInfoKey; // = @"JSONPropertyK
* 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.
*
* Currently only two transformers are supported: "BUYPublicationsDate" and "BUYShippingRateDate".
* Currently, only attributes (instances of NSAttributeDescription) use value transformers.
*/
@property (nonatomic, readonly, getter=buy_JSONValueTransformerName) NSString *JSONValueTransformerName;
......
......@@ -46,6 +46,8 @@ NSString * const BUYJSONPropertyKeyUserInfoKey = @"JSONPropertyKey";
// This is defined by mogenerator
static NSString * const BUYAttributeValueClassNameKey = @"attributeValueClassName";
static NSString * const BUYDateFormat = @"yyyy-MM-dd'T'HH:mm:ssZ";
#pragma mark -
@interface NSObject (BUYValueTransforming)
......@@ -69,6 +71,7 @@ static NSString * const BUYAttributeValueClassNameKey = @"attributeValueClassNam
// value type transformers
[NSValueTransformer setValueTransformer:[[BUYURLTransformer alloc] init] forName:BUYURLTransformerName];
[NSValueTransformer setValueTransformer:[BUYDateTransformer dateTransformerWithFormat:BUYDateFormat] forName:BUYDateTransformerName];
});
return self.userInfo[BUYJSONValueTransformerUserInfoKey];
}
......
......@@ -68,3 +68,16 @@ FOUNDATION_EXPORT const unsigned char BuyVersionString[];
#import <Buy/BUYStoreViewController.h>
#import <Buy/BUYTheme.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 @@
//
@import Foundation;
#import "BUYSerializable.h"
@class PKPaymentToken;
@class BUYAccountCredentials;
@class BUYCart;
@class BUYCheckout;
@class BUYCreditCard;
......@@ -37,6 +37,8 @@
@class BUYProductVariant;
@class BUYShop;
@class BUYCollection;
@class BUYOrder;
@class BUYModelManager;
/**
* The sort order for products in a collection
......@@ -176,12 +178,10 @@ typedef void (^BUYDataProductBlock)(BUYProduct *product, 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 page Index of the page requested
* @param reachedEnd Boolean indicating whether additional pages exist
* @param error An optional NSError
* @param error Optional NSError
*/
typedef void (^BUYDataCollectionsListBlock)(NSArray<BUYCollection *> *collections, NSUInteger page, BOOL reachedEnd, 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;
/**
*
*/
@property (nonatomic, strong) BUYModelManager *modelManager;
/**
* Queue where callbacks will be called
* defaults to main queue
*/
......@@ -260,7 +265,7 @@ typedef void (^BUYDataGiftCardBlock)(BUYGiftCard *giftCard, NSError *error);
/**
* 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)
......@@ -515,7 +520,7 @@ typedef void (^BUYDataGiftCardBlock)(BUYGiftCard *giftCard, NSError *error);
*
* @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
......@@ -536,6 +541,6 @@ typedef void (^BUYDataGiftCardBlock)(BUYGiftCard *giftCard, NSError *error);
*
* @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
......@@ -24,20 +24,22 @@
// THE SOFTWARE.
//
#import "BUYClient_Internal.h"
#import "BUYAddress.h"
#import "BUYCart.h"
#import "BUYCheckout.h"
#import "BUYCheckout_Private.h"
#import "BUYCreditCard.h"
#import "BUYClient.h"
#import "BUYCollection.h"
#import "BUYCollection+Additions.h"
#import "BUYError.h"
#import "BUYGiftCard.h"
#import "BUYModelManager.h"
#import "BUYOrder.h"
#import "BUYProduct.h"
#import "BUYShippingRate.h"
#import "BUYShop.h"
#import "NSDecimalNumber+BUYAdditions.h"
#import "NSDictionary+BUYAdditions.h"
#import "NSURLComponents+BUYAdditions.h"
#if __has_include(<PassKit/PassKit.h>)
......@@ -47,18 +49,22 @@
#define kGET @"GET"
#define kPOST @"POST"
#define kPATCH @"PATCH"
#define kPUT @"PUT"
#define kDELETE @"DELETE"
#define kJSONType @"application/json"
#define kShopifyError @"shopify"
#define kMinSuccessfulStatusCode 200
#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 kBUYClientPathCollectionPublications = @"collection_listings";
NSString *const BUYClientCustomerAccessToken = @"X-Shopify-Customer-Access-Token";
@interface BUYClient () <NSURLSessionDelegate>
@property (nonatomic, strong) NSString *shopDomain;
......@@ -83,18 +89,19 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi
self = [super init];
if (self) {
self.modelManager = [BUYModelManager modelManager];
self.shopDomain = shopDomain;
self.apiKey = apiKey;
self.appId = appId;
self.applicationName = [[NSBundle mainBundle] infoDictionary][@"CFBundleName"] ?: @"";
self.queue = dispatch_get_main_queue();
self.session = [self createUrlSession];
self.session = [self urlSession];
self.pageSize = 25;
}
return self;
}
- (NSURLSession *)createUrlSession
- (NSURLSession *)urlSession
{
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
......@@ -124,7 +131,7 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi
return [self getRequestForURL:shopComponents.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
BUYShop *shop = nil;
if (json && error == nil) {
shop = [[BUYShop alloc] initWithDictionary:json];
shop = [self.modelManager insertShopWithJSONDictionary:json];
}
block(shop, error);
}];
......@@ -140,7 +147,7 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi
NSArray *products = nil;
if (json && error == nil) {
products = [BUYProduct convertJSONArray:json[kBUYClientPathProductPublications]];
products = [self.modelManager insertProductsWithJSONArray:json[kBUYClientPathProductPublications]];
}
block(products, page, [self hasReachedEndOfPage:products] || error, error);
}];
......@@ -169,7 +176,7 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi
NSArray *products = nil;
if (json && error == nil) {
products = [BUYProduct convertJSONArray:json[kBUYClientPathProductPublications]];
products = [self.modelManager insertProductsWithJSONArray:json[kBUYClientPathProductPublications]];
}
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." }];
......@@ -194,7 +201,7 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi
NSArray *collections = 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);
}];
......@@ -219,7 +226,7 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi
NSArray *products = 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);
}];
......@@ -291,7 +298,7 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi
{
BUYCheckout *checkout = nil;
if (error == nil) {
checkout = [[BUYCheckout alloc] initWithDictionary:json[@"checkout"]];
checkout = [self.modelManager insertCheckoutWithJSONDictionary:json[@"checkout"]];
}
block(checkout, error);
}
......@@ -301,7 +308,7 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi
checkout.marketingAttribution = @{@"medium": @"iOS", @"source": self.applicationName};
checkout.sourceName = @"mobile_app";
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];
}
}
......@@ -317,7 +324,7 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi
- (NSURLSessionDataTask *)createCheckoutWithCartToken:(NSString *)cartToken completion:(BUYDataCheckoutBlock)block
{
BUYCheckout *checkout = [[BUYCheckout alloc] initWithCartToken:cartToken];
BUYCheckout *checkout = [self.modelManager checkoutwithCartToken:cartToken];
[self configureCheckout:checkout];
NSDictionary *json = [checkout jsonDictionaryForCheckout];
......@@ -348,7 +355,7 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi
block(nil, [NSError errorWithDomain:kShopifyError code:BUYShopifyError_NoGiftCardSpecified userInfo:nil]);
}
else {
BUYGiftCard *giftCard = [[BUYGiftCard alloc] initWithDictionary:@{ @"code" : giftCardCode }];
BUYGiftCard *giftCard = [self.modelManager giftCardWithCode:giftCardCode];
NSURLComponents *components = [self URLComponentsForCheckoutsAppendingPath:@"gift_cards"
checkoutToken:checkout.token
queryItems:nil];
......@@ -389,14 +396,13 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi
- (void)updateCheckout:(BUYCheckout *)checkout withGiftCardDictionary:(NSDictionary *)giftCardDictionary addingGiftCard:(BOOL)addingGiftCard
{
NSMutableArray *giftCardArray = [NSMutableArray arrayWithArray:checkout.giftCards];
BUYGiftCard *giftCard = [[BUYGiftCard alloc] initWithDictionary:giftCardDictionary];
if (addingGiftCard) {
[giftCardArray addObject:giftCard];
BUYGiftCard *giftCard = [self.modelManager insertGiftCardWithJSONDictionary:giftCardDictionary];
[checkout.giftCardsSet addObject:giftCard];
} else {
[giftCardArray removeObject:giftCard];
[checkout removeGiftCardWithIdentifier:giftCardDictionary[@"id"]];
}
checkout.giftCards = [giftCardArray copy];
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.
......@@ -552,7 +558,7 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi
task = [self getRequestForURL:components.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
NSArray *shippingRates = nil;
if (error == nil && json) {
shippingRates = [BUYShippingRate convertJSONArray:json[@"shipping_rates"]];
shippingRates = [self.modelManager insertShippingRatesWithJSONArray:json[@"shipping_rates"]];
}
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
......@@ -567,7 +573,7 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_listi
#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;
......
//
// 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 @@
#import "NSEntityDescription+BUYAdditions.h"
#import "NSPropertyDescription+BUYAdditions.h"
// Custom value transformer names
NSString * const BUYPublicationsDateTransformerName = @"BUYPublicationsDate";
// Structured value transformer names
NSString * const BUYDeliveryRangeTransformerName = @"BUYDeliveryRange";
NSString * const BUYFlatArrayTransformerName = @"BUYFlatArray";
NSString * const BUYProductTagsTransformerName = @"BUYProductTags";
NSString * const BUYPublicationsDateFormat = @"yyyy-MM-dd'T'HH:mm:ssZ";
@interface BUYModelManager ()
@property (nonatomic, strong) NSManagedObjectModel *model;
@end
......@@ -54,10 +49,6 @@ NSString * const BUYPublicationsDateFormat = @"yyyy-MM-dd'T'HH:mm:ssZ";
{
static dispatch_once_t 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:[BUYFlatCollectionTransformer arrayTransformer] forName:BUYFlatArrayTransformerName];
[NSValueTransformer setValueTransformer:[BUYFlatCollectionTransformer setTransformerWithSeparator:@", "] forName:BUYProductTagsTransformerName];
......
......@@ -27,37 +27,16 @@
#import <Foundation/Foundation.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>
/**
* The identifier of any Shopify model object.
*/
@property (nonatomic, strong, readonly) NSNumber *identifier;
/**
* Objects marked as "dirty" are unsynced with Shopify and will
* sync on any calls to Shopify when updating a checkout.
*/
@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;
- (void)markPropertyAsDirty:(NSString *)property;
- (void)markAsClean;
......
......@@ -43,7 +43,7 @@
- (instancetype)init
{
return [self initWithDictionary:nil];
return [self initWithModelManager:nil JSONDictionary:nil];
}
- (instancetype)initWithDictionary:(NSDictionary *)dictionary
......@@ -100,23 +100,6 @@
[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
{
self.dirtyObserver = [BUYObserver observeProperties:properties ofObject:self];
......@@ -124,29 +107,14 @@
#pragma mark - Dynamic JSON Serialization
+ (NSArray *)propertyNames
{
static NSMutableDictionary *namesCache;
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;
- (NSArray *)propertyNames
{
return [self.entity.JSONEncodedProperties allKeys];
}
+ (NSEntityDescription *)entity
- (NSEntityDescription *)entity
{
@throw BUYAbstractMethod();
return [self.modelManager buy_entityWithName:[[self class] entityName]];
}
+ (NSString *)entityName
......@@ -161,23 +129,17 @@
- (instancetype)initWithModelManager:(id<BUYModelManager>)modelManager JSONDictionary:(NSDictionary *)dictionary
{
self = [super init];
self = [self init];
if (self) {
self.modelManager = modelManager;
[self updateWithDictionary:dictionary];
self.JSONDictionary = dictionary;
if ([[self class] tracksDirtyProperties]) {
[self trackDirtyProperties:[[self class] propertyNames]];
[self trackDirtyProperties:[self propertyNames]];
}
}
return self;
}
- (void)updateWithDictionary:(NSDictionary *)dictionary
{
_identifier = dictionary[@"id"];
[self markAsClean];
}
- (NSDictionary *)jsonDictionaryForCheckout
{
return self.JSONDictionary;
......@@ -198,11 +160,6 @@
return NO;
}
- (NSEntityDescription *)entity
{
return [self.modelManager buy_entityWithName:[[self class] entityName]];
}
- (NSDictionary *)JSONDictionary
{
// JSON generation starts in `-buy_JSONForObject`.
......
......@@ -74,11 +74,6 @@
*/
+ (BOOL)tracksDirtyProperties;
/**
* Use the values in the given dictionary to update properties.
*/
- (void)updateWithDictionary:(NSDictionary *)dictionary;
@optional
- (instancetype)initWithModelManager:(id<BUYModelManager>)modelManager JSONDictionary:(NSDictionary *)dictionary;
......
<?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">
<attribute name="address1" optional="YES" attributeType="String" syncable="YES">
<userInfo>
......@@ -255,6 +255,7 @@
<entry key="documentation" value="The URL Scheme of the host app."/>
</userInfo>
</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="creditCard" optional="YES" maxCount="1" deletionRule="Cascade" destinationEntity="MaskedCreditCard" inverseName="checkout" inverseEntity="MaskedCreditCard" syncable="YES">
<userInfo>
......@@ -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."/>
</userInfo>
</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">
<attribute name="collectionId" optional="YES" attributeType="Integer 64" defaultValueString="0" syncable="YES">
<userInfo>
......@@ -327,7 +340,6 @@
<attribute name="publishedAt" optional="YES" attributeType="Date" syncable="YES">
<userInfo>
<entry key="documentation" value="The publish date for the collection."/>
<entry key="JSONValueTransformer" value="BUYPublicationsDate"/>
</userInfo>
</attribute>
<attribute name="title" optional="YES" attributeType="String" syncable="YES">
......@@ -704,7 +716,6 @@
<attribute name="publishedAt" optional="YES" attributeType="Date" syncable="YES">
<userInfo>
<entry key="documentation" value="The publish date for a product."/>
<entry key="JSONValueTransformer" value="BUYPublicationsDate"/>
</userInfo>
</attribute>
<attribute name="tags" optional="YES" attributeType="Transformable" syncable="YES">
......@@ -972,12 +983,14 @@
<memberEntity name="TaxLine"/>
<memberEntity name="ShippingRate"/>
<memberEntity name="Address"/>
<memberEntity name="CheckoutAttribute"/>
</configuration>
<elements>
<element name="Address" positionX="126" positionY="521" width="128" height="225"/>
<element name="Cart" positionX="-576" positionY="558" width="128" height="60"/>
<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="Customer" positionX="106" positionY="1263" width="128" height="60"/>
<element name="Discount" positionX="126" positionY="877" width="128" height="105"/>
......
//
// BUYCart.h
// _BUYCart.h
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -24,25 +24,11 @@
// THE SOFTWARE.
//
@import Foundation;
#import "BUYSerializable.h"
#import "_BUYCart.h"
@class BUYLineItem;
@class BUYCartLineItem;
@class BUYProductVariant;
/**
* 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;
@interface BUYCart : _BUYCart {}
/**
* Returns true if the cart is acceptable to send to Shopify.
......
//
// BUYCart.m
// _BUYCart.m
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -25,94 +25,91 @@
//
#import "BUYCart.h"
#import "BUYCartLineItem.h"
#import "BUYProductVariant.h"
@interface BUYCart ()
@property (nonatomic, strong, nonnull) NSMutableSet<BUYCartLineItem *> *lineItemsSet;
@end
#import "./BUYCartLineItem.h"
#import "BUYModelManager.h"
@implementation BUYCart
#if !defined CORE_DATA_PERSISTENCE
- (instancetype)init
{
self = [super init];
if (self) {
self.lineItemsSet = [[NSMutableSet alloc] init];
self.lineItems = [NSOrderedSet orderedSet];
}
return self;
}
- (nonnull NSArray<BUYCartLineItem *> *)lineItems
{
return [self.lineItemsSet allObjects];
}
#endif
- (BOOL)isValid
{
return [self.lineItemsSet count] > 0;
return [self.lineItems count] > 0;
}
- (void)clearCart
{
[self.lineItemsSet removeAllObjects];
self.lineItems = [NSOrderedSet orderedSet];
}
#pragma mark - Simple Cart Editing
- (void)addVariant:(nonnull BUYProductVariant *)variant
- (void)addVariant:(BUYProductVariant *)variant
{
BUYCartLineItem *lineItem = [[BUYCartLineItem alloc] initWithVariant:variant];
BUYCartLineItem *existingLineItem = [self.lineItemsSet member:lineItem];
if (existingLineItem) {
existingLineItem.quantity = [existingLineItem.quantity decimalNumberByAdding:[NSDecimalNumber one]];
} else {
[self.lineItemsSet addObject:lineItem];
[self willChangeValueForKey:BUYCartRelationships.lineItems];
BUYCartLineItem *lineItem = [self linetItemForVariant:variant];
if (lineItem) {
[lineItem incrementQuantity];
}
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];
BUYCartLineItem *existingLineItem = [self.lineItemsSet member:lineItem];
if (existingLineItem) {
existingLineItem.quantity = [existingLineItem.quantity decimalNumberBySubtracting:[NSDecimalNumber one]];
if ([[existingLineItem quantity] isEqual:[NSDecimalNumber zero]]) {
[self.lineItemsSet removeObject:existingLineItem];
}
[self willChangeValueForKey:BUYCartRelationships.lineItems];
BUYCartLineItem *lineItem = [self linetItemForVariant:variant];
if (lineItem && [lineItem decrementQuantity].integerValue <= 0) {
[self.lineItemsSet removeObject:lineItem];
}
[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];
BUYCartLineItem *existingLineItem = [self.lineItemsSet member:lineItem];
if (existingLineItem && quantity > 0) {
existingLineItem.quantity = (NSDecimalNumber*)[NSDecimalNumber numberWithInteger:quantity];
} else if (existingLineItem && quantity == 0) {
[self.lineItemsSet removeObject:existingLineItem];
} else {
lineItem.quantity = (NSDecimalNumber*)[NSDecimalNumber numberWithInteger:quantity];
[self.lineItemsSet addObject:lineItem];
[self willChangeValueForKey:BUYCartRelationships.lineItems];
BUYCartLineItem *lineItem = [self linetItemForVariant:variant];
if (quantity == 0 && lineItem != nil) {
[self.lineItemsSet removeObject:lineItem];
}
else if (quantity > 0) {
if (lineItem == nil) {
lineItem = [self newCartLineItemWithVariant:variant];
[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];
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;
return [[self.lineItems filteredOrderedSetUsingPredicate:[NSPredicate predicateWithFormat:@"variant = %@", variant]] lastObject];
}
@end
//
// BUYCartLineItem.h
// _BUYCartLineItem.h
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -24,23 +24,48 @@
// THE SOFTWARE.
//
#import "BUYLineItem.h"
#import <Buy/_BUYCartLineItem.h>
/**
* BUYCartLineItem is a subclass of BUYLineItem that extends the object
* 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).
* Newly inserted `CartLineItem`s have an initial quantity of 1.
*/
@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
* when created using the preferred `initWithVariant:` initializer.
* Subtract the amount from the current quantity.
*/
@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
\ No newline at end of file
//
// BUYCartLineItem.m
// _BUYCartLineItem.m
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -26,39 +26,81 @@
#import "BUYCartLineItem.h"
#import "BUYProductVariant.h"
@interface BUYCartLineItem ()
@property (nonatomic, strong) BUYProductVariant *variant;
@end
#import "BUYModelManager.h"
@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) {
self.variant = variant;
self.quantity = [NSDecimalNumber one];
}
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];
}
- (NSDecimalNumber *)addQuantity:(NSDecimalNumber *)amount
{
NSDecimalNumber *quantity = [self.quantity decimalNumberByAdding:amount];
self.quantity = quantity;
return quantity;
}
- (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)isEqual:(id)object
- (NSDecimalNumber *)incrementQuantity
{
if (self == object) return YES;
if (![object isKindOfClass:self.class]) return NO;
BOOL same = ([self.identifier isEqual:((BUYObject*)object).identifier]) || [self.variantId isEqual:((BUYCartLineItem*)object).variant.identifier];
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];
return hash;
BUYCartLineItem *lineItem = [self insertCartLineItemWithJSONDictionary:nil];
lineItem.variant = variant;
return lineItem;
}
@end
//
// BUYCollection.h
// _BUYCollection.h
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -24,57 +24,26 @@
// THE SOFTWARE.
//
@import Foundation;
#import "BUYObject.h"
#import <Buy/_BUYCollection.h>
#import "BUYClient.h"
/**
* 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;
@interface BUYCollection : _BUYCollection {}
/**
* The html description
*/
@property (nonatomic, strong, readonly) NSString *htmlDescription;
@property (nonatomic, readonly) NSDate *createdAtDate NS_DEPRECATED_IOS(8_0, 9_0);
@property (nonatomic, readonly) NSDate *updatedAtDate NS_DEPRECATED_IOS(8_0, 9_0);
@property (nonatomic, readonly) NSDate *publishedAtDate NS_DEPRECATED_IOS(8_0, 9_0);
/**
* The collection's image URL
*/
@property (nonatomic, strong, readonly) NSURL *imageURL;
@property (nonatomic, readonly) NSURL *imageURL NS_DEPRECATED_IOS(8_0, 9_0);
/**
* 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;
@property (nonatomic, readonly) NSString *stringDescription;
/**
* 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
//
// BUYCollection.m
// _BUYCollection.m
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -25,35 +25,64 @@
//
#import "BUYCollection.h"
#import "NSDateFormatter+BUYAdditions.h"
#import "NSURL+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
#import "BUYImageLink.h"
#import "NSString+BUYAdditions.h"
@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];
_title = dictionary[@"title"];
_htmlDescription = dictionary[@"body_html"];
_imageURL = [NSURL buy_urlWithString:[dictionary buy_objectForKey:@"image"][@"src"]];
_handle = dictionary[@"handle"];
_published = [dictionary[@"published"] boolValue];
_collectionId = dictionary[@"collection_id"];
NSDateFormatter *dateFormatter = [NSDateFormatter dateFormatterForPublications];
_createdAtDate = [dateFormatter dateFromString:dictionary[@"created_at"]];
_updatedAtDate = [dateFormatter dateFromString:dictionary[@"updated_at"]];
_publishedAtDate = [dateFormatter dateFromString:dictionary[@"published_at"]];
switch (sort) {
case BUYCollectionSortBestSelling:
return @"best-selling";
case BUYCollectionSortCreatedAscending:
return @"created-ascending";
case BUYCollectionSortCreatedDescending:
return @"created-descending";
case BUYCollectionSortPriceAscending:
return @"price-ascending";
case BUYCollectionSortPriceDescending:
return @"price-descending";
case BUYCollectionSortTitleAscending:
return @"title-ascending";
case BUYCollectionSortTitleDescending:
return @"title-descending";
default:
return @"collection-default";
}
}
@end
//
// BUYImageLink.h
// _BUYImageLink.h
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -24,41 +24,64 @@
// THE SOFTWARE.
//
#import "BUYObject.h"
#import <Buy/_BUYImageLink.h>
/**
* Products are easier to sell if customers can see pictures of them, which is why there are product images.
*/
@interface BUYImageLink : BUYObject
@import UIKit;
/**
* Specifies the location of the product image.
*/
@property (nonatomic, readonly, copy) NSString *src;
// Defines for common maximum image sizes
typedef NS_ENUM(NSUInteger, BUYImageURLSize) {
BUYImageURLSize100x100,
BUYImageURLSize160x160,
BUYImageURLSize240x240,
BUYImageURLSize480x480,
BUYImageURLSize600x600,
BUYImageURLSize1024x1024,
BUYImageURLSize2048x2048
};
/**
* An array of variant ids associated with the image.
*/
@property (nonatomic, readonly, copy) NSArray<NSNumber *> *variantIds;
@interface BUYImageLink : _BUYImageLink {}
/**
* Creation date of the image
*/
@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
//
// BUYImageLink.m
// _BUYImageLink.m
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -25,22 +25,104 @@
//
#import "BUYImageLink.h"
#import "NSDateFormatter+BUYAdditions.h"
#import "NSString+BUYAdditions.h"
#import "NSURL+BUYAdditions.h"
@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];
_variantIds = [dictionary[@"variant_ids"] copy];
_productId = [dictionary[@"product_id"] copy];
_position = [dictionary[@"position"] copy];
if (pixelSize <= 100.0) return BUYImageURLSize100x100;
if (pixelSize <= 160.0) return BUYImageURLSize160x160;
if (pixelSize <= 240.0) return BUYImageURLSize240x240;
if (pixelSize <= 480.0) return BUYImageURLSize480x480;
if (pixelSize <= 600.0) return BUYImageURLSize600x600;
if (pixelSize <= 1024.0) return BUYImageURLSize1024x1024;
NSDateFormatter *dateFormatter = [NSDateFormatter dateFormatterForPublications];
_createdAtDate = [dateFormatter dateFromString:dictionary[@"created_at"]];
_updatedAtDate = [dateFormatter dateFromString:dictionary[@"updated_at"]];
return BUYImageURLSize2048x2048;
}
@end
//
// BUYLineItem.h
// _BUYLineItem.h
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -24,104 +24,25 @@
// THE SOFTWARE.
//
#import "BUYSerializable.h"
#import "BUYObject.h"
#import <Buy/_BUYLineItem.h>
#import <Buy/BUYModelManager.h>
@class BUYProductVariant;
@class BUYCartLineItem, BUYProductVariant;
/**
* This represents a BUYLineItem on a BUYCart or on a BUYCheckout.
*/
@interface BUYLineItem : BUYObject <BUYSerializable>
@interface BUYLineItem : _BUYLineItem {}
/**
* The unique line item identifier
*/
@property (nonatomic, strong, readonly) NSString *lineItemIdentifier;
- (instancetype)initWithVariant:(BUYProductVariant *)variant NS_DEPRECATED_IOS(8_0, 9_0, "Use `BUYModelManager` to create new instances of model objects instead");
- (instancetype)initWithCartLineItem:(BUYCartLineItem *)cartLineItem NS_DEPRECATED_IOS(8_0, 9_0, "Use `BUYModelManager` to create new instances of model objects instead");
/**
* 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;
@property (readonly) NSString *lineItemIdentifier NS_DEPRECATED_IOS(8_0, 9_0);
/**
* 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;
@end
/**
* Custom properties set on the line item
*/
@property (nonatomic, copy) NSDictionary *properties;
@class BUYCartLineItem;
/**
* Service provider who is doing the fulfillment
*/
@property (nonatomic, readonly, copy) NSString *fulfillmentService;
@interface BUYModelManager (BUYLineItemCreation)
/**
* Initialize a BUYLineItem with an optional variant.
* 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;
- (BUYLineItem *)lineItemWithVariant:(BUYProductVariant *)variant;
- (BUYLineItem *)lineItemWithCartLineItem:(BUYCartLineItem *)cartLineItem;
@end
//
// BUYLineItem.m
// _BUYLineItem.m
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -20,96 +20,75 @@
// 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 "BUYLineItem.h"
#import "BUYCartLineItem.h"
#import "BUYProductVariant.h"
#import "NSDecimalNumber+BUYAdditions.h"
#import "NSString+BUYAdditions.h"
#import "BUYProduct.h"
@interface BUYLineItem ()
@implementation 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;
/**
* Have model manager responsible for instantiation, and allow deprecated
* initializers for backwards compatability
*/
- (instancetype)initWithCartLineItem:(BUYCartLineItem *)cartLineItem
{
BUYLineItem *lineItem = [[BUYLineItem alloc] initWithModelManager:cartLineItem.modelManager JSONDictionary:nil];
[lineItem updateWithLineItem:cartLineItem];
return lineItem;
}
@end
- (instancetype)initWithVariant:(BUYProductVariant *)variant
{
BUYLineItem *lineItem = [[BUYLineItem alloc] initWithModelManager:variant.modelManager JSONDictionary:nil];
[lineItem updateWithVariant:variant];
return lineItem;
}
@implementation BUYLineItem
- (void)updateWithVariant:(BUYProductVariant *)variant
{
self.variantId = variant.identifier;
self.quantity = variant ? [NSDecimalNumber one] : [NSDecimalNumber zero];
self.price = variant.price ?: [NSDecimalNumber zero];
self.title = variant.title ?: @"";
self.requiresShipping = variant.requiresShipping;
self.compareAtPrice = variant.compareAtPrice;
self.grams = variant.grams;
}
- (instancetype)init
- (void)updateWithLineItem:(BUYCartLineItem *)lineItem
{
return [self initWithVariant:nil];
[self updateWithVariant:lineItem.variant];
self.quantity = lineItem.quantity;
}
- (instancetype)initWithVariant:(BUYProductVariant *)variant
- (NSString *)lineItemIdentifier
{
self = [super init];
if (self) {
self.variantId = variant.identifier;
self.productId = variant.product.productId;
self.quantity = variant ? [NSDecimalNumber one] : [NSDecimalNumber zero];
self.price = variant ? [variant price] : [NSDecimalNumber zero];
self.title = variant ? [variant title] : @"";
self.requiresShipping = variant.requiresShipping;
self.compareAtPrice = variant.compareAtPrice;
self.grams = variant.grams;
}
return self;
return self.identifier;
}
- (void)updateWithDictionary:(NSDictionary *)dictionary
@end
@implementation BUYModelManager (BUYLineItemCreation)
- (BUYLineItem *)lineItemWithVariant:(BUYProductVariant *)variant
{
self.lineItemIdentifier = dictionary[@"id"];
self.variantId = dictionary[@"variant_id"];
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];
BUYLineItem *lineItem = [self insertLineItemWithJSONDictionary:nil];
[lineItem updateWithVariant:variant];
return lineItem;
}
- (NSDictionary *)jsonDictionaryForCheckout
- (BUYLineItem *)lineItemWithCartLineItem:(BUYCartLineItem *)cartLineItem
{
NSMutableDictionary *lineItem = [[NSMutableDictionary alloc] init];
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) {
lineItem[@"price"] = self.price;
}
if (self.properties) {
lineItem[@"properties"] = self.properties;
}
lineItem[@"requires_shipping"] = self.requiresShipping ?: @NO;
BUYLineItem *lineItem = [self insertLineItemWithJSONDictionary:nil];
[lineItem updateWithLineItem:cartLineItem];
return lineItem;
}
......
//
// BUYOption.h
// _BUYOption.h
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -24,27 +24,8 @@
// THE SOFTWARE.
//
#import "BUYObject.h"
#import <Buy/_BUYOption.h>
/**
* 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;
@interface BUYOption : _BUYOption {}
@end
//
// BUYOption.m
// _BUYOption.m
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -28,13 +28,4 @@
@implementation BUYOption
- (void)updateWithDictionary:(NSDictionary *)dictionary
{
[super updateWithDictionary:dictionary];
_name = [dictionary[@"name"] copy];
_position = dictionary[@"position"];
_productId = [dictionary[@"product_id"] copy];
}
@end
//
// BUYOptionValue.h
// _BUYOptionValue.h
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -24,24 +24,10 @@
// THE SOFTWARE.
//
#import "BUYObject.h"
#import <Buy/_BUYOptionValue.h>
@interface BUYOptionValue : BUYObject
@interface BUYOptionValue : _BUYOptionValue {}
/**
* 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;
- (BOOL)isEqualToOptionValue:(BUYOptionValue *)other;
@end
//
// BUYOptionValue.m
// _BUYOptionValue.m
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -28,37 +28,22 @@
@implementation BUYOptionValue
- (void)updateWithDictionary:(NSDictionary *)dictionary
- (BOOL)isEqualToOptionValue:(BUYOptionValue *)other
{
[super updateWithDictionary:dictionary];
_name = [dictionary[@"name"] copy];
_value = [dictionary[@"value"] copy];
_optionId = [dictionary[@"option_id"] copy];
return [other isKindOfClass:[self class]] && [self.name isEqual:other.name] && [self.optionId isEqual:other.optionId];
}
#if !defined CORE_DATA_PERSISTENCE
- (BOOL)isEqual:(id)object
{
BOOL same = NO;
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;
return [super isEqual:object] || [self isEqualToOptionValue:object];
}
- (NSUInteger)hash
{
NSUInteger hash = [self.value hash];
hash ^= [self.optionId hash];
return hash;
NSUInteger hash = self.name.hash;
return ((hash << 5) + hash) + self.optionId.hash;
}
#endif
@end
//
// BUYOrder.h
// _BUYOrder.h
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -24,18 +24,15 @@
// THE SOFTWARE.
//
#import "BUYObject.h"
#import <Buy/_BUYOrder.h>
@interface BUYOrder : BUYObject
@interface BUYOrder : _BUYOrder {}
/**
* URL for the website showing the order status
*/
@property (nonatomic, strong, readonly) NSURL *statusURL;
@end
@interface BUYModelManager (BUYOrder)
/**
* The customer's order name as represented by a number.
*/
@property (nonatomic, strong, readonly) NSString *name;
- (NSArray<BUYOrder *> *)ordersWithJSONDictionary:(NSDictionary *)json;
@end
//
// BUYOrder.m
// _BUYOrder.m
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -25,24 +25,53 @@
//
#import "BUYOrder.h"
#import "NSURL+BUYAdditions.h"
#import "NSDictionary+BUYAdditions.h"
#import "BUYLineItem.h"
@interface BUYOrder ()
@implementation BUYOrder
@property (nonatomic, strong) NSURL *statusURL;
@property (nonatomic, strong) NSString *name;
- (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"]];
[super setJSONDictionary:JSONDictionary];
// 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
@implementation BUYOrder
@implementation BUYModelManager (BUYOrder)
- (void)updateWithDictionary:(NSDictionary *)dictionary
- (NSArray<BUYOrder *> *)ordersWithJSONDictionary:(NSDictionary *)json
{
[super updateWithDictionary:dictionary];
NSString *statusURLString = dictionary[@"status_url"];
self.statusURL = [NSURL buy_urlWithString:statusURLString];
self.name = [dictionary buy_objectForKey:@"name"];
NSArray *orders = [json objectForKey:@"orders"];
return (id)[self buy_objectsWithEntityName:[BUYOrder entityName] JSONArray:orders];
}
@end
......@@ -24,94 +24,42 @@
// THE SOFTWARE.
//
#import "BUYObject.h"
#import <Buy/_BUYProduct.h>
@class BUYProductVariant;
@class BUYImageLink;
@class BUYOption;
@interface BUYProduct : _BUYProduct {}
/**
* A BUYProduct is an individual item for sale in a Shopify shop.
*/
@interface BUYProduct : BUYObject
/**
* 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;
@property (nonatomic, readonly, copy) NSDate *createdAtDate;
@property (nonatomic, readonly, copy) NSDate *updatedAtDate;
@property (nonatomic, readonly, copy) NSDate *publishedAtDate;
@property (nonatomic, readonly, copy) NSString *stringDescription;
/**
* 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;
@end
/**
* The product is published on the current sales channel
*/
@property (nonatomic, readonly, assign) BOOL published;
@interface BUYProduct (Options)
/**
* 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
//
// BUYProduct.m
// _BUYProduct.m
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -24,39 +24,94 @@
// THE SOFTWARE.
//
#import "BUYImageLink.h"
#import "BUYOption.h"
#import "BUYProduct.h"
#import "BUYModelManager.h"
#import "BUYOption.h"
#import "BUYOptionValue.h"
#import "BUYProductVariant.h"
#import "NSDateFormatter+BUYAdditions.h"
#import "NSDictionary+BUYAdditions.h"
#import "NSString+BUYAdditions.h"
@implementation BUYProduct
- (void)updateWithDictionary:(NSDictionary *)dictionary
@synthesize stringDescription=_stringDescription;
- (NSDate *)createdAtDate
{
return self.createdAt;
}
- (NSDate *)updatedAtDate
{
return self.updatedAt;
}
- (NSDate *)publishedAtDate
{
[super updateWithDictionary:dictionary];
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];
_title = [dictionary[@"title"] copy];
_handle = [dictionary[@"handle"] copy];
_productId = [dictionary[@"product_id"] copy];
_vendor = [dictionary[@"vendor"] copy];
_productType = [dictionary[@"product_type"] copy];
_variants = [BUYProductVariant convertJSONArray:dictionary[@"variants"] block:^(BUYProductVariant *variant) {
variant.product = self;
}];
_images = [BUYImageLink convertJSONArray:dictionary[@"images"]];
_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];
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
{
if ([self.variants count] == 1) {
BUYProductVariant *productVariant = [self.variants firstObject];
BUYOptionValue *optionValue = [productVariant.options anyObject];
NSString *defaultTitleString = @"Default Title";
NSString *defaultString = @"Default";
if ([productVariant.title isEqualToString:defaultTitleString] &&
([optionValue.value isEqualToString:defaultTitleString] || [optionValue.value isEqualToString:defaultString])) {
return YES;
}
}
return NO;
}
@end
//
// BUYProductVariant.h
// _BUYProductVariant.h
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -24,72 +24,29 @@
// THE SOFTWARE.
//
#import "BUYObject.h"
#import <Buy/_BUYProductVariant.h>
@class BUYProduct;
@class BUYOptionValue;
/**
* 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;
@interface BUYProductVariant : _BUYProductVariant {}
/**
* 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
//
// BUYProductVariant.m
// _BUYProductVariant.m
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -25,29 +25,32 @@
//
#import "BUYProductVariant.h"
#import "NSDecimalNumber+BUYAdditions.h"
#import "BUYOptionValue.h"
@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];
_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];
return nil;
}
+ (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
//
// BUYShop.h
// _BUYShop.h
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -24,66 +24,8 @@
// THE SOFTWARE.
//
#import "BUYObject.h"
#import <Buy/_BUYShop.h>
/**
* 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;
@interface BUYShop : _BUYShop {}
@end
//
// BUYShop.m
// _BUYShop.m
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -28,21 +28,4 @@
@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
//
// BUYAddress.h
// _BUYAddress.h
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -24,72 +24,26 @@
// THE SOFTWARE.
//
#import "BUYObject.h"
#import "BUYSerializable.h"
#import <Buy/_BUYAddress.h>
/**
* 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;
@interface BUYAddress : _BUYAddress {}
/**
* 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
//
// BUYAddress.m
// _BUYAddress.m
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -25,65 +25,38 @@
//
#import "BUYAddress.h"
#import "NSString+BUYAdditions.h"
#import "NSDictionary+BUYAdditions.h"
@implementation BUYAddress
- (void)updateWithDictionary:(NSDictionary *)dictionary
-(NSString *)countryCode
{
self.address1 = dictionary[@"address1"];
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"];
return [[super countryCode] uppercaseString];
}
- (NSDictionary *)jsonDictionaryForCheckout
- (BOOL)isPartialAddress
{
NSMutableDictionary *json = [[NSMutableDictionary alloc] init];
json[@"address1"] = [self.address1 buy_trim] ?: @"";
json[@"address2"] = [self.address2 buy_trim] ?: @"";
json[@"city"] = [self.city buy_trim] ?: @"";
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];
if ([countryCode length] > 0) {
json[@"country_code"] = countryCode;
}
NSString *province = [self.province buy_trim];
if ([province length] > 0) {
json[@"province"] = province;
if (self.address1.length == 0 ||
self.firstName.length == 0 ||
self.lastName.length == 0) {
return YES;
}
NSString *provinceCode = [self.provinceCode buy_trim];
if ([provinceCode length] > 0) {
json[@"province_code"] = provinceCode;
}
return json;
return NO;
}
-(NSString *)countryCode
- (BOOL)isValidAddressForShippingRates
{
return [_countryCode uppercaseString];
BOOL valid = NO;
if (self.city.length > 0 &&
self.zip.length > 0 &&
self.province.length > 0 &&
(self.country.length > 0 || self.countryCode.length == 2)) {
valid = YES;
}
return valid;
}
@end
//
// BUYCheckout.h
// _BUYCheckout.h
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -24,275 +24,36 @@
// THE SOFTWARE.
//
#import "BUYObject.h"
#import "BUYSerializable.h"
#import <Buy/_BUYCheckout.h>
#import <Buy/_BUYProductVariant.h>
#import <Buy/BUYModelManager.h>
@class BUYAddress;
@class BUYCart;
@class BUYCreditCard;
@class BUYDiscount;
@class BUYMaskedCreditCard;
@class BUYOrder;
@class BUYShippingRate;
@class BUYTaxLine;
@class BUYLineItem;
@class BUYGiftCard;
@class BUYCheckoutAttribute;
@class BUYCart, BUYCartLineItem, BUYAddress, BUYGiftCard;
/**
* 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>
@interface BUYCheckout : _BUYCheckout {}
/**
* The customer's email address
*/
@property (nonatomic, copy) NSString *email;
@property (nonatomic, copy) NSNumber *taxesIncluded;
@property (nonatomic, readonly, copy) NSDate *createdAtDate;
@property (nonatomic, readonly, copy) NSDate *updatedAtDate;
@property (nonatomic, readonly, copy) NSDictionary *attributesDictionary;
/**
* Unique token for the checkout on Shopify
*/
@property (nonatomic, copy, readonly) NSString *token;
@property (nonatomic) BOOL hasToken;
/**
* Unique token for a cart which can be used to convert to a checkout
*/
@property (nonatomic, copy, readonly) NSString *cartToken;
- (instancetype)initWithCart:(BUYCart *)cart NS_DEPRECATED_IOS(8_0, 9_0, "Use `BUYModelManager` to create new instances of model objects instead");
- (instancetype)initWithCartToken:(NSString *)token NS_DEPRECATED_IOS(8_0, 9_0, "Use `BUYModelManager` to create new instances of model objects instead");;
/**
* States whether or not the fulfillment requires shipping
*/
@property (nonatomic, assign, readonly) BOOL requiresShipping;
- (void)updateWithCart:(BUYCart *)cart;
/**
* States whether or not the taxes are included in the price
*/
@property (nonatomic, assign, readonly) BOOL taxesIncluded;
- (BUYGiftCard *)giftCardWithIdentifier:(NSNumber *)identifier;
- (void)removeGiftCardWithIdentifier:(NSNumber *)identifier;
/**
* 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
@end
/**
* The unique order ID
*/
@property (nonatomic, copy, readonly) NSNumber *orderId DEPRECATED_MSG_ATTRIBUTE("Available on the BUYOrder object");
@interface BUYModelManager (BUYCheckoutCreating)
/**
* URL for the website showing the order status
*/
@property (nonatomic, strong, readonly) NSURL *orderStatusURL DEPRECATED_MSG_ATTRIBUTE("Available on the BUYOrder object");
- (BUYCheckout *)checkout;
- (BUYCheckout *)checkoutWithCart:(BUYCart *)cart;
- (BUYCheckout *)checkoutWithVariant:(BUYProductVariant *)productVariant;
- (BUYCheckout *)checkoutwithCartToken:(NSString *)token;
@end
......@@ -24,22 +24,10 @@
// THE SOFTWARE.
//
#import "BUYObject.h"
#import "BUYSerializable.h"
#import <Buy/_BUYCheckoutAttribute.h>
/**
* A BUYCheckoutAttribute represents a checkout attributes key and value
*/
@interface BUYCheckoutAttribute : BUYObject <BUYSerializable>
/**
* The attribute name
*/
@property (nonatomic, strong, nonnull) NSString *name;
/**
* The attribute value
*/
@property (nonatomic, strong, nonnull) NSString *value;
@interface BUYCheckoutAttribute : _BUYCheckoutAttribute
@end
......@@ -28,17 +28,6 @@
@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 same = NO;
......
//
// BUYDiscount.h
// _BUYDiscount.h
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -24,36 +24,12 @@
// THE SOFTWARE.
//
#import "BUYObject.h"
#import "BUYSerializable.h"
#import <Buy/_BUYDiscount.h>
#import <Buy/BUYModelManager.h>
/**
* BUYDiscount represents a discount that is applied to the BUYCheckout.
*/
@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 BUYDiscount : _BUYDiscount {}
@end
@interface BUYModelManager (BUYDiscountCreating)
- (BUYDiscount *)discountWithCode:(NSString *)code;
@end
//
// BUYDiscount.m
// _BUYDiscount.m
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -25,29 +25,24 @@
//
#import "BUYDiscount.h"
#import "NSDecimalNumber+BUYAdditions.h"
#import "NSString+BUYAdditions.h"
#import "NSEntityDescription+BUYAdditions.h"
@implementation BUYDiscount
- (instancetype)initWithCode:(NSString *)code
- (NSDictionary *)JSONEncodedProperties
{
return [super initWithDictionary:@{@"code": code ?: @""}];
return [self.entity.JSONEncodedProperties dictionaryWithValuesForKeys:@[@"code"]];
}
- (void)updateWithDictionary:(NSDictionary *)dictionary
{
[super updateWithDictionary:dictionary];
self.code = dictionary[@"code"];
self.amount = [NSDecimalNumber buy_decimalNumberFromJSON:dictionary[@"amount"]];
self.applicable = [dictionary[@"applicable"] boolValue];
}
@end
- (NSDictionary *)jsonDictionaryForCheckout
@implementation BUYModelManager (BUYDiscountCreating)
- (BUYDiscount *)discountWithCode:(NSString *)code
{
NSMutableDictionary *json = [[NSMutableDictionary alloc] init];
json[@"code"] = [self.code buy_trim] ?: @"";
return json;
BUYDiscount *discount = [self buy_objectWithEntityName:[BUYDiscount entityName] JSONDictionary:nil];
discount.code = code ?: @"";
return discount;
}
@end
@end
\ No newline at end of file
//
// BUYGiftCard.h
// _BUYGiftCard.h
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -24,30 +24,15 @@
// THE SOFTWARE.
//
#import "BUYObject.h"
#import "BUYSerializable.h"
#import <Buy/_BUYGiftCard.h>
#import <Buy/BUYModelManager.h>
@interface BUYGiftCard : BUYObject <BUYSerializable>
@interface BUYGiftCard : _BUYGiftCard {}
/**
* 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;
@end
/**
* The amount left on the gift card after being applied to this checkout.
*/
@property (nonatomic, readonly, strong) NSDecimalNumber *balance;
@interface BUYModelManager (BUYGiftCardCreation)
/**
* The amount of the gift card used by this checkout.
*/
@property (nonatomic, readonly, strong) NSDecimalNumber *amountUsed;
- (BUYGiftCard *)giftCardWithCode:(NSString *)code;
@end
//
// BUYGiftCard.m
// _BUYGiftCard.m
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -25,41 +25,23 @@
//
#import "BUYGiftCard.h"
#import "NSDecimalNumber+BUYAdditions.h"
@implementation BUYGiftCard
- (void)updateWithDictionary:(NSDictionary *)dictionary
- (NSDictionary *)jsonDictionaryForCheckout
{
[super updateWithDictionary:dictionary];
_code = dictionary[@"code"];
_lastCharacters = dictionary[@"last_characters"];
_balance = [NSDecimalNumber buy_decimalNumberFromJSON:dictionary[@"balance"]];
_amountUsed = [NSDecimalNumber buy_decimalNumberFromJSON:dictionary[@"amount_used"]];
return @{ @"gift_card" : self.JSONDictionary };
}
- (NSDictionary *)jsonDictionaryForCheckout
@end
@implementation BUYModelManager (BUYGiftCardCreation)
- (BUYGiftCard *)giftCardWithCode:(NSString *)code
{
NSMutableDictionary *json = [[NSMutableDictionary alloc] init];
if (_code) {
json[@"code"] = _code;
}
if (_lastCharacters) {
json[@"last_characters"] = _lastCharacters;
}
if (_balance) {
json[@"balance"] = _balance;
}
if (_amountUsed) {
json[@"amount_used"] = _amountUsed;
}
return @{ @"gift_card" : json };
BUYGiftCard *giftCard = [self buy_objectWithEntityName:[BUYGiftCard entityName] JSONDictionary:nil];
giftCard.code = code;
return giftCard;
}
@end
//
// BUYMaskedCreditCard.h
// _BUYMaskedCreditCard.h
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -24,41 +24,8 @@
// THE SOFTWARE.
//
#import "BUYObject.h"
#import <Buy/_BUYMaskedCreditCard.h>
/**
* 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;
@interface BUYMaskedCreditCard : _BUYMaskedCreditCard {}
@end
//
// BUYMaskedCreditCard.m
// _BUYMaskedCreditCard.m
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -28,18 +28,4 @@
@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
//
// BUYShippingRate.h
// _BUYShippingRate.h
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -24,32 +24,8 @@
// THE SOFTWARE.
//
#import "BUYObject.h"
#import "BUYSerializable.h"
#import <Buy/_BUYShippingRate.h>
/**
* 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;
@interface BUYShippingRate : _BUYShippingRate {}
@end
//
// BUYShippingRate.m
// _BUYShippingRate.m
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -25,52 +25,7 @@
//
#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
- (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
//
// BUYTaxLine.h
// _BUYTaxLine.h
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -24,27 +24,8 @@
// THE SOFTWARE.
//
#import "BUYObject.h"
#import "BUYSerializable.h"
#import <Buy/_BUYTaxLine.h>
/**
* 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;
@interface BUYTaxLine : _BUYTaxLine {}
@end
//
// BUYTaxLine.m
// _BUYTaxLine.m
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -25,17 +25,7 @@
//
#import "BUYTaxLine.h"
#import "NSDecimalNumber+BUYAdditions.h"
@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
......@@ -64,6 +64,7 @@ extern const struct BUYCheckoutAttributes {
} BUYCheckoutAttributes;
extern const struct BUYCheckoutRelationships {
__unsafe_unretained NSString *attributes;
__unsafe_unretained NSString *billingAddress;
__unsafe_unretained NSString *creditCard;
__unsafe_unretained NSString *discount;
......@@ -80,6 +81,7 @@ extern const struct BUYCheckoutUserInfo {
__unsafe_unretained NSString *documentation;
} BUYCheckoutUserInfo;
@class BUYCheckoutAttribute;
@class BUYAddress;
@class BUYMaskedCreditCard;
@class BUYDiscount;
......@@ -300,6 +302,10 @@ extern const struct BUYCheckoutUserInfo {
*/
@property (nonatomic, strong) NSURL* webReturnToURL;
@property (nonatomic, strong) NSSet *attributes;
- (NSMutableSet*)attributesSet;
@property (nonatomic, strong) BUYAddress *billingAddress;
/**
......@@ -351,6 +357,10 @@ extern const struct BUYCheckoutUserInfo {
@end
@interface _BUYCheckout (AttributesCoreDataGeneratedAccessors)
@end
@interface _BUYCheckout (GiftCardsCoreDataGeneratedAccessors)
- (void)insertObject:(BUYGiftCard*)value inGiftCardsAtIndex:(NSUInteger)idx;
......
......@@ -62,6 +62,7 @@ const struct BUYCheckoutAttributes BUYCheckoutAttributes = {
};
const struct BUYCheckoutRelationships BUYCheckoutRelationships = {
.attributes = @"attributes",
.billingAddress = @"billingAddress",
.creditCard = @"creditCard",
.discount = @"discount",
......@@ -175,6 +176,12 @@ const struct BUYCheckoutUserInfo BUYCheckoutUserInfo = {
[self setReservationTimeLeft:@(value_)];
}
- (NSMutableSet*)attributesSet {
return (NSMutableSet*)[self mutableSetValueForKey:@"attributes"];
}
- (NSMutableOrderedSet*)giftCardsSet {
return (NSMutableOrderedSet*)[self mutableOrderedSetValueForKey:@"giftCards"];
......
//
// BUYAddress+Additions.h
// _BUYCheckoutAttribute.h
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -23,28 +23,51 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// DO NOT EDIT. This file is machine-generated and constantly overwritten.
// Make changes to BUYCheckoutAttribute.h instead.
@import Foundation;
#import "BUYAddress.h"
#import "BUYObject.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
* 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
* The attribute name.
*/
- (BOOL)isPartialAddress;
@interface _BUYCheckoutAttribute : BUYObject
+ (NSString *)entityName;
@property (nonatomic, strong) NSString* name;
/**
* 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.
* The attribute value.
*/
- (BOOL)isValidAddressForShippingRates;
@property (nonatomic, strong) NSString* value;
@property (nonatomic, strong) BUYCheckout *checkout;
@end
//
// BUYProductVariant+Options.m
// _BUYCheckoutAttribute.h
// Mobile Buy SDK
//
// Created by Shopify.
......@@ -23,33 +23,60 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// 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
{
return (NSArray<BUYCheckoutAttribute *> *)[self buy_objectsWithEntityName:@"CheckoutAttribute" JSONArray:array];
}
- (NSArray<BUYCheckoutAttribute *> *)allCheckoutAttributeObjects
{
for (BUYOptionValue *value in self.options) {
if ([value.name isEqualToString:optionName]) {
return value;
}
}
return nil;
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];
for (BUYProductVariant *variant in productVariants) {
for (BUYOptionValue *opValue in variant.options) {
if ([opValue isEqual:optionValue]) {
[filteredArray addObject:variant];
}
}
}
return [filteredArray copy];
return (BUYCheckoutAttribute *)[self buy_objectWithEntityName:@"CheckoutAttribute" identifier:@(identifier)];
}
@end
......@@ -105,7 +105,7 @@
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]
attributes:@{NSStrikethroughStyleAttributeName: @(NSUnderlineStyleSingle)}];
self.comparePriceLabel.attributedText = attributedString;
......
......@@ -97,7 +97,7 @@ CGFloat const buttonWidth = 10.0f;
- (void)setOptionsForProductVariant:(BUYProductVariant *)productVariant
{
NSArray *productOptions = productVariant.options;
NSArray *productOptions = productVariant.options.allObjects;
switch (productVariant.options.count) {
case 3:
......
......@@ -28,7 +28,7 @@
#import "BUYImageView.h"
#import "BUYOptionSelectionNavigationController.h"
#import "BUYPresentationControllerWithNavigationController.h"
#import "BUYProduct+Options.h"
#import "BUYProduct.h"
#import "BUYProductViewController.h"
#import "BUYImageKit.h"
#import "BUYProductView.h"
......@@ -397,8 +397,9 @@ CGFloat const BUYMaxProductViewHeight = 640.0;
[self.variantCell setOptionsForProductVariant:self.selectedProductVariant];
}
if (self.productView.productViewHeader.collectionView) {
[self.productView.productViewHeader setImageForSelectedVariant:_selectedProductVariant withImages:self.product.images];
[self.productView updateBackgroundImage:self.product.images];
NSArray *images = self.product.images.array;
[self.productView.productViewHeader setImageForSelectedVariant:_selectedProductVariant withImages:images];
[self.productView updateBackgroundImage:images];
}
if (self.productView.productViewFooter) {
[self.productView.productViewFooter updateButtonsForProductVariant:selectedProductVariant];
......@@ -460,7 +461,7 @@ CGFloat const BUYMaxProductViewHeight = 640.0;
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
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;
- (BUYCart *)cart
{
BUYCart *cart = [[BUYCart alloc] init];
BUYCart *cart = [self.client.modelManager insertCartWithJSONDictionary:nil];
[cart addVariant:self.selectedProductVariant];
return cart;
}
......
......@@ -25,7 +25,6 @@
//
#import "BUYImageKit.h"
#import "BUYProduct+Options.h"
#import "BUYOptionSelectionViewController.h"
#import "BUYOptionValue.h"
#import "BUYOptionValueCell.h"
......
......@@ -28,13 +28,14 @@
#import "BUYOptionSelectionNavigationController.h"
#import "BUYOptionSelectionViewController.h"
#import "BUYPresentationControllerForVariantSelection.h"
#import "BUYProduct+Options.h"
#import "BUYProductVariant+Options.h"
#import "BUYProduct.h"
#import "BUYProductVariant.h"
#import "BUYTheme.h"
#import "BUYTheme+Additions.h"
#import "BUYVariantSelectionViewController.h"
#import "BUYVariantOptionBreadCrumbsView.h"
#import "BUYOption.h"
#import "BUYOptionValue.h"
@interface BUYVariantSelectionViewController () <BUYOptionSelectionDelegate>
......
......@@ -65,3 +65,16 @@
#import "BUYStoreViewController.h"
#import "BUYTheme.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 @@
+ (nullable NSString *)buy_emailFromRecord:(nullable ABRecordRef)record;
/**
* 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)updateWithRecord:(nullable ABRecordRef)record NS_DEPRECATED_IOS(8_0, 9_0, "Use the CNContact backed `updateWithContact:` instead");
/**
* 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);
- (void)updateWithContact:(nullable PKContact*)contact NS_AVAILABLE_IOS(9_0);
@end
......@@ -30,7 +30,6 @@
#import "BUYGiftCard.h"
#import "BUYApplePayAdditions.h"
#import "BUYDiscount.h"
#import "BUYAddress+Additions.h"
#import "NSDecimalNumber+BUYAdditions.h"
#import "NSDate+BUYAdditions.h"
......@@ -137,13 +136,11 @@
return email;
}
+ (nonnull BUYAddress *)buy_addressFromRecord:(nullable ABRecordRef)record
- (void)updateWithRecord:(nullable ABRecordRef)record
{
BUYAddress *address = [[BUYAddress alloc] init];
//Grab the simple information
address.firstName = (__bridge_transfer NSString *)ABRecordCopyValue(record, kABPersonFirstNameProperty);
address.lastName = (__bridge_transfer NSString *)ABRecordCopyValue(record, kABPersonLastNameProperty);
self.firstName = (__bridge_transfer NSString *)ABRecordCopyValue(record, kABPersonFirstNameProperty);
self.lastName = (__bridge_transfer NSString *)ABRecordCopyValue(record, kABPersonLastNameProperty);
//Grab the address information
ABMultiValueRef addressMultiValue = ABRecordCopyValue(record, kABPersonAddressProperty);
......@@ -152,17 +149,17 @@
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.
address.address1 = (__bridge NSString *)CFDictionaryGetValue(firstAddress, kABPersonAddressStreetKey);
address.city = (__bridge NSString *)CFDictionaryGetValue(firstAddress, kABPersonAddressCityKey);
address.province = (__bridge NSString *)CFDictionaryGetValue(firstAddress, kABPersonAddressStateKey);
address.zip = (__bridge NSString *)CFDictionaryGetValue(firstAddress, kABPersonAddressZIPKey);
self.address1 = (__bridge NSString *)CFDictionaryGetValue(firstAddress, kABPersonAddressStreetKey);
self.city = (__bridge NSString *)CFDictionaryGetValue(firstAddress, kABPersonAddressCityKey);
self.province = (__bridge NSString *)CFDictionaryGetValue(firstAddress, kABPersonAddressStateKey);
self.zip = (__bridge NSString *)CFDictionaryGetValue(firstAddress, kABPersonAddressZIPKey);
// The Checkout API accepts country OR ISO country code.
// We default to the ISO country code because it's more
// reliable regardless of locale. Fallback to country if
// we do not receive it (iOS 8 sometimes)
address.countryCode = (__bridge NSString *)CFDictionaryGetValue(firstAddress, kABPersonAddressCountryCodeKey);
if ([address.countryCode length] == 0) {
address.country = (__bridge NSString *)CFDictionaryGetValue(firstAddress, kABPersonAddressCountryKey);
self.countryCode = (__bridge NSString *)CFDictionaryGetValue(firstAddress, kABPersonAddressCountryCodeKey);
if ([self.countryCode length] == 0) {
self.country = (__bridge NSString *)CFDictionaryGetValue(firstAddress, kABPersonAddressCountryKey);
}
}
CFSafeRelease(allAddresses);
......@@ -172,43 +169,37 @@
ABMultiValueRef phoneMultiValue = ABRecordCopyValue(record, kABPersonPhoneProperty);
CFArrayRef allPhoneNumbers = ABMultiValueCopyArrayOfAllValues(phoneMultiValue);
if (allPhoneNumbers && CFArrayGetCount(allPhoneNumbers) > 0) {
address.phone = (__bridge NSString *)CFArrayGetValueAtIndex(allPhoneNumbers, 0);
self.phone = (__bridge NSString *)CFArrayGetValueAtIndex(allPhoneNumbers, 0);
}
CFSafeRelease(phoneMultiValue);
CFSafeRelease(allPhoneNumbers);
return address;
}
+ (nonnull BUYAddress *)buy_addressFromContact:(nullable PKContact*)contact
- (void)updateWithContact:(nullable PKContact*)contact
{
BUYAddress *address = [[BUYAddress alloc] init];
address.firstName = contact.name.givenName;
address.lastName = contact.name.familyName;
self.firstName = contact.name.givenName;
self.lastName = contact.name.familyName;
if (contact.postalAddress) {
// break up the address:
NSArray *addressComponents = [contact.postalAddress.street componentsSeparatedByString:@"\n"];
address.address1 = addressComponents[0];
address.address2 = (addressComponents.count > 1) ? addressComponents[1] : nil;
address.city = contact.postalAddress.city;
address.province = contact.postalAddress.state;
address.zip = contact.postalAddress.postalCode;
self.address1 = addressComponents[0];
self.address2 = (addressComponents.count > 1) ? addressComponents[1] : nil;
self.city = contact.postalAddress.city;
self.province = contact.postalAddress.state;
self.zip = contact.postalAddress.postalCode;
// The Checkout API accepts country OR ISO country code.
// We default to the ISO country code because it's more
// reliable regardless of locale. Fallback to country if
// we do not receive it (iOS 8 sometimes)
address.countryCode = [contact.postalAddress.ISOCountryCode length] ? contact.postalAddress.ISOCountryCode : nil;
if (address.countryCode == nil) {
address.country = contact.postalAddress.country;
self.countryCode = [contact.postalAddress.ISOCountryCode length] ? contact.postalAddress.ISOCountryCode : nil;
if (self.countryCode == nil) {
self.country = contact.postalAddress.country;
}
}
address.phone = contact.phoneNumber.stringValue;
return address;
self.phone = contact.phoneNumber.stringValue;
}
@end
......@@ -27,6 +27,9 @@
@import Foundation;
@import PassKit;
#import <Buy/BUYModelManager.h>
@class BUYAddress;
@class BUYClient;
@class BUYCheckout;
@class BUYShop;
......@@ -60,7 +63,7 @@
* @param payment the authorized 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`
......@@ -68,7 +71,7 @@
* @param shippingMethod The selected shipping method
* @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`
......@@ -84,7 +87,7 @@
* @param contact The selected contact
* @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
......@@ -107,3 +110,25 @@
@property (nonatomic, strong, readonly) BUYShop *shop;
@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 @@
//
#import "BUYApplePayHelpers.h"
#import "BUYApplePayAdditions.h"
#import "BUYClient.h"
#import "BUYCheckout.h"
#import "BUYApplePayAdditions.h"
#import "BUYError.h"
#import "BUYAddress+Additions.h"
#import "BUYModelManager.h"
#import "BUYShop.h"
const NSTimeInterval PollDelay = 0.5;
......@@ -88,19 +88,23 @@ const NSTimeInterval PollDelay = 0.5;
completion:(void (^)(PKPaymentAuthorizationStatus status))completion
{
// 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)]) {
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 {
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)]) {
self.checkout.billingAddress = [BUYAddress buy_addressFromContact:payment.billingContact];
self.checkout.billingAddress = [self buyAddressWithContact:payment.billingContact];
} 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) {
......@@ -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
{
......@@ -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
{
self.checkout.shippingAddress = [BUYAddress buy_addressFromRecord:address];
self.checkout.shippingAddress = [self buyAddressWithABRecord:address];
[self updateCheckoutWithAddressCompletion: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];
}
......@@ -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
// 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) {
self.checkout.partialAddresses = YES;
self.checkout.partialAddresses = @YES;
}
if ([self.checkout.shippingAddress isValidAddressForShippingRates]) {
......@@ -338,3 +351,22 @@ const NSTimeInterval PollDelay = 0.5;
}
@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