Exemplo n.º 1
0
def handleInsertCallStatusCode(insertCallResponse,
                               idType,
                               id,
                               checkClassId=None,
                               verticalType=None):
    if insertCallResponse.status_code == 200:
        print('%sId (%s) insertion success!\n' % (idType, id))
    elif insertCallResponse.status_code == 409:  # id resource exists for this issuer account
        print('%sId: (%s) already exists. %s' % (idType, id, EXISTS_MESSAGE))

        # for object insert, do additional check
        if idType == "object":
            getCallResponse = None
            # get existing object Id data
            if verticalType == VerticalType.OFFER:
                getCallResponse = restMethods.getOfferObject(
                    id)  # if it is a new object Id, expected status is 409
            else:
                raise ValueError(
                    "resource definition for vertical: (%s) not implemented yet. Check README.md > Implementing other verticals"
                    % (verticalType))
            # check if object's classId matches target classId
            classIdOfObjectId = getCallResponse.json()['classId']
            if classIdOfObjectId != checkClassId and checkClassId is not None:
                raise ValueError(
                    'the classId of inserted object is (%s). It does not match the target classId (%s). The saved object will not have the class properties you expect.'
                    % (classIdOfObjectId, checkClassId))
    else:
        raise ValueError('%s insert issue.' % (idType),
                         insertCallResponse.text)

    return
Exemplo n.º 2
0
def makeObjectJwt(verticalType, classId, objectId):
    signedJwt = None
    classResourcePayload = None
    objectResourcePayload = None
    classResponse = None
    objectResponse = None

    try:
        if verticalType == VerticalType.OFFER:
            # get class definition
            classResourcePayload = resourceDefinitions.makeOfferClassResource(
                classId)

            # get object definition
            objectResourcePayload = resourceDefinitions.makeOfferObjectResource(
                classId, objectId)

            print('\nMaking REST call to insert class')
            # make authorized REST call to explicitly insert class into Google server.
            # if this is successful, you can check/update class definitions in Merchant Center GUI: https://pay.google.com/gp/m/issuer/list
            classResponse = restMethods.insertOfferClass(classResourcePayload)

            # check if object ID exist
            objectResponse = restMethods.getOfferObject(
                objectId)  # if it is a new object Id, expected status is 409
        else:
            raise ValueError(
                "resource definition for vertical: (%s) not implemented yet. Check README.md > Implementing other verticals"
                % (verticalType))

        # continue based on response status. Check https://developers.google.com/pay/passes/reference/v1/statuscodes
        # check class insert response. Will print out if class insert succeeds or not. Throws error if class resource is malformed.
        handleInsertCallStatusCode(classResponse, "class", classId, None, None)

        # check object get response. Will print out if object exists or not. Throws error if object resource is malformed, or if existing objectId's classId does not match the expected classId
        handleGetCallStatusCode(objectResponse, "object", objectId, classId)

        # put into JSON Web Token (JWT) format for Google Pay API for Passes
        googlePassJwt = jwt.googlePassJwt()
        if verticalType == VerticalType.OFFER:
            # only need to add object resource definition in JWT because class was pre-inserted via REST call
            googlePassJwt.addOfferObject(objectResourcePayload)
        else:
            raise ValueError(
                'JWT format for %s is not implemented yet. For proper JWT format, check %s'
                %
                (verticalType,
                 'https://developers.google.com/pay/passes/reference/s2w-reference#google-pay-api-for-passes-jwt'
                 ))

        # sign JSON to make signed JWT
        signedJwt = googlePassJwt.generateSignedJwt()

    except ValueError as err:
        print(err.args)

    # return "object" JWT. Try putting it into save link.
    # See https://developers.google.com/pay/passes/guides/get-started/implementing-the-api/save-to-google-pay#add-link-to-email
    return signedJwt
Exemplo n.º 3
0
def makeFatJwt(verticalType, classId, objectId):
    signedJwt = None
    classResourcePayload = None
    objectResourcePayload = None
    classResponse = None
    objectResponse = None

    try:
        if verticalType == VerticalType.OFFER:
            # get class definition
            classResourcePayload = resourceDefinitions.makeOfferClassResource(
                classId)

            # get object definition
            objectResourcePayload = resourceDefinitions.makeOfferObjectResource(
                classId, objectId)

            # see if Ids exist in Google backend
            ## for a Fat JWT, the first time a user hits the save button, the class and object are inserted
            classResponse = restMethods.getOfferClass(classId)
            objectResponse = restMethods.getOfferObject(objectId)
        else:
            raise ValueError(
                "resource definition for vertical: (%s) not implemented yet. Check README.md > Implementing other verticals"
                % (verticalType))

        # check response status. Check https://developers.google.com/pay/passes/reference/v1/statuscodes
        # check class get response. Will print out if class exists or not. Throws error if class resource is malformed.
        handleGetCallStatusCode(classResponse, "class", classId, None)

        # check object get response. Will print out if object exists or not. Throws error if object resource is malformed, or if existing objectId's classId does not match the expected classId
        handleGetCallStatusCode(objectResponse, "object", objectId, classId)

        # put into JSON Web Token (JWT) format for Google Pay API for Passes
        googlePassJwt = jwt.googlePassJwt()
        if verticalType == VerticalType.OFFER:
            # need to add both class and object resource definitions into JWT because no REST calls made to pre-insert
            googlePassJwt.addOfferClass(classResourcePayload)
            googlePassJwt.addOfferObject(objectResourcePayload)
        else:
            raise ValueError(
                'JWT format for %s is not implemented yet. For proper JWT format, check %s'
                %
                (verticalType,
                 'https://developers.google.com/pay/passes/reference/s2w-reference#google-pay-api-for-passes-jwt'
                 ))

        # sign JSON to make signed JWT
        signedJwt = googlePassJwt.generateSignedJwt()

    except ValueError as err:
        print(err.args)

    # return "fat" JWT. Try putting it into JS web button
    # note button needs to be rendered in local web server who's domain matched the ORIGINS
    # defined in the JWT. See https://developers.google.com/pay/passes/reference/s2w-reference
    return signedJwt