|
| 1 | +/// Represent a Stripe Account Entity. |
| 2 | +/// |
| 3 | +public struct StripeAccount: Decodable { |
| 4 | + public static let gatewayID = "woocommerce-stripe" |
| 5 | + |
| 6 | + public let status: WCPayAccountStatusEnum |
| 7 | + /// Indicates whether the payment gateway is a live account that can accept actual payments, or just a test/developer account. |
| 8 | + /// Not to be confused with "test mode" which is a separate concept (see `isInTestMode`) |
| 9 | + /// Note: Using WCPayAccountStatusEnum is somewhat inappropriate here, since it ties this Stripe account concept |
| 10 | + /// to WooCommerce Payments - although in reality both extensions use the same account on the backend. |
| 11 | + /// TODO: Introduce a layer of abstraction and translate each extension's account status into a |
| 12 | + /// PaymentGatewayAccountStatusEnum or somesuch. |
| 13 | + /// |
| 14 | + public let isLiveAccount: Bool |
| 15 | + /// Indicates whether the payment gateway is currently in "Test" or "Debug" mode |
| 16 | + /// |
| 17 | + public let isInTestMode: Bool |
| 18 | + public let hasPendingRequirements: Bool |
| 19 | + public let hasOverdueRequirements: Bool |
| 20 | + public let currentDeadline: Date? |
| 21 | + /// An alphanumeric string set by the merchant, e.g. `MYSTORE.COM` |
| 22 | + /// See https://stripe.com/docs/statement-descriptors |
| 23 | + public let statementDescriptor: String |
| 24 | + /// A three character lowercase currency code, e.g. `usd` |
| 25 | + /// See https://stripe.com/docs/api/accounts/object#account_object-default_currency |
| 26 | + public let defaultCurrency: String |
| 27 | + public let supportedCurrencies: [String] |
| 28 | + /// A two character country code, e.g. `US` |
| 29 | + /// See https://stripe.com/docs/api/accounts/object#account_object-country |
| 30 | + public let country: String |
| 31 | + /// A boolean flag indicating if this Account is eligible for card present payments |
| 32 | + public let isCardPresentEligible: Bool |
| 33 | + |
| 34 | + public init( |
| 35 | + status: WCPayAccountStatusEnum, |
| 36 | + isLiveAccount: Bool, |
| 37 | + isInTestMode: Bool, |
| 38 | + hasPendingRequirements: Bool, |
| 39 | + hasOverdueRequirements: Bool, |
| 40 | + currentDeadline: Date?, |
| 41 | + statementDescriptor: String, |
| 42 | + defaultCurrency: String, |
| 43 | + supportedCurrencies: [String], |
| 44 | + country: String, |
| 45 | + isCardPresentEligible: Bool |
| 46 | + ) { |
| 47 | + self.status = status |
| 48 | + self.isLiveAccount = isLiveAccount |
| 49 | + self.isInTestMode = isInTestMode |
| 50 | + self.hasPendingRequirements = hasPendingRequirements |
| 51 | + self.hasOverdueRequirements = hasOverdueRequirements |
| 52 | + self.currentDeadline = currentDeadline |
| 53 | + self.statementDescriptor = statementDescriptor |
| 54 | + self.defaultCurrency = defaultCurrency |
| 55 | + self.supportedCurrencies = supportedCurrencies |
| 56 | + self.country = country |
| 57 | + self.isCardPresentEligible = isCardPresentEligible |
| 58 | + } |
| 59 | + |
| 60 | + /// The public initializer for WCPay Account. |
| 61 | + /// |
| 62 | + public init(from decoder: Decoder) throws { |
| 63 | + let container = try decoder.container(keyedBy: CodingKeys.self) |
| 64 | + let status = try container.decode(WCPayAccountStatusEnum.self, forKey: .status) |
| 65 | + let isLiveAccount = try container.decode(Bool.self, forKey: .isLive) |
| 66 | + let isInTestMode = try container.decode(Bool.self, forKey: .testMode) |
| 67 | + let hasPendingRequirements = try container.decode(Bool.self, forKey: .hasPendingRequirements) |
| 68 | + let hasOverdueRequirements = try container.decode(Bool.self, forKey: .hasOverdueRequirements) |
| 69 | + let currentDeadline = try container.decodeIfPresent(Date.self, forKey: .currentDeadline) |
| 70 | + let statementDescriptor = try container.decode(String.self, forKey: .statementDescriptor) |
| 71 | + let currencyContainer = try container.nestedContainer(keyedBy: CurrencyCodingKeys.self, forKey: .storeCurrencies) |
| 72 | + let defaultCurrency = try currencyContainer.decode(String.self, forKey: .defaultCurrency) |
| 73 | + let supportedCurrencies = try currencyContainer.decode([String].self, forKey: .supported) |
| 74 | + let country = try container.decode(String.self, forKey: .country) |
| 75 | + /// Ignore the state of the In-Person Payments Beta flag (`.cardPresentEligible`). Allow any site. See #5030 |
| 76 | + /// let cardPresentEligible = try container.decodeIfPresent(Bool.self, forKey: .cardPresentEligible) ?? false |
| 77 | + |
| 78 | + self.init( |
| 79 | + status: status, |
| 80 | + isLiveAccount: isLiveAccount, |
| 81 | + isInTestMode: isInTestMode, |
| 82 | + hasPendingRequirements: hasPendingRequirements, |
| 83 | + hasOverdueRequirements: hasOverdueRequirements, |
| 84 | + currentDeadline: currentDeadline, |
| 85 | + statementDescriptor: statementDescriptor, |
| 86 | + defaultCurrency: defaultCurrency, |
| 87 | + supportedCurrencies: supportedCurrencies, |
| 88 | + country: country, |
| 89 | + isCardPresentEligible: true |
| 90 | + ) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +public extension StripeAccount { |
| 95 | + static let noAccount = StripeAccount( |
| 96 | + status: .noAccount, |
| 97 | + isLiveAccount: false, |
| 98 | + isInTestMode: false, |
| 99 | + hasPendingRequirements: false, |
| 100 | + hasOverdueRequirements: false, |
| 101 | + currentDeadline: nil, |
| 102 | + statementDescriptor: "", |
| 103 | + defaultCurrency: "", |
| 104 | + supportedCurrencies: [], |
| 105 | + country: "", |
| 106 | + isCardPresentEligible: false |
| 107 | + ) |
| 108 | +} |
| 109 | + |
| 110 | +private extension StripeAccount { |
| 111 | + enum CodingKeys: String, CodingKey { |
| 112 | + case status |
| 113 | + case isLive = "is_live" |
| 114 | + case testMode = "test_mode" |
| 115 | + case hasPendingRequirements = "has_pending_requirements" |
| 116 | + case hasOverdueRequirements = "has_overdue_requirements" |
| 117 | + case currentDeadline = "current_deadline" |
| 118 | + case statementDescriptor = "statement_descriptor" |
| 119 | + case storeCurrencies = "store_currencies" |
| 120 | + case country |
| 121 | + case cardPresentEligible = "card_present_eligible" |
| 122 | + } |
| 123 | + |
| 124 | + enum CurrencyCodingKeys: String, CodingKey { |
| 125 | + case defaultCurrency = "default" |
| 126 | + case supported |
| 127 | + } |
| 128 | +} |
0 commit comments