-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #113 from vapor-community/customer-portal
Added support for the Customer Portal API.
- Loading branch information
Showing
5 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
Sources/StripeKit/Billing/Customer Portal/CustomerSession.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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? | ||
} |
45 changes: 45 additions & 0 deletions
45
Sources/StripeKit/Billing/Customer Portal/CustomerSessionRoutes.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<StripeCustomerSession> | ||
} | ||
|
||
extension CustomerSessionRoutes { | ||
public func create(customer: String, returnUrl: String? = nil) -> EventLoopFuture<StripeCustomerSession> { | ||
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<StripeCustomerSession> { | ||
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)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters