Skip to content

Commit

Permalink
Add support to message and otp on create transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
SandroMachado committed Nov 16, 2017
1 parent 3abf3f0 commit 72d25b3
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 21 deletions.
16 changes: 15 additions & 1 deletion Source/Model/Card.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,25 @@ open class Card: BaseModel, Mappable {
- returns: A promise with the transaction.
*/
open func createTransaction(commit: Bool, transactionRequest: TransactionRequest) -> Promise<Transaction> {
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 transactionCommitRequest: A transactionCommitRequest with an optional transaction message.
- parameter transactionRequest: The transaction request information.

- returns: A promise with the transaction.
*/
open func createTransaction(commit: Bool, otp: String?, transactionRequest: TransactionRequest) -> Promise<Transaction> {
guard let id = self.id else {
return Promise<Transaction>(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)
}
Expand Down
4 changes: 2 additions & 2 deletions Source/Model/Transaction/TransactionCardDepositRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ open class TransactionCardDepositRequest: TransactionDepositRequest {
- 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
}
Expand Down
4 changes: 2 additions & 2 deletions Source/Model/Transaction/TransactionDepositRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ open class TransactionDepositRequest: TransactionRequest {
- parameter denomination: The denomination of the transaction request.
- 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
}
Expand Down
8 changes: 7 additions & 1 deletion Source/Model/Transaction/TransactionRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -33,6 +38,7 @@ open class TransactionRequest: Mappable {
*/
open func mapping(map: Map) {
self.denomination <- map["denomination"]
self.message <- map["message"]
}

}
4 changes: 2 additions & 2 deletions Source/Model/Transaction/TransactionTransferRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ open class TransactionTransferRequest: TransactionRequest {
- parameter denomination: The denomination 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
}
Expand Down
10 changes: 8 additions & 2 deletions Source/Service/UserCardService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand Down
19 changes: 10 additions & 9 deletions Tests/IntegrationTests/ModelTests/CardTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down Expand Up @@ -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 {
Expand All @@ -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.")
Expand All @@ -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.")
Expand All @@ -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 {
Expand All @@ -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.")
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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.")
Expand Down
4 changes: 2 additions & 2 deletions Tests/IntegrationTests/ServiceTests/UserCardServiceTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand All @@ -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.")
Expand Down

0 comments on commit 72d25b3

Please sign in to comment.