Skip to content

Commit b6d9a88

Browse files
committed
Return card reader connection tokens from wcpay or stripe endpoints
1 parent 2013271 commit b6d9a88

File tree

9 files changed

+103
-44
lines changed

9 files changed

+103
-44
lines changed

Networking/Networking.xcodeproj/project.pbxproj

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@
178178
3158FE7C26129E2100E566B9 /* wcpay-account-restricted-pending.json in Resources */ = {isa = PBXBuildFile; fileRef = 3158FE7B26129E2100E566B9 /* wcpay-account-restricted-pending.json */; };
179179
3158FE8026129EC300E566B9 /* wcpay-account-restricted-overdue.json in Resources */ = {isa = PBXBuildFile; fileRef = 3158FE7F26129EC300E566B9 /* wcpay-account-restricted-overdue.json */; };
180180
3158FE8426129F3A00E566B9 /* wcpay-account-unknown-status.json in Resources */ = {isa = PBXBuildFile; fileRef = 3158FE8326129F3A00E566B9 /* wcpay-account-unknown-status.json */; };
181+
31723C892787A299007F1405 /* stripe-connection-token.json in Resources */ = {isa = PBXBuildFile; fileRef = 31723C882787A299007F1405 /* stripe-connection-token.json */; };
181182
3178A49F2703E5CF00A8B4CA /* WCPayReaderLocationMapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3178A49E2703E5CF00A8B4CA /* WCPayReaderLocationMapper.swift */; };
182183
31799AF8270508C600D78179 /* WCPayReaderLocation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31799AF7270508C600D78179 /* WCPayReaderLocation.swift */; };
183184
31799AFC2705189200D78179 /* wcpay-location.json in Resources */ = {isa = PBXBuildFile; fileRef = 31799AFB2705189200D78179 /* wcpay-location.json */; };
@@ -559,8 +560,8 @@
559560
D8C11A5C22DFCF8100D4A88D /* order-stats-v4-year-alt.json in Resources */ = {isa = PBXBuildFile; fileRef = D8C11A5B22DFCF8100D4A88D /* order-stats-v4-year-alt.json */; };
560561
D8C251D0230BD72700F49782 /* ProductReviewMapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8C251CF230BD72700F49782 /* ProductReviewMapper.swift */; };
561562
D8EDFE1E25EE87F1003D2213 /* WCPayRemote.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8EDFE1D25EE87F1003D2213 /* WCPayRemote.swift */; };
562-
D8EDFE2225EE88C9003D2213 /* WCPayConnectionToken.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8EDFE2125EE88C9003D2213 /* WCPayConnectionToken.swift */; };
563-
D8EDFE2625EE8A60003D2213 /* WCPayConnectionTokenMapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8EDFE2525EE8A60003D2213 /* WCPayConnectionTokenMapper.swift */; };
563+
D8EDFE2225EE88C9003D2213 /* ReaderConnectionToken.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8EDFE2125EE88C9003D2213 /* ReaderConnectionToken.swift */; };
564+
D8EDFE2625EE8A60003D2213 /* ReaderConnectionTokenMapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8EDFE2525EE8A60003D2213 /* ReaderConnectionTokenMapper.swift */; };
564565
D8FBFF0B22D3ADB1006E3336 /* OrderStatsRemoteV4.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8FBFF0A22D3ADB1006E3336 /* OrderStatsRemoteV4.swift */; };
565566
D8FBFF0D22D3AF4A006E3336 /* StatsGranularityV4.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8FBFF0C22D3AF4A006E3336 /* StatsGranularityV4.swift */; };
566567
D8FBFF0F22D3B25E006E3336 /* WooAPIVersionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8FBFF0E22D3B25E006E3336 /* WooAPIVersionTests.swift */; };
@@ -805,6 +806,7 @@
805806
3158FE7B26129E2100E566B9 /* wcpay-account-restricted-pending.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "wcpay-account-restricted-pending.json"; sourceTree = "<group>"; };
806807
3158FE7F26129EC300E566B9 /* wcpay-account-restricted-overdue.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "wcpay-account-restricted-overdue.json"; sourceTree = "<group>"; };
807808
3158FE8326129F3A00E566B9 /* wcpay-account-unknown-status.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "wcpay-account-unknown-status.json"; sourceTree = "<group>"; };
809+
31723C882787A299007F1405 /* stripe-connection-token.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "stripe-connection-token.json"; sourceTree = "<group>"; };
808810
3178A49E2703E5CF00A8B4CA /* WCPayReaderLocationMapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WCPayReaderLocationMapper.swift; sourceTree = "<group>"; };
809811
31799AF7270508C600D78179 /* WCPayReaderLocation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WCPayReaderLocation.swift; sourceTree = "<group>"; };
810812
31799AFB2705189200D78179 /* wcpay-location.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "wcpay-location.json"; sourceTree = "<group>"; };
@@ -1192,8 +1194,8 @@
11921194
D8C11A5B22DFCF8100D4A88D /* order-stats-v4-year-alt.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "order-stats-v4-year-alt.json"; sourceTree = "<group>"; };
11931195
D8C251CF230BD72700F49782 /* ProductReviewMapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductReviewMapper.swift; sourceTree = "<group>"; };
11941196
D8EDFE1D25EE87F1003D2213 /* WCPayRemote.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WCPayRemote.swift; sourceTree = "<group>"; };
1195-
D8EDFE2125EE88C9003D2213 /* WCPayConnectionToken.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WCPayConnectionToken.swift; sourceTree = "<group>"; };
1196-
D8EDFE2525EE8A60003D2213 /* WCPayConnectionTokenMapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WCPayConnectionTokenMapper.swift; sourceTree = "<group>"; };
1197+
D8EDFE2125EE88C9003D2213 /* ReaderConnectionToken.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReaderConnectionToken.swift; sourceTree = "<group>"; };
1198+
D8EDFE2525EE8A60003D2213 /* ReaderConnectionTokenMapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReaderConnectionTokenMapper.swift; sourceTree = "<group>"; };
11971199
D8FBFF0A22D3ADB1006E3336 /* OrderStatsRemoteV4.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OrderStatsRemoteV4.swift; sourceTree = "<group>"; };
11981200
D8FBFF0C22D3AF4A006E3336 /* StatsGranularityV4.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatsGranularityV4.swift; sourceTree = "<group>"; };
11991201
D8FBFF0E22D3B25E006E3336 /* WooAPIVersionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = WooAPIVersionTests.swift; path = NetworkingTests/Requests/WooAPIVersionTests.swift; sourceTree = SOURCE_ROOT; };
@@ -1691,7 +1693,7 @@
16911693
3192F21F260D33BB0067FEF9 /* WCPayAccount.swift */,
16921694
31799AF7270508C600D78179 /* WCPayReaderLocation.swift */,
16931695
3192F223260D34C40067FEF9 /* WCPayAccountStatusEnum.swift */,
1694-
D8EDFE2125EE88C9003D2213 /* WCPayConnectionToken.swift */,
1696+
D8EDFE2125EE88C9003D2213 /* ReaderConnectionToken.swift */,
16951697
318E8FD426C31F9500F519D7 /* WCPayCustomer.swift */,
16961698
31054705262E278100C5C02B /* WCPayPaymentIntent.swift */,
16971699
3105470B262E27F000C5C02B /* WCPayPaymentIntentStatusEnum.swift */,
@@ -1866,6 +1868,7 @@
18661868
7495AACE225D366D00801A89 /* variation-as-product.json */,
18671869
02A26F192744F5FC008E4EDB /* wc-site-settings-partial.json */,
18681870
D800DA0D25EFEC21001E13CE /* wcpay-connection-token.json */,
1871+
31723C882787A299007F1405 /* stripe-connection-token.json */,
18691872
318E8FD626C322EA00F519D7 /* wcpay-customer.json */,
18701873
318E8FD826C324D900F519D7 /* wcpay-customer-error.json */,
18711874
3158FE5F26129ADD00E566B9 /* wcpay-account-none.json */,
@@ -1966,7 +1969,7 @@
19661969
021A84D9257DF92800BC71D1 /* ShippingLabelRefundMapper.swift */,
19671970
311D412D2783C07D00052F64 /* StripeAccountMapper.swift */,
19681971
3192F21B260D32550067FEF9 /* WCPayAccountMapper.swift */,
1969-
D8EDFE2525EE8A60003D2213 /* WCPayConnectionTokenMapper.swift */,
1972+
D8EDFE2525EE8A60003D2213 /* ReaderConnectionTokenMapper.swift */,
19701973
318E8FD226C31F1C00F519D7 /* WCPayCustomerMapper.swift */,
19711974
3178A49E2703E5CF00A8B4CA /* WCPayReaderLocationMapper.swift */,
19721975
31054701262E04F700C5C02B /* WCPayPaymentIntentMapper.swift */,
@@ -2417,6 +2420,7 @@
24172420
D8FBFF1522D3BE09006E3336 /* order-stats-v4-defaults.json in Resources */,
24182421
7495AACF225D366D00801A89 /* variation-as-product.json in Resources */,
24192422
0272E3FB254AABB800436277 /* order-with-line-item-attributes-before-API-support.json in Resources */,
2423+
31723C892787A299007F1405 /* stripe-connection-token.json in Resources */,
24202424
74A1D266211898F000931DFA /* site-visits-year.json in Resources */,
24212425
743BF8BE21191B63008A9D87 /* site-visits.json in Resources */,
24222426
CE0A0F1F223998A10075ED8D /* products-load-all.json in Resources */,
@@ -2673,7 +2677,7 @@
26732677
4568E2242459D3230007E478 /* Post.swift in Sources */,
26742678
B557DA0320975500005962F4 /* Remote.swift in Sources */,
26752679
B53EF53C21814900003E146F /* SuccessResultMapper.swift in Sources */,
2676-
D8EDFE2625EE8A60003D2213 /* WCPayConnectionTokenMapper.swift in Sources */,
2680+
D8EDFE2625EE8A60003D2213 /* ReaderConnectionTokenMapper.swift in Sources */,
26772681
CC6A1FF5270E042200F6AF4A /* OrderMetaData.swift in Sources */,
26782682
DEC51B02276AFB35009F3DF4 /* SystemStatus+DropinMustUsePlugin.swift in Sources */,
26792683
02C254A4256371B200A04423 /* ShippingLabelSettings.swift in Sources */,
@@ -2772,7 +2776,7 @@
27722776
B5BB1D1020A237FB00112D92 /* Address.swift in Sources */,
27732777
CE43066A23465F340073CBFF /* Refund.swift in Sources */,
27742778
B524194121AC60A700D6FC0A /* DotcomDevice.swift in Sources */,
2775-
D8EDFE2225EE88C9003D2213 /* WCPayConnectionToken.swift in Sources */,
2779+
D8EDFE2225EE88C9003D2213 /* ReaderConnectionToken.swift in Sources */,
27762780
4599FC5824A624BD0056157A /* ProductTagListMapper.swift in Sources */,
27772781
26B2F74B24C696C00065CCC8 /* LeaderboardRow.swift in Sources */,
27782782
45A4B86225D3086600776FB4 /* ShippingLabelAddressValidationError.swift in Sources */,
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/// Mapper: Card reader connection token
2+
///
3+
struct ReaderConnectionTokenMapper: Mapper {
4+
5+
/// (Attempts) to convert a dictionary into a connection token.
6+
///
7+
func map(response: Data) throws -> ReaderConnectionToken {
8+
let decoder = JSONDecoder()
9+
10+
return try decoder.decode(ReaderConnectionTokenEnvelope.self, from: response).token
11+
}
12+
}
13+
14+
15+
/// ReaderConnectionTokenEnvelope Disposable Entity
16+
///
17+
/// `Load connection Token` endpoint returns the requested connection token and test mode in the `data` key. This entity
18+
/// allows us to parse all the things with JSONDecoder.
19+
///
20+
private struct ReaderConnectionTokenEnvelope: Decodable {
21+
let token: ReaderConnectionToken
22+
23+
private enum CodingKeys: String, CodingKey {
24+
case token = "data"
25+
}
26+
}

Networking/Networking/Mapper/WCPayConnectionTokenMapper.swift

Lines changed: 0 additions & 26 deletions
This file was deleted.

Networking/Networking/Model/WCPayConnectionToken.swift renamed to Networking/Networking/Model/ReaderConnectionToken.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
/// Represent a WCPay connection token Entity.
1+
/// Represent a card reader connection token Entity.
22
///
3-
public struct WCPayConnectionToken: Decodable {
3+
public struct ReaderConnectionToken: Decodable {
44
public let token: String
55
public let testMode: Bool
66

@@ -9,7 +9,7 @@ public struct WCPayConnectionToken: Decodable {
99
self.testMode = testMode
1010
}
1111

12-
/// The public initializer for WCPay Connection Token.
12+
/// Public initializer.
1313
///
1414
public init(from decoder: Decoder) throws {
1515
let container = try decoder.container(keyedBy: CodingKeys.self)
@@ -22,7 +22,7 @@ public struct WCPayConnectionToken: Decodable {
2222
}
2323

2424

25-
private extension WCPayConnectionToken {
25+
private extension ReaderConnectionToken {
2626
enum CodingKeys: String, CodingKey {
2727
case secret = "secret"
2828
case testMode = "test_mode"

Networking/Networking/Remote/StripeRemote.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@ import Foundation
33
/// Stripe (Extension): Remote Endpoints
44
///
55
public class StripeRemote: Remote {
6+
/// Loads a card reader connection token for a given site ID and parses the response
7+
/// - Parameters:
8+
/// - siteID: Site for which we'll fetch the connection token.
9+
/// - completion: Closure to be executed upon completion.
10+
public func loadConnectionToken(for siteID: Int64,
11+
completion: @escaping(Result<ReaderConnectionToken, Error>) -> Void) {
12+
let request = JetpackRequest(wooApiVersion: .mark3, method: .post, siteID: siteID, path: Path.connectionTokens)
13+
14+
let mapper = ReaderConnectionTokenMapper()
15+
16+
enqueue(request, mapper: mapper, completion: completion)
17+
}
18+
619
/// Loads a Stripe account for a given site ID and parses the response
720
/// - Parameters:
821
/// - siteID: Site for which we'll fetch the Stripe account info.
@@ -31,6 +44,7 @@ public class StripeRemote: Remote {
3144
//
3245
private extension StripeRemote {
3346
enum Path {
47+
static let connectionTokens = "wc_stripe/connection_tokens"
3448
static let accounts = "wc_stripe/account/summary"
3549
}
3650

Networking/Networking/Remote/WCPayRemote.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ import Foundation
33
/// WCPay: Remote Endpoints
44
///
55
public class WCPayRemote: Remote {
6-
7-
/// Loads a WCPay connection token for a given site ID and parses the rsponse
6+
/// Loads a card reader connection token for a given site ID and parses the response
87
/// - Parameters:
9-
/// - siteID: Site for which we'll fetch the WCPay Connection token.
8+
/// - siteID: Site for which we'll fetch the connection token.
109
/// - completion: Closure to be executed upon completion.
1110
public func loadConnectionToken(for siteID: Int64,
12-
completion: @escaping(Result<WCPayConnectionToken, Error>) -> Void) {
11+
completion: @escaping(Result<ReaderConnectionToken, Error>) -> Void) {
1312
let request = JetpackRequest(wooApiVersion: .mark3, method: .post, siteID: siteID, path: Path.connectionTokens)
1413

15-
let mapper = WCPayConnectionTokenMapper()
14+
let mapper = ReaderConnectionTokenMapper()
1615

1716
enqueue(request, mapper: mapper, completion: completion)
1817
}

Networking/NetworkingTests/Remote/StripeRemoteTests.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,41 @@ final class StripeRemoteTests: XCTestCase {
2121
network.removeAllSimulatedResponses()
2222
}
2323

24+
/// Verifies that loadConnectionToken properly parses the sample response.
25+
///
26+
func test_loadConnectionToken_properly_returns_parsed_token() {
27+
let remote = WCPayRemote(network: network)
28+
let expectation = self.expectation(description: "Load card reader token from Stripe extension")
29+
30+
let expectedToken = "a connection token"
31+
32+
network.simulateResponse(requestUrlSuffix: "payments/connection_tokens", filename: "stripe-connection-token")
33+
remote.loadConnectionToken(for: sampleSiteID) { result in
34+
if case let .success(token) = result {
35+
XCTAssertEqual(token.token, expectedToken)
36+
expectation.fulfill()
37+
}
38+
}
39+
40+
wait(for: [expectation], timeout: Constants.expectationTimeout)
41+
}
42+
43+
/// Verifies that loadConnectionToken properly relays Networking Layer errors.
44+
///
45+
func test_loadConnectionToken_properly_relays_networking_errors() {
46+
let remote = WCPayRemote(network: network)
47+
let expectation = self.expectation(description: "Load WCPay token contains errors")
48+
49+
remote.loadConnectionToken(for: sampleSiteID) { result in
50+
if case let .failure(error) = result {
51+
XCTAssertNotNil(error)
52+
expectation.fulfill()
53+
}
54+
}
55+
56+
wait(for: [expectation], timeout: Constants.expectationTimeout)
57+
}
58+
2459
/// Verifies that loadAccount properly handles the nominal response. We'll also validate the
2560
/// statement descriptor, currencies and country here.
2661
///

Networking/NetworkingTests/Remote/WCPayRemoteTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ final class WCPayRemoteTests: XCTestCase {
3333
///
3434
func test_loadConnectionToken_properly_returns_parsed_token() {
3535
let remote = WCPayRemote(network: network)
36-
let expectation = self.expectation(description: "Load WCPay token")
36+
let expectation = self.expectation(description: "Load card reader token from WCPay extension")
3737

3838
let expectedToken = "a connection token"
3939

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"data": {
3+
"object": "terminal.connection_token",
4+
"secret": "a connection token",
5+
"test_mode": false
6+
}
7+
}

0 commit comments

Comments
 (0)