Skip to content

Commit 41756a5

Browse files
authored
Merge pull request #8587 from woocommerce/feat/8586-payment-gateway-migration
REST API: Migrate payment gateway endpoints
2 parents 1f4fddf + cc8e878 commit 41756a5

File tree

8 files changed

+235
-7
lines changed

8 files changed

+235
-7
lines changed

Networking/Networking.xcodeproj/project.pbxproj

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -696,9 +696,9 @@
696696
DE42F9632967C8B900D514C2 /* ReportOrderTotalsMapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE42F9622967C8B900D514C2 /* ReportOrderTotalsMapperTests.swift */; };
697697
DE42F9652967F34400D514C2 /* refund-single-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DE42F9642967F34400D514C2 /* refund-single-without-data.json */; };
698698
DE42F9672967F61D00D514C2 /* refunds-all-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DE42F9662967F61D00D514C2 /* refunds-all-without-data.json */; };
699-
DE42F96F296BC9A700D514C2 /* countries-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DE42F96E296BC9A700D514C2 /* countries-without-data.json */; };
700699
DE42F96B296BC23800D514C2 /* customer-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DE42F96A296BC23800D514C2 /* customer-without-data.json */; };
701700
DE42F96D296BC67E00D514C2 /* wc-analytics-customers-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DE42F96C296BC67E00D514C2 /* wc-analytics-customers-without-data.json */; };
701+
DE42F96F296BC9A700D514C2 /* countries-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DE42F96E296BC9A700D514C2 /* countries-without-data.json */; };
702702
DE50295928C5BD0200551736 /* JetpackUser.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE50295828C5BD0200551736 /* JetpackUser.swift */; };
703703
DE50295B28C5F99700551736 /* DotcomUser.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE50295A28C5F99700551736 /* DotcomUser.swift */; };
704704
DE50295D28C6068B00551736 /* JetpackUserMapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE50295C28C6068B00551736 /* JetpackUserMapper.swift */; };
@@ -714,6 +714,8 @@
714714
DE97C3922861B8E20042E973 /* CouponEncoderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE97C3912861B8E20042E973 /* CouponEncoderTests.swift */; };
715715
DE9D6BCC270D769C00BA6562 /* shipping-label-address-without-name-validation-success.json in Resources */ = {isa = PBXBuildFile; fileRef = DE9D6BCB270D769B00BA6562 /* shipping-label-address-without-name-validation-success.json */; };
716716
DE9DEEF5291CF1B40070AD7C /* site-plugin-without-envelope.json in Resources */ = {isa = PBXBuildFile; fileRef = DE9DEEF4291CF1B40070AD7C /* site-plugin-without-envelope.json */; };
717+
DEA6B1C4296C0F45005AA5E9 /* payment-gateway-cod-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DEA6B1C3296C0F45005AA5E9 /* payment-gateway-cod-without-data.json */; };
718+
DEA6B1C6296C13FB005AA5E9 /* payment-gateway-list-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DEA6B1C5296C13FB005AA5E9 /* payment-gateway-list-without-data.json */; };
717719
DEC2961C26BBE764005A056B /* ShippingLabelCustomsForm.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEC2961B26BBE764005A056B /* ShippingLabelCustomsForm.swift */; };
718720
DEC51A95274CDA52009F3DF4 /* SitePluginMapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEC51A94274CDA52009F3DF4 /* SitePluginMapper.swift */; };
719721
DEC51A97274DD962009F3DF4 /* plugin.json in Resources */ = {isa = PBXBuildFile; fileRef = DEC51A96274DD962009F3DF4 /* plugin.json */; };
@@ -1510,9 +1512,9 @@
15101512
DE42F9622967C8B900D514C2 /* ReportOrderTotalsMapperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReportOrderTotalsMapperTests.swift; sourceTree = "<group>"; };
15111513
DE42F9642967F34400D514C2 /* refund-single-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "refund-single-without-data.json"; sourceTree = "<group>"; };
15121514
DE42F9662967F61D00D514C2 /* refunds-all-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "refunds-all-without-data.json"; sourceTree = "<group>"; };
1513-
DE42F96E296BC9A700D514C2 /* countries-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "countries-without-data.json"; sourceTree = "<group>"; };
15141515
DE42F96A296BC23800D514C2 /* customer-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "customer-without-data.json"; sourceTree = "<group>"; };
15151516
DE42F96C296BC67E00D514C2 /* wc-analytics-customers-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "wc-analytics-customers-without-data.json"; sourceTree = "<group>"; };
1517+
DE42F96E296BC9A700D514C2 /* countries-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "countries-without-data.json"; sourceTree = "<group>"; };
15161518
DE50295828C5BD0200551736 /* JetpackUser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JetpackUser.swift; sourceTree = "<group>"; };
15171519
DE50295A28C5F99700551736 /* DotcomUser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DotcomUser.swift; sourceTree = "<group>"; };
15181520
DE50295C28C6068B00551736 /* JetpackUserMapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JetpackUserMapper.swift; sourceTree = "<group>"; };
@@ -1528,6 +1530,8 @@
15281530
DE97C3912861B8E20042E973 /* CouponEncoderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CouponEncoderTests.swift; sourceTree = "<group>"; };
15291531
DE9D6BCB270D769B00BA6562 /* shipping-label-address-without-name-validation-success.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "shipping-label-address-without-name-validation-success.json"; sourceTree = "<group>"; };
15301532
DE9DEEF4291CF1B40070AD7C /* site-plugin-without-envelope.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "site-plugin-without-envelope.json"; sourceTree = "<group>"; };
1533+
DEA6B1C3296C0F45005AA5E9 /* payment-gateway-cod-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "payment-gateway-cod-without-data.json"; sourceTree = "<group>"; };
1534+
DEA6B1C5296C13FB005AA5E9 /* payment-gateway-list-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "payment-gateway-list-without-data.json"; sourceTree = "<group>"; };
15311535
DEC2961B26BBE764005A056B /* ShippingLabelCustomsForm.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShippingLabelCustomsForm.swift; sourceTree = "<group>"; };
15321536
DEC51A94274CDA52009F3DF4 /* SitePluginMapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SitePluginMapper.swift; sourceTree = "<group>"; };
15331537
DEC51A96274DD962009F3DF4 /* plugin.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = plugin.json; sourceTree = "<group>"; };
@@ -2133,6 +2137,8 @@
21332137
B559EBA820A0B5B100836CD4 /* Responses */ = {
21342138
isa = PBXGroup;
21352139
children = (
2140+
DEA6B1C5296C13FB005AA5E9 /* payment-gateway-list-without-data.json */,
2141+
DEA6B1C3296C0F45005AA5E9 /* payment-gateway-cod-without-data.json */,
21362142
DE42F96E296BC9A700D514C2 /* countries-without-data.json */,
21372143
DE42F96C296BC67E00D514C2 /* wc-analytics-customers-without-data.json */,
21382144
DE42F96A296BC23800D514C2 /* customer-without-data.json */,
@@ -2952,6 +2958,7 @@
29522958
CE20179320E3EFA7005B4C18 /* broken-orders.json in Resources */,
29532959
D8FBFF2722D529F2006E3336 /* order-stats-v4-month.json in Resources */,
29542960
02EF1672292F0D1900D90AD6 /* load-plan-success.json in Resources */,
2961+
DEA6B1C6296C13FB005AA5E9 /* payment-gateway-list-without-data.json in Resources */,
29552962
2685C0DE263B5A4200D9EE97 /* add-on-groups.json in Resources */,
29562963
DEC51A9B274E3206009F3DF4 /* plugin-inactive.json in Resources */,
29572964
CCF48B382628AEAE0034EA83 /* shipping-label-account-settings-no-payment-methods.json in Resources */,
@@ -3099,6 +3106,7 @@
30993106
74159623224D2C86003C21CF /* settings-product.json in Resources */,
31003107
266C7F9225AD3C88006ED243 /* attribute-term.json in Resources */,
31013108
D865CE6E278CC19A002C8520 /* stripe-location.json in Resources */,
3109+
DEA6B1C4296C0F45005AA5E9 /* payment-gateway-cod-without-data.json in Resources */,
31023110
CEC4BF93234E7EE0008D9195 /* refunds-all.json in Resources */,
31033111
D8FBFF1522D3BE09006E3336 /* order-stats-v4-defaults.json in Resources */,
31043112
7495AACF225D366D00801A89 /* variation-as-product.json in Resources */,

Networking/Networking/Mapper/PaymentGatewayListMapper.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ struct PaymentGatewayListMapper: Mapper {
1717
decoder.userInfo = [
1818
.siteID: siteID,
1919
]
20-
return try decoder.decode(PaymentGatewayListEnvelope.self, from: response).paymentGateways
20+
do {
21+
return try decoder.decode(PaymentGatewayListEnvelope.self, from: response).paymentGateways
22+
} catch {
23+
return try decoder.decode([PaymentGateway].self, from: response)
24+
}
2125
}
2226
}
2327

Networking/Networking/Mapper/PaymentGatewayMapper.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ struct PaymentGatewayMapper: Mapper {
1717
decoder.userInfo = [
1818
.siteID: siteID,
1919
]
20-
return try decoder.decode(PaymentGatewayEnvelope.self, from: response).paymentGateway
20+
do {
21+
return try decoder.decode(PaymentGatewayEnvelope.self, from: response).paymentGateway
22+
} catch {
23+
return try decoder.decode(PaymentGateway.self, from: response)
24+
}
2125
}
2226
}
2327

Networking/Networking/Remote/PaymentGatewayRemote.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ public class PaymentGatewayRemote: Remote {
1111
/// - completion: Closure to be executed upon completion.
1212
///
1313
public func loadAllPaymentGateways(siteID: Int64, completion: @escaping (Result<[PaymentGateway], Error>) -> Void) {
14-
let request = JetpackRequest(wooApiVersion: .mark3, method: .get, siteID: siteID, path: Constants.path)
14+
let request = JetpackRequest(wooApiVersion: .mark3,
15+
method: .get,
16+
siteID: siteID,
17+
path: Constants.path,
18+
availableAsRESTRequest: true)
1519
let mapper = PaymentGatewayListMapper(siteID: siteID)
1620
enqueue(request, mapper: mapper, completion: completion)
1721
}
@@ -35,7 +39,8 @@ public class PaymentGatewayRemote: Remote {
3539
method: .put,
3640
siteID: siteID,
3741
path: path,
38-
parameters: parameters)
42+
parameters: parameters,
43+
availableAsRESTRequest: true)
3944

4045
let mapper = PaymentGatewayMapper(siteID: siteID)
4146

Networking/NetworkingTests/Mapper/PaymentGatewayListMapperTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ final class PaymentGatewayListMapperTests: XCTestCase {
1919
// Then
2020
assertEqual(expectedGateways, gateways)
2121
}
22+
23+
func test_payment_gateway_list_is_decoded_from_json_response_without_data_envelope() throws {
24+
// Given
25+
let jsonData = try XCTUnwrap(Loader.contentsOf("payment-gateway-list-without-data"))
26+
let expectedGateways = [Self.bankTransferGateway, Self.checkGateway, Self.cashGateway, Self.paypalGateway]
27+
28+
// When
29+
let gateways = try PaymentGatewayListMapper(siteID: Self.sampleSiteID).map(response: jsonData)
30+
31+
// Then
32+
assertEqual(expectedGateways, gateways)
33+
}
2234
}
2335

2436
// MARK: Private Helpers

Networking/NetworkingTests/Mapper/PaymentGatewayMapperTests.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@ final class PaymentGatewayMapperTests: XCTestCase {
3636

3737
XCTAssertEqual(paymentGateway, expectedPaymentGateway)
3838
}
39+
40+
/// Verifies that the fields are all parsed correctly
41+
///
42+
func test_PaymentGatewaysList_map_parses_all_fields_in_result_when_response_has_no_data_envelope() throws {
43+
let paymentGateway = try mapRetrievePaymentGatewayResponseWithoutDataEnvelope()
44+
45+
let expectedPaymentGateway = PaymentGateway(siteID: dummySiteID,
46+
gatewayID: "cod",
47+
title: "Cash on delivery",
48+
description: "Pay with cash upon delivery.",
49+
enabled: true,
50+
features: [.products],
51+
instructions: "Pay with cash upon delivery.")
52+
53+
XCTAssertEqual(paymentGateway, expectedPaymentGateway)
54+
}
3955
}
4056

4157

@@ -53,11 +69,17 @@ private extension PaymentGatewayMapperTests {
5369
return try PaymentGatewayMapper(siteID: dummySiteID).map(response: response)
5470
}
5571

56-
/// Returns the PaymentGatewayMapper output from `payment-gateway-cod.json`
72+
/// Returns the PaymentGateway output from `payment-gateway-cod.json`
5773
///
5874
func mapRetrievePaymentGatewayResponse() throws -> PaymentGateway {
5975
return try mapPaymentGateway(from: "payment-gateway-cod")
6076
}
6177

78+
/// Returns the PaymentGateway output from `payment-gateway-cod-without-data.json`
79+
///
80+
func mapRetrievePaymentGatewayResponseWithoutDataEnvelope() throws -> PaymentGateway {
81+
return try mapPaymentGateway(from: "payment-gateway-cod-without-data")
82+
}
83+
6284
struct FileNotFoundError: Error {}
6385
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"id": "cod",
3+
"title": "Cash on delivery",
4+
"description": "Pay with cash upon delivery.",
5+
"order": "",
6+
"enabled": true,
7+
"method_title": "Cash on delivery",
8+
"method_description": "Have your customers pay with cash (or by other means) upon delivery.",
9+
"method_supports": [
10+
"products"
11+
],
12+
"settings": {
13+
"title": {
14+
"id": "title",
15+
"label": "Title",
16+
"description": "Payment method description that the customer will see on your checkout.",
17+
"type": "safe_text",
18+
"value": "Cash on delivery",
19+
"default": "Cash on delivery",
20+
"tip": "Payment method description that the customer will see on your checkout.",
21+
"placeholder": ""
22+
},
23+
"instructions": {
24+
"id": "instructions",
25+
"label": "Instructions",
26+
"description": "Instructions that will be added to the thank you page.",
27+
"type": "textarea",
28+
"value": "Pay with cash upon delivery.",
29+
"default": "Pay with cash upon delivery.",
30+
"tip": "Instructions that will be added to the thank you page.",
31+
"placeholder": ""
32+
},
33+
"enable_for_methods": {
34+
"id": "enable_for_methods",
35+
"label": "Enable for shipping methods",
36+
"description": "If COD is only available for certain methods, set it up here. Leave blank to enable for all methods.",
37+
"type": "multiselect",
38+
"value": "",
39+
"default": "",
40+
"tip": "If COD is only available for certain methods, set it up here. Leave blank to enable for all methods.",
41+
"placeholder": "",
42+
"options": {
43+
"Flat rate": {
44+
"flat_rate": "Any &quot;Flat rate&quot; method",
45+
"flat_rate:1": "Other locations &ndash; Flat rate (#1)"
46+
},
47+
"Free shipping": {
48+
"free_shipping": "Any &quot;Free shipping&quot; method",
49+
"free_shipping:4": "Other locations &ndash; Free shipping (#4)"
50+
},
51+
"Local pickup": {
52+
"local_pickup": "Any &quot;Local pickup&quot; method"
53+
}
54+
}
55+
},
56+
"enable_for_virtual": {
57+
"id": "enable_for_virtual",
58+
"label": "Accept COD if the order is virtual",
59+
"description": "",
60+
"type": "checkbox",
61+
"value": "yes",
62+
"default": "yes",
63+
"tip": "",
64+
"placeholder": ""
65+
}
66+
}
67+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
[
2+
{
3+
"id": "bacs",
4+
"title": "Direct bank transfer",
5+
"description": "Make your payment directly into our bank account. Please use your Order ID as the payment reference. Your order will not be shipped until the funds have cleared in our account.",
6+
"order": "",
7+
"enabled": false,
8+
"method_title": "Direct bank transfer",
9+
"method_description": "Take payments in person via BACS. More commonly known as direct bank/wire transfer",
10+
"method_supports": [
11+
"products"
12+
]
13+
},
14+
{
15+
"id": "cheque",
16+
"title": "Check payments",
17+
"description": "Please send a check to Store Name, Store Street, Store Town, Store State / County, Store Postcode.",
18+
"order": "",
19+
"enabled": false,
20+
"method_title": "Check payments",
21+
"method_description": "Take payments in person via checks. This offline gateway can also be useful to test purchases.",
22+
"method_supports": [
23+
"products"
24+
]
25+
},
26+
{
27+
"id": "cod",
28+
"title": "Cash on delivery",
29+
"description": "Pay with cash upon delivery.",
30+
"order": "",
31+
"enabled": true,
32+
"method_title": "Cash on delivery",
33+
"method_description": "Have your customers pay with cash (or by other means) upon delivery.",
34+
"method_supports": [
35+
"products"
36+
],
37+
"settings": {
38+
"title": {
39+
"id": "title",
40+
"label": "Title",
41+
"description": "Payment method description that the customer will see on your checkout.",
42+
"type": "safe_text",
43+
"value": "Cash on delivery",
44+
"default": "Cash on delivery",
45+
"tip": "Payment method description that the customer will see on your checkout.",
46+
"placeholder": ""
47+
},
48+
"instructions": {
49+
"id": "instructions",
50+
"label": "Instructions",
51+
"description": "Instructions that will be added to the thank you page.",
52+
"type": "textarea",
53+
"value": "Pay with cash upon delivery.",
54+
"default": "Pay with cash upon delivery.",
55+
"tip": "Instructions that will be added to the thank you page.",
56+
"placeholder": ""
57+
},
58+
"enable_for_methods": {
59+
"id": "enable_for_methods",
60+
"label": "Enable for shipping methods",
61+
"description": "If COD is only available for certain methods, set it up here. Leave blank to enable for all methods.",
62+
"type": "multiselect",
63+
"value": "",
64+
"default": "",
65+
"tip": "If COD is only available for certain methods, set it up here. Leave blank to enable for all methods.",
66+
"placeholder": "",
67+
"options": {
68+
"Flat rate": {
69+
"flat_rate": "Any &quot;Flat rate&quot; method",
70+
"flat_rate:1": "Other locations &ndash; Flat rate (#1)"
71+
},
72+
"Free shipping": {
73+
"free_shipping": "Any &quot;Free shipping&quot; method",
74+
"free_shipping:4": "Other locations &ndash; Free shipping (#4)"
75+
},
76+
"Local pickup": {
77+
"local_pickup": "Any &quot;Local pickup&quot; method"
78+
}
79+
}
80+
},
81+
"enable_for_virtual": {
82+
"id": "enable_for_virtual",
83+
"label": "Accept COD if the order is virtual",
84+
"description": "",
85+
"type": "checkbox",
86+
"value": "yes",
87+
"default": "yes",
88+
"tip": "",
89+
"placeholder": ""
90+
}
91+
}
92+
},
93+
{
94+
"id": "paypal",
95+
"title": "PayPal",
96+
"description": "Pay via PayPal; you can pay with your credit card if you don't have a PayPal account.",
97+
"order": "",
98+
"enabled": false,
99+
"method_title": "PayPal Standard",
100+
"method_description": "PayPal Standard redirects customers to PayPal to enter their payment information.",
101+
"method_supports": [
102+
"products",
103+
"refunds"
104+
]
105+
}
106+
]

0 commit comments

Comments
 (0)