Phone 8 Wallet III : WalletAgents
This is the third part of a series about the Windows Phone 8 Wallet.
The menu :
Phone 8 Wallet II : Membership and Transactions
Phone 8 Wallet III : WalletAgents
Phone 8 Wallet IV : Wallet and NFC
Let’s talk about Wallet Agents. Not much to talk about it as it is fairly simple (If you are not using payment as I do).
What is a WalletAgent? MSDN states :
An implementation of a BackgroundAgent specifically designed to enable a wallet app to be notified about wallet operations related to wallet items specific to that app.
So the agent is called when you make specific kind of actions on a wallet item. It is also called (either automatically by the Wallet, or by the user) to refresh the wallet item.
Creating a Wallet Agent
Just add a new Background Agent to your solution :

Open your agent .cs file et make it inherit from WalletAgent instead of BackgroundAgent :
public class ContosoWalletAgent : Microsoft.Phone.Wallet.WalletAgent
Now, override the OnRefreshData method. It is the only method that interest me, as I am only working with WalletTransactionItem.
In this example, I’ll add 10 credits on the balance every time the item is refreshed :
protected override async void OnRefreshData(Microsoft.Phone.Wallet.RefreshDataEventArgs args)
{
foreach (var walletItem in args.Items)
{
var transactionitem = walletItem as WalletTransactionItem;
if (transactionitem == null) continue;
int balance;
if (int.TryParse(transactionitem.DisplayBalance.Split(' ')[0], out balance))
{
balance = balance + 10;
transactionitem.DisplayBalance = balance + " points";
transactionitem.TransactionHistory.Add(Guid.NewGuid().ToString(), new WalletTransaction
{
Description = "Agent operation",
DisplayAmount = "+ 10",
IsTransactionTimeValid = true,
});
await transactionitem.SaveAsync();
}
else
{
// Error, do something
}
}
base.OnRefreshData(args);
}
Don’t forget to call either base.OnRefreshData(args) or NotifyComplete at the end of the method, to warn the task is complete.
Now add the agent solution to your solution :

Now if you refresh the card associated with the agent, you’ll see the balance changing :

Conclusion
Well as you see there is not much to talk about BackgroundAgent when using the WalletTransactionItem. It is different if you use payment and secure elements, but it is a totally different (and complex) world. If I’m lucky to work on such solution one day, I’ll be happy to blog about it.
A small sample app with WalletAgent HERE
