PHP code example of laulamanapps / google-wallet

1. Go to this page and download the library: Download laulamanapps/google-wallet library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

laulamanapps / google-wallet example snippets


use LauLamanApps\GoogleWallet\Object\Barcode;
use LauLamanApps\GoogleWallet\Object\BarcodeType;
use LauLamanApps\GoogleWallet\Object\GenericClass;
use LauLamanApps\GoogleWallet\Object\GenericObject;
use LauLamanApps\GoogleWallet\Object\Image;
use LauLamanApps\GoogleWallet\Object\TextModule;
use LauLamanApps\GoogleWallet\PassPayload;
use LauLamanApps\GoogleWallet\SaveUrlFactory;
use LauLamanApps\GoogleWallet\ServiceAccount;

$serviceAccount = ServiceAccount::fromJsonFile('/path/to/service-account.json');

// Ids are '<issuerId>.<identifier>'
$object = new GenericObject('3388000000012345678.member-0001', '3388000000012345678.membership');
$object->setCardTitle('ACME Membership');
$object->setHeader('John Doe');
$object->setSubheader('Gold member');
$object->setHexBackgroundColor('#4285f4');
$object->setLogo(Image::fromUri('https://example.com/logo.png', 'ACME logo'));
$object->setBarcode(new Barcode(BarcodeType::QrCode, 'member-0001'));
$object->addTextModule(new TextModule('Member since', '2020'));

$payload = new PassPayload();
$payload->addGenericClass(new GenericClass('3388000000012345678.membership'));
$payload->addGenericObject($object);

$factory = new SaveUrlFactory($serviceAccount, ['https://example.com']);
$saveUrl = $factory->create($payload);
// https://pay.google.com/gp/v/save/eyJhbGciOiJSUzI1NiIs...

$object->addMerchantLocation(52.3676, 4.9041);

use LauLamanApps\GoogleWallet\Api\AccessTokenProvider;
use LauLamanApps\GoogleWallet\Api\Message;
use LauLamanApps\GoogleWallet\Api\WalletApiClient;
use LauLamanApps\GoogleWallet\Object\GenericObject;
use LauLamanApps\GoogleWallet\Object\State;
use LauLamanApps\GoogleWallet\ServiceAccount;

$serviceAccount = ServiceAccount::fromJsonFile('/path/to/service-account.json');
$client = new WalletApiClient(new AccessTokenProvider($serviceAccount));

// Partially update an issued pass (PATCH; use updateObject() for a full PUT)
$object = new GenericObject('3388000000012345678.member-0001', '3388000000012345678.membership');
$object->setHeader('John Doe');
$object->setState(State::Active);
$client->patchObject($object);

// Notify pass holders
$client->addMessage($object, new Message('Membership renewed', 'Your membership is extended until 2027.'));

// Fetch the current server-side state of a pass
$data = $client->getObject('genericObject', '3388000000012345678.member-0001');

$class = new GenericClass('3388000000012345678.membership');
$class->setCallbackUrl('https://example.com/wallet/callback'); // must be https://

use LauLamanApps\GoogleWallet\Callback\CallbackVerifier;
use LauLamanApps\GoogleWallet\Callback\EventType;
use LauLamanApps\GoogleWallet\Callback\GoogleKeyProvider;
use LauLamanApps\GoogleWallet\Exception\CallbackException;

$verifier = new CallbackVerifier('3388000000012345678', GoogleKeyProvider::production());

try {
    $event = $verifier->verify($rawPostBody);
} catch (CallbackException $exception) {
    // Respond with a 4xx and do NOT act on the payload.
}

$event->getClassId();   // '3388000000012345678.membership'
$event->getObjectId();  // '3388000000012345678.member-0001'
$event->getEventType(); // EventType::Save or EventType::Delete
$event->getNonce();     // callbacks are delivered at least once; deduplicate on the nonce

use LauLamanApps\GoogleWallet\JwtSigner;

$signer = new JwtSigner($serviceAccount);
$jwt = $signer->sign([
    'iss' => $serviceAccount->getClientEmail(),
    'aud' => 'google',
    'typ' => 'savetowallet',
    'iat' => time(),
    'origins' => ['https://example.com'],
    'payload' => $payload->toArray(),
]);