Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Api client targeting wrong URL & Fix data type of subscription status response #5

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/AppStoreServerApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
{
//Call to https://developer.apple.com/documentation/appstoreserverapi/get_all_subscription_statuses

string path = $"v1/subscriptions/{transactionId}";
string path = $"/inApps/v1/subscriptions/{transactionId}";

return this.MakeRequest<SubscriptionStatusResponse>(path, HttpMethod.Get)!;
}
Expand All @@ -62,7 +62,7 @@
queryParameters.Add("paginationToken", paginationToken);
}

string path = $"v1/notifications/history";
const string path = "/inApps/v1/notifications/history";

return this.MakeRequest<NotificationHistoryResponse>(
path,
Expand All @@ -75,7 +75,7 @@
/// <summary>
/// Get a customer’s in-app purchase transaction history for your app.
/// </summary>
/// <returns>A list of transactions associated with the provided Transaction Id</returns>
/// <returns>A list of transactions associated with the provided Transaction ID</returns>
public Task<TransactionHistoryResponse?> GetTransactionHistory(string transactionId, string revisionToken = "")
{
//Call to https://developer.apple.com/documentation/appstoreserverapi/get_transaction_history
Expand All @@ -85,7 +85,7 @@
queryParameters.Add("revision", revisionToken);
}

string path = $"v2/history/{transactionId}";
string path = $"/inApps/v2/history/{transactionId}";

return this.MakeRequest<TransactionHistoryResponse>(path, HttpMethod.Get, queryParameters);
}
Expand All @@ -101,9 +101,9 @@
/// </remarks>
public Task SendConsumptionData(string transactionId, ConsumptionRequest consumptionRequest)
{
string path = $"v1/transactions/consumption/{transactionId}";
string path = $"/inApps/v1/transactions/consumption/{transactionId}";

return this.MakeRequest<object?>(path, HttpMethod.Put, null, consumptionRequest, fetchResponse: false);
return this.MakeRequest<object?>(path, HttpMethod.Put, null, consumptionRequest, false);

Check warning on line 106 in src/AppStoreServerApiClient.cs

View workflow job for this annotation

GitHub Actions / build

The type 'object?' cannot be used as type parameter 'TReturn' in the generic type or method 'AppStoreServerApiClient.MakeRequest<TReturn>(string, HttpMethod, Dictionary<string, string>?, object?, bool)'. Nullability of type argument 'object?' doesn't match 'class' constraint.

Check warning on line 106 in src/AppStoreServerApiClient.cs

View workflow job for this annotation

GitHub Actions / build

The type 'object?' cannot be used as type parameter 'TReturn' in the generic type or method 'AppStoreServerApiClient.MakeRequest<TReturn>(string, HttpMethod, Dictionary<string, string>?, object?, bool)'. Nullability of type argument 'object?' doesn't match 'class' constraint.
}

private static string CreateBearerToken(string keyId, string issuerId, string signingKey, string bundleId)
Expand Down Expand Up @@ -142,9 +142,8 @@
{
string token = CreateBearerToken(keyId, issuerId, signingKey, bundleId);

Uri url = new(environment.BaseUrl, path);
UriBuilder builder = new(environment.BaseUrl) { Path = path };

var builder = new UriBuilder(url);
if (queryParameters != null && queryParameters.Any())
{
NameValueCollection query = HttpUtility.ParseQueryString(builder.Query);
Expand Down
2 changes: 1 addition & 1 deletion src/Models/AppStoreDataTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ public class SubscriptionStatusResponse
/// <summary>
/// The unique identifier of the app that the notification applies to.
/// </summary>
public string? AppAppleId { get; set; }
public long AppAppleId { get; set; }

/// <summary>
/// The bundle identifier of the app.
Expand Down
46 changes: 41 additions & 5 deletions tests/AppStoreServerApiClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,59 @@ private static AppStoreServerApiClient GetAppStoreServerApiClient(MockHttpMessag
[Fact]
public async Task GetAllSubscriptionStatuses_Success()
{
const string responseData = """
{
"environment" : "Sandbox",
"bundleId" : "com.test.app",
"appAppleId" : 1234567890,
"data" : [ {
"subscriptionGroupIdentifier" : "98765",
"lastTransactions" : [ {
"originalTransactionId" : "123454321",
"status" : 2,
"signedTransactionInfo" : "eyabc",
"signedRenewalInfo" : "eyxyz"
} ]
} ]
}
""";
var mockHttp = new MockHttpMessageHandler();
mockHttp
.When($"{AppStoreEnvironment.LocalTesting.BaseUrl}v1/subscriptions/123456")
.Respond("application/json", "{\"data\":[]}");
.When("https://local-testing-base-url/inApps/v1/subscriptions/123456")
.Respond("application/json", responseData);

AppStoreServerApiClient client = GetAppStoreServerApiClient(mockHttp);
SubscriptionStatusResponse response = await client.GetAllSubscriptionStatuses("123456");

Assert.NotNull(response);
Assert.Equal("Sandbox", response.Environment);
Assert.Equal("com.test.app", response.BundleId);
Assert.Equal(1234567890, response.AppAppleId);
Assert.Collection(
response.Data,
data =>
{
Assert.Equal("98765", data.SubscriptionGroupIdentifier);
Assert.Collection(
data.LastTransactions,
transaction =>
{
Assert.Equal("123454321", transaction.OriginalTransactionId);
Assert.Equal(TransactionsItemSubscriptionStatus.Expired, transaction.Status);
Assert.Equal("eyabc", transaction.SignedTransactionInfo);
Assert.Equal("eyxyz", transaction.SignedRenewalInfo);
}
);
}
);
}

[Fact]
public async Task GetNotificationHistory_Success()
{
var mockHttp = new MockHttpMessageHandler();
mockHttp
.When($"{AppStoreEnvironment.LocalTesting.BaseUrl}v1/notifications/history")
.When($"https://local-testing-base-url/inApps/v1/notifications/history")
.Respond("application/json", "{\"notificationHistory\":[]}");

AppStoreServerApiClient client = GetAppStoreServerApiClient(mockHttp);
Expand All @@ -63,7 +99,7 @@ public async Task GetTransactionHistory_Success()
{
var mockHttp = new MockHttpMessageHandler();
mockHttp
.When($"{AppStoreEnvironment.LocalTesting.BaseUrl}v2/history/123456")
.When($"https://local-testing-base-url/inApps/v2/history/123456")
.Respond("application/json", "{\"signedTransactions\":[]}");

AppStoreServerApiClient client = GetAppStoreServerApiClient(mockHttp);
Expand All @@ -77,7 +113,7 @@ public async Task SendConsumptionData_Success()
{
var mockHttp = new MockHttpMessageHandler();
mockHttp
.When($"{AppStoreEnvironment.LocalTesting.BaseUrl}v1/transactions/consumption/123456")
.When($"https://local-testing-base-url/inApps/v1/transactions/consumption/123456")
.Respond(System.Net.HttpStatusCode.OK);

AppStoreServerApiClient client = GetAppStoreServerApiClient(mockHttp);
Expand Down
Loading