Giter Club home page Giter Club logo

Comments (5)

ncalteen avatar ncalteen commented on September 2, 2024 1

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.

ncalteen avatar ncalteen commented on September 2, 2024

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.

DboukAli98 avatar DboukAli98 commented on September 2, 2024

Thanks @ncalteen for your help

from rest-samples.

Techboy000 avatar Techboy000 commented on September 2, 2024

@ncalteen Hey :-)
Could you please post the same example for PHP. I also struggle to update passes (object).
Thank you.

from rest-samples.

Techboy000 avatar Techboy000 commented on September 2, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.