코드 스니펫

다음 패스 카테고리 중 하나를 선택하여 사용 방법을 자세히 알아보세요.


이 섹션에는 기프트 카드의 코드 스니펫이 포함되어 있습니다. 다음 클래스와 객체가 나와 있습니다.

JSON GiftCardClass

GiftCardClass를 정의한 후 다음 REST URI에 대해 POST 요청을 실행하여 해당 클래스를 삽입합니다.

https://walletobjects.googleapis.com/walletobjects/v1/giftCardClass

엄격한 오류 파싱을 설정하고 중복 ID 필드와 같은 더 많은 오류를 포착하려면 REST URI에 strict=true 매개변수를 추가합니다.

https://walletobjects.googleapis.com/walletobjects/v1/giftCardClass?strict=true

다음 예시에서는 GiftCardClass를 나타내는 JSON 리소스를 보여줍니다. JSON은 POST 요청의 본문으로 보내야 합니다. 다음 코드 샘플은 GiftCardClass를 정의하고 삽입하는 구체적인 방법을 보여줍니다.

리소스

{
  "kind" : "walletobjects#giftCardClass",
  "id" : "1234567890.ExampleGCClass",
  "issuerName" : "Baconrista",
  "merchantName" : "Baconrista",
  "programLogo" : {
    "kind" : "walletobjects#image",
    "sourceUri" : {
      "kind" : "walletobjects#uri",
      "uri" : "https://farm8.staticflickr.com/7340/11177041185_a61a7f2139_o.jpg"
    }
  },
  "textModulesData" : [
    {
      "header" : "Where to Redeem",
      "body" : "All US gift cards are redeemable in any US and Puerto Rico" +
                " Baconrista retail locations, or online at Baconrista.com where" +
                " available, for merchandise or services."
    }
  ],
  "linksModuleData" : {
    "uris" : [
      {
        "kind" : "walletobjects#uri",
        "uri" : "https://www.baconrista.com/",
        "description" : "Baconrista"
      }
    ]
  },
  "locations" : [{
    "kind" : "walletobjects#latLongPoint",
    "latitude" : 37.422601,
    "longitude" : -122.085286
  }],
  "reviewStatus" : "underReview",
  "hexBackgroundColor": "#ffffff",
  "heroImage": {
    "kind": "walletobjects#image",
    "sourceUri": {
      "kind": "walletObjects#uri",
      "uri": "https://farm8.staticflickr.com/7302/11177240353_115daa5729_o.jpg"
    }
  }
}

자바

// Define Text Module Data
List<TextModuleData> textModulesData = new ArrayList<TextModuleData>();

TextModuleData textModuleData = new TextModuleData().setHeader("Where to Redeem")
    .setBody("All US gift cards are redeemable in any US and Puerto Rico " +
           "Baconrista retail locations, or online at Baconrista.com where" +
           "available, for merchandise or services.");
textModulesData.add(textModuleData);

// Define Links Module Data
List<Uri> uris = new ArrayList<Uri>();
Uri uri1 = new Uri().setDescription("Baconrista").setUri("http://www.baconrista.com/");
uris.add(uri1);
LinksModuleData linksModuleData = new LinksModuleData().setUris(uris);

// Define Geofence locations
List<LatLongPoint> locations = new ArrayList<LatLongPoint>();
locations.add(new LatLongPoint().setLatitude(37.422601).setLongitude(
    -122.085286));

// Create class
GiftCardClass wobClass = new GiftCardClass()
    .setId("1234567890.ExampleGCClass")
    .setIssuerName("Baconrista")
    .setMerchantName("Baconrista")
    .setProgramLogo(
        new Image().setSourceUri(new ImageUri()
            .setUri("http://farm8.staticflickr.com/7340/11177041185_a61a7f2139_o.jpg")))
    .setTextModulesData(textModulesData)
    .setLinksModuleData(linksModuleData)
    .setReviewStatus("underReview").setMultipleDevicesAndHoldersAllowedStatus('multipleHolders')
    .setLocations(locations);

response = client.giftcardclass().insert(wobClass).execute();

PHP

// Define text module data.
$textModulesData = array(
    array(
        'header' => 'Where to Redeem',
        'body' => 'All US gift cards are redeemable in any US and Puerto Rico ' .
            'Baconrista retail locations, or online at Baconrista.com where ' .
            'available, for merchandise or services.'
    )
);
// Define links module data.
$linksModuleData = new Google_Service_Walletobjects_LinksModuleData();
$uris = array (
    array(
        'uri' => 'http://www.baconrista.com',
        'kind' => 'walletobjecs#uri',
        'description' => 'Baconrista'
    )
);
$linksModuleData->setUris($uris);

// A list of locations at which the Gift card Class can be used.
$locations = array(
    array(
        'kind' => 'walletobjects#latLongPoint',
        'latitude' => 37.422601,
        'longitude' => -122.085286
    )
);
// Source uri of program logo.
$uriInstance = new Google_Service_Walletobjects_ImageUri();
$imageInstance = new Google_Service_Walletobjects_Image();
$uriInstance->setUri(
    'http://farm8.staticflickr.com/7340/11177041185_a61a7f2139_o.jpg'
);
$imageInstance->setSourceUri($uriInstance);

// Create wallet class.
$giftCardClass = new Google_Service_Walletobjects_GiftCardClass();
$giftCardClass->setId('1234567890.ExampleGCClass');
$giftCardClass->setIssuerName('Baconrista');
$giftCardClass->setMerchantName('Baconrista');
$giftCardClass->setProgramLogo($imageInstance);
$giftCardClass->setLinksModuleData($linksModuleData);
$giftCardClass->setTextModulesData($textModulesData);
$giftCardClass->setReviewStatus('underReview');
$giftCardClass->setMultipleDevicesAndHoldersAllowedStatus('multipleHolders');
$giftCardClass->setLocations($locations);

$service->giftcardclass->insert($giftCardClass);

Python

giftcard_class = {
  'kind': 'walletobjects#giftCardClass',
  'id': '1234567890.ExampleGCClass',
  'issuerName': 'Baconrista',
  'merchantName': 'Baconrista',
  'issuerData': {
      'kind': 'walletobjects#typedValue'
  },
  'programLogo': {
      'kind': 'walletobjects#image',
      'sourceUri': {
          'kind': 'walletobjects#uri',
          'uri': 'http://farm8.staticflickr.com/7340/11177041185_a61a7f2139_o.jpg'
      }
  },
  'textModulesData': [{
    'header': 'Where to Redeem',
    'body': 'All US gift cards are redeemable in any US and Puerto Rico' +
            ' Baconrista retail locations, or online at Baconrista.com where'
            ' available, for merchandise or services.'
  }],
  'linksModuleData': {
    'uris': [
      {
        'kind': 'walletobjects#uri',
        'uri': 'http://www.baconrista.com',
        'description': 'Baconrista'
      }]
  },
  'locations': [{
      'kind': 'walletobjects#latLongPoint',
      'latitude': 37.422601,
      'longitude': -122.085286
  }],
  'multipleDevicesAndHoldersAllowedStatus': 'multipleHolders',
  'reviewStatus': 'underReview'
}
api_request = service.giftcardclass().insert(body=giftcard_class)
api_response = api_request.execute()

JSON GiftCardObject

다음 샘플은 GiftCardObject를 정의합니다.

리소스

{
  "kind" : "walletobjects#giftCardObject",
  "classId" : "1234567890.ExampleGCClass",
  "id" : "1234567890.ExampleGCObject",
  "cardNumber" : "123jkl4889",
  "pin" : "1111",
  "eventNumber" : "123456",
  "balance" : {
    "kind" : "walletobjects#money",
    "micros" : 20000000,
    "currencyCode" : "USD"
  },
  "balanceUpdateTime" : {
    "date" : '2014-01-12T23:20:50.52Z'
  },
  "barcode" : {
    "alternateText" : "12345",
    "type" : "qrCode",
    "value" : "28343E3"
  },
  "textModulesData" : [{
    "header" : "Earn double points",
    "body" : "Jane, don't forget to use your Baconrista Rewards when " +
              "paying with this gift card to earn additional points"
  }],
  "linksModuleData" : {
    "uris" : [
      {
        "kind" : "walletobjects#uri",
        "uri" : "https://www.baconrista.com/mybalance?id=1234567890",
        "description" : "My Baconrista Gift Card Purchases"
      }]
  },
  "state" : "active"
}

자바

Barcode barcode = new Barcode().setType("qrCode")
    .setValue("28343E3")
    .setAlternateText("12345")

Money balance = new Money();
balance.setCurrencyCode("USD");
balance.setMicros(20000000L);

DateTime balanceUpdateTime = new DateTime();
balanceUpdateTime.setDate(new com.google.api.client.util.DateTime(new Date()));

// Define Text Module Data
List<TextModuleData> textModulesData = new ArrayList<TextModuleData>();
TextModuleData textModuleData = new TextModuleData()
    .setHeader("Earn double points")
    .setBody("Jane, don't forget to use your Baconrista Rewards when " +
                "paying with this gift card to earn additional points");
textModulesData.add(textModuleData);

// Define Links Module Data
List<Uri> uris = new ArrayList<Uri>();
Uri uri1 = new Uri().setDescription("My Baconrista Gift Card Purchases")
    .setUri("http://www.baconrista.com/mybalance?id=1234567890");
uris.add(uri1);
LinksModuleData linksModuleData = new LinksModuleData().setUris(uris);

// Define Wallet Instance
GiftCardObject object = new GiftCardObject()
    .setClassId("1234567890.ExampleGCClass").setId("1234567890.ExampleGCObject")
    .setState("active").setBarcode(barcode)
    .setTextModulesData(textModulesData)
    .setLinksModuleData(linksModuleData)
    .setBalance(balance)
    .setBalanceUpdateTime(balanceUpdateTime)
    .setEventNumber("123456").setCardNumber("123jkl4889").setPin("1111");

PHP

// Define barcode type and value.
$barcode = new Google_Service_Walletobjects_Barcode();
$barcode->setAlternateText('12345');
$barcode->setType('qrCode');
$barcode->setValue('28343E3');
// Define text module data.
$textModulesData = array(
    array(
        'header' => 'Earn double points',
        'body' => 'Jane, don\'t forget to use your Baconrista Rewards when '.
                  'paying with this gift card to earn additional points'
    )
);
// Define links module data.
$linksModuleData = new Google_Service_Walletobjects_LinksModuleData();
$uris = array (
    array(
        'uri' => 'http://www.baconrista.com/mybalance?id=1234567890',
        'kind' => 'walletobjecs#uri',
        'description' => 'My Baconrista Gift Card Purchases'
    )
);
$linksModuleData->setUris($uris);

$balance = new Google_Service_Walletobjects_Money();
$balance->setKind('walletobjects#money');
$balance->setMicros(20000000);
$balance->setCurrencyCode('USD');

$balanceUpdateTime = new Google_Service_Walletobjects_DateTime();
date_default_timezone_set('UTC');
$balanceUpdateTime->setDate(date("Y-m-d\TH:i:s.u\Z"));

// Create wallet object.
$giftCardObject = new Google_Service_Walletobjects_GiftCardObject();
$giftCardObject->setClassId("1234567890.ExampleGCClass");
$giftCardObject->setId("1234567890.ExampleGCObject");
$giftCardObject->setState('active');
$giftCardObject->setBarcode($barcode);
$giftCardObject->setLinksModuleData($linksModuleData);
$giftCardObject->setTextModulesData($textModulesData);
$giftCardObject->setBalance($balance);
$giftCardObject->setBalanceUpdateTime($balanceUpdateTime);
$giftCardObject->setCardNumber('123jkl4889');
$giftCardObject->setEventNumber('123456');
$giftCardObject->setPin('1111');

Python

giftcard_object = {
    'kind' : 'walletobjects#giftCardObject',
    'classId' : '1234567890.ExampleGCClass',
    'id' : '1234567890.ExampleGCObject',
    'cardNumber' : '123jkl4889',
    'pin' : '1111',
    'eventNumber' : '123456',
    'balance' : {
      'kind' : 'walletobjects#money',
      'micros' : 20000000,
      'currencyCode' : 'USD'
    },
    'balanceUpdateTime' : {
      'date' : datetime.datetime.utcnow().isoformat("T") + "Z"
    },
    'barcode' : {
        'alternateText' : '12345',
        'type' : 'qrCode',
        'value' : '28343E3'
    },
    'textModulesData': [{
      'header': 'Earn double points',
      'body': 'Jane, don\'t forget to use your Baconrista Rewards when  ' +
              'paying with this gift card to earn additional points. '
    }],
    'linksModuleData': {
      'uris': [
        {
          'kind': 'walletobjects#uri',
          'uri': 'http://www.baconrista.com/mybalance?id=1234567890',
          'description': 'My Baconrista Gift Card Purchases'
        }]
    },
    'state': 'active'
}

GiftCardObject를 삽입하는 방법에는 두 가지가 있습니다.

첫 번째 방법은 다음 REST URI에 POST 요청을 수행하는 것입니다(본문에 객체를 정의함).

https://walletobjects.googleapis.com/walletobjects/v1/giftCardObject

두 번째 방법은 GiftCardObject 정의에서 JWT를 만들고 이 JWT를 Google Pay에 저장 버튼에 넣는 것입니다. 자세한 내용은 웹 버튼 추가를 참조하세요.

Android API를 사용하여 GiftCardObject 추가

다음 예시에서는 Android API 월렛 SDK 패키지를 사용하여 GiftCardObject를 삽입하는 방법을 보여줍니다.

//Define Messages Data
List messages = new ArrayList();
WalletObjectMessage message = WalletObjectMessage.newBuilder()
        .setHeader("Hi Jane!")
        .setBody("Thanks for saving your gift card.")
        .build();
messages.add(message);

// Define Links Module Data
List linkModulesData = new ArrayList();
UriData uri1 = new UriData("http://www.baconrista.com/mybalance?id=1234567890", "My Baconrista Gift Card Purchases");
linkModulesData.add(uri1);

// Define Text Module Data
List textModulesData = new ArrayList();
TextModuleData textModuleData = new TextModuleData("Jane's Baconrista Gift Card", "Use your gift card when you purchase your next bacon fat latte!");
textModulesData.add(textModuleData);

// Build and Return New Gift Card Object
return GiftCardWalletObject.newBuilder()
        .setId("2967745143867465930.GiftCardObjectBarcodeTextTest4")
        .setClassId("2967745143867465930.GiftCardClass1501161237")
        .setState(WalletObjectsConstants.State.ACTIVE)
        .setCardNumber("1234567890")
        .setPin("1234")
        .setTitle("This is my GiftCard")
        .setBalanceMicros(1000000)
        .setBalanceCurrencyCode("USD")
        .setBalanceUpdateTime(1425943338)
        .setEventNumber("1")
        .setIssuerName("Baconrista")
        .setBarcodeType("qrCode")
        .setBarcodeValue("28343E3")
        .setBarcodeAlternateText("12345")
        .addMessages(messages)
        .addLinksModuleDataUris(linkModulesData)
        .addTextModulesData(textModulesData)
        .build();

자세한 내용은 Google Pay에 패스 저장을 참조하세요.