Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Add Email and FullName parameters #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Spreedly/CreditCard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
Expand Down
34 changes: 25 additions & 9 deletions Spreedly/RequestSerializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
}
10 changes: 10 additions & 0 deletions Spreedly/SpreedlyAPIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down