diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c3b67ec..6d58adb5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,10 @@ # Changelog +## 10.1.0 - 2020-11-28 +* [#113](https://github.com/vapor-community/stripe-kit/pull/113) Added support for [Customer Portal](https://stripe.com/docs/api/customer_portal) API. + ## 10.0.0 - 2020-11-10 -* [#122](https://github.com/vapor-community/stripe-kit/pull/112) Updated to latest 2020-08-27 API version. +* [#112](https://github.com/vapor-community/stripe-kit/pull/112) Updated to latest 2020-08-27 API version. ## 8.0.0 - 2020-07-21 * [#98](https://github.com/vapor-community/stripe-kit/pull/98) Added support for the `Price`s API. diff --git a/README.md b/README.md index ec257be8..2381838f 100644 --- a/README.md +++ b/README.md @@ -228,6 +228,7 @@ See the [Vapor helper library](https://github.com/vapor-community/stripe) to use * [x] Mandates * [x] PaymentIntents * [x] SetupIntents +* [x] SetupAttempts * [x] Payouts * [x] Prices * [x] Products @@ -247,11 +248,13 @@ See the [Vapor helper library](https://github.com/vapor-community/stripe) to use * [x] Coupons * [x] Credit Notes * [x] Customer Balance Transactions +* [x] Customer Portal * [x] Customer Tax IDs * [x] Discounts * [x] Invoices * [x] Invoice Items * [x] Plans +* [x] Promotion Codes * [x] Products * [x] Subscriptions * [x] Subscription items diff --git a/Sources/StripeKit/Billing/Customer Portal/CustomerSession.swift b/Sources/StripeKit/Billing/Customer Portal/CustomerSession.swift new file mode 100644 index 00000000..e3c88f43 --- /dev/null +++ b/Sources/StripeKit/Billing/Customer Portal/CustomerSession.swift @@ -0,0 +1,25 @@ +// +// CustomerSession.swift +// +// +// Created by Andrew Edwards on 11/28/20. +// + +import Foundation + +public struct StripeCustomerSession: StripeModel { + /// Unique identifier for the object. + public var id: String + /// String representing the object’s type. Objects of the same type share the same value. + public var object: String? + /// Time at which the object was created. Measured in seconds since the Unix epoch. + public var created: Date + /// The ID of the customer for this session. + public var customer: String? + /// Has the value true if the object exists in live mode or the value false if the object exists in test mode. + public var livemode: Bool? + /// The URL to which Stripe should send customers when they click on the link to return to your website. + public var returnUrl: String? + /// The short-lived URL of the session giving customers access to the customer portal. + public var url: String? +} diff --git a/Sources/StripeKit/Billing/Customer Portal/CustomerSessionRoutes.swift b/Sources/StripeKit/Billing/Customer Portal/CustomerSessionRoutes.swift new file mode 100644 index 00000000..6f705d08 --- /dev/null +++ b/Sources/StripeKit/Billing/Customer Portal/CustomerSessionRoutes.swift @@ -0,0 +1,45 @@ +// +// CustomerSessionRoutes.swift +// +// +// Created by Andrew Edwards on 11/28/20. +// + +import NIO +import NIOHTTP1 + +public protocol CustomerSessionRoutes { + + /// Creates a session of the customer portal. + /// - Parameters: + /// - customer: The ID of an existing customer. + /// - returnUrl: The URL to which Stripe should send customers when they click on the link to return to your website. This field is required if a default return URL has not been configured for the portal. + func create(customer: String, returnUrl: String?) -> EventLoopFuture +} + +extension CustomerSessionRoutes { + public func create(customer: String, returnUrl: String? = nil) -> EventLoopFuture { + create(customer: customer, returnUrl: returnUrl) + } +} + +public struct StripeCustomerSessionRoutes: CustomerSessionRoutes { + public var headers: HTTPHeaders = [:] + + private let apiHandler: StripeAPIHandler + private let sessions = APIBase + APIVersion + "billing_portal/sessions" + + init(apiHandler: StripeAPIHandler) { + self.apiHandler = apiHandler + } + + public func create(customer: String, returnUrl: String?) -> EventLoopFuture { + var body: [String: Any] = ["customer": customer] + + if let returnUrl = returnUrl { + body["return_url"] = returnUrl + } + + return apiHandler.send(method: .POST, path: sessions, body: .string(body.queryParameters)) + } +} diff --git a/Sources/StripeKit/StripeClient.swift b/Sources/StripeKit/StripeClient.swift index 0631540f..985063ca 100644 --- a/Sources/StripeKit/StripeClient.swift +++ b/Sources/StripeKit/StripeClient.swift @@ -41,6 +41,7 @@ public final class StripeClient { public var coupons: CouponRoutes public var creditNotes: CreditNoteRoutes public var customerBalanceTransactions: CustomerBalanceTransactionRoutes + public var customerSession: CustomerSessionRoutes public var customerTaxIds: CustomerTaxIDRoutes public var discounts: DiscountRoutes public var invoices: InvoiceRoutes @@ -137,6 +138,7 @@ public final class StripeClient { coupons = StripeCouponRoutes(apiHandler: handler) creditNotes = StripeCreditNoteRoutes(apiHandler: handler) customerBalanceTransactions = StripeCustomerBalanceTransactionRoutes(apiHandler: handler) + customerSession = StripeCustomerSessionRoutes(apiHandler: handler) customerTaxIds = StripeCustomerTaxIDRoutes(apiHandler: handler) discounts = StripeDiscountRoutes(apiHandler: handler) invoices = StripeInvoiceRoutes(apiHandler: handler)