| 1 | using System.Text.Json.Serialization; |
| 2 | |
| 3 | namespace HBank.Sdk; |
| 4 | |
| 5 | /// <summary>A payment (or a refund, <c>Type == "refund"</c>), as <see cref="HBankClient.GetPaymentAsync"/> returns it.</summary> |
| 6 | public sealed record Payment |
| 7 | { |
| 8 | [JsonPropertyName("id")] public string Id { get; init; } = ""; |
| 9 | [JsonPropertyName("type")] public string Type { get; init; } = "payment"; |
| 10 | [JsonPropertyName("amount")] public decimal Amount { get; init; } |
| 11 | [JsonPropertyName("currency")] public string Currency { get; init; } = ""; |
| 12 | /// <summary>pending, completed or aborted.</summary> |
| 13 | [JsonPropertyName("status")] public string Status { get; init; } = ""; |
| 14 | [JsonPropertyName("description")] public string? Description { get; init; } |
| 15 | /// <summary>Set once the payment is completed.</summary> |
| 16 | [JsonPropertyName("reference")] public string? Reference { get; init; } |
| 17 | [JsonPropertyName("payment_code")] public string? PaymentCode { get; init; } |
| 18 | [JsonPropertyName("refunded")] public decimal Refunded { get; init; } |
| 19 | [JsonPropertyName("refundable")] public decimal Refundable { get; init; } |
| 20 | [JsonPropertyName("refund_of")] public string? RefundOf { get; init; } |
| 21 | [JsonPropertyName("subscription_id")] public string? SubscriptionId { get; init; } |
| 22 | /// <summary>Every subscription this payment belongs to: the one it charged, or all those a checkout of several started.</summary> |
| 23 | [JsonPropertyName("subscription_ids")] public IReadOnlyList<string> SubscriptionIds { get; init; } = []; |
| 24 | |
| 25 | [JsonIgnore] public bool Completed => Status == "completed"; |
| 26 | } |
| 27 | |
| 28 | /// <summary>A QR code for a pending payment: put <see cref="Url"/> in a QR code, or show the SVG at <see cref="QrSvg"/>.</summary> |
| 29 | public sealed record PaymentCode( |
| 30 | [property: JsonPropertyName("code")] string Code, |
| 31 | [property: JsonPropertyName("url")] string Url, |
| 32 | [property: JsonPropertyName("qr_svg")] string QrSvg); |
| 33 | |
| 34 | public sealed record Refund |
| 35 | { |
| 36 | [JsonPropertyName("id")] public string Id { get; init; } = ""; |
| 37 | [JsonPropertyName("amount")] public decimal Amount { get; init; } |
| 38 | [JsonPropertyName("currency")] public string Currency { get; init; } = ""; |
| 39 | [JsonPropertyName("reference")] public string Reference { get; init; } = ""; |
| 40 | [JsonPropertyName("reason")] public string? Reason { get; init; } |
| 41 | } |
| 42 | |
| 43 | /// <summary>The refund, and the refunded payment with updated <c>Refunded</c> / <c>Refundable</c>.</summary> |
| 44 | public sealed record RefundResult( |
| 45 | [property: JsonPropertyName("refund")] Refund Refund, |
| 46 | [property: JsonPropertyName("transaction")] Payment Payment); |
| 47 | |
| 48 | public sealed record SubscriptionCustomer([property: JsonPropertyName("name")] string Name); |
| 49 | |
| 50 | public sealed record Subscription |
| 51 | { |
| 52 | [JsonPropertyName("id")] public string Id { get; init; } = ""; |
| 53 | /// <summary>offered, active or canceled.</summary> |
| 54 | [JsonPropertyName("status")] public string Status { get; init; } = ""; |
| 55 | /// <summary>Within a period that's been paid for: give access while this is true.</summary> |
| 56 | [JsonPropertyName("active_period")] public bool ActivePeriod { get; init; } |
| 57 | [JsonPropertyName("amount")] public decimal Amount { get; init; } |
| 58 | [JsonPropertyName("currency")] public string Currency { get; init; } = ""; |
| 59 | /// <summary>day, week, month or year.</summary> |
| 60 | [JsonPropertyName("interval")] public string Interval { get; init; } = ""; |
| 61 | [JsonPropertyName("interval_count")] public int IntervalCount { get; init; } = 1; |
| 62 | [JsonPropertyName("description")] public string? Description { get; init; } |
| 63 | /// <summary>Send this to the customer.</summary> |
| 64 | [JsonPropertyName("url")] public string Url { get; init; } = ""; |
| 65 | [JsonPropertyName("qr_svg")] public string QrSvg { get; init; } = ""; |
| 66 | [JsonPropertyName("subscribed_at")] public DateTimeOffset? SubscribedAt { get; init; } |
| 67 | [JsonPropertyName("subscribed_for_days")] public int? SubscribedForDays { get; init; } |
| 68 | [JsonPropertyName("subscribed_for_seconds")] public long? SubscribedForSeconds { get; init; } |
| 69 | [JsonPropertyName("canceled")] public bool Canceled { get; init; } |
| 70 | [JsonPropertyName("canceled_at")] public DateTimeOffset? CanceledAt { get; init; } |
| 71 | /// <summary>customer, merchant, bank or schedule.</summary> |
| 72 | [JsonPropertyName("canceled_by")] public string? CanceledBy { get; init; } |
| 73 | [JsonPropertyName("current_period_start")] public DateTimeOffset? CurrentPeriodStart { get; init; } |
| 74 | [JsonPropertyName("current_period_end")] public DateTimeOffset? CurrentPeriodEnd { get; init; } |
| 75 | [JsonPropertyName("next_charge_at")] public DateTimeOffset? NextChargeAt { get; init; } |
| 76 | [JsonPropertyName("ends_at")] public DateTimeOffset? EndsAt { get; init; } |
| 77 | [JsonPropertyName("last_day")] public DateTimeOffset? LastDay { get; init; } |
| 78 | [JsonPropertyName("charges")] public int Charges { get; init; } |
| 79 | [JsonPropertyName("last_error")] public string? LastError { get; init; } |
| 80 | [JsonPropertyName("customer")] public SubscriptionCustomer? Customer { get; init; } |
| 81 | } |
| 82 | |
| 83 | /// <summary>The first payment of a subscription: hand <see cref="TransactionId"/> to the pay button / hosted checkout.</summary> |
| 84 | public sealed record SubscriptionCheckout( |
| 85 | [property: JsonPropertyName("transaction_id")] string TransactionId, |
| 86 | [property: JsonPropertyName("subscription")] Subscription Subscription); |
| 87 | |
| 88 | /// <summary>One first payment for several subscriptions: paying it starts all of <see cref="Subscriptions"/> (in your order).</summary> |
| 89 | public sealed record SubscriptionsCheckout( |
| 90 | [property: JsonPropertyName("transaction_id")] string TransactionId, |
| 91 | [property: JsonPropertyName("subscriptions")] IReadOnlyList<Subscription> Subscriptions); |
| 92 | |
| 93 | /// <summary>One subscription in <c>CreateSubscriptionsCheckoutAsync</c>: one you already offered, or a new offer.</summary> |
| 94 | public sealed record SubscriptionCheckoutItem |
| 95 | { |
| 96 | public string? SubscriptionId { get; private init; } |
| 97 | public decimal Amount { get; private init; } |
| 98 | public string Currency { get; private init; } = ""; |
| 99 | public string Interval { get; private init; } = ""; |
| 100 | public int IntervalCount { get; private init; } = 1; |
| 101 | public string? Description { get; private init; } |
| 102 | |
| 103 | /// <summary>A subscription you already offered ("sub_…").</summary> |
| 104 | public static SubscriptionCheckoutItem Existing(string subscriptionId) => new() { SubscriptionId = subscriptionId }; |
| 105 | |
| 106 | /// <summary>A new offer, made with the checkout (same fields as CreateSubscriptionAsync).</summary> |
| 107 | public static SubscriptionCheckoutItem New(decimal amount, string currency, string interval, int intervalCount = 1, string? description = null) => |
| 108 | new() { Amount = amount, Currency = currency, Interval = interval, IntervalCount = intervalCount, Description = description }; |
| 109 | |
| 110 | public static implicit operator SubscriptionCheckoutItem(string subscriptionId) => Existing(subscriptionId); |
| 111 | } |
| 112 | |
| 113 | /// <param name="ToRate">Converts currency_from into currency_to.</param> |
| 114 | /// <param name="FromRate">The other way.</param> |
| 115 | public sealed record Rate( |
| 116 | [property: JsonPropertyName("to_rate")] decimal ToRate, |
| 117 | [property: JsonPropertyName("from_rate")] decimal FromRate); |