After game restart first purchase is contained in receipt but the next ones is the same as first one so new purchases is not added. I afraid players can be charged for purchase but on my server I will not receive new purchases instead receipt with old one so they can do not receive in game currency. Will in production I receive a receipts with new consumable every time player purchase it? I use Unity3d In-app purchasing 5.0.1.
I messed the code in previous post:
using Jose;
using Newtonsoft.Json;
using System.IdentityModel.Tokens.Jwt;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Text;
private static ECDsa LoadApplePrivateKey(string filePath)
{
var keyText = File.ReadAllText(filePath)
.Replace("-----BEGIN PRIVATE KEY-----", "")
.Replace("-----END PRIVATE KEY-----", "")
.Replace("\r", "")
.Replace("\n", "")
.Trim();
var keyBytes = Convert.FromBase64String(keyText);
var ecdsa = ECDsa.Create();
ecdsa.ImportPkcs8PrivateKey(keyBytes, out _);
return ecdsa;
}
private string GenerateApiToken()
{
var payload = new Dictionary<string, object>
{
{ "iss", _issuerId },
{ "iat", DateTimeOffset.UtcNow.ToUnixTimeSeconds() },
{ "exp", DateTimeOffset.UtcNow.AddMinutes(5).ToUnixTimeSeconds() },
{ "aud", "appstoreconnect-v1" },
{ "bid", _bundleId }
};
var extraHeaders = new Dictionary<string, object>
{
{ "alg", "ES256" },
{ "kid", _keyId },
{ "typ", "JWT" }
};
string token = JWT.Encode(payload, _privateKey, JwsAlgorithm.ES256, extraHeaders);
_logger.LogInformation("Generated JWT: {Token}", token);
return token;
}
public static ApplePurchase DecodeJws(string jws)
{
var parts = jws.Split('.');
if (parts.Length != 3) throw new ArgumentException("Invalid JWS format");
var payloadJson = Encoding.UTF8.GetString(Base64Url.Decode(parts[1]));
dynamic payload = JsonConvert.DeserializeObject(payloadJson);
return new ApplePurchase
{
transactionId = payload.transactionId,
productId = payload.productId,
timePurchased = payload.purchaseDate,
environment = payload.environment ?? "Unknown"
};
}