我正在尝试从我的 PHP 网站添加一张卡片,并且它可以正常工作,但有一部分除外:
TextModuleData
未添加到卡片中。
我直接从 PHP Generic Pass 演示模板中获取代码,并进行了一些修改:
$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}";
}
}
正如我所说:只要创建通行证,就会显示名称、徽标和我添加的其他内容。只是缺少了
TextModuleData
。
我有一种感觉,这是因为我还需要创建(或更新?)一个类,但我不确定。目前还不清楚我是否应该为每个用户(500+)创建一个类和一个对象,或者重用一些东西。
如果有人能将我推向正确的方向,我将不胜感激。
编辑:我认为添加这个可能会做到这一点,但我得到了“出了点问题”。请再试一次。'创建通行证时出错。
$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']",
],
],
],
],
],
],
],
],
],
]);
这方面的文档确实很糟糕。
看起来可能是因为
Google_Service_Walletobjects_TextModuleData
中有两个 textModulesData
实例。您应该有一个类,然后使用它为生成的每个通道创建一个单独的对象。
我也遇到同样的问题。您有关于此问题的任何更新吗?