Skip to content

Commit

Permalink
Merge pull request #71 from uphold/feature/transaction-reference
Browse files Browse the repository at this point in the history
Add support to create transactions with reference
  • Loading branch information
d-moreira authored May 22, 2018
2 parents 336e545 + 0bf8a65 commit 51242d8
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 4 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,16 @@ card.createTransaction(transactionTransferRequest).then { (transaction: Transact
/// Do something with the error.
})

/// A transaction to a destination (card id, crypto address, email, phone number or username) with reference.
let transactionTransferRequest = TransactionTransferRequest(denomination: transactionDenominationRequest, destination: "[email protected]", reference: "123456")

card.createTransaction(transactionTransferRequest).then { (transaction: Transaction) -> () in
/// Commit the transaction.
transaction.commit(TransactionCommitRequest("Commit message"))
}.error({ (error: ErrorType) -> Void in
/// Do something with the error.
})

/// A deposit from an ACH or SEPA account.
let transactionDepositRequest = TransactionDepositRequest(denomination: transactionDenominationRequest, origin: "accountId")

Expand Down
8 changes: 7 additions & 1 deletion Source/Model/Card/Address.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@ open class Address: Mappable {
/// The network of the created address.
public private(set) final var network: String?

/// The tag of the created address if available.
public private(set) final var tag: String?

/**
Constructor.

- parameter id: The id of the created address.
- parameter network: The network of the created address.
- parameter tag: The tag of the created address.
*/
public init(id: String, network: String) {
public init(id: String, network: String, tag: String) {
self.id = id
self.network = network
self.tag = tag
}

// MARK: Required by the ObjectMapper.
Expand All @@ -39,6 +44,7 @@ open class Address: Mappable {
open func mapping(map: Map) {
self.id <- map["id"]
self.network <- map["network"]
self.tag <- map["tag"]
}

}
8 changes: 7 additions & 1 deletion Source/Model/Transaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ open class Transaction: BaseModel, Mappable {
/// Other parameters of this transaction.
public private(set) final var params: Parameters?

/// The reference of this transaction.
public private(set) final var reference: String?

/// When a transaction is cancelled this contains the transaction ID of the transaction which refunds the amount back to the user.
public private(set) final var refundedById: String?

Expand Down Expand Up @@ -69,11 +72,12 @@ open class Transaction: BaseModel, Mappable {
- parameter normalized: The transaction details normalized.
- parameter origin: The sender of the funds.
- parameter params: Other parameters of this transaction.
- parameter reference: The reference of the transaction.
- parameter refundedById: When a transaction is cancelled this contains the transaction ID of the transaction which refunds the amount back to the user.
- parameter status: The current status of the transaction.
- parameter type: The nature of the transaction.
*/
public init(id: String, createdAt: String, denomination: Denomination, destination: Destination, fees: [Fee], message: String, network: String, normalized: [NormalizedTransaction], origin: Origin, params: Parameters, refundedById: String, status: String, type: String) {
public init(id: String, createdAt: String, denomination: Denomination, destination: Destination, fees: [Fee], message: String, network: String, normalized: [NormalizedTransaction], origin: Origin, params: Parameters, reference: String, refundedById: String, status: String, type: String) {
self.id = id
self.createdAt = createdAt
self.denomination = denomination
Expand All @@ -84,6 +88,7 @@ open class Transaction: BaseModel, Mappable {
self.normalized = normalized
self.origin = origin
self.params = params
self.reference = reference
self.refundedById = refundedById
self.status = status
self.type = type
Expand Down Expand Up @@ -115,6 +120,7 @@ open class Transaction: BaseModel, Mappable {
normalized <- map["normalized"]
origin <- map["origin"]
params <- map["params"]
reference <- map["reference"]
refundedById <- map["RefundedById"]
status <- map["status"]
type <- map["type"]
Expand Down
18 changes: 18 additions & 0 deletions Source/Model/Transaction/TransactionTransferRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ open class TransactionTransferRequest: TransactionRequest {
/// The destination of the transaction request.
public private(set) final var destination: String?

/// The reference of the transaction request.
public private(set) final var reference: String?

/**
Constructor.

Expand All @@ -19,6 +22,20 @@ open class TransactionTransferRequest: TransactionRequest {
self.destination = destination
}

/**
Constructor.

- parameter denomination: The denomination of the transaction request.
- parameter destination: The destination of the transaction request.
- parameter reference: The reference of the transaction request.
*/
public init(denomination: TransactionDenominationRequest, destination: String, reference: String) {
super.init(denomination: denomination)

self.destination = destination
self.reference = reference
}

// MARK: Required by the ObjectMapper.

/**
Expand All @@ -39,6 +56,7 @@ open class TransactionTransferRequest: TransactionRequest {
super.mapping(map: map)

self.destination <- map["destination"]
self.reference <- map["reference"]
}

}
27 changes: 26 additions & 1 deletion Tests/IntegrationTests/ModelTests/CardTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ class CardTest: UpholdTestCase {
let testExpectation = expectation(description: "User test: create address.")

let card: Card = Fixtures.loadCard()
card.adapter = MockRestAdapter(body: "{\"id\": \"foo\",\"network\": \"bar\"}")
card.adapter = MockRestAdapter(body: "{\"id\": \"foo\",\"network\": \"bar\",\"tag\": \"foobar\"}")

card.createAddress(addressRequest: AddressRequest(network: "bitcoin")).then { (address: Address) -> Void in
XCTAssertEqual(address.id, "foo", "Failed: Wrong adrress id.")
XCTAssertEqual(address.network, "bar", "Failed: Wrong address network.")
XCTAssertEqual(address.tag, "foobar", "Failed: Wrong address network.")

testExpectation.fulfill()
}.catch(execute: { (_: Error) in
Expand Down Expand Up @@ -102,6 +103,7 @@ class CardTest: UpholdTestCase {
"\"message\": \"foobar\"," +
"\"network\": \"qux\"," +
"\"status\": \"pending\"," +
"\"reference\": \"123456\"," +
"\"RefundedById\": \"foobiz\"," +
"\"createdAt\": \"2014-08-27T00:01:11.616Z\"," +
"\"denomination\": {" +
Expand Down Expand Up @@ -361,6 +363,29 @@ class CardTest: UpholdTestCase {
wait()
}

func testCreateTransactionTransferWithReferenceShouldReturnTheTransaction() {
let testExpectation = expectation(description: "Card test: create transaction transfer.")

let card: Card = Fixtures.loadCard()
let json: String = "{" +
"\"id\": \"foobar\"," +
"}"
card.adapter = MockRestAdapter(body: json)
let transactionDenominationRequest = TransactionDenominationRequest(amount: "foo", currency: "bar")
let transactionRequest = TransactionTransferRequest(denomination: transactionDenominationRequest, destination: "foobiz", reference: "123456")

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.")

testExpectation.fulfill()
}.catch(execute: { (_: Error) in
XCTFail("User test: create transaction transfer error.")
})

wait()
}

func testCreateTransactionTransferShouldReturnUnexpectedResponseError() {
let testExpectation = expectation(description: "Card test: create transaction transfer.")

Expand Down
3 changes: 2 additions & 1 deletion Tests/UtilTests/Fixtures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ public class Fixtures {
"transactionId": faker.lorem.characters(amount: 24),
"transactionMessage": faker.lorem.characters(amount: 24),
"transactionNetwork": faker.lorem.characters(amount: 24),
"transactionReference": faker.lorem.numerify("123456789"),
"transactionRefundedById": faker.lorem.characters(amount: 24),
"transactionRefunds": faker.lorem.characters(amount: 24),
"transactionStatus": faker.lorem.characters(amount: 24),
Expand Down Expand Up @@ -212,7 +213,7 @@ public class Fixtures {
let origin = Origin(accountId: fakerFields["originAccountId"]!, cardId: fakerFields["originCardId"]!, accountType: fakerFields["originAccountType"]!, amount: fakerFields["originAmount"]!, base: fakerFields["originBase"]!, commission: fakerFields["originCommission"]!, currency: fakerFields["originCurrency"]!, description: fakerFields["originDescription"]!, fee: fakerFields["originFee"]!, merchant: originMerchant, node: originNode, rate: fakerFields["originRate"]!, sources: sources, type: fakerFields["originType"]!, username: fakerFields["originUsername"]!)
let parameters = Parameters(currency: fakerFields["parametersCurrency"]!, margin: fakerFields["parametersMargin"]!, pair: fakerFields["parametersPair"]!, progress: fakerFields["parametersProgress"]!, rate: fakerFields["parametersRate"]!, refunds: fakerFields["parametersRefunds"]!, ttl: NSString(string: fakerFields["parametersTtl"]!).integerValue, txid: fakerFields["parametersTxid"]!, type: fakerFields["parametersType"]!)

return Transaction(id: fakerFields["transactionId"]!, createdAt: fakerFields["transactionCreatedAt"]!, denomination: denomination, destination: destination, fees: fees, message: fakerFields["transactionMessage"]!, network: fakerFields["transactionNetwork"]!, normalized: normalized, origin: origin, params: parameters, refundedById: fakerFields["transactionRefundedById"]!, status: fakerFields["transactionStatus"]!, type: fakerFields["transactionType"]!)
return Transaction(id: fakerFields["transactionId"]!, createdAt: fakerFields["transactionCreatedAt"]!, denomination: denomination, destination: destination, fees: fees, message: fakerFields["transactionMessage"]!, network: fakerFields["transactionNetwork"]!, normalized: normalized, origin: origin, params: parameters, reference: fakerFields["transactionReference"]!, refundedById: fakerFields["transactionRefundedById"]!, status: fakerFields["transactionStatus"]!, type: fakerFields["transactionType"]!)
}

/**
Expand Down

0 comments on commit 51242d8

Please sign in to comment.