Skip to content

Commit

Permalink
Merge pull request #142 from vapor-community/december-update
Browse files Browse the repository at this point in the history
API updates.
  • Loading branch information
Andrewangeta authored Dec 17, 2021
2 parents ac78042 + 2024e8f commit 369ccae
Show file tree
Hide file tree
Showing 33 changed files with 644 additions and 38 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 16.0.0 - 2021-12-17
* [#142](https://github.com/vapor-community/stripe-kit/pull/142) ⚠️ Breaking changes ⚠️ Multiple API updates.

## 15.0.1 - 2021-11-13
* [#141](https://github.com/vapor-community/stripe-kit/pull/141) Fixes exchange rate decoding type.

Expand Down
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@
## Installation
To start using StripeKit, in your `Package.swift`, add the following

~~~~swift
.package(url: "https://github.com/vapor-community/stripe-kit.git", from: "14.0.0")
~~~~
```swift
.package(url: "https://github.com/vapor-community/stripe-kit.git", from: "16.0.0")
```

## Using the API
Initialize the `StripeClient`

~~~~swift
```swift
let httpClient = HTTPClient(..)
let stripe = StripeClient(httpClient: httpClient, eventLoop: eventLoop, apiKey: "sk_12345")
~~~~
```

And now you have acess to the APIs via `stripe`.

The APIs you have available correspond to what's implemented.

For example to use the `charges` API, the stripeclient has a property to access that API via routes.

~~~~swift
```swift
stripe.charges.create(amount: 2500,
currency: .usd,
description: "A server written in swift.",
Expand All @@ -40,7 +40,7 @@ For example to use the `charges` API, the stripeclient has a property to access
print("Sorry you have to use Node.js 🤢")
}
}
~~~~
```

## Expandable objects

Expand Down Expand Up @@ -245,7 +245,9 @@ See the [Vapor helper library](https://github.com/vapor-community/stripe) to use
* [x] Coupons
* [x] Promotion Codes
* [x] Discounts
* [x] Tax Codes
* [x] Tax Rates
* [x] Shipping Rates
---
### Checkout
* [x] Sessions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public struct StripePortalConfiguration: StripeModel {
public var isDefault: Bool?
/// 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?
/// Set of key-value pairs that you can attach to an object. This can be useful for storing additional information about the object in a structured format.
public var metadata: [String: String]?
/// Time at which the object was last updated. Measured in seconds since the Unix epoch.
public var updated: Date?
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ public protocol PortalConfigurationRoutes {
/// - businessProfile: The business information shown to customers in the portal.
/// - features: Information about the features available in the portal.
/// - defaultReturnUrl: The default URL to redirect customers to when they click on the portal’s link to return to your website. This can be overriden when creating the session.
/// - metadata: Set of key-value pairs that you can attach to an object. This can be useful for storing additional information about the object in a structured format. Individual keys can be unset by posting an empty value to them. All keys can be unset by posting an empty value to metadata.
func create(businessProfile: [String: Any],
features: [String: Any],
defaultReturnUrl: String?) -> EventLoopFuture<StripePortalConfiguration>
defaultReturnUrl: String?,
metadata: [String: String]?) -> EventLoopFuture<StripePortalConfiguration>

/// Updates a configuration that describes the functionality of the customer portal.
/// - Parameters:
Expand All @@ -25,11 +27,13 @@ public protocol PortalConfigurationRoutes {
/// - businessProfile: The business information shown to customers in the portal.
/// - defaultReturnUrl: The default URL to redirect customers to when they click on the portal’s link to return to your website. This can be overriden when creating the session.
/// - features: Information about the features available in the portal.
/// - metadata: Set of key-value pairs that you can attach to an object. This can be useful for storing additional information about the object in a structured format. Individual keys can be unset by posting an empty value to them. All keys can be unset by posting an empty value to metadata.
func update(configuration: String,
active: Bool?,
businessProfile: [String: Any]?,
defaultReturnUrl: String?,
features: [String: Any]?) -> EventLoopFuture<StripePortalConfiguration>
features: [String: Any]?,
metadata: [String: String]?) -> EventLoopFuture<StripePortalConfiguration>

/// Retrieves a configuration that describes the functionality of the customer portal.
/// - Parameter configuration: The identifier of the configuration to retrieve.
Expand All @@ -48,22 +52,26 @@ public protocol PortalConfigurationRoutes {
extension PortalConfigurationRoutes {
public func create(businessProfile: [String: Any],
features: [String: Any],
defaultReturnUrl: String? = nil) -> EventLoopFuture<StripePortalConfiguration> {
defaultReturnUrl: String? = nil,
metadata: [String: String]? = nil) -> EventLoopFuture<StripePortalConfiguration> {
create(businessProfile: businessProfile,
features: features,
defaultReturnUrl: defaultReturnUrl)
defaultReturnUrl: defaultReturnUrl,
metadata: metadata)
}

public func update(configuration: String,
active: Bool? = nil,
businessProfile: [String: Any]? = nil,
defaultReturnUrl: String? = nil,
features: [String: Any]? = nil) -> EventLoopFuture<StripePortalConfiguration> {
features: [String: Any]? = nil,
metadata: [String: String]? = nil) -> EventLoopFuture<StripePortalConfiguration> {
update(configuration: configuration,
active: active,
businessProfile: businessProfile,
defaultReturnUrl: defaultReturnUrl,
features: features)
features: features,
metadata: metadata)
}

public func retrieve(configuration: String) -> EventLoopFuture<StripePortalConfiguration> {
Expand All @@ -87,7 +95,8 @@ public struct StripePortalConfigurationRoutes: PortalConfigurationRoutes {

public func create(businessProfile: [String: Any],
features: [String: Any],
defaultReturnUrl: String?) -> EventLoopFuture<StripePortalConfiguration> {
defaultReturnUrl: String?,
metadata: [String: String]?) -> EventLoopFuture<StripePortalConfiguration> {
var body: [String: Any] = [:]

businessProfile.forEach { body["business_profile[\($0)]"] = $1 }
Expand All @@ -98,14 +107,19 @@ public struct StripePortalConfigurationRoutes: PortalConfigurationRoutes {
body["default_return_url"] = defaultReturnUrl
}

if let metadata = metadata {
metadata.forEach { body["metadata[\($0)]"] = $1 }
}

return apiHandler.send(method: .POST, path: portalconfiguration, body: .string(body.queryParameters), headers: headers)
}

public func update(configuration: String,
active: Bool?,
businessProfile: [String: Any]?,
defaultReturnUrl: String?,
features: [String: Any]?) -> EventLoopFuture<StripePortalConfiguration> {
features: [String: Any]?,
metadata: [String: String]?) -> EventLoopFuture<StripePortalConfiguration> {
var body: [String: Any] = [:]

if let active = active {
Expand All @@ -124,6 +138,10 @@ public struct StripePortalConfigurationRoutes: PortalConfigurationRoutes {
body["default_return_url"] = defaultReturnUrl
}

if let metadata = metadata {
metadata.forEach { body["metadata[\($0)]"] = $1 }
}

return apiHandler.send(method: .POST, path: "\(portalconfiguration)/\(configuration)", body: .string(body.queryParameters), headers: headers)
}

Expand Down
37 changes: 30 additions & 7 deletions Sources/StripeKit/Checkout/SessionRoutes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ public protocol SessionRoutes {
/// - mode: The mode of the Checkout Session, one of `payment`, `setup`, or `subscription`.
/// - paymentIntentData: A subset of parameters to be passed to PaymentIntent creation.
/// - paymentMethodOptions: Payment-method-specific configuration.
/// - phoneNumberCollection: Controls phone number collection settings for the session. We recommend that you review your privacy policy and check with your legal contacts before using this feature. Learn more about collecting phone numbers with Checkout.
/// - setupIntentData: A subset of parameters to be passed to SetupIntent creation for Checkout Sessions in `setup` mode.
/// - shippingAddressCollection: When set, provides configuration for Checkout to collect a shipping address from a customer.
/// - shippingRates: The shipping rate to apply to this Session. Currently, only up to one may be specified
/// - shipppingOptions:The shipping rate options to apply to this Session.
/// - submitType: Describes the type of transaction being performed by Checkout in order to customize relevant text on the page, such as the submit button. `submit_type` can only be specified on Checkout Sessions in payment mode, but not Checkout Sessions in subscription or setup mode. Supported values are `auto`, `book`, `donate`, or `pay`.
/// - subscriptionData: A subset of parameters to be passed to subscription creation.
/// - taxIdCollection: Controls tax ID collection settings for the session.
Expand All @@ -52,14 +53,21 @@ public protocol SessionRoutes {
mode: StripeSessionMode?,
paymentIntentData: [String: Any]?,
paymentMethodOptions: [String: Any]?,
phoneNumberCollection: Bool?,
setupIntentData: [String: Any]?,
shippingAddressCollection: [String: Any]?,
shippingRates: [String]?,
shippingOptions: [[String: Any]]?,
submitType: StripeSessionSubmitType?,
subscriptionData: [String: Any]?,
taxIdCollection: [String: Any]?,
expand: [String]?) -> EventLoopFuture<StripeSession>

/// A Session can be expired when it is in one of these statuses: open
/// After it expires, a customer can’t complete a Session and customers loading the Session see a message saying the Session is expired.
/// - Parameter id: The ID of the Checkout Session.
/// - Returns: A `StripeSession`.
func expire(id: String) -> EventLoopFuture<StripeSession>

/// Retrieves a Session object.
///
/// - Parameters:
Expand Down Expand Up @@ -100,9 +108,10 @@ extension SessionRoutes {
mode: StripeSessionMode? = nil,
paymentIntentData: [String: Any]? = nil,
paymentMethodOptions: [String: Any]? = nil,
phoneNumberCollection: Bool? = nil,
setupIntentData: [String: Any]? = nil,
shippingAddressCollection: [String: Any]? = nil,
shippingRates: [String]? = nil,
shippingOptions: [[String: Any]]? = nil,
submitType: StripeSessionSubmitType? = nil,
subscriptionData: [String: Any]? = nil,
taxIdCollection: [String: Any]? = nil,
Expand All @@ -123,15 +132,20 @@ extension SessionRoutes {
mode: mode,
paymentIntentData: paymentIntentData,
paymentMethodOptions: paymentMethodOptions,
phoneNumberCollection: phoneNumberCollection,
setupIntentData: setupIntentData,
shippingAddressCollection: shippingAddressCollection,
shippingRates: shippingRates,
shippingOptions: shippingOptions,
submitType: submitType,
subscriptionData: subscriptionData,
taxIdCollection: taxIdCollection,
expand: expand)
}

public func expire(id: String) -> EventLoopFuture<StripeSession> {
expire(id: id)
}

public func retrieve(id: String, expand: [String]? = nil) -> EventLoopFuture<StripeSession> {
return retrieve(id: id, expand: expand)
}
Expand Down Expand Up @@ -171,9 +185,10 @@ public struct StripeSessionRoutes: SessionRoutes {
mode: StripeSessionMode?,
paymentIntentData: [String: Any]?,
paymentMethodOptions: [String: Any]?,
phoneNumberCollection: Bool?,
setupIntentData: [String: Any]?,
shippingAddressCollection: [String: Any]?,
shippingRates: [String]?,
shippingOptions: [[String: Any]]?,
submitType: StripeSessionSubmitType?,
subscriptionData: [String: Any]?,
taxIdCollection: [String: Any]?,
Expand Down Expand Up @@ -234,6 +249,10 @@ public struct StripeSessionRoutes: SessionRoutes {
paymentMethodOptions.forEach { body["payment_method_options[\($0)]"] = $1 }
}

if let phoneNumberCollection = phoneNumberCollection {
body["phone_number_collection[enabled]"] = phoneNumberCollection
}

if let setupIntentData = setupIntentData {
setupIntentData.forEach { body["setup_intent_data[\($0)]"] = $1 }
}
Expand All @@ -242,8 +261,8 @@ public struct StripeSessionRoutes: SessionRoutes {
shippingAddressCollection.forEach { body["shipping_address_collection[\($0)]"] = $1 }
}

if let shippingRates = shippingRates {
body["shipping_rates"] = shippingRates
if let shippingOptions = shippingOptions {
body["shipping_options"] = shippingOptions
}

if let submitType = submitType {
Expand All @@ -265,6 +284,10 @@ public struct StripeSessionRoutes: SessionRoutes {
return apiHandler.send(method: .POST, path: sessions, body: .string(body.queryParameters), headers: headers)
}

public func expire(id: String) -> EventLoopFuture<StripeSession> {
apiHandler.send(method: .POST, path: "\(sessions)/\(id)/expire", headers: headers)
}

public func retrieve(id: String, expand: [String]?) -> EventLoopFuture<StripeSession> {
var queryParams = ""
if let expand = expand {
Expand Down
31 changes: 31 additions & 0 deletions Sources/StripeKit/Checkout/Sessions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public struct StripeSession: StripeModel {
@Expandable<StripePaymentIntent> public var paymentIntent: String?
/// Payment-method-specific configuration for the PaymentIntent or SetupIntent of this CheckoutSession.
public var paymentMethodOptions: StripeSessionPaymentMethodOptions?
/// Details on the state of phone number collection for the session.
public var phoneNumberCOllection: StripeSessionPhoneNumberCollection?
/// A list of the types of payment methods (e.g. card) this Checkout Session is allowed to accept.
public var paymentMethodTypes: [StripeSessionPaymentMethodType]?
/// The payment status of the Checkout Session, one of `paid`, `unpaid`, or `no_payment_required`. You can use this value to decide when to fulfill your customer’s order.
Expand All @@ -55,8 +57,14 @@ public struct StripeSession: StripeModel {
public var shipping: StripeShippingLabel?
/// When set, provides configuration for Checkout to collect a shipping address from a customer.
public var shippingAddressCollection: StripeSessionShippingAddressCollection?
/// The shipping rate options applied to this Session.
public var shipppingOptions: [StripeSessionShippingOption]?
/// The ID of the ShippingRate for Checkout Sessions in payment mode.
@Expandable<StripeShippingRate> public var shippingRate: String?
/// Describes the type of transaction being performed by Checkout in order to customize relevant text on the page, such as the submit button. `submit_type` can only be specified on Checkout Sessions in `payment` mode, but not Checkout Sessions in `subscription` or `setup` mode. Supported values are `auto`, `book`, `donate`, or `pay`.
public var submitType: StripeSessionSubmitType?
/// The status of the Checkout Session, one of `open`, `complete`, or `expired`.
public var status: StripeSessionStatus?
/// The ID of the subscription created if one or more plans were provided.
@Expandable<StripeSubscription> public var subscription: String?
/// The URL the customer will be directed to after the payment or subscription creation is successful.
Expand Down Expand Up @@ -88,6 +96,8 @@ public struct StripeSessionCustomerDetails: StripeModel {
public var taxExempt: String?
/// The customer’s tax IDs at time of checkout.
public var taxIds: [StripeSessionCustomerDetailsTaxId]?
/// The customer’s phone number at the time of checkout
public var phone: String?
}

public struct StripeSessionCustomerDetailsTaxId: StripeModel {
Expand Down Expand Up @@ -195,6 +205,11 @@ public struct StripeSessionPaymentMethodOptions: StripeModel {
public var oxxo: StripeSessionPaymentMethodOptionsOXXO?
}

public struct StripeSessionPhoneNumberCollection: StripeModel {
/// Indicates whether phone number collection is enabled for the session
public var enabled: Bool
}

public struct StripeSessionPaymentMethodOptionsAcssDebit: StripeModel {
/// Currency supported by the bank account. Returned when the Session is in `setup` mode.
public var currency: StripeSessionPaymentMethodOptionsAcssDebitCurrency?
Expand Down Expand Up @@ -280,13 +295,29 @@ public struct StripeSessionShippingAddressCollection: StripeModel {
public var allowedCountries: [String]?
}

public struct StripeSessionShippingOption: StripeModel {
/// A non-negative integer in cents representing how much to charge.
public var shippingAmount: Int?
/// The shipping rate.
@Expandable<StripeShippingRate> public var shippingRate: String?
}

public enum StripeSessionSubmitType: String, StripeModel {
case auto
case book
case donate
case pay
}

public enum StripeSessionStatus: String, Codable {
/// The checkout session is still in progress. Payment processing has not started
case open
/// The checkout session is complete. Payment processing may still be in progress
case complete
/// The checkout session has expired. No further processing will occur
case expired
}

public struct StripeSessionTotalDetails: StripeModel {
/// This is the sum of all the line item discounts.
public var amountDiscount: Int?
Expand Down
Loading

0 comments on commit 369ccae

Please sign in to comment.