# TCGBPurchase Class Reference

Inherits from | NSObject
Declared in | TCGBPurchase.h TCGBPurchase.m

## Overview

The TCGBPurchase class provides several APIs related to purchasing processes.
Before using these APIs, You should be logged in. Because every TCGBPurchase API need informations that are obtained from TCGBServer.
If you do not have been authenticated, you will get TCGB_ERROR_NOT_LOGGED_IN error.

## Request Item List

### `+ requestItemListPurchasableWithCompletion:`

This is the primary method for obtaining ItemList which is registered at NHN Cloud IAP Console and Apple Itunes Connect.

`+ (void)requestItemListPurchasableWithCompletion:(void ( ^ ) ( NSArray<TCGBPurchasableItem*> *_Nullable purchasableItemArray , TCGBError *_Nullable error ))completion`

#### Parameters

`completion` | completion may return the NSArray of TCGBPurchasableItem. If there is an error, TCGBError will be returned.

#### Discussion

Request a item list which is purchasable. This list has items which are registered in both Market(AppStore) and NHN Cloud IAP Console.

Warning: You should call this method after `logged in`, otherwise you will get TCGB_ERROR_NOT_LOGGED_IN error in the completion.

### Usage Example

    - (void)viewDidLoad {
        [TCGBPurchase requestItemListPurchasableWithCompletion:^(NSArray *purchasableItemArray, TCGBError *error) {
            NSMutableArray *itemArrayMutable = [[NSMutableArray alloc] init];
            [purchasableItemArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                TCGBPurchasableItem *item = (TCGBPurchasableItem *)obj;

                [itemArrayMutable addObject:item];
            }];
        }];
    }

#### Declared In

`TCGBPurchase.h`

## Request Purchasing Item

### `+ requestPurchaseWithItemSeq:viewController:completion:`

This is the method to request purchasing item which identifier is itemSeq. There is a viewController parameter and you may put your top most viewController. If you don’t, this method will find out top most view controller and put it in the parameter.

`+ (void)requestPurchaseWithItemSeq:(long)itemSeq viewController:(UIViewController *)viewController completion:(void ( ^ ) ( TCGBPurchasableReceipt *_Nullable purchasableReceipt , TCGBError *_Nullable error ))completion`

#### Parameters

`itemSeq` | itemID which you want to purchase.
`viewController` | represent to current viewcontroller.
`completion` | completion may return the TCGBPurchasableReceipt instance. If there is an error, TCGBError will be returned.

#### Discussion

Request Purchasing Item that has itemId.

Warning: You should call this method after `logged in`, otherwise you will get TCGB_ERROR_NOT_LOGGED_IN error in the completion.

### Usage Example

    - (void)purchasingItem:(long)itemSeq {
        [TCGBPurchase requestPurchaseWithItemSeq:itemSeq viewController:self completion:^(TCGBPurchasableReceipt *purchasableReceipt, TCGBError *error) {
            if (error.code == TCGB_ERROR_PURCHASE_USER_CANCELED) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Item Purchase"
                                                                    message:[NSString stringWithFormat:@"You Canceled."]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
                    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
                    [alertController addAction:defaultAction];

                    [parentViewController presentViewController:alertController animated:NO completion:nil];
                });
            } else if (error) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Item Purchase"
                                                                     message:[NSString stringWithFormat:@"There was an Error : %ld", (long)error.code]
                                                                     preferredStyle:UIAlertControllerStyleAlert];
                    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
                    [alertController addAction:defaultAction];
                    [parentViewController presentViewController:alertController animated:NO completion:nil];
                });
            }
        }];
    }

#### Declared In

`TCGBPurchase.h`

### `+ requestPurchaseWithGamebaseProductId:viewController:completion:`

This is the method to request purchasing item which identifier is gamebaseProductID. There is a viewController parameter and you may put your top most viewController. If you don’t, this method will find out top most view controller and put it in the parameter.

`+ (void)requestPurchaseWithGamebaseProductId:(NSString *)gamebaseProductId viewController:(UIViewController *)viewController completion:(void ( ^ ) ( TCGBPurchasableReceipt *_Nullable purchasableReceipt , TCGBError *_Nullable error ))completion`

#### Parameters

`gamebaseProductId` | gamebaseProductID which you want to purchase.
`viewController` | represent to current viewcontroller.
`completion` | completion may return the TCGBPurchasableReceipt instance. If there is an error, TCGBError will be returned.

#### Discussion

Request Purchasing Item that has gamebaseProductID.

Warning: You should call this method after `logged in`, otherwise you will get TCGB_ERROR_NOT_LOGGED_IN error in the completion.

### Usage Example

- (void)purchasingItem:(NSString )gamebaseProductId { [TCGBPurchase requestPurchaseWithGamebaseProductId:gamebaseProductId viewController:self completion:^(TCGBPurchasableReceipt purchasableReceipt, TCGBError error) { if (error.code == TCGB_ERROR_PURCHASE_USER_CANCELED) { dispatch_async(dispatch_get_main_queue(), ^{ UIAlertController alertController = [UIAlertController alertControllerWithTitle:@“Item Purchase” message:[NSString stringWithFormat:@“You Canceled.”] preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@“OK” style:UIAlertActionStyleDefault handler:nil]; [alertController addAction:defaultAction];

                 [parentViewController presentViewController:alertController animated:NO completion:nil];
             });
         } else if (error) {
             dispatch_async(dispatch_get_main_queue(), ^{
                 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Item Purchase"
                                                                  message:[NSString stringWithFormat:@"There was an Error : %ld", (long)error.code]
                                                                  preferredStyle:UIAlertControllerStyleAlert];
                 UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
                 [alertController addAction:defaultAction];
                 [parentViewController presentViewController:alertController animated:NO completion:nil];
             });
         }

  }]; }

#### Declared In

`TCGBPurchase.h`

### `+ requestPurchaseWithGamebaseProductId:payload:viewController:completion:`

This is the method to request purchasing item which identifier is gamebaseProductID. There is a viewController parameter and you may put your top most viewController. If you don’t, this method will find out top most view controller and put it in the parameter. You can set a custom payment. After payment is completed, you will receive it throgh purchasableReceipt.

`+ (void)requestPurchaseWithGamebaseProductId:(NSString *)gamebaseProductId payload:(nullable NSString *)payload viewController:(UIViewController *)viewController completion:(void ( ^ ) ( TCGBPurchasableReceipt *_Nullable purchasableReceipt , TCGBError *_Nullable error ))completion`

#### Parameters

`gamebaseProductId` | gamebaseProductID which you want to purchase.
`payload` | user define payload
`viewController` | represent to current viewcontroller.
`completion` | completion may return the TCGBPurchasableReceipt instance. If there is an error, TCGBError will be returned.

#### Discussion

Request Purchasing Item that has gamebaseProductID.

Warning: You should call this method after `logged in`, otherwise you will get TCGB_ERROR_NOT_LOGGED_IN error in the completion.

### Usage Example

- (void)purchasingItem:(NSString )gamebaseProductId payload:(NSString )payload { [TCGBPurchase requestPurchaseWithGamebaseProductId:gamebaseProductId payload:payload viewController:self completion:^(TCGBPurchasableReceipt purchasableReceipt, TCGBError error) { if (error.code == TCGB_ERROR_PURCHASE_USER_CANCELED) { dispatch_async(dispatch_get_main_queue(), ^{ UIAlertController alertController = [UIAlertController alertControllerWithTitle:@“Item Purchase” message:[NSString stringWithFormat:@“You Canceled.”] preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction defaultAction = [UIAlertAction actionWithTitle:@“OK” style:UIAlertActionStyleDefault handler:nil]; [alertController addAction:defaultAction];

                 [parentViewController presentViewController:alertController animated:NO completion:nil];
             });
         } else if (error) {
             dispatch_async(dispatch_get_main_queue(), ^{
                 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Item Purchase"
                                                                  message:[NSString stringWithFormat:@"There was an Error : %ld", (long)error.code]
                                                                  preferredStyle:UIAlertControllerStyleAlert];
                 UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
                 [alertController addAction:defaultAction];
                 [parentViewController presentViewController:alertController animated:NO completion:nil];
             });
         }

  }]; }

#### Declared In

`TCGBPurchase.h`

## Request non-consumed Item List

### `+ requestItemListOfNotConsumedWithConfiguration:completion:`

This method provides an item list that have non-consumed.

`+ (void)requestItemListOfNotConsumedWithConfiguration:(TCGBPurchasableConfiguration *)configuration completion:(void ( ^ ) ( NSArray<TCGBPurchasableReceipt*> *_Nullable purchasableReceiptArray , TCGBError *_Nullable error ))completion`

#### Parameters

`configuration` | This configuration is applied to the behavior of IAP information.
`completion` | completion may return the NSArray of TCGBPurchasableReceipt instances. This instance has the paymentSequence, itemSequence, PurchaseToken information. If there is an error, TCGBError will be returned.

#### Discussion

Request a Item List which is not consumed. You should deliver this itemReceipt to your game server to consume it or request consuming API to NHN Cloud IAP Server. You may call this method after logged in and deal with these non-consumed items.

Warning: You should call this method after `logged in`, otherwise you will get TCGB_ERROR_NOT_LOGGED_IN error in the completion.

### Usage Example

     - (void)viewDidLoad {
         TCGBPurchasableConfiguration *config = [[TCGBPurchasableConfiguration alloc] init];
         config.allStores = NO;
         [TCGBPurchase requestItemListOfNotConsumedWithConfiguration:config completion:^(NSArrayTCGBPurchasableReceipt *> *purchasableReceiptArray, TCGBError *error) {
             // should deal with this not-consumed items.
         }];
     }

#### Declared In

`TCGBPurchase.h`

## Request retry purchasing processes

### `+ requestActivatedPurchasesWithConfiguration:completion:`

This method provides item list that are currently subscribed.

`+ (void)requestActivatedPurchasesWithConfiguration:(TCGBPurchasableConfiguration *)configuration completion:(void ( ^ ) ( NSArray<TCGBPurchasableReceipt*> *_Nullable purchasableReceiptArray , TCGBError *_Nullable error ))completion`

#### Parameters

`configuration` | This configuration is applied to the behavior of IAP information.
`completion` | completion pass to API result.

#### Discussion

Request a list of payment products that are currently subscribed.

#### Declared In

`TCGBPurchase.h`

### `+ requestRestoreWithCompletion:`

This method restore auto-renewable subscription products.

`+ (void)requestRestoreWithCompletion:(void ( ^ ) ( NSArray<TCGBPurchasableReceipt*> *_Nullable purchasableReceiptArray , TCGBError *_Nullable error ))completion`

#### Parameters

`completion` | The handler to execute after the restore is complete.

#### Availability

Added 2.6.0.

#### Discussion

Restores the transaction of the AppStore’s auto-renewable subscription product and returns a list of active subscription products.

#### Declared In

`TCGBPurchase.h`

## Other Methods

### `+ requestActivatedPurchasesWithCompletion:`

Added 2.6.0.

`+ (void)requestActivatedPurchasesWithCompletion:(void ( ^ ) ( NSArray<TCGBPurchasableReceipt*> *purchasableReceiptArray , TCGBError *error ))completion`

#### Availability

Added 2.6.0.

#### Declared In

`TCGBPurchase.h`

### `+ setStoreCode:`

This is a method for set up the store. It should be available StoreCode such as “AS”. In normal cases, This method is not needed. Most of application uses “AS”(Apple AppStore) as a store. If not, you should set this value for using a specific store. (Deprecated: As of release 2.79.0, use [TCGBConfiguration configurationWithAppID:appVersion:zoneType:storeCode:] method instead.)

`+ (void)setStoreCode:(NSString *)storeCode`

#### Parameters

`storeCode` | storeCode represents store that item is registered and where you can buy item.

#### Discussion

Set a specific store for access this store.

#### Declared In

`TCGBPurchase.h`

### `+ storeCode`

This method returns a store code that is set up. (Deprecated: As of release 2.79.0, use TCGBConfiguration.storeCode instead.)

`+ (NSString *)storeCode`

#### Return Value

storecode that is already set up.

#### Declared In

`TCGBPurchase.h`

### `+ setPromotionIAPHandler:`

This is a method for add observer to App Store Connect for receiving a notification - paymentQueue:shouldAddStorePayment:forProduct: -.
If you want to deliver some items that have been purchased to user, you should implement this handler.
You can receive a TCGBPurchasableReceipt object and receive whether error or success by callback.
This handler return same information with ‘requestPurchaseWithItemSeq:viewController:completion:’ (Deprecated: As of release 2.80.0, use +[TCGBGamebase addEventHandler:] method instead.

### Usage Example

    - (void)wantToAddTransactionObserver {
        [TCGBPurchase setPromotionIAPHandler:^(TCGBPurchasableReceipt *purchasableReceipt, TCGBError *error) {
            // should deal with this not-consumed items.
        }];
    }</span>)

`+ (void)setPromotionIAPHandler:(void ( ^ ) ( TCGBPurchasableReceipt *purchasableReceipt , TCGBError *error ))handler`

#### Parameters

`handler` | handler may return the result of purchase by App Store Promotion.

#### Discussion

Warning: You should call this method after `logged in`, and you sould call this before an other APIs in TCGBPurchase.
This method can work on iOS11 or later.

#### Declared In

`TCGBPurchase.h`

## deprecated Methods

### `+ requestItemListAtIAPConsoleWithCompletion:`

This is the method for obtaining ItemList which is registered at NHN Cloud IAP Console. (Deprecated: As of release 2.76.0, this API is not used anymore.

### Usage Example

    - (void)viewDidLoad {
        [TCGBPurchase requestItemListAtIAPConsoleWithCompletion:^(NSArray *purchasableItemArray, TCGBError *error) {
            NSMutableArray *itemArrayMutable = [[NSMutableArray alloc] init];
            [purchasableItemArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                TCGBPurchasableItem *item = (TCGBPurchasableItem *)obj;
                [itemArrayMutable addObject:item];
            }];
        }];
    }</span>)

`+ (void)requestItemListAtIAPConsoleWithCompletion:(void ( ^ ) ( NSArray<TCGBPurchasableItem*> *_Nullable purchasableItemArray , TCGBError *_Nullable error ))completion`

#### Parameters

`completion` | completion may return the NSArray of TCGBPurchasableItem. If there is an error, TCGBError will be returned.

#### Discussion

Request a item list which is purchasable. This list has items which are only registered in NHN Cloud IAP Console, not Market(AppStore)

Warning: You should call this method after `logged in`, otherwise you will get TCGB_ERROR_NOT_LOGGED_IN error in the completion.

#### Declared In

`TCGBPurchase.h`

### `+ requestRetryTransactionWithCompletion:`

This method is for retrying failed purchasing proccesses. (Deprecated: As of release 2.6.0, SDK retries transaction automatically in internal process. You don’t need to call explicitly. Result is always success.

### Usage Example

    - (void)viewDidLoad {
        [TCGBPurchase requestRetryTransactionWithCompletion:^(TCGBPurchasableRetryTransactionResult *transactionResult, TCGBError *error) {
            // should deal with this retry transaction result.
            // if succeeded, you may send result to your gameserver and add item to user.
        }];
    }</span>)

`+ (void)requestRetryTransactionWithCompletion:(void ( ^ ) ( TCGBPurchasableRetryTransactionResult *_Nullable transactionResult , TCGBError *_Nullableerror ))completion`

#### Parameters

`completion` | completion may return the TCGBPurchasableRetryTransactionResult which has two member variables which are named ‘successList’ and ‘failList’. These two variables are array of TCGBPurchasableReceipt. Each key has a list of TCGBPurchasableReceipt that has uncompleted purchase information. If there is an error, TCGBError will be returned.

#### Discussion

Request a retrying transaction which is not completed to IAP Server

#### Declared In

`TCGBPurchase.h`