Skip to content

Commit

Permalink
Merge pull request #36 from vapor-community/2.0.4
Browse files Browse the repository at this point in the history
Add support for specifying a Test and Production API key
  • Loading branch information
anthonycastelli authored Apr 29, 2018
2 parents 784ec55 + 51edf58 commit 92481e4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
8 changes: 6 additions & 2 deletions Sources/Stripe/API/StripeRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ extension HTTPHeaderName {
return .init("Stripe-Version")
}
public static var stripeAccount: HTTPHeaderName {
return .init("Stripe-Version")
return .init("Stripe-Account")
}
}

Expand All @@ -55,10 +55,12 @@ extension HTTPHeaders {
public class StripeAPIRequest: StripeRequest {
private let httpClient: Client
private let apiKey: String
private let testApiKey: String?

init(httpClient: Client, apiKey: String) {
init(httpClient: Client, apiKey: String, testApiKey: String?) {
self.httpClient = httpClient
self.apiKey = apiKey
self.testApiKey = testApiKey
}

public func send<SM: StripeModel>(method: HTTPMethod, path: String, query: String, body: String, headers: HTTPHeaders) throws -> Future<SM> {
Expand All @@ -68,6 +70,8 @@ public class StripeAPIRequest: StripeRequest {

headers.forEach { finalHeaders.add(name: $0.name, value: $0.value) }

// Get the appropiate API key based on the environment and if the test key is present
let apiKey = self.httpClient.container.environment == .development ? (self.testApiKey ?? self.apiKey) : self.apiKey
finalHeaders.add(name: .authorization, value: "Bearer \(apiKey)")

let request = HTTPRequest(method: method, url: URL(string: "\(path)?\(query)") ?? .root, headers: finalHeaders, body: encodedHTTPBody)
Expand Down
19 changes: 14 additions & 5 deletions Sources/Stripe/Provider/Provider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,25 @@ import Vapor

public struct StripeConfig: Service {
public let apiKey: String
public let testApiKey: String?

public init(apiKey: String) {
self.apiKey = apiKey
self.testApiKey = nil
}

public init(productionKey: String, testKey: String) {
self.apiKey = productionKey
self.testApiKey = testKey
}
}

public final class StripeProvider: Provider {
public static let repositoryName = "stripe-provider"

public init(){}
public func boot(_ worker: Container) throws {}
public init() { }

public func boot(_ worker: Container) throws { }

public func didBoot(_ worker: Container) throws -> EventLoopFuture<Void> {
return .done(on: worker)
Expand All @@ -29,7 +38,7 @@ public final class StripeProvider: Provider {
services.register { (container) -> StripeClient in
let httpClient = try container.make(Client.self)
let config = try container.make(StripeConfig.self)
return StripeClient(apiKey: config.apiKey, client: httpClient)
return StripeClient(apiKey: config.apiKey, testKey: config.testApiKey, client: httpClient)
}
}
}
Expand Down Expand Up @@ -57,8 +66,8 @@ public struct StripeClient: Service {
public var transfer: StripeTransferRoutes
public var transferReversals: StripeTransferReversalRoutes

internal init(apiKey: String, client: Client) {
let apiRequest = StripeAPIRequest(httpClient: client, apiKey: apiKey)
internal init(apiKey: String, testKey: String?, client: Client) {
let apiRequest = StripeAPIRequest(httpClient: client, apiKey: apiKey, testApiKey: testKey)

balance = StripeBalanceRoutes(request: apiRequest)
charge = StripeChargeRoutes(request: apiRequest)
Expand Down

0 comments on commit 92481e4

Please sign in to comment.