Skip to content

Commit

Permalink
Merge ce63a72 into 03a456c
Browse files Browse the repository at this point in the history
  • Loading branch information
romero-ios committed Jun 24, 2019
2 parents 03a456c + ce63a72 commit 28253cd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 70 deletions.
37 changes: 11 additions & 26 deletions Source/Order.swift
Expand Up @@ -59,7 +59,7 @@ final public class Order: NSObject, Codable {
/**
The customer related to the order
*/
public var customer: Customer = Customer()
public var customer: Customer?

/**
The total order value in pennies (e.g. 3999 for $39.99)
Expand Down Expand Up @@ -120,38 +120,23 @@ final public class Order: NSObject, Codable {
/**
The id for the transacting customer in your system (required).
*/
public var id: String?

/**
If you prefer to provide your own hashed email, you can set it here following the below instructions,
otherwise, use **setEmail(email:)** and we will hash it for you.
let id: String

/**
The SHA-256 hash of the transacting customer’s lowercase email, as a 64-character hex string.
The value of the e-mail address must be converted to lowercase before computing the hash.
The hash itself may use uppercase or lowercase hex characters.
*/
public var emailSha256: String?
/**
The email of the transacting customer.
Providing a value for this field will generate a SHA-256 hash
and populate the **emailSha256** field for you.
**Note**: The value of the e-mail address must be converted to lowercase before
computing the hash. The hash itself may use uppercase or lowercase hex characters.
*/
var email: String?

- Parameter email: the plain-text email to be converted to SHA-256 hash.
*/
public func setEmail(_ email: String) {
emailSha256 = email.lowercased().sha256
@objc public init(id: String) {
self.id = id
}

/**
The customer’s IDFA.
*/
public var advertisingId: String?


enum CodingKeys: String, CodingKey {
case email
case id
case emailSha256 = "email_sha256"
case advertisingId = "advertising_id"
}
}

Expand Down
58 changes: 14 additions & 44 deletions Tests/UnitTests/OrderTests.swift
Expand Up @@ -27,7 +27,7 @@ import XCTest

class OrderTests: XCTestCase {

func testInitialization_requiredProperties() {
func testInitialization_requiredPropertiesOnly() {
// Arrange
let id = "order-abc"
let date: Date = Date.ISO8601Formatter.date(from: "2019-06-17T12:08:10-04:00")!
Expand All @@ -43,7 +43,7 @@ class OrderTests: XCTestCase {
XCTAssertEqual(order.currencyCode, "USD")
XCTAssertEqual(order.purchaseDate, date.ISO8601String)
XCTAssertEqual(order.lineItems, lineItems)
XCTAssertNotNil(order.customer)
XCTAssertNil(order.customer)
XCTAssertNil(order.customerOrderId)
}

Expand All @@ -54,7 +54,7 @@ class OrderTests: XCTestCase {
let currencyCode = "EUR"
let lineItems = [Order.LineItem(identifier: "unique-id-1234", total: 400)]
let customerOrderId = "123"
let customer = Order.Customer()
let customer = Order.Customer(id: "123")

let order = Order(id: id,
purchaseDate: date,
Expand Down Expand Up @@ -86,7 +86,7 @@ class OrderTests: XCTestCase {

let currencyCode = "EUR"
let customerOrderId = "123"
let customer = Order.Customer()
let customer = Order.Customer(id: "123")

// Act
order.currencyCode = currencyCode
Expand All @@ -105,15 +105,13 @@ class OrderTests: XCTestCase {
let id = "derp123"
let amount: Int64 = 499
let lineItems: [Order.LineItem] = []
let customerDictionary: [String: AnyHashable] = [:]
let order = Order(id: id, amount: amount)
let date = order.purchaseDate
let expectedOrderDictionary: [String: AnyHashable] = ["order_id": id,
"amount": amount,
"currency": "USD",
"purchase_date": date,
"line_items": lineItems,
"customer": customerDictionary]
"line_items": lineItems]

// Act
guard let actualOrderDictionary = order.dictionaryRepresentation as? [String: AnyHashable] else {
Expand All @@ -125,58 +123,30 @@ class OrderTests: XCTestCase {
XCTAssertEqual(expectedOrderDictionary, actualOrderDictionary)
}

func testCustomerInitialization_hashingEmail() {
func testCustomerInitialization_requiredPropertiesOnly() {
// Arrange
let id = "123"
let email = "betty@usebutton.com"
let emailSha256 = "c399e8d0e89e9f09aa14a36392e4cb0d058ab28b16247e80eab78ea5541a20d3"
let advertisingId = "123"

// Act
let customer = Order.Customer()
customer.id = id
customer.setEmail(email)
customer.advertisingId = advertisingId
let customer = Order.Customer(id: id)

// Assert
XCTAssertEqual(customer.id, id)
XCTAssertEqual(customer.emailSha256, emailSha256)
XCTAssertEqual(customer.advertisingId, advertisingId)
XCTAssertNil(customer.email)
}

func testCustomerInitialization_providingHashedEmail() {
// Arrange
let id = "123"
let emailSha256 = "1234567"
let advertisingId = "123"

// Act
let customer = Order.Customer()
customer.id = id
customer.emailSha256 = emailSha256
customer.advertisingId = advertisingId

// Assert
XCTAssertEqual(customer.id, id)
XCTAssertEqual(customer.emailSha256, emailSha256)
XCTAssertEqual(customer.advertisingId, advertisingId)
}

func testCustomerInitialization_hashingCapitalizedEmail() {
func testCustomerInitialization_allProperties() {
// Arrange
let id = "123"
let email = "BETTY@usebutton.com"
let emailSha256 = "c399e8d0e89e9f09aa14a36392e4cb0d058ab28b16247e80eab78ea5541a20d3"
let advertisingId = "123"
let email = "betty@usebutton.com"

// Act
let customer = Order.Customer()
customer.id = id
customer.setEmail(email)
customer.advertisingId = advertisingId
let customer = Order.Customer(id: id)
customer.email = email

// Assert
XCTAssertEqual(customer.emailSha256, emailSha256)
XCTAssertEqual(customer.id, id)
XCTAssertEqual(customer.email, email)
}

func testLineItemInitialization_requiredPropertiesOnly() {
Expand Down

0 comments on commit 28253cd

Please sign in to comment.