Ejemplo n.º 1
0
        /// <summary>
        /// Get defined class with Google Pay API for Passes REST API
        /// See https://developers.google.com/pay/passes/reference/v1/transitclass/get
        /// </summary>
        /// <param name="id">id of class</param>
        /// <returns>Class</returns>
        public TransitClass getTransitClass(string id)
        {
            TransitClass response = null;
            // Uses the Google Pay API for Passes C# client lib to get a Transit class
            // 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/transitclass/get
            WalletobjectsService service = new WalletobjectsService(new Google.Apis.Services.BaseClientService.Initializer()
            {
                HttpClientInitializer = this.credential
            });

            try
            {
                response = service.Transitclass.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);
        }
Ejemplo n.º 2
0
 public void addTransitClass(TransitClass transitClass)
 {
     if (this.payload.transitClasses == null)
     {
         this.payload.transitClasses = new List <TransitClass>();
     }
     this.payload.transitClasses.Add(transitClass);
 }
        /// <summary>
        /// Generate a transit class
        /// See https://developers.google.com/pay/passes/reference/v1/transitclass
        /// </summary>
        /// <param name="classId"> the unique identifier for a class</param>
        /// <returns>the inserted transit class</returns>
        public TransitClass makeTransitClassResource(string classId)
        {
            // Define the resource representation of the Class
            // values should be from your DB/services; here we hardcode information
            // below defines an transit class. For more properties, check:
            //// https://developers.google.com/pay/passes/reference/v1/transitclass/insert
            //// https://developers.google.com/pay/passes/guides/pass-verticals/transit-passes/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
            TransitClass payload = new TransitClass();

            //required fields
            payload.Id           = classId;
            payload.IssuerName   = "Baconrista Bus";
            payload.ReviewStatus = "underReview";
            payload.Logo         = new Image()
            {
                SourceUri = new Uri()
                {
                    UriValue = "https://live.staticflickr.com/65535/48690277162_cd05f03f4d_o.png", Description = "Baconrista Bus"
                }
            };
            payload.TransitType = "bus";
            //optional fields. See design and reference api for more
            payload.TextModulesData = new List <TextModuleData>()
            {
                new TextModuleData()
                {
                    Header = "Custom Transit Details",
                    Body   = "Baconrista Bus has served snacks in-transit since its founding."
                }
            };
            payload.LinksModuleData = new LinksModuleData()
            {
                Uris = new List <Uri>()
                {
                    new Uri()
                    {
                        UriValue    = "http://maps.google.com/",
                        Description = "Nearby Locations"
                    },
                    new Uri()
                    {
                        UriValue    = "tel:6505555555",
                        Description = "Call Customer Service"
                    }
                }
            };
            return(payload);
        }
        public void TestInsertTransitClassAndObject()
        {
            RestMethods         restMethods         = RestMethods.getInstance();
            ResourceDefinitions resourceDefinitions = ResourceDefinitions.getInstance();
            VerticalType        verticalType        = Services.VerticalType.TRANSIT;
            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}";
            TransitClass  classResponse  = restMethods.insertTransitClass(resourceDefinitions.makeTransitClassResource(classId));
            TransitObject objectResponse = restMethods.insertTransitObject(resourceDefinitions.makeTransitObjectResource(objectId, classId));

            Assert.NotNull(classResponse);
            Assert.NotNull(objectResponse);
        }
Ejemplo n.º 5
0
        /// <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);
            }
        }