Files
rest-samples/dotnet/DemoGiftcard.cs.example
Stephen McDonald d20d65b1d3 Initial
2022-05-05 15:52:06 +10:00

200 lines
6.6 KiB
Plaintext

/*
* Copyright 2022 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// [START setup]
using Google.Apis.Auth.OAuth2;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Net.Http.Json;
using System.Net.Http.Headers;
using System.Text.RegularExpressions;
// Path to service account key file obtained from Google CLoud Console.
var serviceAccountJson = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS") ?? "/path/to/key.json";
// Issuer ID obtained from Google Pay Business Console.
var issuerId = Environment.GetEnvironmentVariable("WALLET_ISSUER_ID") ?? "<issuer ID>";
// Developer defined ID for the wallet class.
var classId = Environment.GetEnvironmentVariable("WALLET_CLASS_ID") ?? "test-giftCard-class-id";
// Developer defined ID for the user, eg an email address.
var userId = Environment.GetEnvironmentVariable("WALLET_USER_ID") ?? "test@example.com";
// ID for the wallet object, must be in the form `issuerId.userId` where userId is alphanumeric.
var objectId = String.Format("{0}.{1}-{2}", issuerId, new Regex(@"[^\w.-]", RegexOptions.Compiled).Replace(userId, "_"), classId);
// [END setup]
///////////////////////////////////////////////////////////////////////////////
// Create authenticated HTTP client, using service account file.
///////////////////////////////////////////////////////////////////////////////
// [START auth]
var credentials = (ServiceAccountCredential) GoogleCredential
.FromFile(serviceAccountJson)
.CreateScoped(new[] { "https://www.googleapis.com/auth/wallet_object.issuer" })
.UnderlyingCredential;
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await credentials.GetAccessTokenForRequestAsync());
// [END auth]
///////////////////////////////////////////////////////////////////////////////
// Create a class via the API (this can also be done in the business console).
///////////////////////////////////////////////////////////////////////////////
// [START class]
var classUrl = "https://walletobjects.googleapis.com/walletobjects/v1/giftCardClass/";
var classPayload = new
{
id = $"{issuerId}.{classId}",
issuerName = "test issuer name",
merchantName = "Test merchant name",
allowMultipleUsersPerObject = "true",
reviewStatus = "underReview"
};
var classResponse = await httpClient.PostAsJsonAsync(classUrl, classPayload);
var classContent = await classResponse.Content.ReadAsStringAsync();
Console.WriteLine("class POST response: " + classContent);
// [END class]
///////////////////////////////////////////////////////////////////////////////
// Create an object via the API.
///////////////////////////////////////////////////////////////////////////////
// [START object]
var objectUrl = "https://walletobjects.googleapis.com/walletobjects/v1/giftCardObject/";
var objectPayload = new
{
id = objectId,
classId = $"{issuerId}.{classId}",
heroImage = new
{
sourceUri = new
{
uri = "https://farm4.staticflickr.com/3723/11177041115_6e6a3b6f49_o.jpg",
description = "Test heroImage description"
}
},
textModulesData = new object[]
{
new
{
header = "Test text module header",
body = "Test text module body"
}
},
linksModuleData = new
{
uris = new object[]
{
new
{
kind = "walletobjects#uri",
uri = "http://maps.google.com/",
description = "Test link module uri description"
},
new
{
kind = "walletobjects#uri",
uri = "tel:6505555555",
description = "Test link module tel description"
}
}
},
imageModulesData = new object[]
{
new
{
mainImage = new
{
kind = "walletobjects#image",
sourceUri = new
{
kind = "walletobjects#uri",
uri = "http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg",
description = "Test image module description"
}
}
}
},
barcode = new
{
kind = "walletobjects#barcode",
type = "qrCode",
value = "Test QR Code"
},
cardNumber = "Test card number",
cardPin = "Test card pin",
balance = new
{
kind = "walletobjects#money",
micros = 20000000,
currencyCode = "USD"
},
balanceUpdateTime = new
{
date = "2020-04-12T16:20:50.52Z"
},
locations = new object[]
{
new
{
kind = "walletobjects#latLongPoint",
latitude = 37.424015499999996,
longitude = -122.09259560000001
}
}
};
var objectResponse = await httpClient.GetAsync($"{objectUrl}{objectId}");
if ((int) objectResponse.StatusCode == 404)
{
objectResponse = await httpClient.PostAsJsonAsync(objectUrl, objectPayload);
}
var objectContent = await objectResponse.Content.ReadAsStringAsync();
Console.WriteLine("object GET or POST response: " + objectContent);
// [END object]
///////////////////////////////////////////////////////////////////////////////
// Create a JWT for the object, and encode it to create a "Save" URL.
///////////////////////////////////////////////////////////////////////////////
// [START jwt]
var claims = new JwtPayload();
claims.Add("iss", credentials.Id); // `client_email` in service account file.
claims.Add("aud", "google");
claims.Add("origins", new string[] { "www.example.com" });
claims.Add("typ", "savetowallet");
claims.Add("payload", new
{
giftCardObjects = new object[]
{
new
{
id = objectId,
},
},
});
var key = new RsaSecurityKey(credentials.Key);
var signingCredentials = new SigningCredentials(key, SecurityAlgorithms.RsaSha256);
var jwt = new JwtSecurityToken(new JwtHeader(signingCredentials), claims);
var token = new JwtSecurityTokenHandler().WriteToken(jwt);
var saveUrl = $"https://pay.google.com/gp/v/save/{token}";
Console.WriteLine(saveUrl);
// [END jwt]