Adding a Google Wallet generic pass from a PHP app

854 Views Asked by At

I'm trying to add a card from my PHP website and I have it working, except for one part: the TextModuleData is not added to the card.

I've taken the code straight from the PHP Generic Pass demo template with a few modifications:

$googlepass = new GooglePass();
$googlepass->createJWTNewObjects('issuer_id', 'my_class_suffix', $randomString);
class GooglePass {

// authorisation stuff first

public function createJwtNewObjects(string $issuerId, string $classSuffix, string $objectSuffix, $user)
{

    // See link below for more information on required properties
    // https://developers.google.com/wallet/generic/rest/v1/genericobject
    $newObject = new Google_Service_Walletobjects_GenericObject([
        'id' => "{$issuerId}.{$objectSuffix}",
        'classId' => "{$issuerId}.{$classSuffix}",
        'state' => 'ACTIVE',
        
        'header' => new Google_Service_Walletobjects_LocalizedString([
            'defaultValue' => new Google_Service_Walletobjects_TranslatedString([
                'language' => 'en-US',
                'value' => $user->name,
            ])
        ]),
        'textModulesData' => [
            new Google_Service_Walletobjects_TextModuleData([
                'header' => 'Location',
                'body' => $user->location,
                'id' => 'location'
            ]),
            new Google_Service_Walletobjects_TextModuleData([
                'header' => 'Other Details',
                'body' => $user->detail,
                'id' => 'otherDetails'
            ])
        ]
    ]);

    // The service account credentials are used to sign the JWT
    $serviceAccount = json_decode(file_get_contents($this->keyFilePath), true);

    // Create the JWT as an array of key/value pairs
    $claims = [
        'iss' => $serviceAccount['client_email'],
        'aud' => 'google',
        'origins' => ['www.mysite.com'],
        'typ' => 'savetowallet',
        'payload' => [
            'genericObjects' => [
                $newObject
            ]
        ]
    ];

    $token = JWT::encode(
        $claims,
        $serviceAccount['private_key'],
        'RS256'
    );

    print "Add to Google Wallet link\n";
    print "https://pay.google.com/gp/v/save/{$token}";

    return "https://pay.google.com/gp/v/save/{$token}";
}
}

As I say: this works insofar as the pass is created, the name, logo and other things I've added show. It's only the TextModuleData which is missing.

I have a feeling this is because I need to be creating (or updating?) a class as well, but I'm not sure. It's unclear whether I should be creating a class and an object for every user (500+) or reusing things.

If anyone can push me in the right direction, I'd appreciate that.


Edit: I thought adding this might do it, but I get a 'Something went wrong. Please try again.' error when creating the pass.

$newClass = new Google_Service_Walletobjects_GenericClass([
    'id' => "{$issuerId}.{$classSuffix}",
    'classTemplateInfo' => [
        'cardTemplateOverride' => [
            'cardRowTemplateInfos' => [
                [
                    'twoItems' => [
                        'startItem' => [
                            'firstValue' => [
                                'fields' => [
                                    [
                                        'fieldPath' => "object.textModulesData['points']",
                                    ],
                                ],
                            ],
                        ],
                        'endItem' => [
                            'firstValue' => [
                                'fields' => [
                                    [
                                        'fieldPath' => "object.textModulesData['contacts']",
                                    ],
                                ],
                            ],
                        ],
                    ],
                ],
            ],
        ],
    ],
]);

The docs on this sure do suck.

1

There are 1 best solutions below

0
On

It looks like it could be because you have two Google_Service_Walletobjects_TextModuleData instances in textModulesData. You should have one Class, and then use that to create a separate Object for each pass that's generated.