Example #1
0
        /// <summary>
        /// Get an object with Google Pay API for Passes REST API
        /// See https://developers.google.com/pay/passes/reference/v1/eventticketobject/get
        /// </summary>
        /// <param name="id">id of the object</param>
        /// <returns>object as was saved</returns>
        public EventTicketObject getEventTicketObject(string id)
        {
            EventTicketObject response = null;
            // Uses the Google Pay API for Passes C# client lib to get a EventTicket object
            // check the devsite for newest client lib: https://developers.google.com/pay/passes/support/libraries#libraries
            // check reference API to see the underlying REST call:
            // https://developers.google.com/pay/passes/reference/v1/eventticketobject/get
            WalletobjectsService service = new WalletobjectsService(new Google.Apis.Services.BaseClientService.Initializer()
            {
                HttpClientInitializer = this.credential
            });

            try
            {
                response = service.Eventticketobject.Get(id).Execute();
            }
            catch (Google.GoogleApiException ge)
            {
                System.Console.WriteLine(">>>> [START] Google Server Error response:");
                System.Console.WriteLine(ge.Message);
                System.Console.WriteLine(">>>> [END] Google Server Error response\n");
            }
            catch (System.Exception e)
            {
                System.Console.WriteLine(e.StackTrace);
            }
            return(response);
        }
Example #2
0
 public void addEventTicketObject(EventTicketObject eventTicketObject)
 {
     if (this.payload.eventTicketObjects == null)
     {
         this.payload.eventTicketObjects = new List <EventTicketObject>();
     }
     this.payload.eventTicketObjects.Add(eventTicketObject);
 }
        public void TestInsertEventTicketClassAndObject()
        {
            RestMethods         restMethods         = RestMethods.getInstance();
            ResourceDefinitions resourceDefinitions = ResourceDefinitions.getInstance();
            VerticalType        verticalType        = Services.VerticalType.EVENTTICKET;
            Config            config         = Config.getInstance();
            string            UUID           = Guid.NewGuid().ToString();
            string            classUid       = $"{verticalType.ToString()}_CLASS_{UUID}";
            string            classId        = $"{config.getIssuerId()}.{classUid}";
            string            UUIDobj        = Guid.NewGuid().ToString();
            string            objectUid      = $"{verticalType.ToString()}_OBJECT_{UUIDobj}";
            string            objectId       = $"{config.getIssuerId()}.{objectUid}";
            EventTicketClass  classResponse  = restMethods.insertEventTicketClass(resourceDefinitions.makeEventTicketClassResource(classId));
            EventTicketObject objectResponse = restMethods.insertEventTicketObject(resourceDefinitions.makeEventTicketObjectResource(objectId, classId));

            Assert.NotNull(classResponse);
            Assert.NotNull(objectResponse);
        }
        /// <summary>
        /// Generate an event ticket object
        /// See https://developers.google.com/pay/passes/reference/v1/eventticketobject
        /// </summary>
        /// <param name="objectId"> the unique identifier for an object</param>
        /// <returns>the inserted event ticket object</returns>
        public EventTicketObject makeEventTicketObjectResource(string objectId, string classId)
        {
            // Define the resource representation of the Object
            // values should be from your DB/services; here we hardcode information
            // below defines an eventticket object. For more properties, check:
            //// https://developers.google.com/pay/passes/reference/v1/eventticketobject/insert
            //// https://developers.google.com/pay/passes/guides/pass-verticals/event-tickets/design

            // There is a client lib to help make the data structure. Newest client is on
            // devsite:
            //// https://developers.google.com/pay/passes/support/libraries#libraries
            EventTicketObject payload = new EventTicketObject();

            //required fields
            payload.Id      = objectId;
            payload.ClassId = classId;
            payload.State   = "active";
            //optional fields. See design and reference api for more
            payload.Barcode = new Barcode()
            {
                Type          = "qrCode",
                Value         = "1234abc",
                AlternateText = "1234abc"
            };
            payload.TicketHolderName = "Sir Bacon IV";
            payload.TicketNumber     = "abc123";
            payload.SeatInfo         = new EventSeat()
            {
                Seat = new LocalizedString()
                {
                    DefaultValue = new TranslatedString()
                    {
                        Language = "en-US",
                        Value    = "42"
                    }
                },
                Row = new LocalizedString()
                {
                    DefaultValue = new TranslatedString()
                    {
                        Language = "en-US",
                        Value    = "G3"
                    }
                },
                Section = new LocalizedString()
                {
                    DefaultValue = new TranslatedString()
                    {
                        Language = "en-US",
                        Value    = "5"
                    }
                },
                Gate = new LocalizedString()
                {
                    DefaultValue = new TranslatedString()
                    {
                        Language = "en-US",
                        Value    = "A"
                    }
                }
            };
            return(payload);
        }
        /// <summary>
        /// Generates a signed "fat" JWT.
        /// No REST calls made.
        /// Use fat JWT in JS web button.
        /// Fat JWT is too long to be used in Android intents.
        /// Possibly might break in redirects.
        /// </summary>
        /// <param name="verticalType"> pass type to created</param>
        /// <param name="classId">the unique identifier for the class</param>
        /// <param name="objectId">the unique identifier for the object</param>
        /// <returns></returns>
        public string makeFatJwt(VerticalType verticalType, string classId, string objectId)
        {
            ResourceDefinitions resourceDefinitions = ResourceDefinitions.getInstance();
            RestMethods         restMethods         = RestMethods.getInstance();
            // create JWT to put objects and class into JSON Web Token (JWT) format for Google Pay API for Passes
            Jwt googlePassJwt = new Jwt();

            // get class definition, object definition and see if Ids exist. for a fat JWT, first time a user hits the save button, the class and object are inserted
            try
            {
                switch (verticalType)
                {
                case VerticalType.OFFER:
                    OfferClass  offerClass  = resourceDefinitions.makeOfferClassResource(classId);
                    OfferObject offerObject = resourceDefinitions.makeOfferObjectResource(objectId, classId);
                    System.Console.WriteLine("\nMaking REST call to get class and object to see if they exist.");
                    OfferClass  classResponse  = restMethods.getOfferClass(classId);
                    OfferObject objectResponse = restMethods.getOfferObject(objectId);
                    // check responses
                    if (!(classResponse is null))
                    {
                        System.Console.WriteLine($"classId: {classId} already exists.");
                    }
                    if (!(objectResponse is null))
                    {
                        System.Console.WriteLine($"objectId: {objectId} already exists.");
                    }
                    if (!(classResponse is null) && objectResponse.ClassId != offerObject.ClassId)
                    {
                        System.Console.WriteLine($"the classId of inserted object is ({objectResponse.ClassId}). " +
                                                 $"It does not match the target classId ({offerObject.ClassId}). The saved object will not " +
                                                 "have the class properties you expect.");
                    }
                    // need to add both class and object resource definitions into JWT because no REST calls made to pre-insert
                    googlePassJwt.addOfferClass(offerClass);
                    googlePassJwt.addOfferObject(offerObject);
                    break;

                case VerticalType.LOYALTY:
                    LoyaltyClass  loyaltyClass  = resourceDefinitions.makeLoyaltyClassResource(classId);
                    LoyaltyObject loyaltyObject = resourceDefinitions.makeLoyaltyObjectResource(objectId, classId);
                    System.Console.WriteLine("\nMaking REST call to get class and object to see if they exist.");
                    LoyaltyClass  loyaltyClassResponse  = restMethods.getLoyaltyClass(classId);
                    LoyaltyObject loyaltyObjectResponse = restMethods.getLoyaltyObject(objectId);
                    // check responses
                    if (!(loyaltyClassResponse is null))
                    {
                        System.Console.WriteLine($"classId: {classId} already exists.");
                    }
                    if (!(loyaltyObjectResponse is null))
                    {
                        System.Console.WriteLine($"objectId: {objectId} already exists.");
                    }
                    if (!(loyaltyClassResponse is null) && loyaltyObjectResponse.ClassId != loyaltyObject.ClassId)
                    {
                        System.Console.WriteLine($"the classId of inserted object is ({loyaltyObjectResponse.ClassId}). " +
                                                 $"It does not match the target classId ({loyaltyObject.ClassId}). The saved object will not " +
                                                 "have the class properties you expect.");
                    }
                    // need to add both class and object resource definitions into JWT because no REST calls made to pre-insert
                    googlePassJwt.addLoyaltyClass(loyaltyClass);
                    googlePassJwt.addLoyaltyObject(loyaltyObject);
                    break;

                case VerticalType.EVENTTICKET:
                    EventTicketClass  eventTicketClass  = resourceDefinitions.makeEventTicketClassResource(classId);
                    EventTicketObject eventTicketObject = resourceDefinitions.makeEventTicketObjectResource(objectId, classId);
                    System.Console.WriteLine("\nMaking REST call to get class and object to see if they exist.");
                    EventTicketClass  eventTicketClassResponse  = restMethods.getEventTicketClass(classId);
                    EventTicketObject eventTicketObjectResponse = restMethods.getEventTicketObject(objectId);
                    // check responses
                    if (!(eventTicketClassResponse is null))
                    {
                        System.Console.WriteLine($"classId: {classId} already exists.");
                    }
                    if (!(eventTicketObjectResponse is null))
                    {
                        System.Console.WriteLine($"objectId: {objectId} already exists.");
                    }
                    if (!(eventTicketClassResponse is null) && eventTicketObjectResponse.ClassId != eventTicketObject.ClassId)
                    {
                        System.Console.WriteLine($"the classId of inserted object is ({eventTicketObjectResponse.ClassId}). " +
                                                 $"It does not match the target classId ({eventTicketObject.ClassId}). The saved object will not " +
                                                 "have the class properties you expect.");
                    }
                    // need to add both class and object resource definitions into JWT because no REST calls made to pre-insert
                    googlePassJwt.addEventTicketClass(eventTicketClass);
                    googlePassJwt.addEventTicketObject(eventTicketObject);
                    break;

                case VerticalType.FLIGHT:
                    FlightClass  flightClass  = resourceDefinitions.makeFlightClassResource(classId);
                    FlightObject flightObject = resourceDefinitions.makeFlightObjectResource(objectId, classId);
                    System.Console.WriteLine("\nMaking REST call to get class and object to see if they exist.");
                    FlightClass  flightClassResponse  = restMethods.getFlightClass(classId);
                    FlightObject flightObjectResponse = restMethods.getFlightObject(objectId);
                    // check responses
                    if (!(flightClassResponse is null))
                    {
                        System.Console.WriteLine($"classId: {classId} already exists.");
                    }
                    if (!(flightObjectResponse is null))
                    {
                        System.Console.WriteLine($"objectId: {objectId} already exists.");
                    }
                    if (!(flightClassResponse is null) && flightObjectResponse.ClassId != flightObject.ClassId)
                    {
                        System.Console.WriteLine($"the classId of inserted object is ({flightObjectResponse.ClassId}). " +
                                                 $"It does not match the target classId ({flightObject.ClassId}). The saved object will not " +
                                                 "have the class properties you expect.");
                    }
                    // need to add both class and object resource definitions into JWT because no REST calls made to pre-insert
                    googlePassJwt.addFlightClass(flightClass);
                    googlePassJwt.addFlightObject(flightObject);
                    break;

                case VerticalType.GIFTCARD:
                    GiftCardClass  giftCardClass  = resourceDefinitions.makeGiftCardClassResource(classId);
                    GiftCardObject giftCardObject = resourceDefinitions.makeGiftCardObjectResource(objectId, classId);
                    System.Console.WriteLine("\nMaking REST call to get class and object to see if they exist.");
                    GiftCardClass  giftCardClassResponse  = restMethods.getGiftCardClass(classId);
                    GiftCardObject giftCardObjectResponse = restMethods.getGiftCardObject(objectId);
                    // check responses
                    if (!(giftCardClassResponse is null))
                    {
                        System.Console.WriteLine($"classId: {classId} already exists.");
                    }
                    if (!(giftCardObjectResponse is null))
                    {
                        System.Console.WriteLine($"objectId: {objectId} already exists.");
                    }
                    if (!(giftCardClassResponse is null) && giftCardObjectResponse.ClassId != giftCardObject.ClassId)
                    {
                        System.Console.WriteLine($"the classId of inserted object is ({giftCardObjectResponse.ClassId}). " +
                                                 $"It does not match the target classId ({giftCardObject.ClassId}). The saved object will not " +
                                                 "have the class properties you expect.");
                    }
                    // need to add both class and object resource definitions into JWT because no REST calls made to pre-insert
                    googlePassJwt.addGiftCardClass(giftCardClass);
                    googlePassJwt.addGiftCardObject(giftCardObject);
                    break;

                case VerticalType.TRANSIT:
                    TransitClass  transitClass  = resourceDefinitions.makeTransitClassResource(classId);
                    TransitObject transitObject = resourceDefinitions.makeTransitObjectResource(objectId, classId);
                    System.Console.WriteLine("\nMaking REST call to get class and object to see if they exist.");
                    TransitClass  transitClassResponse  = restMethods.getTransitClass(classId);
                    TransitObject transitObjectResponse = restMethods.getTransitObject(objectId);
                    // check responses
                    if (!(transitClassResponse is null))
                    {
                        System.Console.WriteLine($"classId: {classId} already exists.");
                    }
                    if (!(transitObjectResponse is null))
                    {
                        System.Console.WriteLine($"objectId: {objectId} already exists.");
                    }
                    if (!(transitClassResponse is null) && transitObjectResponse.ClassId != transitObject.ClassId)
                    {
                        System.Console.WriteLine($"the classId of inserted object is ({transitObjectResponse.ClassId}). " +
                                                 $"It does not match the target classId ({transitObject.ClassId}). The saved object will not " +
                                                 "have the class properties you expect.");
                    }
                    // need to add both class and object resource definitions into JWT because no REST calls made to pre-insert
                    googlePassJwt.addTransitClass(transitClass);
                    googlePassJwt.addTransitObject(transitObject);
                    break;
                }
                // return "fat" JWT. Try putting it into JS web button
                // note button needs to be rendered in local web server who's domain matches the ORIGINS
                // defined in the JWT. See https://developers.google.com/pay/passes/reference/s2w-reference
                return(googlePassJwt.generateSignedJwt());
            }
            catch (System.Exception e)
            {
                System.Console.WriteLine(e.Message);
                return(null);
            }
        }