Skip to content

Commit 933e5dc

Browse files
committed
5976 Add WCPayChargesRemote
1 parent 101a1c6 commit 933e5dc

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

Networking/Networking.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
0359EA1F27AAE4680048DE2D /* WCPayChargeMapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0359EA1E27AAE4680048DE2D /* WCPayChargeMapperTests.swift */; };
9191
0359EA2127AAE58C0048DE2D /* wcpay-charge-card-present.json in Resources */ = {isa = PBXBuildFile; fileRef = 0359EA2027AAE58C0048DE2D /* wcpay-charge-card-present.json */; };
9292
0359EA2527AAF7D60048DE2D /* wcpay-charge-card.json in Resources */ = {isa = PBXBuildFile; fileRef = 0359EA2427AAF7D60048DE2D /* wcpay-charge-card.json */; };
93+
0359EA2927AC2AAD0048DE2D /* wcpay-charge-error.json in Resources */ = {isa = PBXBuildFile; fileRef = 0359EA2827AC2AAD0048DE2D /* wcpay-charge-error.json */; };
9394
03DCB72626244B9B00C8953D /* Coupon.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03DCB72526244B9B00C8953D /* Coupon.swift */; };
9495
03DCB7402624AD7D00C8953D /* CouponListMapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03DCB73F2624AD7D00C8953D /* CouponListMapper.swift */; };
9596
03DCB7442624AD9B00C8953D /* CouponListMapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03DCB7432624AD9A00C8953D /* CouponListMapperTests.swift */; };
@@ -762,6 +763,7 @@
762763
0359EA1E27AAE4680048DE2D /* WCPayChargeMapperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WCPayChargeMapperTests.swift; sourceTree = "<group>"; };
763764
0359EA2027AAE58C0048DE2D /* wcpay-charge-card-present.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "wcpay-charge-card-present.json"; sourceTree = "<group>"; };
764765
0359EA2427AAF7D60048DE2D /* wcpay-charge-card.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "wcpay-charge-card.json"; sourceTree = "<group>"; };
766+
0359EA2827AC2AAD0048DE2D /* wcpay-charge-error.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "wcpay-charge-error.json"; sourceTree = "<group>"; };
765767
03DCB72526244B9B00C8953D /* Coupon.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Coupon.swift; sourceTree = "<group>"; };
766768
03DCB73F2624AD7D00C8953D /* CouponListMapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CouponListMapper.swift; sourceTree = "<group>"; };
767769
03DCB7432624AD9A00C8953D /* CouponListMapperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CouponListMapperTests.swift; sourceTree = "<group>"; };
@@ -2017,6 +2019,7 @@
20172019
077F39D726A58EB600ABEADC /* systemStatus.json */,
20182020
0359EA2427AAF7D60048DE2D /* wcpay-charge-card.json */,
20192021
0359EA2027AAE58C0048DE2D /* wcpay-charge-card-present.json */,
2022+
0359EA2827AC2AAD0048DE2D /* wcpay-charge-error.json */,
20202023
45CCFCE927A2E59B0012E8CB /* inbox-note-list.json */,
20212024
4513382727A96DE700AE5E78 /* inbox-note.json */,
20222025
);
@@ -2581,6 +2584,7 @@
25812584
B524194721AC643900D6FC0A /* device-settings.json in Resources */,
25822585
3158A49F2729F3F600C3CFA8 /* wcpay-account-live-live.json in Resources */,
25832586
318E8FD926C324D900F519D7 /* wcpay-customer-error.json in Resources */,
2587+
0359EA2927AC2AAD0048DE2D /* wcpay-charge-error.json in Resources */,
25842588
CEF88DAB233E911A00BED485 /* order-fully-refunded.json in Resources */,
25852589
02698CFA24C188E9005337C4 /* product-variations-load-all-alternative-types.json in Resources */,
25862590
CC0786632678F79500BA9AC1 /* shipping-label-purchase-success.json in Resources */,

Networking/Networking/Remote/WCPayRemote.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,24 @@ public class WCPayRemote: Remote {
6060

6161
enqueue(request, mapper: mapper, completion: completion)
6262
}
63+
64+
/// Fetches the details of a charge, if available. See https://stripe.com/docs/api/charges/object
65+
/// Also note that the JSON returned by the WCPay endpoint is an abridged copy of Stripe's response.
66+
/// - Parameters:
67+
/// - siteID: Site for which we'll fetch the charge.
68+
/// - chargeID: ID of the charge to fetch
69+
/// - completion: Closure to be run on completion.
70+
public func fetchCharge(for siteID: Int64,
71+
chargeID: String,
72+
completion: @escaping (Result<WCPayCharge, Error>) -> Void) {
73+
let path = "\(Path.charges)/\(chargeID)"
74+
75+
let request = JetpackRequest(wooApiVersion: .mark3, method: .post, siteID: siteID, path: path, parameters: [:])
76+
77+
let mapper = WCPayChargeMapper(siteID: siteID)
78+
79+
enqueue(request, mapper: mapper, completion: completion)
80+
}
6381
}
6482

6583
// MARK: - CardReaderCapableRemote
@@ -104,6 +122,7 @@ private extension WCPayRemote {
104122
static let captureTerminalPayment = "capture_terminal_payment"
105123
static let createCustomer = "create_customer"
106124
static let locations = "payments/terminal/locations/store"
125+
static let charges = "payments/charges"
107126
}
108127

109128
enum AccountParameterKeys {

Networking/NetworkingTests/Remote/WCPayRemoteTests.swift

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,4 +661,49 @@ final class WCPayRemoteTests: XCTestCase {
661661

662662
XCTAssertTrue(result.isFailure)
663663
}
664+
665+
func test_fetchCharge_properly_returns_charge_with_payment_method_details() throws {
666+
let remote = WCPayRemote(network: network)
667+
let chargeID = "ch_3KMVap2EdyGr1FMV1uKJEWtg"
668+
let expectedPaymentMethodDetails = WCPayPaymentMethodDetails.cardPresent(
669+
details: .init(brand: .visa,
670+
last4: "9969",
671+
funding: .credit,
672+
receipt: .init(accountType: .credit,
673+
applicationPreferredName: "Stripe Credit",
674+
dedicatedFileName: "A000000003101001")))
675+
676+
network.simulateResponse(
677+
requestUrlSuffix: "payments/charges/\(chargeID)",
678+
filename: "wcpay-charge-card-present"
679+
)
680+
681+
let result: Result<WCPayCharge, Error> = waitFor { promise in
682+
remote.fetchCharge(for: self.sampleSiteID, chargeID: chargeID) { result in
683+
promise(result)
684+
}
685+
}
686+
687+
XCTAssertTrue(result.isSuccess)
688+
let charge = try result.get()
689+
XCTAssertEqual(charge.paymentMethodDetails, expectedPaymentMethodDetails)
690+
}
691+
692+
func test_fetchCharge_properly_handles_error_response() throws {
693+
let remote = WCPayRemote(network: network)
694+
let chargeID = "ch_3KMVapErrorERROR"
695+
696+
network.simulateResponse(
697+
requestUrlSuffix: "payments/charges/\(chargeID)",
698+
filename: "wcpay-charge-error"
699+
)
700+
701+
let result: Result<WCPayCharge, Error> = waitFor { promise in
702+
remote.fetchCharge(for: self.sampleSiteID, chargeID: chargeID) { result in
703+
promise(result)
704+
}
705+
}
706+
707+
XCTAssertTrue(result.isFailure)
708+
}
664709
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"error": "wcpay_get_charge",
3+
"message": "Error: No such charge: 'ch_3KMVapErrorERROR'"
4+
}

0 commit comments

Comments
 (0)