Go to Content



Library

Home > Library

Try & Purchase using In-App Purchase

By bada Master | 7034 views | Jan 19, 2012

Many application developers provide a Lite Version for free to generate user interest and encourage them to download the paid or full version. We will describe the procedures to download an app of this kind in the bada platform.

  For more detailed information, refer to the ‘instructions’ section on the Samsung Apps Seller site (http://seller.samsungapps.com/login/signIn.as?returnURL=/main/sellerMain.as).
 This section describes how to register an app with the Seller Office and implement the code in order to implement the Try & Purchase option that enables users to use a 30 day trial version and then purchase the app item.
Note that, to sell an application, you need to have the ‘commercial seller’ status. For this, developer has to get the approval from the admin by giving proper details.
First connect to the Samsung App Seller Office (http://seller.samsungapps.com/).

  Seller site application page
After logging in, select ‘Item’ in the Application tab.

  Adding an item
 
Press the Add Item Group marked in the figure above. Then the following popup window is displayed.

  The Add Item Group Popup

Fill in the field marked with * in the figure above and click OK.

  The list after adding an Item Group

Confirm that the Item Group has been added in the list above and click on the Item Group to add an Item.

Popup to add item information
 
If you click the Add Item button, a popup window appears where you can enter the item information.
Fill in each of the fields and click Confirm. Then you will be able to confirm that the Item has been successfully added to the Group.

  The screen after adding an Item

As shown in the figure above, you will be able to confirm that the Group ID is set to 100000009099 and the Group Title is set to “Item Test”.

Now, we will briefly describe the code to access the information above in an application.
 
The following configuration is required for the application.

Namespaces used :
 Osp::Commerce::Store, Osp::System, Osp::Graphic, Osp::Io
Header files to #include :
 Fcommerce.h, Fsystem.h
Required privilege level :
 NORMAL
Required privilege groups :
 COMMERCE_SERVICE, SYSTEM_SERVICE

The following sample code shows how to implement purchasing the full version app 30 days after first running the app.

Main Form

When the app is run for the first time, the app saves this date to the database and whenever the app is run, it checks if 30 days have elapsed by comparing today's date to the first saved date.

If 30 days have elapsed after first running the app, the “Your trial version has expired” message appears. In this case, if a user clicks the “Buy It” button, you can get the list containing the item information that you registered above.

If you search the Help document with the Osp::Commerce::Store keyword, you will be able to find 2 interfaces and 4 classes.

Classes

interface   IItemServiceListener
  This interface provides a listener that receives the events associated with the ItemService class. More...
interface   IPurchaseServiceListener
  This interface provides a listener that receives the events associated with the PurchaseService class. More...
class   ItemInfo
  This class provides the methods for accessing the item information from within an application. More...
class   ItemService
  This class provides the functionalities to query information pertaining to the item. More...
class   PurchaseInfo
  This class represents the purchase information in an application. More...
class   PurchaseService
  This class provides methods to purchase an item and to obtain purchase information. More...

Now, we will describe how to code using the above classes and interfaces.

First, you have to inherit IitemServiceListener and IPurchaseServiceListener in the header file to inherit the ItemService and PurchaseService listeners.

#ifndef _TRIALLICENSE_H_
#define _TRIALLICENSE_H_
 
#include
#include
#include
#include
 
class TrialLicense :
       public Osp::Ui::Controls::Form,
       public Osp::Ui::IActionEventListener,
       public Osp::Commerce::Store::IItemServiceListener,
       public Osp::Commerce::Store::IPurchaseServiceListener
{
// Construction
public:
       TrialLicense(void);
       virtual ~TrialLicense(void);
       bool Initialize(void);
 
       // IPurchaseServiceListener
       void OnItemPurchaseCompleted(RequestId reqId, const Osp::Commerce::Store::PaymentId& paymentId, 
                        const Osp::Commerce::Store::ItemId& itemId, const Osp::Base::String& itemName, double price,
                        const Osp::Base::String& currency, const Osp::Base::DateTime& purchaseDate, result r,
                        const Osp::Base::String& errorCode, const Osp::Base::String& errorMsg);
 
       void OnPurchaseInfoReceivedN(RequestId reqId, Osp::Base::Collection::IList* pPurchaseInfoList,
                                      result r, const Osp::Base::String& errorCode, const Osp::Base::String& errorMsg);
 
       // IItemServiceListener
       void OnItemListReceivedN(RequestId reqId, Osp::Base::Collection::IList* pItemInfoList, result r,
                                  const Osp::Base::String& errorCode, const Osp::Base::String& errorMsg);
 
       void GetPurchaseInfo(void);
 
           // Implementation
protected:
///////////////////////////////////////////////////////////////////
// declaration of member variables (Omitted)
///////////////////////////////////////////////////////////////////

 
public:
       virtual result OnInitializing(void);
       virtual result OnTerminating(void);
       virtual void OnActionPerformed(const Osp::Ui::Control& source, int actionId);
       result Validation(void);
};
 
#endif //_TRIALLICENSE_H_


If the Buy it button is pressed, you can request the lists of items that can be purchased by calling the GetItemList() method.
Here, set the parameter groupId to the Item Group ID you entered on the seller site.
 
The following code is executed in the OnActionPerformed() method when the Buy it button is pressed.

result r = E_SUCCESS;
RequestId reqId;
String groupId = ITEM_GROUP_ID;
r = __pItemService->GetItemList(groupId, 1, 1, reqId);
if(!IsFailed(r))
{
    __pPopup = new Popup();
    __pPopup->Construct(true,Dimension(400, 200));
    __pPopup->SetTitleText(L"Connecting...");
    __pPopup->SetShowState(true);
    __pPopup->Show();
}
else
{
   MessageBox messageBox;
   messageBox.Construct(L"", L"Cannot access stote at the moment, try it later!",
                                       MSGBOX_STYLE_NONE, 1000);
    int modeResult;
    messageBox.ShowAndWait(modeResult);
}

If the GetItemList() function is called, the OnItemListReceivedN() function which is the callback function of IitemServiceListener is called.
The following is the OnItemListReceivedN() callback function.

void TrialLicense::OnItemListReceivedN(RequestId reqId, Osp::Base::Collection::IList* pItemInfoList, \
              result r, const Osp::Base::String& errorCode, const Osp::Base::String& errorMsg)
{
    if(!IsFailed(r))
    {
          if(pItemInfoList->GetCount() > 0)
          {
                IEnumerator* pIEnum = pItemInfoList->GetEnumeratorN();
                while((pIEnum->MoveNext()) == E_SUCCESS){
                     ItemInfo* pInfo = static_cast(pIEnum->GetCurrent());
                     __itemId = pInfo->GetId();
 
                     ///////////////////////////////////////////////////////////////////
                     // This part is to implement the popup window.(Omitted)
 
                   ///////////////////////////////////////////////////////////////////

               }
               delete pIEnum;
        }
        pItemInfoList->RemoveAll(true);
       delete pItemInfoList;
    }
    else
    {
    ///////////////////////////////////////////////////////////////////
    // This part is to handle exceptions.(Omitted)
    ///////////////////////////////////////////////////////////////////

 
   }
}

  The popup window that shows the Title and ID of the Item in the Item Group
 
The callback parameter pItemInfoList contains the ItemInfo class values.The source code above displays the item title in the ItemInfo by using the GetName() function in the title field in the popup window and creates and displays the popup window asking users whether they want to purchase the corresponding item.
 
The operations after pressing the Confirm button in the popup control are as follows.
result r = E_SUCCESS;
RequestId reqId = INVALID_REQUEST_ID;
String groupId = ITEM_GROUP_ID;
r = __pPurchaseService->PurchaseItem(groupId, __itemId, reqId);
if (IsFailed(r) || reqId == INVALID_REQUEST_ID) {
    int modalResult;
    MessageBox msgBox;
    if(r == E_INVALID_OPERATION){
            msgBox.Construct(L"Purchase", L"Prior purchase has not finished.", MSGBOX_STYLE_NONE, 1500);
    }
   else{
            msgBox.Construct(L"Purchase", L"Failed!", MSGBOX_STYLE_NONE, 1500);
   }
   msgBox.ShowAndWait(modalResult);
}
In the code above, the PurchaseItem function is called using the PurchaseService class to perform the purchase of the corresponding item. The arguments for the function are set to the GroupId value described above and the __itemId value delivered through the ItemInfo from the OnItemListReceivedN() function. If the function is called, the OnItemPurchaseComplete() function is called by IPurchaseService.
 At this time, you can confirm that the function has been performed normally through the result value of the function.
The following is the OnItemPurchaseComplete() callback function.
void TrialLicense::OnItemPurchaseCompleted(RequestId reqId,
                                                           const Osp::Commerce::Store::PaymentId& paymentId,
                                                           const Osp::Commerce::Store::ItemId& itemId,
                                                           const Osp::Base::String& itemName, double price,
                                                           const Osp::Base::String& currency,
                                                           const Osp::Base::DateTime& purchaseDate, result r,
                                                           const Osp::Base::String& errorCode, const Osp::Base::String& errorMsg)
{
       if(__pPopup)
       {
             __pPopup->SetShowState(false);
             delete __pPopup;
              __pPopup = null;
       }
       if(IsFailed(r)){
           ///////////////////////////////////////////////////////////////////
           // This part is to handle exceptions.(Omitted)
           ///////////////////////////////////////////////////////////////////

       }
       else{
              int modalResult;
              MessageBox msgBox;
              msgBox.Construct(L"Purchase", L"Completed!", MSGBOX_STYLE_NONE, 1500);
              msgBox.ShowAndWait(modalResult);
              GetPurchaseInfo();
       }
}
The above code shows the popup window that shows whether the operation has been successfully completed according to the result value in the code. If the operation is completed successfully, the GetPurchaseInfo() function is called to acquire the list of the items purchased in the app.
void
TrialLicense::GetPurchaseInfo(void)
{
      result r = E_SUCCESS;
      RequestId reqId = INVALID_REQUEST_ID;
      DateTime* pStartDate = new DateTime();
      DateTime* pEndDate = new DateTime();
      String groupId = ITEM_GROUP_ID;
      StartDate->SetValue(2009, 10, 1);
      pEndDate->SetValue(2012, 12, 31);
      r = __pPurchaseService->GetPurchaseInfo(groupId, null, pStartDate, pEndDate, 1, 10, reqId);
      if (IsFailed(r)) {
           int modalResult;
           MessageBox* pMsgBox = new MessageBox();
           pMsgBox->Construct(L"Warning", L"Getting Purchased Item List Failed!", MSGBOX_STYLE_NONE, 1500);
           pMsgBox->ShowAndWait(modalResult);}
           delete pStartDate;
           delete pEndDate;
}
In the code above, the GetPurchaseInfo() function is callekd to acquire the list of the items purchased in the application.At this time, the arguments are GroupId, the optional argument PaymentId, the start and end dates to search for purchases, the start and end point values of the list values and the RequestId value.

The following is a definition of the function.
result  GetPurchaseInfo (const Osp::Base::String &itemGroupId, PaymentId *pPaymentId,
                                    Osp::Base::DateTime *pStartDate, Osp::Base::DateTime *pEndDate, int startNum,
                                    int endNum, RequestId &reqId)

If the above function is called, the OnPurchaseInfoReceivedN() function of IPurchaseServiceListener is called as a callback function and the list of the actually purchased items is delivered through the argument pPurchaseInfoList.
void
TrialLicense::OnPurchaseInfoReceivedN(RequestId reqId, Osp::Base::Collection::IList* pPurchaseInfoList, \
result r, const Osp::Base::String& errorCode, const Osp::Base::String& errorMsg)
{
      if (IsFailed(r)) {
                int modalResult;
                MessageBox msgBox;
                 msgBox.Construct(L"Warning", L"Getting Purchase Info Failed!", MSGBOX_STYLE_NONE, 1500);
                 msgBox.ShowAndWait(modalResult);
       }else {
       if(pPurchaseInfoList->GetCount() > 0){
                 IEnumerator* pIEnum = pPurchaseInfoList->GetEnumeratorN();
                 while((pIEnum->MoveNext()) == E_SUCCESS){
                 PurchaseInfo* pPurchaseInfo = (PurchaseInfo*)pIEnum->GetCurrent();
                __pLicenseInfo->SetText(pPurchaseInfo->GetReserved1());
               RequestRedraw(true);
        }
     } 
     pPurchaseInfoList->RemoveAll(true);
    delete pPurchaseInfoList;
  }
}
pPurchaseInfoList in the above function contains the values that can be acquired from the PurchaseInfo class and you can identify whether the user has purchased the full version of the app or not.
Did this document help you? Yes | It's good,but ... | Not Helpful