From e71d88c97c28f9d10df20bac5c01597f22da7292 Mon Sep 17 00:00:00 2001 From: Vasily Ulianov Date: Tue, 5 Jun 2018 22:41:19 +0300 Subject: [PATCH] Finally working --- Sources/Stripe/API/Routes/EphemeralKeyRoutes.swift | 11 +++++++---- Sources/Stripe/API/StripeRequest.swift | 4 +++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Sources/Stripe/API/Routes/EphemeralKeyRoutes.swift b/Sources/Stripe/API/Routes/EphemeralKeyRoutes.swift index 1feffa0..93a4182 100644 --- a/Sources/Stripe/API/Routes/EphemeralKeyRoutes.swift +++ b/Sources/Stripe/API/Routes/EphemeralKeyRoutes.swift @@ -11,7 +11,7 @@ import Vapor public protocol EphemeralKeyRoutes { associatedtype EK: EphemeralKey - func create(customer: String, apiVersion: String) throws -> Future + func create(customer: String, apiVersion: String?) throws -> Future func delete(ephemeralKey: String) throws -> Future } @@ -22,9 +22,12 @@ public struct StripeEphemeralKeyRoutes: EphemeralKeyRoutes { self.request = request } - public func create(customer: String, apiVersion: String) throws -> Future { - var headers = HTTPHeaders() - headers.add(name: "stripe_version", value: apiVersion) + public func create(customer: String, apiVersion: String? = nil) throws -> Future { + var headers: HTTPHeaders = [:] + + if let otherApiVersion = apiVersion { + headers.replaceOrAdd(name: .stripeVersion, value: otherApiVersion) + } let body = ["customer": customer] return try request.send(method: .POST, path: StripeAPIEndpoint.ephemeralKeys.endpoint, body: body.queryParameters, headers: headers) diff --git a/Sources/Stripe/API/StripeRequest.swift b/Sources/Stripe/API/StripeRequest.swift index 4f8f9de..dc0b57e 100644 --- a/Sources/Stripe/API/StripeRequest.swift +++ b/Sources/Stripe/API/StripeRequest.swift @@ -66,7 +66,9 @@ public class StripeAPIRequest: StripeRequest { public func send(method: HTTPMethod, path: String, query: String, body: String, headers: HTTPHeaders) throws -> Future { var finalHeaders: HTTPHeaders = .stripeDefault - headers.forEach { finalHeaders.add(name: $0.name, value: $0.value) } + headers.forEach { + finalHeaders.replaceOrAdd(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