Comments (5)
That's exactly it :) I'm working on adding these examples to the repo for each language, as well as a few others I noted are missing.
If you need help getting a complete example for updating passes via PHP please let me know and I can add something here in the meantime!
from rest-samples.
Hello,
I'd be happy to help out with that! It's on my task list to include code samples for this, notifications, and a few other use cases, so I will update this repo soon and reflect this in the documentation. In the meantime, there are a couple different options for updating loyalty cards...
update
vs. patch
methods
At the beginning, I have to stress the difference here. When you use the update
method for a class or object, that replaces the existing configuration completely. This is best when you plan to completely change the way the class or object works, and you want to ensure no old information is left behind.
When you use the patch
method, only the fields you include in the request are updated; existing fields will not be changed unless they're included in the request. This is best when you want to update some fields, but not all (such as changing a user's points balance).
I'll include examples of patch
method calls below. If you're looking to do an update
instead, the code would be the same except for the method being called. However, with an update
, you need to include the full class or object definition.
Update a class
Any updates to a LoyaltyClass
object will affect all users who have a loyalty card with that classId
. This is useful when you want to do things that affect all card holders, such as add messages to the card details or update the hero image that is displayed on the card.
using Google.Apis.Walletobjects.v1.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
string issuerId = "ISSUER_ID";
string classSuffix = "demo_loyalty";
// Ignore null values when serializing to/from JSON
JsonSerializerSettings excludeNulls = new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore
};
// Create the demo class instance
DemoLoyalty demo = new DemoLoyalty();
// Create the authenticated HTTP client
demo.Auth();
// Check if the class already exists
string classId = $"{issuerId}.{classSuffix}";
Stream responseStream = DemoLoyalty.service.Loyaltyclass
.Get(classId)
.ExecuteAsStream();
StreamReader responseReader = new StreamReader(responseStream);
JObject jsonResponse = JObject.Parse(responseReader.ReadToEnd());
if (jsonResponse.ContainsKey("error"))
{
if (jsonResponse["error"].Value<int>("code") == 404)
{
// Class does not exist
Console.WriteLine($"Class {classId} not found!");
}
else
{
// Something else went wrong...
Console.WriteLine(jsonResponse.ToString());
}
}
else
{
// Class exists
// Update the loyalty program logo and name
// ReviewStatus must be DRAFT or UNDER_REVIEW
LoyaltyClass patchClass = new LoyaltyClass()
{
ReviewStatus = "UNDER_REVIEW",
LocalizedProgramName = new LocalizedString()
{
DefaultValue = new TranslatedString()
{
Language = "en-US",
Value = "Nick's Coffee and Iced Tea"
}
},
ProgramLogo = new Image
{
SourceUri = new ImageUri
{
Uri = "http://farm8.staticflickr.com/7340/11177041185_a61a7f2139_o.jpg"
},
ContentDescription = new LocalizedString
{
DefaultValue = new TranslatedString
{
Language = "en-US",
Value = "Logo description"
}
}
}
};
responseStream = DemoLoyalty.service.Loyaltyclass
.Patch(patchClass, classId)
.ExecuteAsStream();
responseReader = new StreamReader(responseStream);
jsonResponse = JObject.Parse(responseReader.ReadToEnd());
Console.WriteLine("Class patch response");
Console.WriteLine(jsonResponse.ToString());
}
Update an object
The process is much the same as when you update a class, however updating an object only affects the user(s) who have saved that specific object to their Google Wallet app. I think this aligns with the use case you mentioned, where you'd like to update a user's points balance.
using Google.Apis.Walletobjects.v1.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
string issuerId = "ISSUER_ID";
string objectSuffix = "demo_object";
// Ignore null values when serializing to/from JSON
JsonSerializerSettings excludeNulls = new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore
};
// Create the demo class instance
DemoLoyalty demo = new DemoLoyalty();
// Create the authenticated HTTP client
demo.Auth();
// Check if the object already exists
string objectId = $"{issuerId}.{objectSuffix}";
Stream responseStream = DemoLoyalty.service.Loyaltyobject
.Get(objectId)
.ExecuteAsStream();
StreamReader responseReader = new StreamReader(responseStream);
JObject jsonResponse = JObject.Parse(responseReader.ReadToEnd());
if (jsonResponse.ContainsKey("error"))
{
if (jsonResponse["error"].Value<int>("code") == 404)
{
// Object does not exist
Console.WriteLine($"Object {objectId} not found!");
}
else
{
// Something else went wrong...
Console.WriteLine(jsonResponse.ToString());
}
}
else
{
// Object exists
// Update the user's point's balance (e.g. they earned 100 points)
// Previous balance can be found in the GET response
LoyaltyObject patchObject = new LoyaltyObject()
{
LoyaltyPoints = new LoyaltyPoints()
{
Balance = new LoyaltyPointsBalance()
{
Int__ = jsonResponse["loyaltyPoints"]["balance"].Value<int>("int") + 100
}
}
};
responseStream = DemoLoyalty.service.Loyaltyobject
.Patch(patchObject, objectId)
.ExecuteAsStream();
responseReader = new StreamReader(responseStream);
jsonResponse = JObject.Parse(responseReader.ReadToEnd());
Console.WriteLine("Object patch response");
Console.WriteLine(jsonResponse.ToString());
}
I hope this information helps! I'll work on adding these as more official samples into the repo and updating the documentation :)
from rest-samples.
Thanks @ncalteen for your help
from rest-samples.
@ncalteen Hey :-)
Could you please post the same example for PHP. I also struggle to update passes (object).
Thank you.
from rest-samples.
@ncalteen Hey :-) Could you please post the same example for PHP. I also struggle to update passes (object). Thank you.
Okay found it. $response = $this->service->loyaltyobject->update($objectId, $loyaltyObject);
Thanks.
from rest-samples.
Related Issues (20)
- GITHUB_TOKEN Permissions Actions: write Checks: write Contents: write Deployments: write Discussions: write Issues: write Metadata: read Packages: write Pages: write PullRequests: write RepositoryProjects: write SecurityEvents: write Statuses: write Secret source: Actions Prepare workflow directory Prepare all required actions Getting action download info Download action repository 'actions/checkout@v4' (SHA:b4ffde65f46336ab88eb53be808477a3936bae11) Download action repository 'actions/setup-python@v4' (SHA:65d7f2d534ac1bc67fcd62888c5f4f3d2cb2b236) Complete job name: update-date
- 0xbB297d8E8bE2D24FC54B2Ef834D9FbE2c373e064
- 18ga7JY5DeQrGxms1k6k4Qh2DDX43ntiZU HOT 3
- Barcode type must be set error since update HOT 1
- PHP example with ServiceAccountCredentials class vs keyFilePath HOT 8
- Google service "Walletobjects" does not exist or was removed previously HOT 2
- It my HOT 1
- Notifications not working HOT 1
- Walletobjects api service not on Maven? HOT 4
- Generic pass HOT 1
- Creating generic pass with template info using google client library HOT 1
- Problem with generate google passes using .Net C# HOT 2
- @miguelRedit I'm also looking for a way to create a google generic pass using C#, but no results have been found. Have to got anything? Please let me know
- Grateful
- I want Google wallet accessible in Nigeria HOT 1
- Google Wallet Generic Pass Removing Card Title and Custom Font Color HOT 1
- mubashrfrn.md
- Ali AlAli
- https://aljbore.co.uk/
- Getting permission denied while creating passClass HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rest-samples.