Commit 383d64fd by Dima Bart

Merge pull request #201 from Shopify/task/update-api

Convert checkout API to use tokens instead of full objects
parents 846f4050 0594b7fb
...@@ -126,9 +126,7 @@ NSString * const BUYFakeCustomerToken = @"dsfasdgafdg"; ...@@ -126,9 +126,7 @@ NSString * const BUYFakeCustomerToken = @"dsfasdgafdg";
- (void)testCheckoutPaymentWithOnlyGiftCard - (void)testCheckoutPaymentWithOnlyGiftCard
{ {
BUYCheckout *checkout = [[BUYCheckout alloc] initWithModelManager:self.client.modelManager JSONDictionary:@{@"token": @"abcdef", @"payment_due": @0}]; BUYOperation *task = [self.client completeCheckoutWithToken:@"abcdef" paymentToken:nil completion:^(BUYCheckout *checkout, NSError *error) {}];
BUYOperation *task = [self.client completeCheckout:checkout paymentToken:nil completion:^(BUYCheckout *checkout, NSError *error) {}];
XCTAssertNotNil(task); XCTAssertNotNil(task);
} }
......
...@@ -236,7 +236,7 @@ ...@@ -236,7 +236,7 @@
BUYAddress *address = [self address]; BUYAddress *address = [self address];
[self.client createAddress:address forCustomerID:self.customer.identifier.stringValue callback:^(BUYAddress * _Nullable returnedAddress, NSError * _Nullable error) { [self.client createAddress:address customerID:self.customer.identifier.stringValue callback:^(BUYAddress * _Nullable returnedAddress, NSError * _Nullable error) {
[OHHTTPStubs stubUsingResponseWithKey:@"testCustomerLogin" useMocks:[self shouldUseMocks]]; [OHHTTPStubs stubUsingResponseWithKey:@"testCustomerLogin" useMocks:[self shouldUseMocks]];
...@@ -271,7 +271,7 @@ ...@@ -271,7 +271,7 @@
}]; }];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client getAddressWithID:self.createdAddress.identifier forCustomerID:self.customer.identifier.stringValue callback:^(BUYAddress * _Nullable address, NSError * _Nullable error) { [self.client getAddressWithID:self.createdAddress.identifier customerID:self.customer.identifier.stringValue callback:^(BUYAddress * _Nullable address, NSError * _Nullable error) {
XCTAssertNotNil(address); XCTAssertNotNil(address);
XCTAssertNil(error); XCTAssertNil(error);
...@@ -295,7 +295,7 @@ ...@@ -295,7 +295,7 @@
BUYAddress *modifiedAddress = [self addressByModyfyingAddress:self.createdAddress]; BUYAddress *modifiedAddress = [self addressByModyfyingAddress:self.createdAddress];
[self.client updateAddress:modifiedAddress forCustomerID:self.customer.identifier.stringValue callback:^(BUYAddress * _Nullable returnedAddress, NSError * _Nullable error) { [self.client updateAddress:modifiedAddress customerID:self.customer.identifier.stringValue callback:^(BUYAddress * _Nullable returnedAddress, NSError * _Nullable error) {
XCTAssertNotNil(returnedAddress); XCTAssertNotNil(returnedAddress);
XCTAssertNil(error); XCTAssertNil(error);
...@@ -325,7 +325,7 @@ ...@@ -325,7 +325,7 @@
}]; }];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client deleteAddress:self.createdAddress forCustomerID:self.customer.identifier.stringValue callback:^(BUYStatus status, NSError * _Nullable error) { [self.client deleteAddressWithID:self.createdAddress.identifier customerID:self.customer.identifier.stringValue callback:^(BUYStatus status, NSError * _Nullable error) {
XCTAssertEqual(status, 204); XCTAssertEqual(status, 204);
XCTAssertNil(error); XCTAssertNil(error);
......
...@@ -174,7 +174,7 @@ ...@@ -174,7 +174,7 @@
[OHHTTPStubs stubUsingResponseWithKey:@"testGetCollection_0" useMocks:[self shouldUseMocks]]; [OHHTTPStubs stubUsingResponseWithKey:@"testGetCollection_0" useMocks:[self shouldUseMocks]];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client getCollections:^(NSArray *collections, NSError *error) { [self.client getCollectionsPage:1 completion:^(NSArray<BUYCollection *> *collections, NSUInteger page, BOOL reachedEnd, NSError * _Nullable error) {
XCTAssertNotNil(collections); XCTAssertNotNil(collections);
XCTAssertNil(error); XCTAssertNil(error);
......
...@@ -154,7 +154,7 @@ ...@@ -154,7 +154,7 @@
[OHHTTPStubs stubUsingResponseWithKey:@"testCheckoutFlowUsingCreditCard_2" useMocks:[self shouldUseMocks]]; [OHHTTPStubs stubUsingResponseWithKey:@"testCheckoutFlowUsingCreditCard_2" useMocks:[self shouldUseMocks]];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client getShippingRatesForCheckout:_checkout completion:^(NSArray *shippingRates, BUYStatus status, NSError *error) { [self.client getShippingRatesForCheckoutWithToken:_checkout.token completion:^(NSArray *shippingRates, BUYStatus status, NSError *error) {
XCTAssertNil(error); XCTAssertNil(error);
XCTAssertEqual(status, BUYStatusComplete); XCTAssertEqual(status, BUYStatusComplete);
...@@ -241,7 +241,7 @@ ...@@ -241,7 +241,7 @@
}]; }];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client completeCheckout:_checkout paymentToken:paymentToken completion:^(BUYCheckout *returnedCheckout, NSError *error) { [self.client completeCheckoutWithToken:_checkout.token paymentToken:paymentToken completion:^(BUYCheckout *returnedCheckout, NSError *error) {
XCTAssertNil(error); XCTAssertNil(error);
XCTAssertNotNil(returnedCheckout); XCTAssertNotNil(returnedCheckout);
XCTAssertNotNil(returnedCheckout.order); XCTAssertNotNil(returnedCheckout.order);
...@@ -296,7 +296,7 @@ ...@@ -296,7 +296,7 @@
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client applyGiftCardWithCode:self.giftCardCode toCheckout:_checkout completion:^(BUYCheckout *checkout, NSError *error) { [self.client applyGiftCardCode:self.giftCardCode toCheckout:_checkout completion:^(BUYCheckout *checkout, NSError *error) {
XCTAssertNil(error); XCTAssertNil(error);
_checkout = checkout; _checkout = checkout;
...@@ -331,7 +331,7 @@ ...@@ -331,7 +331,7 @@
[OHHTTPStubs stubUsingResponseWithKey:@"testApplyingInvalidGiftCardToCheckout_2" useMocks:[self shouldUseMocks]]; [OHHTTPStubs stubUsingResponseWithKey:@"testApplyingInvalidGiftCardToCheckout_2" useMocks:[self shouldUseMocks]];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client applyGiftCardWithCode:@"000" toCheckout:_checkout completion:^(BUYCheckout *checkout, NSError *error) { [self.client applyGiftCardCode:@"000" toCheckout:_checkout completion:^(BUYCheckout *checkout, NSError *error) {
XCTAssertNotNil(error); XCTAssertNotNil(error);
XCTAssertEqual(422, error.code); XCTAssertEqual(422, error.code);
...@@ -355,7 +355,7 @@ ...@@ -355,7 +355,7 @@
[OHHTTPStubs stubUsingResponseWithKey:@"testApplyingInvalidGiftCardToCheckout_2" useMocks:[self shouldUseMocks]]; [OHHTTPStubs stubUsingResponseWithKey:@"testApplyingInvalidGiftCardToCheckout_2" useMocks:[self shouldUseMocks]];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client applyGiftCardWithCode:self.giftCardCodeExpired toCheckout:_checkout completion:^(BUYCheckout *checkout, NSError *error) { [self.client applyGiftCardCode:self.giftCardCodeExpired toCheckout:_checkout completion:^(BUYCheckout *checkout, NSError *error) {
XCTAssertNotNil(error); XCTAssertNotNil(error);
XCTAssertEqual(422, error.code); XCTAssertEqual(422, error.code);
...@@ -471,7 +471,7 @@ ...@@ -471,7 +471,7 @@
NSDecimalNumber *originalPaymentDue = [_checkout.paymentDue copy]; NSDecimalNumber *originalPaymentDue = [_checkout.paymentDue copy];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client applyGiftCardWithCode:self.giftCardCode2 toCheckout:_checkout completion:^(BUYCheckout *checkout, NSError *error) { [self.client applyGiftCardCode:self.giftCardCode2 toCheckout:_checkout completion:^(BUYCheckout *checkout, NSError *error) {
XCTAssertNil(error); XCTAssertNil(error);
_checkout = checkout; _checkout = checkout;
...@@ -517,7 +517,7 @@ ...@@ -517,7 +517,7 @@
NSDecimalNumber *originalPaymentDue = [_checkout.paymentDue copy]; NSDecimalNumber *originalPaymentDue = [_checkout.paymentDue copy];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client applyGiftCardWithCode:self.giftCardCode3 toCheckout:_checkout completion:^(BUYCheckout *checkout, NSError *error) { [self.client applyGiftCardCode:self.giftCardCode3 toCheckout:_checkout completion:^(BUYCheckout *checkout, NSError *error) {
//NOTE: Is this test failing? Make sure that you have configured giftCardCode above //NOTE: Is this test failing? Make sure that you have configured giftCardCode above
XCTAssertNil(error); XCTAssertNil(error);
_checkout = checkout; _checkout = checkout;
...@@ -752,7 +752,7 @@ ...@@ -752,7 +752,7 @@
XCTestExpectation *expectation2 = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation2 = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client getShippingRatesForCheckout:_checkout completion:^(NSArray *returnedShippingRates, BUYStatus status, NSError *error) { [self.client getShippingRatesForCheckoutWithToken:_checkout.token completion:^(NSArray *returnedShippingRates, BUYStatus status, NSError *error) {
XCTAssertEqual(BUYStatusPreconditionFailed, status); XCTAssertEqual(BUYStatusPreconditionFailed, status);
[expectation2 fulfill]; [expectation2 fulfill];
}]; }];
...@@ -770,7 +770,7 @@ ...@@ -770,7 +770,7 @@
checkout.token = @"bananaaaa"; checkout.token = @"bananaaaa";
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client getShippingRatesForCheckout:checkout completion:^(NSArray *returnedShippingRates, BUYStatus status, NSError *error) { [self.client getShippingRatesForCheckoutWithToken:checkout.token completion:^(NSArray *returnedShippingRates, BUYStatus status, NSError *error) {
XCTAssertEqual(BUYStatusNotFound, status); XCTAssertEqual(BUYStatusNotFound, status);
[expectation fulfill]; [expectation fulfill];
}]; }];
...@@ -933,11 +933,7 @@ ...@@ -933,11 +933,7 @@
[OHHTTPStubs stubUsingResponseWithKey:@"testGetCheckoutWithInvalidToken_0" useMocks:[self shouldUseMocks]]; [OHHTTPStubs stubUsingResponseWithKey:@"testGetCheckoutWithInvalidToken_0" useMocks:[self shouldUseMocks]];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client getCheckoutWithToken:@"zzzzzzzzzzz" completion:^(BUYCheckout *checkout, NSError *error) {
BUYCheckout *badCheckout = [[BUYCheckout alloc] initWithCartToken:@""];
badCheckout.token = @"zzzzzzzzzzz";
[self.client getCheckout:badCheckout completion:^(BUYCheckout *checkout, NSError *error) {
XCTAssertEqual(404, error.code); XCTAssertEqual(404, error.code);
[expectation fulfill]; [expectation fulfill];
...@@ -1025,7 +1021,7 @@ ...@@ -1025,7 +1021,7 @@
// Expire the checkout // Expire the checkout
XCTestExpectation *expectation2 = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation2 = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client removeProductReservationsFromCheckout:_checkout completion:^(BUYCheckout *returnedCheckout, NSError *error) { [self.client removeProductReservationsFromCheckoutWithToken:_checkout.token completion:^(BUYCheckout *returnedCheckout, NSError *error) {
XCTAssertNil(error); XCTAssertNil(error);
......
...@@ -68,7 +68,7 @@ typedef void (^BUYDataAddressBlock)(BUYAddress * _Nullable address, NSError * _N ...@@ -68,7 +68,7 @@ typedef void (^BUYDataAddressBlock)(BUYAddress * _Nullable address, NSError * _N
* *
* @return The associated BUYRequestOperation * @return The associated BUYRequestOperation
*/ */
- (BUYRequestOperation *)getAddressWithID:(NSNumber *)addressID forCustomerID:(NSString *)customerID callback:(BUYDataAddressBlock)block; - (BUYRequestOperation *)getAddressWithID:(NSNumber *)addressID customerID:(NSString *)customerID callback:(BUYDataAddressBlock)block;
/** /**
* POST /api/customers/:customer_id/addresses * POST /api/customers/:customer_id/addresses
...@@ -80,7 +80,7 @@ typedef void (^BUYDataAddressBlock)(BUYAddress * _Nullable address, NSError * _N ...@@ -80,7 +80,7 @@ typedef void (^BUYDataAddressBlock)(BUYAddress * _Nullable address, NSError * _N
* *
* @return The associated BUYRequestOperation * @return The associated BUYRequestOperation
*/ */
- (BUYRequestOperation *)createAddress:(BUYAddress *)address forCustomerID:(NSString *)customerID callback:(BUYDataAddressBlock)block; - (BUYRequestOperation *)createAddress:(BUYAddress *)address customerID:(NSString *)customerID callback:(BUYDataAddressBlock)block;
/** /**
* PUT /api/customers/:customer_id/addresses/:id * PUT /api/customers/:customer_id/addresses/:id
...@@ -92,19 +92,19 @@ typedef void (^BUYDataAddressBlock)(BUYAddress * _Nullable address, NSError * _N ...@@ -92,19 +92,19 @@ typedef void (^BUYDataAddressBlock)(BUYAddress * _Nullable address, NSError * _N
* *
* @return The associated BUYRequestOperation * @return The associated BUYRequestOperation
*/ */
- (BUYRequestOperation *)updateAddress:(BUYAddress *)address forCustomerID:(NSString *)customerID callback:(BUYDataAddressBlock)block; - (BUYRequestOperation *)updateAddress:(BUYAddress *)address customerID:(NSString *)customerID callback:(BUYDataAddressBlock)block;
/** /**
* DELETE /api/customers/:customer_id/addresses/:id * DELETE /api/customers/:customer_id/addresses/:id
* Delete the customer address * Delete the customer address
* *
* @param address Address to delete * @param addressID Address ID to delete
* @param customerID Customer ID for which to delete the address * @param customerID Customer ID for which to delete the address
* @param block (BUYStatus status, NSError *error) * @param block (BUYStatus status, NSError *error)
* *
* @return The associated BUYRequestOperation * @return The associated BUYRequestOperation
*/ */
- (BUYRequestOperation *)deleteAddress:(BUYAddress *)address forCustomerID:(NSString *)customerID callback:(BUYDataStatusBlock)block; - (BUYRequestOperation *)deleteAddressWithID:(NSNumber *)addressID customerID:(NSString *)customerID callback:(BUYDataStatusBlock)block;
@end @end
......
...@@ -45,7 +45,7 @@ ...@@ -45,7 +45,7 @@
}]; }];
} }
- (BUYRequestOperation *)getAddressWithID:(NSNumber *)addressID forCustomerID:(NSString *)customerID callback:(BUYDataAddressBlock)block - (BUYRequestOperation *)getAddressWithID:(NSNumber *)addressID customerID:(NSString *)customerID callback:(BUYDataAddressBlock)block
{ {
NSURL *route = [self urlForCustomersAddressWithID:customerID addressID:addressID]; NSURL *route = [self urlForCustomersAddressWithID:customerID addressID:addressID];
return [self getRequestForURL:route completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) { return [self getRequestForURL:route completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
...@@ -57,7 +57,7 @@ ...@@ -57,7 +57,7 @@
}]; }];
} }
- (BUYRequestOperation *)createAddress:(BUYAddress *)address forCustomerID:(NSString *)customerID callback:(BUYDataAddressBlock)block - (BUYRequestOperation *)createAddress:(BUYAddress *)address customerID:(NSString *)customerID callback:(BUYDataAddressBlock)block
{ {
NSURL *route = [self urlForCustomersAddressesWithID:customerID]; NSURL *route = [self urlForCustomersAddressesWithID:customerID];
return [self postRequestForURL:route object:@{ @"address" : address.JSONDictionary } completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) { return [self postRequestForURL:route object:@{ @"address" : address.JSONDictionary } completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
...@@ -69,7 +69,7 @@ ...@@ -69,7 +69,7 @@
}]; }];
} }
- (BUYRequestOperation *)updateAddress:(BUYAddress *)address forCustomerID:(NSString *)customerID callback:(BUYDataAddressBlock)block - (BUYRequestOperation *)updateAddress:(BUYAddress *)address customerID:(NSString *)customerID callback:(BUYDataAddressBlock)block
{ {
BUYAssert(address.identifier, @"Failed to update address. Address must have a valid identifier."); BUYAssert(address.identifier, @"Failed to update address. Address must have a valid identifier.");
...@@ -83,11 +83,11 @@ ...@@ -83,11 +83,11 @@
}]; }];
} }
- (BUYRequestOperation *)deleteAddress:(BUYAddress *)address forCustomerID:(NSString *)customerID callback:(BUYDataStatusBlock)block - (BUYRequestOperation *)deleteAddressWithID:(NSNumber *)addressID customerID:(NSString *)customerID callback:(BUYDataStatusBlock)block
{ {
BUYAssert(address.identifier, @"Failed to update address. Address must have a valid identifier."); BUYAssert(addressID, @"Failed to update address. Address must have a valid identifier.");
NSURL *route = [self urlForCustomersAddressWithID:customerID addressID:address.identifier]; NSURL *route = [self urlForCustomersAddressWithID:customerID addressID:addressID];
return [self deleteRequestForURL:route completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) { return [self deleteRequestForURL:route completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
block(response.statusCode, error); block(response.statusCode, error);
}]; }];
......
...@@ -70,6 +70,8 @@ typedef void (^BUYDataGiftCardBlock)(BUYGiftCard * _Nullable giftCard, NSError * ...@@ -70,6 +70,8 @@ typedef void (^BUYDataGiftCardBlock)(BUYGiftCard * _Nullable giftCard, NSError *
@interface BUYClient (Checkout) @interface BUYClient (Checkout)
#pragma mark - Checkout -
/** /**
* Updates or create a checkout based on wether or not it has a token * Updates or create a checkout based on wether or not it has a token
* *
...@@ -102,81 +104,56 @@ typedef void (^BUYDataGiftCardBlock)(BUYGiftCard * _Nullable giftCard, NSError * ...@@ -102,81 +104,56 @@ typedef void (^BUYDataGiftCardBlock)(BUYGiftCard * _Nullable giftCard, NSError *
- (BUYRequestOperation *)createCheckoutWithCartToken:(NSString *)cartToken completion:(BUYDataCheckoutBlock)block; - (BUYRequestOperation *)createCheckoutWithCartToken:(NSString *)cartToken completion:(BUYDataCheckoutBlock)block;
/** /**
* Applies a gift card code to the checkout. * Updates a given BUYCheckout on Shopify.
*
* @param giftCardCode The gift card code to apply on an existing checkout on Shopify. Note: This is not the same as the gift card identifier.
* @param checkout An existing BUYCheckout on Shopify
* @param block (^BUYDataCheckoutBlock)(BUYCheckout *checkout, NSError *error);
*
* @return The associated BUYRequestOperation
*/
- (BUYRequestOperation *)applyGiftCardWithCode:(NSString *)giftCardCode toCheckout:(BUYCheckout *)checkout completion:(BUYDataCheckoutBlock)block;
/**
* Removes a gift card from the checkout.
*
* @param giftCardCode The BUYGiftCard identifier to remove on an existing checkout on Shopify.
* @param checkout An existing BUYCheckout on Shopify
* @param block (^BUYDataCheckoutBlock)(BUYCheckout *checkout, NSError *error);
*
* @return The associated BUYRequestOperation
*/
- (BUYRequestOperation *)removeGiftCard:(BUYGiftCard *)giftCard fromCheckout:(BUYCheckout *)checkout completion:(BUYDataCheckoutBlock)block;
/**
* Retrieves an updated version of a BUYCheckout from Shopify.
* *
* Note: There's no guarantee that the BUYCheckout returned will be the same as the one that is passed in. * Note: There's no guarantee that the BUYCheckout returned will be the same as the one that is passed in.
* We recommended using the BUYCheckout returned in the block. * We recommended using the BUYCheckout returned in the block.
* *
* @param checkout The BUYCheckout to retrieve (updated) from Shopify * Note: A BUYCheckout object with an `orderId` is a completed checkout.
*
* @param checkout The BUYCheckout to updated on Shopify
* @param block (^BUYDataCheckoutBlock)(BUYCheckout *checkout, NSError *error); * @param block (^BUYDataCheckoutBlock)(BUYCheckout *checkout, NSError *error);
* *
* @return The associated BUYRequestOperation * @return The associated BUYRequestOperation
*/ */
- (BUYRequestOperation *)getCheckout:(BUYCheckout *)checkout completion:(BUYDataCheckoutBlock)block; - (BUYRequestOperation *)updateCheckout:(BUYCheckout *)checkout completion:(BUYDataCheckoutBlock)block;
/** /**
* Updates a given BUYCheckout on Shopify. * Retrieves an updated BUYCheckout.
*
* Note: There's no guarantee that the BUYCheckout returned will be the same as the one that is passed in.
* We recommended using the BUYCheckout returned in the block.
* *
* Note: A BUYCheckout object with an `orderId` is a completed checkout. * @param checkoutToken The checkout token for which to retrieve the updated checkout
* * @param block (^BUYDataCheckoutBlock)(BUYCheckout *checkout, NSError *error);
* @param checkout The BUYCheckout to updated on Shopify
* @param block (^BUYDataCheckoutBlock)(BUYCheckout *checkout, NSError *error);
* *
* @return The associated BUYRequestOperation * @return The associated BUYRequestOperation
*/ */
- (BUYRequestOperation *)updateCheckout:(BUYCheckout *)checkout completion:(BUYDataCheckoutBlock)block; - (BUYRequestOperation *)getCheckoutWithToken:(NSString *)checkoutToken completion:(BUYDataCheckoutBlock)block;
/** /**
* Finalizes the BUYCheckout and charges the payment provider (ex: Credit Card, Apple Pay, etc). * Finalizes the BUYCheckout associated with the token and charges the payment provider (ex: Credit Card, Apple Pay, etc).
* This enqueues a completion job on Shopify and returns immediately. * This enqueues a completion job on Shopify and returns immediately.
* You must get the job's status by calling checkCompletionStatusOfCheckout:block * You must get the job's status by calling checkCompletionStatusOfCheckout:block
* *
* Note: There's no guarantee that the BUYCheckout returned will be the same as the one that is passed in. * Note: There's no guarantee that the BUYCheckout returned will be the same as the one that is passed in.
* We recommended using the BUYCheckout returned in the block. * We recommended using the BUYCheckout returned in the block.
* *
* @param checkout The BUYCheckout to complete * @param checkoutToken The checkout token for which to complete checkout. May be nil if the total checkout amount is equal to $0.00
* @param paymentToken Opaque payment token object. May be nil if the total checkout amount is equal to $0.00 * @param paymentToken Opaque payment token object. May be nil if the total checkout amount is equal to $0.00
* @param block (^BUYDataCheckoutBlock)(BUYCheckout *checkout, NSError *error); * @param block (^BUYDataCheckoutBlock)(BUYCheckout *checkout, NSError *error);
* *
* @return The associated BUYOperation * @return The associated BUYOperation
*/ */
- (BUYOperation *)completeCheckout:(BUYCheckout *)checkout paymentToken:(_Nullable id<BUYPaymentToken>)paymentToken completion:(BUYDataCheckoutBlock)block; - (BUYOperation *)completeCheckoutWithToken:(nullable NSString *)checkoutToken paymentToken:(_Nullable id<BUYPaymentToken>)paymentToken completion:(BUYDataCheckoutBlock)block;
/** /**
* Retrieve the status of a BUYCheckout. This checks the status of the current payment processing job for the provided checkout. * Retrieve the status of a checkout with token. This checks the status of the current payment processing job for the provided checkout.
* Once the job is complete (status == BUYStatusComplete), you can retrieve the completed order by calling `getCheckout:completion` * Once the job is complete (status == BUYStatusComplete), you can retrieve the completed order by calling `getCheckout:completion`
* *
* @param checkout The BUYCheckout to retrieve completion status for * @param checkoutToken The checkout token for which to retrieve completion status
* @param block (^BUYDataStatusBlock)(BUYCheckout *checkout, BUYStatus status, NSError *error); * @param block (^BUYDataStatusBlock)(BUYCheckout *checkout, BUYStatus status, NSError *error);
* *
* @return The associated BUYRequestOperation * @return The associated BUYRequestOperation
*/ */
- (BUYRequestOperation *)getCompletionStatusOfCheckout:(BUYCheckout *)checkout completion:(BUYDataStatusBlock)block; - (BUYRequestOperation *)getCompletionStatusOfCheckoutWithToken:(NSString *)checkoutToken completion:(BUYDataStatusBlock)block;
/** /**
* Retrieve the status of a checkout given a URL obtained in the UIApplicationDelegate method `application:sourceApplication:annotation` * Retrieve the status of a checkout given a URL obtained in the UIApplicationDelegate method `application:sourceApplication:annotation`
...@@ -188,20 +165,20 @@ typedef void (^BUYDataGiftCardBlock)(BUYGiftCard * _Nullable giftCard, NSError * ...@@ -188,20 +165,20 @@ typedef void (^BUYDataGiftCardBlock)(BUYGiftCard * _Nullable giftCard, NSError *
*/ */
- (BUYRequestOperation *)getCompletionStatusOfCheckoutURL:(NSURL *)url completion:(BUYDataStatusBlock)block; - (BUYRequestOperation *)getCompletionStatusOfCheckoutURL:(NSURL *)url completion:(BUYDataStatusBlock)block;
#pragma mark - Shipping Rates #pragma mark - Shipping Rates -
/** /**
* Retrieves a list of applicable shipping rates for a given BUYCheckout. * Retrieves a list of applicable shipping rates for a given checkout token.
* Add the preferred/selected BUYShippingRate to BUYCheckout, then update BUYCheckout * Add the preferred/selected BUYShippingRate to BUYCheckout, then update BUYCheckout
* *
* @param checkout The BUYCheckout to retrieve shipping rates for * @param checkoutToken The checkout token for which to retrieve shipping rates
* @param block (^BUYDataShippingRatesBlock)(NSArray *shippingRates, BUYStatus status, NSError *error); * @param block (^BUYDataShippingRatesBlock)(NSArray *shippingRates, BUYStatus status, NSError *error);
* *
* @return The associated BUYRequestOperation * @return The associated BUYRequestOperation
*/ */
- (BUYRequestOperation *)getShippingRatesForCheckout:(BUYCheckout *)checkout completion:(BUYDataShippingRatesBlock)block; - (BUYRequestOperation *)getShippingRatesForCheckoutWithToken:(NSString *)checkoutToken completion:(BUYDataShippingRatesBlock)block;
#pragma mark - Payment Management #pragma mark - Cards -
/** /**
* Prepares a credit card for usage during the checkout process. This sends it to Shopify's secure servers. * Prepares a credit card for usage during the checkout process. This sends it to Shopify's secure servers.
...@@ -222,16 +199,40 @@ typedef void (^BUYDataGiftCardBlock)(BUYGiftCard * _Nullable giftCard, NSError * ...@@ -222,16 +199,40 @@ typedef void (^BUYDataGiftCardBlock)(BUYGiftCard * _Nullable giftCard, NSError *
- (BUYRequestOperation *)storeCreditCard:(BUYCreditCard *)creditCard checkout:(BUYCheckout *)checkout completion:(BUYDataCreditCardBlock)block; - (BUYRequestOperation *)storeCreditCard:(BUYCreditCard *)creditCard checkout:(BUYCheckout *)checkout completion:(BUYDataCreditCardBlock)block;
/** /**
* Applies a gift card code to the checkout.
*
* @param giftCardCode The gift card code to apply on an existing checkout on Shopify. Note: This is not the same as the gift card identifier.
* @param checkout An existing BUYCheckout on Shopify
* @param block (^BUYDataCheckoutBlock)(BUYCheckout *checkout, NSError *error);
*
* @return The associated BUYRequestOperation
*/
- (BUYRequestOperation *)applyGiftCardCode:(NSString *)giftCardCode toCheckout:(BUYCheckout *)checkout completion:(BUYDataCheckoutBlock)block;
/**
* Removes a gift card from the checkout.
*
* @param giftCardCode The BUYGiftCard identifier to remove on an existing checkout on Shopify.
* @param checkout An existing BUYCheckout on Shopify
* @param block (^BUYDataCheckoutBlock)(BUYCheckout *checkout, NSError *error);
*
* @return The associated BUYRequestOperation
*/
- (BUYRequestOperation *)removeGiftCard:(BUYGiftCard *)giftCard fromCheckout:(BUYCheckout *)checkout completion:(BUYDataCheckoutBlock)block;
#pragma mark - Reservations -
/**
* Convenience method to release all product inventory reservations by setting its * Convenience method to release all product inventory reservations by setting its
* `reservationTime` to `@0` and calls `updateCheckout:completion`. We recommend creating * `reservationTime` to `@0` and calls `updateCheckout:completion`. We recommend creating
* a new BUYCheckout object from a BUYCart for further API calls. * a new BUYCheckout object from a BUYCart for further API calls.
* *
* @param checkout The BUYCheckout to expire * @param checkoutToken The checkout token for which to expire
* @param block (^BUYDataCheckoutBlock)(BUYCheckout *checkout, NSError *error); * @param block (^BUYDataCheckoutBlock)(BUYCheckout *checkout, NSError *error);
* *
* @return The associated BUYRequestOperation * @return The associated BUYRequestOperation
*/ */
- (BUYRequestOperation *)removeProductReservationsFromCheckout:(BUYCheckout *)checkout completion:(BUYDataCheckoutBlock)block; - (BUYRequestOperation *)removeProductReservationsFromCheckoutWithToken:(NSString *)checkoutToken completion:(BUYDataCheckoutBlock)block;
@end @end
......
...@@ -62,8 +62,8 @@ typedef void (^BUYClientRequestJSONCompletion)(NSDictionary *json, NSHTTPURLResp ...@@ -62,8 +62,8 @@ typedef void (^BUYClientRequestJSONCompletion)(NSDictionary *json, NSHTTPURLResp
@interface BUYClient (PrivateCheckout) @interface BUYClient (PrivateCheckout)
- (BUYRequestOperation *)beginCheckout:(BUYCheckout *)checkout paymentToken:(id<BUYPaymentToken>)paymentToken completion:(BUYDataCheckoutBlock)block; - (BUYRequestOperation *)beginCheckoutWithToken:(NSString *)checkoutToken paymentToken:(id<BUYPaymentToken>)paymentToken completion:(BUYDataCheckoutBlock)block;
- (BUYRequestOperation *)getCompletionStatusOfCheckoutToken:(NSString *)token start:(BOOL)start completion:(BUYDataStatusBlock)block; - (BUYRequestOperation *)getCompletionStatusOfCheckoutWithToken:(NSString *)token start:(BOOL)start completion:(BUYDataStatusBlock)block;
- (BUYRequestOperation *)getCheckout:(BUYCheckout *)checkout start:(BOOL)start completion:(BUYDataCheckoutBlock)block; - (BUYRequestOperation *)getCheckoutWithToken:(NSString *)checkoutToken start:(BOOL)start completion:(BUYDataCheckoutBlock)block;
@end @end
...@@ -158,16 +158,7 @@ typedef void (^BUYDataProductListBlock)(NSArray<BUYProduct *> * _Nullable produc ...@@ -158,16 +158,7 @@ typedef void (^BUYDataProductListBlock)(NSArray<BUYProduct *> * _Nullable produc
* *
* @return The associated BUYRequestOperation * @return The associated BUYRequestOperation
*/ */
- (BUYRequestOperation *)getProductsByIds:(NSArray *)productIds completion:(BUYDataProductsBlock)block; - (BUYRequestOperation *)getProductsByIds:(NSArray<NSString *> *)productIds completion:(BUYDataProductsBlock)block;
/**
* Fetches the collections on the shop
*
* @param block (^BUYDataCollectionsBlock)(NSArray *collections, NSError *error)
*
* @return The associated BUYRequestOperation
*/
- (BUYRequestOperation *)getCollections:(BUYDataCollectionsBlock)block;
/** /**
* Fetches collections based off page * Fetches collections based off page
......
...@@ -112,13 +112,6 @@ static NSString * const BUYCollectionsKey = @"collection_listings"; ...@@ -112,13 +112,6 @@ static NSString * const BUYCollectionsKey = @"collection_listings";
}]; }];
} }
- (BUYRequestOperation *)getCollections:(BUYDataCollectionsBlock)block
{
return [self getCollectionsPage:1 completion:^(NSArray<BUYCollection *> *collections, NSUInteger page, BOOL reachedEnd, NSError *error) {
block(collections, error);
}];
}
- (BUYRequestOperation *)getCollectionsPage:(NSUInteger)page completion:(BUYDataCollectionsListBlock)block - (BUYRequestOperation *)getCollectionsPage:(NSUInteger)page completion:(BUYDataCollectionsListBlock)block
{ {
NSURL *url = [self urlForCollectionListingsWithParameters:@{ NSURL *url = [self urlForCollectionListingsWithParameters:@{
......
...@@ -45,6 +45,7 @@ ...@@ -45,6 +45,7 @@
- (void)updateWithCart:(BUYCart *)cart; - (void)updateWithCart:(BUYCart *)cart;
- (BUYGiftCard *)giftCardWithIdentifier:(NSNumber *)identifier; - (BUYGiftCard *)giftCardWithIdentifier:(NSNumber *)identifier;
- (void)addGiftCard:(BUYGiftCard *)giftCard;
- (void)removeGiftCardWithIdentifier:(NSNumber *)identifier; - (void)removeGiftCardWithIdentifier:(NSNumber *)identifier;
@end @end
......
...@@ -147,6 +147,11 @@ ...@@ -147,6 +147,11 @@
return [[self.giftCards filteredOrderedSetUsingPredicate:predicate] firstObject]; return [[self.giftCards filteredOrderedSetUsingPredicate:predicate] firstObject];
} }
- (void)addGiftCard:(BUYGiftCard *)giftCard
{
[self.giftCardsSet addObject:giftCard];
}
- (void)removeGiftCardWithIdentifier:(NSNumber *)identifier - (void)removeGiftCardWithIdentifier:(NSNumber *)identifier
{ {
BUYGiftCard *giftCard = [self giftCardWithIdentifier:identifier]; BUYGiftCard *giftCard = [self giftCardWithIdentifier:identifier];
......
...@@ -38,8 +38,8 @@ typedef void (^BUYCheckoutOperationCompletion)(BUYCheckout * _Nullable checkout, ...@@ -38,8 +38,8 @@ typedef void (^BUYCheckoutOperationCompletion)(BUYCheckout * _Nullable checkout,
@property (strong, nonatomic, readonly, nonnull) BUYClient *client; @property (strong, nonatomic, readonly, nonnull) BUYClient *client;
+ (instancetype)operationWithClient:(BUYClient *)client checkout:(BUYCheckout *)checkout token:(id<BUYPaymentToken>)token completion:(BUYCheckoutOperationCompletion)completion; + (instancetype)operationWithClient:(BUYClient *)client checkoutToken:(NSString *)checkoutToken token:(id<BUYPaymentToken>)token completion:(BUYCheckoutOperationCompletion)completion;
- (instancetype)initWithClient:(BUYClient *)client checkout:(BUYCheckout *)checkout token:(id<BUYPaymentToken>)token completion:(BUYCheckoutOperationCompletion)completion; - (instancetype)initWithClient:(BUYClient *)client checkoutToken:(NSString *)checkoutToken token:(id<BUYPaymentToken>)token completion:(BUYCheckoutOperationCompletion)completion;
@end @end
......
...@@ -28,12 +28,11 @@ ...@@ -28,12 +28,11 @@
#import "BUYClient+Checkout.h" #import "BUYClient+Checkout.h"
#import "BUYClient+Internal.h" #import "BUYClient+Internal.h"
#import "BUYPaymentToken.h" #import "BUYPaymentToken.h"
#import "BUYCheckout.h"
#import "BUYRequestOperation.h" #import "BUYRequestOperation.h"
@interface BUYCheckoutOperation () @interface BUYCheckoutOperation ()
@property (strong, nonatomic, readonly) BUYCheckout *checkout; @property (strong, nonatomic, readonly) NSString *checkoutToken;
@property (strong, nonatomic, readonly) id<BUYPaymentToken> token; @property (strong, nonatomic, readonly) id<BUYPaymentToken> token;
@property (strong, nonatomic, readonly) BUYCheckoutOperationCompletion completion; @property (strong, nonatomic, readonly) BUYCheckoutOperationCompletion completion;
...@@ -45,19 +44,19 @@ ...@@ -45,19 +44,19 @@
#pragma mark - Init - #pragma mark - Init -
+ (instancetype)operationWithClient:(BUYClient *)client checkout:(BUYCheckout *)checkout token:(id<BUYPaymentToken>)token completion:(BUYCheckoutOperationCompletion)completion + (instancetype)operationWithClient:(BUYClient *)client checkoutToken:(NSString *)checkoutToken token:(id<BUYPaymentToken>)token completion:(BUYCheckoutOperationCompletion)completion
{ {
return [[[self class] alloc] initWithClient:client checkout:checkout token:token completion:completion]; return [[[self class] alloc] initWithClient:client checkoutToken:checkoutToken token:token completion:completion];
} }
- (instancetype)initWithClient:(BUYClient *)client checkout:(BUYCheckout *)checkout token:(id<BUYPaymentToken>)token completion:(BUYCheckoutOperationCompletion)completion - (instancetype)initWithClient:(BUYClient *)client checkoutToken:(NSString *)checkoutToken token:(id<BUYPaymentToken>)token completion:(BUYCheckoutOperationCompletion)completion
{ {
self = [super init]; self = [super init];
if (self) { if (self) {
_client = client; _client = client;
_checkout = checkout; _token = token;
_token = token; _completion = completion;
_completion = completion; _checkoutToken = checkoutToken;
} }
return self; return self;
} }
...@@ -138,7 +137,7 @@ ...@@ -138,7 +137,7 @@
- (BUYRequestOperation *)createBeginOperation - (BUYRequestOperation *)createBeginOperation
{ {
return [self.client beginCheckout:self.checkout paymentToken:self.token completion:^(BUYCheckout *checkout, NSError *error) { return [self.client beginCheckoutWithToken:self.checkoutToken paymentToken:self.token completion:^(BUYCheckout *checkout, NSError *error) {
if (!checkout) { if (!checkout) {
[self finishWithError:error]; [self finishWithError:error];
} }
...@@ -147,7 +146,7 @@ ...@@ -147,7 +146,7 @@
- (BUYRequestOperation *)createPollOperation - (BUYRequestOperation *)createPollOperation
{ {
BUYRequestOperation *operation =[self.client getCompletionStatusOfCheckoutToken:self.checkout.token start:NO completion:^(BUYStatus status, NSError *error) { BUYRequestOperation *operation =[self.client getCompletionStatusOfCheckoutWithToken:self.checkoutToken start:NO completion:^(BUYStatus status, NSError *error) {
if (status != BUYStatusComplete) { if (status != BUYStatusComplete) {
[self finishWithError:error]; [self finishWithError:error];
} }
...@@ -161,7 +160,7 @@ ...@@ -161,7 +160,7 @@
- (BUYRequestOperation *)createGetOperation - (BUYRequestOperation *)createGetOperation
{ {
return [self.client getCheckout:self.checkout start:NO completion:^(BUYCheckout *checkout, NSError *error) { return [self.client getCheckoutWithToken:self.checkoutToken start:NO completion:^(BUYCheckout *checkout, NSError *error) {
if (checkout) { if (checkout) {
[self finishWithCheckout:checkout]; [self finishWithCheckout:checkout];
} else { } else {
......
...@@ -99,7 +99,7 @@ const NSTimeInterval PollDelay = 0.5; ...@@ -99,7 +99,7 @@ const NSTimeInterval PollDelay = 0.5;
id<BUYPaymentToken> token = [[BUYApplePayToken alloc] initWithPaymentToken:payment.token]; id<BUYPaymentToken> token = [[BUYApplePayToken alloc] initWithPaymentToken:payment.token];
//Now that the checkout is up to date, call complete. //Now that the checkout is up to date, call complete.
[self.client completeCheckout:checkout paymentToken:token completion:^(BUYCheckout *checkout, NSError *error) { [self.client completeCheckoutWithToken:checkout.token paymentToken:token completion:^(BUYCheckout *checkout, NSError *error) {
if (checkout && error == nil) { if (checkout && error == nil) {
self.checkout = checkout; self.checkout = checkout;
...@@ -242,7 +242,7 @@ const NSTimeInterval PollDelay = 0.5; ...@@ -242,7 +242,7 @@ const NSTimeInterval PollDelay = 0.5;
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
__block BUYStatus shippingStatus = BUYStatusUnknown; __block BUYStatus shippingStatus = BUYStatusUnknown;
do { do {
[self.client getShippingRatesForCheckout:self.checkout completion:^(NSArray *shippingRates, BUYStatus status, NSError *error) { [self.client getShippingRatesForCheckoutWithToken:self.checkout.token completion:^(NSArray *shippingRates, BUYStatus status, NSError *error) {
shippingStatus = status; shippingStatus = status;
if (error) { if (error) {
...@@ -295,7 +295,7 @@ const NSTimeInterval PollDelay = 0.5; ...@@ -295,7 +295,7 @@ const NSTimeInterval PollDelay = 0.5;
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
while (checkout.token && checkoutStatus != BUYStatusFailed && checkoutStatus != BUYStatusComplete) { while (checkout.token && checkoutStatus != BUYStatusFailed && checkoutStatus != BUYStatusComplete) {
[self.client getCompletionStatusOfCheckout:self.checkout completion:^(BUYStatus status, NSError *error) { [self.client getCompletionStatusOfCheckoutWithToken:self.checkout.token completion:^(BUYStatus status, NSError *error) {
checkoutStatus = status; checkoutStatus = status;
self.lastError = error; self.lastError = error;
dispatch_semaphore_signal(semaphore); dispatch_semaphore_signal(semaphore);
......
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