From 5215f2f0b490789868c78329c447e98855904d3e Mon Sep 17 00:00:00 2001 From: Arturo Murillo Date: Mon, 29 Jan 2018 18:16:51 -0500 Subject: [PATCH] add email and fullName parameters to request --- Spreedly/CreditCard.swift | 2 +- Spreedly/RequestSerializer.swift | 34 +++++++++++++++++++++++--------- Spreedly/SpreedlyAPIClient.swift | 10 ++++++++++ 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/Spreedly/CreditCard.swift b/Spreedly/CreditCard.swift index 946127f..17e9784 100644 --- a/Spreedly/CreditCard.swift +++ b/Spreedly/CreditCard.swift @@ -9,7 +9,7 @@ import Foundation open class CreditCard: NSObject { - open var firstName, lastName, number, verificationValue, month, year: String? + open var firstName, lastName, fullName, number, verificationValue, month, year: String? open var address1, address2, city, state, zip, country, phoneNumber: String? public override init() {} diff --git a/Spreedly/RequestSerializer.swift b/Spreedly/RequestSerializer.swift index 2e06846..4736860 100644 --- a/Spreedly/RequestSerializer.swift +++ b/Spreedly/RequestSerializer.swift @@ -16,8 +16,27 @@ open class RequestSerializer { } open static func serialize(_ creditCard: CreditCard) -> (data: Data?, error: NSError?) { - var dict = [String: String]() + return serialize(creditCard) + } + + open static func serialize(_ creditCard: CreditCard, usingEmail email: String? = nil) -> (data: Data?, error: NSError?) { + let creditCardDict = creditCardDictionary(with: creditCard) + var dict: [String: Any] = ["credit_card": creditCardDict] + if let email = email { + dict["email"] = email + } + let body = [ "payment_method": dict] + do { + let data = try JSONSerialization.data(withJSONObject: body, options: []) + return (data, nil) + } catch let serializeError as NSError { + return (nil, serializeError) + } + } + + private static func creditCardDictionary(with creditCard: CreditCard) -> [String: Any] { + var dict: [String: Any] = [:] if let creditCardFirstName = creditCard.firstName { dict["first_name"] = creditCardFirstName } @@ -26,6 +45,10 @@ open class RequestSerializer { dict["last_name"] = creditCardLastName } + if let creditCardFullName = creditCard.fullName { + dict["full_name"] = creditCardFullName + } + if let creditCardNumber = creditCard.number { dict["number"] = creditCardNumber } @@ -70,13 +93,6 @@ open class RequestSerializer { dict["phone_number"] = creditCardPhoneNumber } - let body = [ "payment_method": [ "credit_card": dict ]] - - do { - let data = try JSONSerialization.data(withJSONObject: body, options: []) - return (data, nil) - } catch let serializeError as NSError { - return (nil, serializeError) - } + return dict } } diff --git a/Spreedly/SpreedlyAPIClient.swift b/Spreedly/SpreedlyAPIClient.swift index 4a07e12..f43da1c 100644 --- a/Spreedly/SpreedlyAPIClient.swift +++ b/Spreedly/SpreedlyAPIClient.swift @@ -35,6 +35,16 @@ open class SpreedlyAPIClient: NSObject { } } + open func createPaymentMethodToken(with creditCard: CreditCard, usingEmail email: String, completion: @escaping SpreedlyAPICompletionBlock) { + let serializedRequest = RequestSerializer.serialize(creditCard, usingEmail: email) + + if serializedRequest.error == nil { + if let data = serializedRequest.data { + self.createPaymentMethodTokenWithData(data, completion: completion) + } + } + } + open func createPaymentMethodTokenWithApplePay(_ payment: PKPayment, completion: @escaping SpreedlyAPICompletionBlock) { self.createPaymentMethodTokenWithData(RequestSerializer.serialize(payment.token.paymentData), completion: completion) }