Skip to content

Commit

Permalink
Merge pull request #67 from vapor-community/files
Browse files Browse the repository at this point in the history
Files implementation
  • Loading branch information
anthonycastelli authored Sep 15, 2018
2 parents 8a1390c + 03b0bd2 commit fb2bf21
Show file tree
Hide file tree
Showing 12 changed files with 551 additions and 114 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
In your `Package.swift` file, add the following

~~~~swift
.package(url: "https://github.com/vapor-community/stripe-provider.git", from: "2.1.2")
.package(url: "https://github.com/vapor-community/stripe-provider.git", from: "2.2.0")
~~~~

Register the config and the provider to your Application
Expand Down Expand Up @@ -54,7 +54,8 @@ And you can always check the documentation to see the required paramaters for sp
* [x] Customers
* [x] Disputes
* [ ] Events
* [ ] File Uploads
* [x] File Links
* [x] File Uploads
* [x] Payouts
* [x] Refunds
* [x] Tokens
Expand Down
142 changes: 36 additions & 106 deletions Sources/Stripe/API/Helpers/Endpoints.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,196 +9,120 @@
import Foundation

internal let APIBase = "https://api.stripe.com/"
internal let FilesAPIBase = "https://files.stripe.com/"
internal let APIVersion = "v1/"

internal enum StripeAPIEndpoint {

/**
BALANCE
This is an object representing your Stripe balance. You can retrieve it to see the balance
currently on your Stripe account. You can also retrieve a list of the balance history, which
contains a list of transactions that contributed to the balance
(e.g., charges, transfers, and so forth). The available and pending amounts for each currency
are broken down further by payment source types.
*/
// MARK: - BALANCE
case balance
case balanceHistory
case balanceHistoryTransaction(String)

/**
CHARGES
To charge a credit or a debit card, you create a charge object. You can retrieve and refund
individual charges as well as list all charges. Charges are identified by a unique random ID.
*/
// MARK: - CHARGES
case charges
case charge(String)
case captureCharge(String)

/**
CUSTOMERS
Customers allow you to perform recurring charges and track multiple charges that are
associated with the same customer. The API allows you to create, delete, and update your customers.
You can retrieve individual customers as well as a list of all your customers.
*/
// MARK: - CUSTOMERS
case customers
case customer(String)
case customerSources(String)
case customerDetachSources(String,String)
case customerDiscount(String)

/**
TOKENS
Tokenization is the process Stripe uses to collect sensitive card or bank account details,
or personally identifiable information (PII), directly from your customers in a secure manner.
A Token representing this information is returned to your server to use. You should use Checkout,
Elements, or Stripe mobile libraries to perform this process, client-side. This ensures that no
sensitive card data touches your server and allows your integration to operate in a PCI compliant way.
*/
// MARK: - TOKENS
case tokens
case token(String)

/**
REFUNDS
Refund objects allow you to refund a charge that has previously been created but not yet refunded.
Funds will be refunded to the credit or debit card that was originally charged. The fees you were
originally charged are also refunded.
*/
// MARK: - REFUNDS
case refunds
case refund(String)

/**
COUPONS
A coupon contains information about a percent-off or amount-off discount you might want to
apply to a customer. Coupons may be applied to invoices or orders.
*/
// MARK: - COUPONS
case coupons
case coupon(String)

/**
PLANS
A subscription plan contains the pricing information for different products and feature levels on your site.
*/
// MARK: - PLANS
case plans
case plan(String)

/**
SOURCES
Source objects allow you to accept a variety of payment methods. They represent a customer's payment instrument
and can be used with the Stripe API just like a card object: once chargeable, they can be charged, or attached
to customers.
*/
// MARK: - SOURCES
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.
*/
// MARK: - SUBSCRIPTION ITEMS
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.
*/
// MARK: - SUBSCRIPTIONS
case subscription
case subscriptions(String)
case subscriptionDiscount(String)

/**
ACCOUNTS
This is an object representing your Stripe account. You can retrieve it to see properties on the account like its
current e-mail address or if the account is enabled yet to make live charges.
*/
// MARK: - ACCOUNTS
case account
case accounts(String)
case accountsReject(String)
case accountsLoginLink(String)

/**
DISPUTES
A dispute occurs when a customer questions your charge with their bank or credit card company.
*/
// MARK: - DISPUTES
case dispute
case disputes(String)
case closeDispute(String)

/**
SKUS
Stores representations of stock keeping units. SKUs describe specific product variations.
*/
// MARK: - SKUS
case sku
case skus(String)

/**
PRODUCTS
Store representations of products you sell in product objects, used in conjunction with SKUs.
*/
// MARK: - PRODUCTS
case product
case products(String)

/**
ORDERS
The purchase of previously defined products
*/
// MARK: - ORDERS
case order
case orders(String)
case ordersPay(String)
case ordersReturn(String)

/**
RETURNS
A return represents the full or partial return of a number of order items.
*/
// MARK: - RETURNS
case orderReturn
case orderReturns(String)

/**
INVOICES
Invoices are statements of what a customer owes for a particular billing period, including subscriptions,
invoice items, and any automatic proration adjustments if necessary.
*/
// MARK: - INVOICES
case invoices
case invoice(String)
case payInvoice(String)
case invoiceLines(String)
case upcomingInvoices

/**
INVOICE ITEMS
Sometimes you want to add a charge or credit to a customer but only actually charge the customer's card at
the end of a regular billing cycle. This is useful for combining several charges to minimize per-transaction
fees or having Stripe tabulate your usage-based billing totals.
*/
// MARK: - INVOICE ITEMS
case invoiceItems
case invoiceItem(String)


/**
EPHEMERAL KEYS
*/
// MARK: - EPHEMERAL KEYS
case ephemeralKeys
case ephemeralKey(String)

/**
TRANSFERS
A Transfer object is created when you move funds between Stripe accounts as part of Connect.
*/
// MARK: - TRANSFERS
case transfer
case transfers(String)
case transferReversal(String)
case transfersReversal(String,String)

/**
PAYOUTS
A Payout object is created when you receive funds from Stripe, or when you initiate a payout to either a bank account or debit card of a connected Stripe account.
*/
// MARK: - PAYOUTS
case payout
case payouts(String)
case payoutsCancel(String)

// MARK: - FILE LINKS
case fileLink
case fileLinks(String)

// MARK: - FILE UPLOAD
case file
case files(String)

var endpoint: String {
switch self {
case .balance: return APIBase + APIVersion + "balance"
Expand Down Expand Up @@ -280,6 +204,12 @@ internal enum StripeAPIEndpoint {
case .payout: return APIBase + APIVersion + "payouts"
case .payouts(let id): return APIBase + APIVersion + "payouts/\(id)"
case .payoutsCancel(let id): return APIBase + APIVersion + "payouts/\(id)/cancel"

case .fileLink: return APIBase + APIVersion + "file_links"
case .fileLinks(let id): return APIBase + APIVersion + "file_links/\(id)"

case .file: return FilesAPIBase + APIVersion + "files"
case .files(let id): return FilesAPIBase + APIVersion + "files/\(id)"
}
}
}
109 changes: 109 additions & 0 deletions Sources/Stripe/API/Routes/FileLinkRoutes.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//
// FileLinkRoutes.swift
// Stripe
//
// Created by Andrew Edwards on 9/14/18.
//

import Vapor

public protocol FileLinkRoutes {
/// Creates a new file link object.
///
/// - Parameters:
/// - file: The ID of the file.
/// - expires: A future timestamp after which the link will no longer be usable.
/// - metadata: Set of key-value pairs that you can attach to an object.
/// - Returns: Returns the file link object if successful, and returns an error otherwise.
func create(file: String, expires: Date?, metadata: [String: String]?) throws -> Future<StripeFileLink>

/// Retrieves the file link with the given ID.
///
/// - Parameter link: The identifier of the file link to be retrieved.
/// - Returns: Returns a file link object if a valid identifier was provided, and returns an error otherwise.
func retrieve(link: String) throws -> Future<StripeFileLink>

/// Updates an existing file link object. Expired links can no longer be updated
///
/// - Parameters:
/// - link: The ID of the file link.
/// - expires: A future timestamp after which the link will no longer be usable, or `now` to expire the link immediately.
/// - metadata: Set of key-value pairs that you can attach to an object.
/// - Returns: Returns the file link object if successful, and returns an error otherwise.
func update(link: String, expires: Any?, metadata: [String: String]?) throws -> Future<StripeFileLink>


/// Returns a list of file links.
///
/// - Parameter filter: A dictionary that contains the filters. More info [here](https://stripe.com/docs/api/curl#list_file_links).
/// - Returns: A `FileLinkList`.
func listAll(filter: [String: Any]?) throws -> Future<FileLinkList>
}

extension FileLinkRoutes {
public func create(file: String, expires: Date? = nil, metadata: [String: String]? = nil) throws -> Future<StripeFileLink> {
return try create(file: file, expires: expires, metadata: metadata)
}

public func retrieve(link: String) throws -> Future<StripeFileLink> {
return try retrieve(link: link)
}

public func update(link: String, expires: Any? = nil, metadata: [String: String]? = nil) throws -> Future<StripeFileLink> {
return try update(link: link, expires: expires, metadata: metadata)
}

public func listAll(filter: [String: Any]? = nil) throws -> Future<FileLinkList> {
return try listAll(filter: filter)
}
}

public struct StripeFileLinkRoutes: FileLinkRoutes {
private let request: StripeRequest

init(request: StripeRequest) {
self.request = request
}

public func create(file: String, expires: Date?, metadata: [String: String]?) throws -> Future<StripeFileLink> {
var body: [String: Any] = [:]
if let expires = expires {
body["expires_at"] = Int(expires.timeIntervalSince1970)
}

if let metadata = metadata {
metadata.forEach { body["metadata[\($0)]"] = $1}
}
return try request.send(method: .POST, path: StripeAPIEndpoint.fileLink.endpoint, body: body.queryParameters)
}

public func retrieve(link: String) throws -> Future<StripeFileLink> {
return try request.send(method: .GET, path: StripeAPIEndpoint.fileLinks(link).endpoint)
}

public func update(link: String, expires: Any?, metadata: [String: String]?) throws -> Future<StripeFileLink> {
var body: [String: Any] = [:]

if let expires = expires as? Date {
body["expires_at"] = Int(expires.timeIntervalSince1970)
}

if let expires = expires as? String {
body["expires_at"] = expires
}

if let metadata = metadata {
metadata.forEach { body["metadata[\($0)]"] = $1}
}
return try request.send(method: .POST, path: StripeAPIEndpoint.fileLinks(link).endpoint, body: body.queryParameters)
}

public func listAll(filter: [String: Any]?) throws -> Future<FileLinkList> {
var queryParams = ""
if let filter = filter {
queryParams = filter.queryParameters
}

return try request.send(method: .GET, path: StripeAPIEndpoint.fileLink.endpoint, query: queryParams)
}
}
Loading

0 comments on commit fb2bf21

Please sign in to comment.