mirror of
https://github.com/streetwriters/notesnook-sync-server.git
synced 2026-02-12 19:22:45 +00:00
common: fix plan change if subscription is trialing
This commit is contained in:
committed by
Abdullah Atta
parent
6304d8178f
commit
9e6a25ec1d
21
Streetwriters.Common/Models/GetCustomerResponse.cs
Normal file
21
Streetwriters.Common/Models/GetCustomerResponse.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
namespace Streetwriters.Common.Models
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Globalization;
|
||||
|
||||
public partial class GetCustomerResponse : PaddleResponse
|
||||
{
|
||||
[JsonPropertyName("data")]
|
||||
public PaddleCustomer Customer { get; set; }
|
||||
}
|
||||
|
||||
public class PaddleCustomer
|
||||
{
|
||||
[JsonPropertyName("email")]
|
||||
public string Email { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -7,13 +7,10 @@ namespace Streetwriters.Common.Models
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Globalization;
|
||||
|
||||
public partial class GetSubscriptionResponse
|
||||
public partial class GetSubscriptionResponse : PaddleResponse
|
||||
{
|
||||
[JsonPropertyName("data")]
|
||||
public Data Data { get; set; }
|
||||
|
||||
[JsonPropertyName("meta")]
|
||||
public Meta Meta { get; set; }
|
||||
}
|
||||
|
||||
public partial class Data
|
||||
@@ -214,10 +211,4 @@ namespace Streetwriters.Common.Models
|
||||
[JsonPropertyName("cancel")]
|
||||
public Uri Cancel { get; set; }
|
||||
}
|
||||
|
||||
public partial class Meta
|
||||
{
|
||||
[JsonPropertyName("request_id")]
|
||||
public string RequestId { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,18 +7,14 @@ namespace Streetwriters.Common.Models
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Globalization;
|
||||
|
||||
public partial class GetTransactionInvoiceResponse
|
||||
public class GetTransactionInvoiceResponse : PaddleResponse
|
||||
{
|
||||
[JsonPropertyName("data")]
|
||||
public Invoice Invoice { get; set; }
|
||||
|
||||
[JsonPropertyName("meta")]
|
||||
public Meta Meta { get; set; }
|
||||
}
|
||||
|
||||
public partial class Invoice
|
||||
{
|
||||
|
||||
[JsonPropertyName("url")]
|
||||
public string Url { get; set; }
|
||||
}
|
||||
|
||||
@@ -7,12 +7,9 @@ namespace Streetwriters.Common.Models
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Globalization;
|
||||
|
||||
public partial class GetTransactionResponse
|
||||
public partial class GetTransactionResponse : PaddleResponse
|
||||
{
|
||||
[JsonPropertyName("data")]
|
||||
public TransactionV2 Transaction { get; set; }
|
||||
|
||||
[JsonPropertyName("meta")]
|
||||
public Meta Meta { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,13 +7,10 @@ namespace Streetwriters.Common.Models
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Globalization;
|
||||
|
||||
public partial class ListTransactionsResponseV2
|
||||
public partial class ListTransactionsResponseV2 : PaddleResponse
|
||||
{
|
||||
[JsonPropertyName("data")]
|
||||
public TransactionV2[] Transactions { get; set; }
|
||||
|
||||
[JsonPropertyName("meta")]
|
||||
public Meta Meta { get; set; }
|
||||
}
|
||||
|
||||
public partial class TransactionV2
|
||||
@@ -497,12 +494,6 @@ namespace Streetwriters.Common.Models
|
||||
// public string CardholderName { get; set; }
|
||||
// }
|
||||
|
||||
public partial class Meta
|
||||
{
|
||||
[JsonPropertyName("pagination")]
|
||||
public Pagination Pagination { get; set; }
|
||||
}
|
||||
|
||||
public partial class Pagination
|
||||
{
|
||||
[JsonPropertyName("per_page")]
|
||||
|
||||
24
Streetwriters.Common/Models/PaddleResponse.cs
Normal file
24
Streetwriters.Common/Models/PaddleResponse.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
namespace Streetwriters.Common.Models
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Globalization;
|
||||
|
||||
public partial class PaddleResponse
|
||||
{
|
||||
[JsonPropertyName("error")]
|
||||
public PaddleError Error { get; set; }
|
||||
}
|
||||
|
||||
public class PaddleError
|
||||
{
|
||||
public string? Type { get; set; }
|
||||
public string? Code { get; set; }
|
||||
public string? Detail { get; set; }
|
||||
[JsonPropertyName("documentation_url")]
|
||||
public string? DocumentationUrl { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -7,13 +7,10 @@ namespace Streetwriters.Common.Models
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Globalization;
|
||||
|
||||
public partial class SubscriptionPreviewResponse
|
||||
public partial class SubscriptionPreviewResponse : PaddleResponse
|
||||
{
|
||||
[JsonPropertyName("data")]
|
||||
public SubscriptionPreviewData Data { get; set; }
|
||||
|
||||
[JsonPropertyName("meta")]
|
||||
public Meta Meta { get; set; }
|
||||
}
|
||||
|
||||
public partial class SubscriptionPreviewData
|
||||
|
||||
@@ -80,26 +80,26 @@ namespace Streetwriters.Common.Services
|
||||
return response.IsSuccessStatusCode;
|
||||
}
|
||||
|
||||
public async Task<SubscriptionPreviewResponse?> PreviewSubscriptionChangeAsync(string subscriptionId, string newProductId)
|
||||
public async Task<SubscriptionPreviewResponse?> PreviewSubscriptionChangeAsync(string subscriptionId, string newProductId, bool isTrialing)
|
||||
{
|
||||
var url = $"{PADDLE_BASE_URI}/subscriptions/{subscriptionId}/preview";
|
||||
var response = await httpClient.PatchAsync(url, JsonContent.Create(new
|
||||
{
|
||||
proration_billing_mode = "prorated_immediately",
|
||||
proration_billing_mode = isTrialing ? "do_not_bill" : "prorated_immediately",
|
||||
items = new[] { new { price_id = newProductId, quantity = 1 } }
|
||||
}));
|
||||
return await response.Content.ReadFromJsonAsync<SubscriptionPreviewResponse>();
|
||||
}
|
||||
|
||||
public async Task<bool> ChangeSubscriptionAsync(string subscriptionId, string newProductId)
|
||||
public async Task<PaddleResponse?> ChangeSubscriptionAsync(string subscriptionId, string newProductId, bool isTrialing)
|
||||
{
|
||||
var url = $"{PADDLE_BASE_URI}/subscriptions/{subscriptionId}";
|
||||
var response = await httpClient.PatchAsync(url, JsonContent.Create(new
|
||||
{
|
||||
proration_billing_mode = "prorated_immediately",
|
||||
proration_billing_mode = isTrialing ? "do_not_bill" : "prorated_immediately",
|
||||
items = new[] { new { price_id = newProductId, quantity = 1 } }
|
||||
}));
|
||||
return response.IsSuccessStatusCode;
|
||||
return await response.Content.ReadFromJsonAsync<PaddleResponse>();
|
||||
}
|
||||
|
||||
public async Task<bool> CancelSubscriptionAsync(string subscriptionId)
|
||||
@@ -125,5 +125,14 @@ namespace Streetwriters.Common.Services
|
||||
}));
|
||||
return response.IsSuccessStatusCode;
|
||||
}
|
||||
|
||||
public async Task<GetCustomerResponse?> FindCustomerFromTransactionAsync(string transactionId)
|
||||
{
|
||||
var transaction = await GetTransactionAsync(transactionId);
|
||||
if (transaction == null) return null;
|
||||
var url = $"{PADDLE_BASE_URI}/customers/{transaction.Transaction.CustomerId}";
|
||||
var response = await httpClient.GetFromJsonAsync<GetCustomerResponse>(url);
|
||||
return response;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user