Skip to content

Commit

Permalink
Merge pull request #4 from vapor-community/ConnectAccounts
Browse files Browse the repository at this point in the history
Connect accounts
  • Loading branch information
anthonycastelli authored Jul 9, 2017
2 parents ffd73b4 + 8cd8e47 commit ff0c62c
Show file tree
Hide file tree
Showing 33 changed files with 3,555 additions and 648 deletions.
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ let package = Package(
],
dependencies: [
.Package(url: "https://github.com/vapor/vapor.git", majorVersion: 2),
.Package(url: "https://github.com/vapor/random.git", majorVersion: 1),
]
)
16 changes: 16 additions & 0 deletions Sources/API/Helpers/Endpoints.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ internal enum API {
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.
*/
case account
case accounts(String)
case accountsReject(String)
case accountsLoginLink(String)



var endpoint: String {
switch self {
case .balance: return APIBase + APIVersion + "balance"
Expand Down Expand Up @@ -150,6 +161,11 @@ internal enum API {
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"

case .account: return APIBase + APIVersion + "accounts"
case .accounts(let id): return APIBase + APIVersion + "accounts/\(id)"
case .accountsReject(let id): return APIBase + APIVersion + "accounts/\(id)/reject"
case .accountsLoginLink(let id): return APIBase + APIVersion + "accounts/\(id)/login_links"
}
}
}
280 changes: 280 additions & 0 deletions Sources/API/Routes/AccountRoutes.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
//
// AccountRoutes.swift
// Stripe
//
// Created by Andrew Edwards on 7/8/17.
//
//

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

public final class AccountRoutes {
let client: StripeClient

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

public func create(type: ConnectedAccountType, email: String?, country: String?, metadata: Node? = nil) throws -> StripeRequest<ConnectAccount> {
var body = Node([:])

body["type"] = Node(type.rawValue)

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

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

if let metadata = metadata?.object {
for (key, value) in metadata {
body["metadata[\(key)]"] = value
}
}

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

public func retrieve(account accountId: String) throws -> StripeRequest<ConnectAccount> {
return try StripeRequest(client: self.client, method: .get, route: .accounts(accountId), query: [:], body: nil, headers: nil)
}

public func update(account accountId: String, businessName: String?, businessPrimaryColor: String?, businessUrl: String?, debitNegativeBalances: Bool?, declineChargeOn: Node?, defaultCurrency: StripeCurrency?, email: String?, externalAccount: Node?, legalEntity: Node?, payoutSchedule: Node?, payoutStatementDescriptor: String?, productDescription: String?, statementDescriptor: String?, supportEmail: String?, supportPhone: String?, supportUrl: String?, tosAcceptance: Node?, metadata: Node? = nil) throws -> StripeRequest<ConnectAccount> {
var body = Node([:])

if let businessname = businessName {
body["business_name"] = Node(businessname)
}

if let businesscolor = businessPrimaryColor {
body["business_primary_color"] = Node(businesscolor)
}

if let businessurl = businessUrl {
body["business_url"] = Node(businessurl)
}

if let debNegBal = debitNegativeBalances {
body["debit_negative_balances"] = Node(debNegBal)
}

if let declinechargeon = declineChargeOn?.object {
for (key, value) in declinechargeon {
body["decline_charge_on[\(key)]"] = value
}
}

if let currency = defaultCurrency {
body["default_currency"] = Node(currency.rawValue)
}

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

if let externalaccount = externalAccount?.object {
for (key,value) in externalaccount {
body["external_account[\(key)]"] = value
}
}

if let legalentity = legalEntity {

if let directors = legalentity["additional_directors"]?.object {

for (key,value) in directors {
body["legal_entity[additional_directors][\(key)]"] = value
}
}

if let owners = legalentity["additional_owners"]?.array {

for index in 0..<owners.count {

if let address = owners[index]["address"]?.object {
for (key,value) in address {
body["legal_entity[additional_owners]"]?[index]?["address[\(key)]"] = value
}
}

if let dob = owners[index]["dob"]?.object {
for (key,value) in dob {
body["legal_entity[additional_owners]"]?[index]?["dob][\(key)]"] = value
}
}

if let firstname = owners[index]["first_name"]?.string {
body["legal_entity[additional_owners]"]?[index]?["first_name"] = Node(firstname)
}

if let lastname = owners[index]["last_name"]?.string {
body["legal_entity[additional_owners"]?[index]?["last_name"] = Node(lastname)
}

if let verification = owners[index]["verification"]?.object {
for(key, value) in verification {
body["legal_entity[additional_owners"]?[index]?["verification][\(key)]"] = value
}
}
}
}

if let address = legalentity["address"]?.object {
for (key, value) in address {
body["legal_entity[address][\(key)]"] = value
}
}

if let businessname = legalentity["business_name"]?.string {
body["legal_entity[business_name]"] = Node(businessname)
}

if let businesstax = legalentity["business_tax_id"]?.string {
body["legal_entity[business_tax_id]"] = Node(businesstax)
}

if let businessvat = legalentity["business_vat_id"]?.string {
body["legal_entity[business_vat_id]"] = Node(businessvat)
}

if let director = legalentity["director"]?.string {
body["legal_entity[director]"] = Node(director)
}

if let dob = legalentity["dob"]?.object {
for (key,value) in dob {
body["legal_entity[dob][\(key)]"] = value
}
}

if let firstname = legalentity["first_name"]?.string {
body["legal_entity[first_name]"] = Node(firstname)
}

if let lastname = legalentity["last_name"]?.string {
body["legal_entity[last_name]"] = Node(lastname)
}

if let gender = legalentity["gender"]?.string {
body["legal_entity[gender]"] = Node(gender)
}

if let maiden = legalentity["maiden_name"]?.string {
body["legal_entity[maiden_name]"] = Node(maiden)
}

if let personaladdress = legalentity["personal_address"]?.object {
for (key, value) in personaladdress {
body["legal_entity[personal_address][\(key)]"] = value
}
}

if let pid = legalentity["personal_id_number"]?.string {
body["legal_entity[personal_id_number]"] = Node(pid)
}

if let phone = legalentity["phone_number"]?.string {
body["legal_entity[phone_number]"] = Node(phone)
}

if let ssnLast4 = legalentity["ssn_last_4"]?.string {
body["legal_entity[ssn_last_4]"] = Node(ssnLast4)
}

if let taxIdReg = legalentity["tax_id_registrar"]?.string {
body["legal_entity[tax_id_registrar]"] = Node(taxIdReg)
}

if let type = legalentity["type"]?.string {
body["legal_entity[type]"] = Node(type)
}

if let verification = legalentity["verification"]?.object {
for (key, value) in verification {
body["legal_entity[verification][\(key)]"] = value
}
}

if let metadata = legalentity["metadata"]?.object {
for (key, value) in metadata {
body["legal_entity[metadata][\(key)]"] = value
}
}
}

if let payoutSchedule = payoutSchedule?.object {
for (key, value) in payoutSchedule {
body["payout_schedule[\(key)]"] = value
}
}

if let payoutstatement = payoutStatementDescriptor?.string {
body["payout_statement_descriptor"] = Node(payoutstatement)
}

if let productDescription = productDescription?.string {
body["product_description"] = Node(productDescription)
}

if let statementdescriptor = statementDescriptor?.string {
body["statement_descriptor"] = Node(statementdescriptor)
}

if let supportEmail = supportEmail?.string {
body["support_email"] = Node(supportEmail)
}

if let supportPhone = supportPhone?.string {
body["support_phone"] = Node(supportPhone)
}

if let supportUrl = supportUrl?.string {
body["support_url"] = Node(supportUrl)
}

if let tosAcceptance = tosAcceptance?.object {
for (key, value) in tosAcceptance {
body["tos_acceptance[\(key)]"] = value
}
}

if let metadata = metadata?.object {
for (key, value) in metadata {
body["metadata[\(key)]"] = value
}
}

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

public func delete(account accountId: String) throws -> StripeRequest<DeletedObject> {
return try StripeRequest(client: self.client, method: .delete, route: .accounts(accountId), query: [:], body: nil, headers: nil)
}

public func reject(account accountId: String, for reason: AccountRejectReason) throws -> StripeRequest<ConnectAccount> {
var body = Node([:])

body["reason"] = Node(reason.rawValue)

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

public func listAll(filter: StripeFilter? = nil) throws -> StripeRequest<AccountList> {
var query = [String : NodeRepresentable]()
if let data = try filter?.createQuery() {
query = data
}
return try StripeRequest(client: self.client, method: .get, route: .account, query: query, body: nil, headers: nil)
}

public func createLoginLink(forAccount accountId: String) throws -> StripeRequest<ConnectLoginLink> {
return try StripeRequest(client: self.client, method: .post, route: .accountsLoginLink(accountId), query: [:], body: nil, headers: nil)
}
}
2 changes: 2 additions & 0 deletions Sources/API/StripeClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class StripeClient {
public private(set) var sources: SourceRoutes!
public private(set) var subscriptionItems: SubscriptionItemRoutes!
public private(set) var subscriptions: SubscriptionRoutes!
public private(set) var account: AccountRoutes!

public init(apiKey: String) throws {
self.apiKey = apiKey
Expand All @@ -38,5 +39,6 @@ public class StripeClient {
self.sources = SourceRoutes(client: self)
self.subscriptionItems = SubscriptionItemRoutes(client: self)
self.subscriptions = SubscriptionRoutes(client: self)
self.account = AccountRoutes(client: self)
}
}
28 changes: 14 additions & 14 deletions Sources/API/StripeRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ public class StripeRequest<T : StripeModelProtocol> {
guard let error = self.response.json?["error"]?.object else { throw self.response.status }
guard let type = error["type"]?.string else { throw self.response.status }
switch type {
case "api_connection_error": throw StripeError.apiConnectionError
case "api_error": throw StripeError.apiError
case "authentication_error": throw StripeError.authenticationError
case "card_error": throw StripeError.cardError
case "invalid_request_error": throw StripeError.invalidRequestError(try self.response.json?.get("error"))
case "rate_limit_error": throw StripeError.rateLimitError
case "validation_error": throw StripeError.validationError
case "api_connection_error": throw StripeError.apiConnectionError(error["message"]?.string ?? "unknown error")
case "api_error": throw StripeError.apiError(error["message"]?.string ?? "unknown error")
case "authentication_error": throw StripeError.authenticationError(error["message"]?.string ?? "unknown error")
case "card_error": throw StripeError.cardError(error["message"]?.string ?? "unknown error")
case "invalid_request_error": throw StripeError.invalidRequestError(error["message"]?.string ?? "unknown error")
case "rate_limit_error": throw StripeError.rateLimitError(error["message"]?.string ?? "unknown error")
case "validation_error": throw StripeError.validationError(error["message"]?.string ?? "unknown error")
default: throw self.response.status
}
}
Expand All @@ -72,13 +72,13 @@ public class StripeRequest<T : StripeModelProtocol> {
guard let error = self.response.json?["error"]?.object else { throw self.response.status }
guard let type = error["type"]?.string else { throw self.response.status }
switch type {
case "api_connection_error": throw StripeError.apiConnectionError
case "api_error": throw StripeError.apiError
case "authentication_error": throw StripeError.authenticationError
case "card_error": throw StripeError.cardError
case "invalid_request_error": throw StripeError.invalidRequestError(try self.response.json?.get("error"))
case "rate_limit_error": throw StripeError.rateLimitError
case "validation_error": throw StripeError.validationError
case "api_connection_error": throw StripeError.apiConnectionError(error["message"]?.string ?? "unknown error")
case "api_error": throw StripeError.apiError(error["message"]?.string ?? "unknown error")
case "authentication_error": throw StripeError.authenticationError(error["message"]?.string ?? "unknown error")
case "card_error": throw StripeError.cardError(error["message"]?.string ?? "unknown error")
case "invalid_request_error": throw StripeError.invalidRequestError(error["message"]?.string ?? "unknown error")
case "rate_limit_error": throw StripeError.rateLimitError(error["message"]?.string ?? "unknown error")
case "validation_error": throw StripeError.validationError(error["message"]?.string ?? "unknown error")
default: throw self.response.status
}
}
Expand Down
Loading

0 comments on commit ff0c62c

Please sign in to comment.