Skip to content

Commit

Permalink
Merge pull request #3 from vapor-community/Subscriptions
Browse files Browse the repository at this point in the history
Subscriptions, SubscriptionItems and Discounts implemented.
  • Loading branch information
anthonycastelli authored Jun 10, 2017
2 parents aaad06c + 5fef8af commit 74d0a21
Show file tree
Hide file tree
Showing 18 changed files with 1,065 additions and 12 deletions.
18 changes: 18 additions & 0 deletions Sources/API/Filter/StripeFilter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ public final class StripeFilter {
public var balanceType: BalanceType?


/**
The status of the subscriptions to retrieve.
*/
public var subscriptionStatus: StripeSubscriptionStatus?

/**
The ID of the plan whose subscriptions will be retrieved.
*/
public var plan: Node?

internal func createBody() throws -> Node {
var node = Node([:])
if let value = self.created {
Expand Down Expand Up @@ -160,6 +170,14 @@ public final class StripeFilter {
node["type"] = value.rawValue.makeNode(in: nil)
}

if let value = self.subscriptionStatus {
node["status"] = value.rawValue.makeNode(in: nil)
}

if let value = self.plan {
node["plan"] = value
}

return node
}

Expand Down
24 changes: 24 additions & 0 deletions Sources/API/Helpers/Endpoints.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ internal enum API {
case customers
case customer(String)
case customerSources(String)
case customerDiscount(String)

/**
TOKENS
Expand Down Expand Up @@ -98,6 +99,21 @@ internal enum API {
case sources
case source(String)

/**
SUBSCRIPTION ITEMS
Subscription items allow you to create customer subscriptions with more than one plan, making it easy to represent complex billing relationships.
*/
case subscriptionItem
case subscriptionItems(String)

/**
SUBSCRIPTIONS
Subscriptions allow you to charge a customer's card on a recurring basis. A subscription ties a customer to a particular plan you've created.
*/
case subscription
case subscriptions(String)
case subscriptionDiscount(String)

var endpoint: String {
switch self {
case .balance: return APIBase + APIVersion + "balance"
Expand All @@ -111,6 +127,7 @@ internal enum API {
case .customers: return APIBase + APIVersion + "customers"
case .customer(let id): return APIBase + APIVersion + "customers/\(id)"
case .customerSources(let id): return APIBase + APIVersion + "customers/\(id)/sources"
case .customerDiscount(let id): return APIBase + APIVersion + "customers/\(id)/discount"

case .tokens: return APIBase + APIVersion + "tokens"
case .token(let token): return APIBase + APIVersion + "tokens/\(token)"
Expand All @@ -126,6 +143,13 @@ internal enum API {

case .sources: return APIBase + APIVersion + "sources"
case .source(let id): return APIBase + APIVersion + "sources/\(id)"

case .subscriptionItem: return APIBase + APIVersion + "subscription_items"
case .subscriptionItems(let id): return APIBase + APIVersion + "subscription_items/\(id)"

case .subscription: return APIBase + APIVersion + "subscriptions"
case .subscriptions(let id): return APIBase + APIVersion + "subscriptions/\(id)"
case .subscriptionDiscount(let id): return APIBase + APIVersion + "subscriptions/\(id)/discount"
}
}
}
14 changes: 13 additions & 1 deletion Sources/API/Routes/CustomerRoutes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,23 @@ public final class CustomerRoutes {
}

/**
Delete a customer
Delete a customer discount
Permanently deletes a customer. It cannot be undone. Also immediately cancels any active subscriptions on the customer.

- parameter customerId: The Customer's ID

- returns: A StripeRequest<> item which you can then use to convert to the corresponding node
*/
public func deleteDiscount(onCustomer customerId: String) throws -> StripeRequest<DeletedObject> {
return try StripeRequest(client: self.client, method: .delete, route: .customerDiscount(customerId), query: [:], body: nil, headers: nil)
}

/**
Delete a customer discount
Removes the currently applied discount on a customer.

- parameter customerId: The Customer's ID

- returns: A StripeRequest<> item which you can then use to convert to the corresponding node
*/
public func delete(customer customerId: String) throws -> StripeRequest<DeletedObject> {
Expand Down
2 changes: 1 addition & 1 deletion Sources/API/Routes/PlanRoutes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public final class PlanRoutes {
- returns: A StripeRequest<> item which you can then use to convert to the corresponding node.
*/

public func create(id: String, amount: Int, currency: StripeCurrency, interval: StripeInterval, name: String, intervalCount: Int?, statementDescriptor: String?, trialPeriodDays: Int?, metadata: Node? = nil) throws -> StripeRequest<Coupon> {
public func create(id: String, amount: Int, currency: StripeCurrency, interval: StripeInterval, name: String, intervalCount: Int?, statementDescriptor: String?, trialPeriodDays: Int?, metadata: Node? = nil) throws -> StripeRequest<Plan> {
var body = Node([:])

body["id"] = Node(id)
Expand Down
150 changes: 150 additions & 0 deletions Sources/API/Routes/SubscriptionItemRoutes.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
//
// SubscriptionItemRoutes.swift
// Stripe
//
// Created by Andrew Edwards on 6/6/17.
//
//

import Foundation
import Node
import Models
import HTTP
import Helpers
import Errors

public final class SubscriptionItemRoutes {

let client: StripeClient

init(client: StripeClient) {
self.client = client
}

/**
Create SubscriptionItem

- parameter planId: The identifier of the plan to add to the subscription.

- parameter prorate: Flag indicating whether to prorate switching plans during a billing cycle.

- parameter prorationDate: If set, the proration will be calculated as though the subscription was updated at the given time.

- parameter quantity: The quantity you’d like to apply to the subscription item you’re creating.

- parameter subscriptionId: The identifier of the subscription to modify.

- returns: A StripeRequest<> item which you can then use to convert to the corresponding node.
*/

public func create(planId: String, prorate: Bool, prorationDate: Date, quantity: Int, subscriptionId: String) throws -> StripeRequest<SubscriptionItem> {

let body: [String: Any] = [
"plan": planId,
"subscription": subscriptionId,
"prorate": prorate,
"proration_date": Int(prorationDate.timeIntervalSince1970),
"quantity": quantity
]

let node = try Node(node: body)

return try StripeRequest(client: self.client, method: .post, route: .subscriptionItem, query: [:], body: Body.data(node.formURLEncoded()), headers: nil)
}

/**
Retrieve a subscriptionItem
Retrieves the invoice item with the given ID.

- parameter subscriptionId: The identifier of the subscription item to retrieve.

- returns: A StripeRequest<> item which you can then use to convert to the corresponding node
*/
public func retrieve(subscriptionItem subscriptionItemId: String) throws -> StripeRequest<SubscriptionItem> {
return try StripeRequest(client: self.client, method: .get, route: .subscriptionItems(subscriptionItemId), query: [:], body: nil, headers: nil)
}

/**
Update a subscription item
Updates the plan or quantity of an item on a current subscription.

- parameter plan: The identifier of the new plan for this subscription item.

- parameter prorate: Flag indicating whether to prorate switching plans during a billing cycle.

- parameter prorationDate: If set, the proration will be calculated as though the subscription was updated at the given time.

- parameter quantity: The quantity you’d like to apply to the subscription item you’re creating.

- parameter subscriptionItemId: The identifier of the subscription item to modify.
*/

public func update(plan: String?, prorate: Bool?, prorationDate: Date?, quantity: Int?, subscriptionItemId: String) throws -> StripeRequest<SubscriptionItem> {
var body = Node([:])

if let plan = plan {
body["plan"] = Node(plan)
}

if let prorate = prorate {
body["prorate"] = Node(prorate)
}
if let prorationdate = prorationDate {
body["proration_date"] = Node(Int(prorationdate.timeIntervalSince1970))
}
if let quantity = quantity {
body["quantity"] = Node(quantity)
}

return try StripeRequest(client: self.client, method: .post, route: .subscriptionItems(subscriptionItemId), query: [:], body: Body.data(body.formURLEncoded()), headers: nil)
}

/**
Delete a subscription item
Deletes an item from the subscription.

- parameter subscriptionItemId: The identifier of the coupon to be deleted.

- parameter prorate: Flag indicating whether to prorate switching plans during a billing cycle.

- parameter prorationDate: If set, the proration will be calculated as though the subscription was updated at the given time.

- returns: A StripeRequest<> item which you can then use to convert to the corresponding node
*/

public func delete(subscriptionItem subscriptionItemId: String, prorate: Bool?, proprationDate: Date?) throws -> StripeRequest<SubscriptionItem> {

var body = Node([:])

if let prorate = prorate {
body["prorate"] = Node(prorate)
}
if let prorationdate = proprationDate {
body["proration_date"] = Node(Int(prorationdate.timeIntervalSince1970))
}

return try StripeRequest(client: self.client, method: .delete, route: .subscriptionItems(subscriptionItemId), query: [:], body: Body.data(body.formURLEncoded()), headers: nil)
}

/**
List all subscription items
Returns a list of your subscription items for a given subscription.

- parameter filter: A Filter item to pass query parameters when fetching results.

- returns: A StripeRequest<> item which you can then use to convert to the corresponding node
*/

public func listAll(subscriptionId: String, filter: StripeFilter?) throws -> StripeRequest<SubscriptionItemList> {
var query = [String : NodeRepresentable]()

if let data = try filter?.createQuery()
{
query = data
}

query["subscription"] = Node(subscriptionId)

return try StripeRequest(client: self.client, method: .get, route: .subscriptionItem, query: query, body: nil, headers: nil)
}
}
Loading

0 comments on commit 74d0a21

Please sign in to comment.