From 2850495c45a7408303270d9c153e618c3ca6420d Mon Sep 17 00:00:00 2001 From: Sandro Machado Date: Thu, 16 Nov 2017 15:03:30 +0000 Subject: [PATCH] Add support to `message` and `otp` on create transaction --- Source/Model/Card.swift | 15 ++++++++++++++- .../TransactionCardDepositRequest.swift | 5 +++-- .../TransactionDepositRequest.swift | 5 +++-- .../Transaction/TransactionRequest.swift | 8 +++++++- .../TransactionTransferRequest.swift | 5 +++-- Source/Service/UserCardService.swift | 10 ++++++++-- .../ModelTests/CardTest.swift | 19 ++++++++++--------- .../ServiceTests/UserCardServiceTest.swift | 4 ++-- 8 files changed, 50 insertions(+), 21 deletions(-) diff --git a/Source/Model/Card.swift b/Source/Model/Card.swift index f96c0f0..8823f5b 100644 --- a/Source/Model/Card.swift +++ b/Source/Model/Card.swift @@ -122,11 +122,24 @@ open class Card: BaseModel, Mappable { - returns: A promise with the transaction. */ open func createTransaction(commit: Bool, transactionRequest: TransactionRequest) -> Promise { + return createTransaction(commit: commit, otp: nil, transactionRequest: transactionRequest) + } + + /** + Creates a transaction. + + - parameter commit: A boolean to indicate if it is to commit the transaction on the creation process. + - parameter otp: The otp code to confirm the transaction. + - parameter transactionRequest: The transaction request information. + + - returns: A promise with the transaction. + */ + open func createTransaction(commit: Bool, otp: String?, transactionRequest: TransactionRequest) -> Promise { guard let id = self.id else { return Promise(error: UnexpectedResponseError(message: "Card id should not be nil.")) } - let request = self.adapter.buildRequest(request: UserCardService.createTransaction(cardId: id, commit: commit, transactionRequest: Mapper().toJSON(transactionRequest))) + let request = self.adapter.buildRequest(request: UserCardService.createTransaction(cardId: id, commit: commit, otp: otp, transactionRequest: Mapper().toJSON(transactionRequest))) return self.adapter.buildResponse(request: request) } diff --git a/Source/Model/Transaction/TransactionCardDepositRequest.swift b/Source/Model/Transaction/TransactionCardDepositRequest.swift index 015dc9b..29a9212 100644 --- a/Source/Model/Transaction/TransactionCardDepositRequest.swift +++ b/Source/Model/Transaction/TransactionCardDepositRequest.swift @@ -11,11 +11,12 @@ open class TransactionCardDepositRequest: TransactionDepositRequest { Constructor. - parameter denomination: The denomination of the transaction request. + - parameter message: The message of the transaction request in case of a deposit. - parameter origin: The origin of the transaction request in case of a deposit. - parameter securityCode: The card's security code. */ - public init(denomination: TransactionDenominationRequest, origin: String, securityCode: String) { - super.init(denomination: denomination, origin: origin) + public init(denomination: TransactionDenominationRequest, message: String?, origin: String, securityCode: String) { + super.init(denomination: denomination, message: message, origin: origin) self.securityCode = securityCode } diff --git a/Source/Model/Transaction/TransactionDepositRequest.swift b/Source/Model/Transaction/TransactionDepositRequest.swift index d31e2c1..6a9fe06 100644 --- a/Source/Model/Transaction/TransactionDepositRequest.swift +++ b/Source/Model/Transaction/TransactionDepositRequest.swift @@ -11,10 +11,11 @@ open class TransactionDepositRequest: TransactionRequest { Constructor. - parameter denomination: The denomination of the transaction request. + - parameter message: The message of the transaction request in case of a deposit. - parameter origin: The origin of the transaction request in case of a deposit. */ - public init(denomination: TransactionDenominationRequest, origin: String) { - super.init(denomination: denomination) + public init(denomination: TransactionDenominationRequest, message: String?, origin: String) { + super.init(denomination: denomination, message: message) self.origin = origin } diff --git a/Source/Model/Transaction/TransactionRequest.swift b/Source/Model/Transaction/TransactionRequest.swift index b60a27c..265f364 100644 --- a/Source/Model/Transaction/TransactionRequest.swift +++ b/Source/Model/Transaction/TransactionRequest.swift @@ -7,13 +7,18 @@ open class TransactionRequest: Mappable { /// The denomination of the transaction request. public private(set) final var denomination: TransactionDenominationRequest? + /// The transaction message. + public private(set) final var message: String? + /** Constructor. - parameter denomination: The denomination of the transaction request. + - parameter message: The transaction message. */ - public init(denomination: TransactionDenominationRequest) { + public init(denomination: TransactionDenominationRequest, message: String?) { self.denomination = denomination + self.message = message } // MARK: Required by the ObjectMapper. @@ -33,6 +38,7 @@ open class TransactionRequest: Mappable { */ open func mapping(map: Map) { self.denomination <- map["denomination"] + self.message <- map["message"] } } diff --git a/Source/Model/Transaction/TransactionTransferRequest.swift b/Source/Model/Transaction/TransactionTransferRequest.swift index 01fedbf..657e36d 100644 --- a/Source/Model/Transaction/TransactionTransferRequest.swift +++ b/Source/Model/Transaction/TransactionTransferRequest.swift @@ -11,10 +11,11 @@ open class TransactionTransferRequest: TransactionRequest { Constructor. - parameter denomination: The denomination of the transaction request. + - parameter message: The message of the transaction request. - parameter destination: The destination of the transaction request. */ - public init(denomination: TransactionDenominationRequest, destination: String) { - super.init(denomination: denomination) + public init(denomination: TransactionDenominationRequest, message: String?, destination: String) { + super.init(denomination: denomination, message: message) self.destination = destination } diff --git a/Source/Service/UserCardService.swift b/Source/Service/UserCardService.swift index 758b361..85683c3 100644 --- a/Source/Service/UserCardService.swift +++ b/Source/Service/UserCardService.swift @@ -63,8 +63,14 @@ open class UserCardService { - returns: A request to create a transaction. */ - static func createTransaction(cardId: String, commit: Bool, transactionRequest: Any) -> Request { - return UpholdClient().post(url: String(format: "/v0/me/cards/%@/transactions", cardId)).query(query: ["commit": commit ? "true" : "false"]).send(data: transactionRequest) + static func createTransaction(cardId: String, commit: Bool, otp: String?, transactionRequest: Any) -> Request { + var request = UpholdClient().post(url: String(format: "/v0/me/cards/%@/transactions", cardId)).query(query: ["commit": commit ? "true" : "false"]).send(data: transactionRequest) + + if let otp = otp { + request = request.set(key: "OTP-Token", value: otp) + } + + return request } /** diff --git a/Tests/IntegrationTests/ModelTests/CardTest.swift b/Tests/IntegrationTests/ModelTests/CardTest.swift index c503f3b..b382de9 100644 --- a/Tests/IntegrationTests/ModelTests/CardTest.swift +++ b/Tests/IntegrationTests/ModelTests/CardTest.swift @@ -163,7 +163,7 @@ class CardTest: UpholdTestCase { card.adapter = MockRestAdapter(body: json) let transactionDenominationRequest = TransactionDenominationRequest(amount: "foo", currency: "bar") - let transactionCardDepositRequest = TransactionCardDepositRequest(denomination: transactionDenominationRequest, origin: "foobiz", securityCode: "1234") + let transactionCardDepositRequest = TransactionCardDepositRequest(denomination: transactionDenominationRequest, message: "foobar", origin: "foobiz", securityCode: "1234") card.createTransaction(transactionRequest: transactionCardDepositRequest).then { (transaction: Transaction) -> Void in XCTAssertEqual(transaction.createdAt, "2014-08-27T00:01:11.616Z", "Failed: Wrong transaction createdAt.") @@ -234,7 +234,7 @@ class CardTest: UpholdTestCase { let card: Card = Mapper().map(JSONString: "{}")! card.adapter = MockRestAdapter() let transactionDenominationRequest = TransactionDenominationRequest(amount: "foo", currency: "bar") - let transactionCardDepositRequest = TransactionCardDepositRequest(denomination: transactionDenominationRequest, origin: "foobar", securityCode: "1234") + let transactionCardDepositRequest = TransactionCardDepositRequest(denomination: transactionDenominationRequest, message: nil, origin: "foobar", securityCode: "1234") card.createTransaction(transactionRequest: transactionCardDepositRequest).catch(execute: { (error: Error) in guard let error = error as? UnexpectedResponseError else { @@ -258,7 +258,7 @@ class CardTest: UpholdTestCase { let card: Card = Fixtures.loadCard() card.adapter = MockRestAdapter(body: Mapper().toJSONString(Fixtures.loadTransaction(fields: ["transactionId": "foobar"]))!) let transactionDenominationRequest = TransactionDenominationRequest(amount: "foo", currency: "bar") - let transactionCardDepositRequest = TransactionCardDepositRequest(denomination: transactionDenominationRequest, origin: "foobar", securityCode: "1234") + let transactionCardDepositRequest = TransactionCardDepositRequest(denomination: transactionDenominationRequest, message: "fuz", origin: "foobar", securityCode: "1234") card.createTransaction(commit: true, transactionRequest: transactionCardDepositRequest).then { (transaction: Transaction) -> Void in XCTAssertEqual(transaction.id, "foobar", "Failed: Wrong transaction id.") @@ -281,7 +281,7 @@ class CardTest: UpholdTestCase { card.adapter = MockRestAdapter(body: json) let transactionDenominationRequest = TransactionDenominationRequest(amount: "foo", currency: "bar") - let transactionDepositRequest = TransactionDepositRequest(denomination: transactionDenominationRequest, origin: "foobiz") + let transactionDepositRequest = TransactionDepositRequest(denomination: transactionDenominationRequest, message: nil, origin: "foobiz") card.createTransaction(transactionRequest: transactionDepositRequest).then { (transaction: Transaction) -> Void in XCTAssertEqual(transaction.id, "foobar", "Failed: Wrong transaction id.") @@ -301,7 +301,7 @@ class CardTest: UpholdTestCase { let card: Card = Mapper().map(JSONString: "{}")! card.adapter = MockRestAdapter() let transactionDenominationRequest = TransactionDenominationRequest(amount: "foo", currency: "bar") - let transactionDepositRequest = TransactionDepositRequest(denomination: transactionDenominationRequest, origin: "foobar") + let transactionDepositRequest = TransactionDepositRequest(denomination: transactionDenominationRequest, message: nil, origin: "foobar") card.createTransaction(transactionRequest: transactionDepositRequest).catch(execute: { (error: Error) in guard let error = error as? UnexpectedResponseError else { @@ -325,7 +325,7 @@ class CardTest: UpholdTestCase { let card: Card = Fixtures.loadCard() card.adapter = MockRestAdapter(body: Mapper().toJSONString(Fixtures.loadTransaction(fields: ["transactionId": "foobar"]))!) let transactionDenominationRequest = TransactionDenominationRequest(amount: "foo", currency: "bar") - let transactionDepositRequest = TransactionDepositRequest(denomination: transactionDenominationRequest, origin: "foobar") + let transactionDepositRequest = TransactionDepositRequest(denomination: transactionDenominationRequest, message: nil, origin: "foobar") card.createTransaction(commit: true, transactionRequest: transactionDepositRequest).then { (transaction: Transaction) -> Void in XCTAssertEqual(transaction.id, "foobar", "Failed: Wrong transaction id.") @@ -347,11 +347,12 @@ class CardTest: UpholdTestCase { "}" card.adapter = MockRestAdapter(body: json) let transactionDenominationRequest = TransactionDenominationRequest(amount: "foo", currency: "bar") - let transactionRequest = TransactionTransferRequest(denomination: transactionDenominationRequest, destination: "foobiz") + let transactionRequest = TransactionTransferRequest(denomination: transactionDenominationRequest, message: "foobar", destination: "foobiz") card.createTransaction(transactionRequest: transactionRequest).then { (transaction: Transaction) -> Void in XCTAssertEqual(transaction.id, "foobar", "Failed: Wrong transaction id.") XCTAssertEqual(transactionRequest.destination, "foobiz", "Failed: Wrong transaction destination.") + XCTAssertEqual(transactionRequest.message, "foobar", "Failed: Wrong transaction message.") testExpectation.fulfill() }.catch(execute: { (_: Error) in @@ -367,7 +368,7 @@ class CardTest: UpholdTestCase { let card: Card = Mapper().map(JSONString: "{}")! card.adapter = MockRestAdapter() let transactionDenominationRequest = TransactionDenominationRequest(amount: "foo", currency: "bar") - let transactionRequest = TransactionTransferRequest(denomination: transactionDenominationRequest, destination: "foobar") + let transactionRequest = TransactionTransferRequest(denomination: transactionDenominationRequest, message: nil, destination: "foobar") card.createTransaction(transactionRequest: transactionRequest).catch(execute: { (error: Error) in guard let error = error as? UnexpectedResponseError else { @@ -391,7 +392,7 @@ class CardTest: UpholdTestCase { let card: Card = Fixtures.loadCard() card.adapter = MockRestAdapter(body: Mapper().toJSONString(Fixtures.loadTransaction(fields: ["transactionId": "foobar"]))!) let transactionDenominationRequest = TransactionDenominationRequest(amount: "foo", currency: "bar") - let transactionRequest = TransactionTransferRequest(denomination: transactionDenominationRequest, destination: "foobar") + let transactionRequest = TransactionTransferRequest(denomination: transactionDenominationRequest, message: "foobuz", destination: "foobar") card.createTransaction(commit: true, transactionRequest: transactionRequest).then { (transaction: Transaction) -> Void in XCTAssertEqual(transaction.id, "foobar", "Failed: Wrong transaction id.") diff --git a/Tests/IntegrationTests/ServiceTests/UserCardServiceTest.swift b/Tests/IntegrationTests/ServiceTests/UserCardServiceTest.swift index f8ddb47..6d69a8b 100644 --- a/Tests/IntegrationTests/ServiceTests/UserCardServiceTest.swift +++ b/Tests/IntegrationTests/ServiceTests/UserCardServiceTest.swift @@ -39,7 +39,7 @@ class UserCardServiceTest: XCTestCase { } func testCreateTransactionShouldReturnTheRequest() { - let request = UserCardService.createTransaction(cardId: "bar", commit: false, transactionRequest: "foobar") + let request = UserCardService.createTransaction(cardId: "bar", commit: false, otp: "foo", transactionRequest: "foobar") XCTAssertEqual(request.url, String(format: "%@/v0/me/cards/bar/transactions", GlobalConfigurations.UPHOLD_API_URL), "Failed: Wrong URL.") XCTAssertEqual(request.query.count, 1, "Failed: Wrong number of query parameters.") @@ -49,7 +49,7 @@ class UserCardServiceTest: XCTestCase { } func testCreateTransactionWithCommitShouldReturnTheRequest() { - let request = UserCardService.createTransaction(cardId: "bar", commit: true, transactionRequest: "foobar") + let request = UserCardService.createTransaction(cardId: "bar", commit: true, otp: "foo", transactionRequest: "foobar") XCTAssertEqual(request.url, String(format: "%@/v0/me/cards/bar/transactions", GlobalConfigurations.UPHOLD_API_URL), "Failed: Wrong URL.") XCTAssertEqual(request.query.count, 1, "Failed: Wrong number of query parameters.")