Two of the many highlighted features in Windows Phone 8 are Wallet and In-App Purchase options. In this post we will have an overview on these features and develop sample applications for demonstrating Deal, Membership Card, Payment Card, In-App Purchase features. You can find the source code of the sample Windows Phone projects mentioned in this blog at https://aka.ms/walletdemo. Once you compile and install these projects on the emulator you will have the screenshot of the applications’ list shown in figure 1. All these features are dependent on the digital Wallet that comes with Windows Phone 8 OS by default. Wallet functionality requires comprehensive security consideration on a mobile device. Some of these features are Trusted Boot and code signing, attack surface reduction, user consent and control, isolation, new Internet Explorer with SmartScreen, device encryption etc. You may find more security overview from this URL address.
Apart from the existing start screen security, like setting a PIN on the lock screen, user can additionally set a PIN on the wallet. Without entering this PIN, Wallet features can’t be initiated. User can set a PIN on the wallet by following the menu shown in figure 2.
Some of the features that you can implement on Windows Phone 8 with sample scenarios are listed below.
Deals: Imagine that you start to use your favorite coffee shop’s Windows Phone App that you downloaded from Windows Phone App Store (In the attached sample code pack, this app is called “W Deals”). In addition to existing features etc. in the app, your coffee shop might send you a deal through the app according to:
Physical location (closer to a shop)
1000th click on an item in the app
You can benefit from this digital deal item on your phone by using the barcode image or send the promocode by NFC etc.
Membership Card: Imagine that your favorite coffee shop gives you a digital membership card. In each coffee purchase (either online coffee capsule purchase or onsite purchase) you may earn loyalty points. Collecting this points, you can order additional goods from the shop. These points or promotions are delivered to you either by NFC tap or through the cloud etc.
Payment Card: Even more, your favorite coffee shop may give you a digital payment card where you can use it online or onsite to purchase products with prepaid balance and see all transactions that you made with this card.
In-App Purchase: Another sample is based on your favorite game app. Your game app creates a digital payment card with custom cash unit (i.e. Gold Coins, Galaxy Lira etc.) or real credit card can be used. You can purchase digital products such as game levels, avatars etc. with this card. These digital products can be defined and published through Windows Phone store by app developer.
1. Wallet featured application development
If you are developing application that uses the wallet features, you must set the ID_CAP_WALLET capability under the Capabilities tab in WMAppManifest.xml file. Otherwise your app will crash in case it tries to access the wallet. Also if you are using In-App Payment feature (last sample project uses this feature) than you also have to set the ID_CAP_WALLET_PAYMENTINSTRUMENTS capability. Capabilities window is shown in figure 3.
For developing applications that uses the deal feature, you have to add Microsoft.Phone.Wallet namespace inside the code page.
Below code is used to add a digital Deal item into the Wallet. Your app may communicate with the company servers over cloud and install an appropriate deal into the wallet of the client’s phone.
In this code snippet, GetBitmapSource function loads an image from the app resource and return it as a BitmapSource typed data. In this sample code we store the images inside the app’s resources folder but you can dynamically download and set appropriate images from your company servers. For accessing the BitmapSource class following namespace should be added on your code page.
Also the name that you set in the contractor of the Deal class is used as an identifier while you search an item in the wallet. Below code uses ContosoDeal identifier to refer this specific deal.
Deal DI = new Deal("ContosoDeal");
MerchantName, Logo336x336, BarcodeImage etc. properties that are used in the above code are some of the members of the deal class. Once you set the values of these members, they will be visible as a digital property of the Deal in the wallet. For demonstration purposes we have used static images and predefined values in this code sample. You can dynamically set appropriate values.
Also the below line of code is used to automatically launch the Wallet after installing a Deal inside it.
Below are the screenshots from the deal item installed on the wallet. You can see the visual correspondence of the values that we set in the sample code above.
If the customer opens the Deal from the Wallet and tap the “open app” link shown in figure 4.c, application that installed this deal will be launched with specified custom parameters. This parameters are programmatically determined during the installation or updated with a wallet agent (will be mentioned later) that is periodically launched by OS. With this agent you can communicate with servers on the cloud etc. with various scenarios.
i.e. You can send promocode that is associated with this deal through the coffee shop’s app and consume the deal through the app by transferring the code from phone to cashier’s device by NFC, over Internet etc. scenarios. Communication between the phone app and the deal item in the wallet will be through this parameter submission.
Data Transfer between a wallet item and the owning application:
Like in the Deal item, other digital items in the wallet can also transfer data to the owner app that creates the item. In case of the Deal item, you can set the NavigationUri member of the class with the data to be transferred.
DI.NavigationUri = new Uri("/mainpage.xaml?par1=val1;par2=val2;…", UriKind.Relative);
In the above sample code, if the user taps the “open app” link as shown in figure 4.c, app that created the Deal item will be launched and mainpage.xaml windows will be opened. Finally “par1=val1;par2=val2;…” values will be passed to this window. By this way you can open not only the mainpage.xaml window but any window that exist in the app by passing any value to it. Inside the target Windows’s code page, you can retrieve and parse the transferred data as shown below:
1.2. Membership Card
Adding items such as Membership, Payment cards etc. requires an access to AddWalletItemTask class. So first of all we need to add the following namespaces in to our code page.
After adding this namespaces, create a static instance of the AddWalletItemTask class for further use and add an event handler to catch any success or failure notifications coming from this instance.
AWIT_Completed method that we implemented will be called whenever a digital item is installed to the wallet. So inside this method we can check and inform the user as if the installation operation is successful or not.
For adding a Membership Card, an instance from WalletTransactionItem class need to be created. Providing a unique string identifier as a parameter to the constructor of this class will let you a direct access to the specific item installed in the wallet.
Below code creates a WalletTransactionItem and sets some of the members of this item with static sample data.
In the above code snippet MemberDetails and PublisherDetails are user defined classes to hold and pass the card member and publisher details within a single object.
Following code sets the AddWalletItemTask type object that we statically created at the main class’s initialization and shows the “Add to Wallet” Membership Card item (WalletTransactionItem) to the user.
AWIT.Item = wtiMember;
Because of security reasons, it is not possible to directly insert an item to the wallet. First you have to get a confirmation from the user (figure 5.) than if the user accepts, it will be installed to the wallet. If the user set a PIN to the wallet than user will be asked to enter the PIN to unlock the wallet before the installation.
All these operations are performed through WalletTransactionItem.
Programming Agents to synchronize your digital item details:
In windows phone 8, it is possible to implement digital agents which are periodically called by OS independent from the associated application’s lifetime. Even the application is not running, OS will run the piece of code implemented in the agent. Because of battery lifetime considerations, time intervals to call this agents are not very short.
To add an agent into your application, first you have to create a separate agent project and add generated DLL from this agent as a reference to the project of your main application. To create an agent project in Visual Studio, you select “Windows Phone Class Library” template under Windows Phone project templates. Replace the content of the generated code file with the following lines of code.
When the agent is invoked by the OS, codes under OnRefreshData method will be executed. Here you can communicate with your server on the cloud to see if there is a promotion, deal etc. specific to the customer that owns the associated digital wallet item. In the above code example, we check if today is customer’s birthday. If yes, we add a special notification near the digital item’s icon (see figure 5.b) by giving piece of information. If the customer tap’s over this notification, associated application will be launched with the parameters specified by the following code in the agent.
WTI.MessageNavigationUri = new Uri("/mainpage.xaml?Param...", UriKind.Relative);
Class, namespace, Project names that you are using in the agent code is very important for referencing of the agent in the main application.
To associate the agent in the main application, open the WMAppManifest.xml file in the main app project. Update the <Tasks> node with the following code.
If you are using different namespace, class etc. names, update the BackgroundServiceAgent node in the above code appropriately. If you fail to write correct information, you will get something like “file not found” error when the agent is invoked etc.
Finally don’t forget to add the agent in to the references of the main application as shown in figure 7.
1.3. Payment Card
Programming logic behind the Payment Cards are similar to the one in Membership Cards mentioned in the previous section. In addition you have Credit Limit, Available Credit, and Transactions like features in this digital item. Instead of using WalletTransactionItem, for payment cards PaymentInstrument class has to be used. Depending on the card type (Credit, Debit, Credit&Debit, and INICIS), PaymentInstrumentKinds property of the object in the following line of code has to be set with appropriate value.
PI.PaymentInstrumentKinds = PaymentInstrumentKinds.Credit;
Like in the Membership Card, you add the item into the Wallet with PaymentInstrument object. Again the user authorization (Figure 8.b) is mandatory to add the card into the wallet.
Figure 8.a.b.c.d.e.f.g.h.i (from left to right): a) Sample Application b-c) Confirmation message to install the payment card d-f) Details of the payment card installed in Wallet. g) After pressıng “Create a Random Transaction” button in the app. h-i) List of transactions and their details in the wallet.
After having the card in the wallet, it is possible to create transactions through this card.
With the below code snippet it is possible to create a transaction and update the credit details etc. on the Payment Card.
If you are offering a custom Payment Card to your customer, you can integrate digital PaymentCard feature of Windows Phone 8 into your app and synchronize the credit amount, transactions etc. with the records on your server. You can use this i.e. prepaid digital card for buying items from your online or offsite stores through your app installed to the customer’s phone. Synchronization can be made through the agent mentioned in previous section or at the payment time.
2. In-App Purchase (IAP)
Have you tried the game “Dredd vs. Zombies” that is available for free on Windows Phone 8 Store? It is a nice application that perfectly utilizes the In-App purchase monetizing model in a free app. Dredd vs. Zombies is a fantastic game that you can buy levels, guns, achievements etc. by In-App purchase feature. If you haven’t tried yet, it is worth to try for the sake of understanding this topic in action.
With IAP you can sell digital items over Windows Phone Store. Yes, store has the required framework for defining and selling your digital items that are associated with your app. To benefit from IAP feature, first you have to develop and publish the IAP featured app on the store. For testing purposes you can initially publish your app as Beta which takes around 24 hours to pass the store certification. Within the same time you can also define In-App purchasable products on DevCenter which also requires certification process before being published on store.
In figure 9, dashboard of the “In App Purchase Demo” named application on DevCenter is shown.
Figure 9: App Dashboard on DevCenter (http://dev.windowsphone.com)
From your application’s dashboard, just switch to the “Products” page (see figure 10.) to see the digital items associated with that application. From this page, you can add new IAP items that are associated with your application. In figure 10, there are two sample items created for demo purposes. You can define two types of IAP item on the store. One is a durable, other is consumable. Durable items are the ones that your customer would buy one time and will use forever, i.e. game levels. Consumables are the ones that are used one time and the customer needs to purchase again for the second time use. i.e. bullets in Dredd game. You can set any price to IAP digital items and customers can make the payment with a credit card.
In figure 12.a main screen of the sample IAP featured app is shown. If you tap the “Order First Consumable Product” button, items on the store that are associated with your app will be listed. When you upload an app to the store with IAP items, association between items and the app are built over the IDs defined in WMAppManifest.xml (see figure 11). If you change this IDs, association will be lost.
For retrieving the list of associated items that exist in the store, following code is used.
var ProdList = await CurrentApp.LoadListingInformationAsync();
For purchasing one of the listed digital items, following code can be used.
It is developer’s responsibility to store the purchase and logging information. Developer can use the retrieved purchase information (see figure 12.c) to track the customer orders. To keep the log of purchase for further query, developer can create a transaction to store the purchase information on company server over cloud. If the communication between the app on customer phone and the servers on cloud is successful, developer can call the following line
to commit the transaction as there is no Internet connection failure etc.
Follow more at https://twitter.com/mustafakasap