Skip to content
This repository was archived by the owner on Feb 4, 2025. It is now read-only.

Commit a2bee76

Browse files
authored
Merge pull request #2627 from wordpress-mobile/2625/payments-summary-endpoint
Add support for `/payments/transactions/summary` endpoint
2 parents 2c9c533 + d857e6d commit a2bee76

File tree

7 files changed

+141
-0
lines changed

7 files changed

+141
-0
lines changed

example/src/androidTest/java/org/wordpress/android/fluxc/mocked/MockedStack_WCInPersonPaymentsTest.kt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package org.wordpress.android.fluxc.mocked
22

33
import kotlinx.coroutines.runBlocking
4+
import org.hamcrest.CoreMatchers.instanceOf
45
import org.junit.Assert
56
import org.junit.Assert.assertEquals
7+
import org.junit.Assert.assertNotNull
68
import org.junit.Assert.assertNull
9+
import org.junit.Assert.assertThat
710
import org.junit.Assert.assertTrue
811
import org.junit.Test
912
import org.wordpress.android.fluxc.model.SiteModel
@@ -17,6 +20,7 @@ import org.wordpress.android.fluxc.model.payments.inperson.WCTerminalStoreLocati
1720
import org.wordpress.android.fluxc.model.payments.inperson.WCTerminalStoreLocationErrorType.MissingAddress
1821
import org.wordpress.android.fluxc.module.ResponseMockingInterceptor
1922
import org.wordpress.android.fluxc.network.rest.wpcom.wc.payments.inperson.InPersonPaymentsRestClient
23+
import org.wordpress.android.fluxc.store.WCInPersonPaymentsStore.InPersonPaymentsPluginType.STRIPE
2024
import org.wordpress.android.fluxc.store.WCInPersonPaymentsStore.InPersonPaymentsPluginType.WOOCOMMERCE_PAYMENTS
2125
import javax.inject.Inject
2226

@@ -215,4 +219,36 @@ class MockedStack_InPersonPaymentsTest : MockedStack_Base() {
215219
assertTrue(result.isError)
216220
assertTrue(result.error?.type is InvalidPostalCode)
217221
}
222+
223+
@Test
224+
fun whenFetchTransactionsSummaryDateParamShouldNotBeSent() = runBlocking {
225+
interceptor.respondWith("wc-pay-fetch-transactions-summary-response.json")
226+
227+
restClient.fetchTransactionsSummary(WOOCOMMERCE_PAYMENTS, testSite, null)
228+
229+
assertNull(interceptor.lastRequest?.url?.queryParameter("dateAfter"))
230+
}
231+
232+
@Test
233+
fun whenFetchTransactionsSummaryForSpecificTimeSlotDateParamShouldBeSent() = runBlocking {
234+
interceptor.respondWith("wc-pay-fetch-transactions-summary-response.json")
235+
236+
val dateAfterParam = "2023-01-01"
237+
restClient.fetchTransactionsSummary(WOOCOMMERCE_PAYMENTS, testSite, dateAfterParam)
238+
239+
assertTrue(interceptor.lastRequest!!.url.query!!.contains("\"date_after\":\"$dateAfterParam\""))
240+
}
241+
242+
@Test
243+
fun whenFetchTransactionsSummaryInvokedWithStripePluginShouldThrowException() = runBlocking {
244+
interceptor.respondWith("wc-pay-fetch-transactions-summary-response.json")
245+
try {
246+
restClient.fetchTransactionsSummary(STRIPE, testSite, null)
247+
} catch (e: IllegalStateException) {
248+
assertNotNull(e)
249+
assertThat(e, instanceOf(IllegalStateException::class.java))
250+
assertEquals("Stripe does not support fetching transactions summary", e.message)
251+
}
252+
Unit
253+
}
218254
}

example/src/androidTest/java/org/wordpress/android/fluxc/release/ReleaseStack_InPersonPaymentsWCPayTest.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import kotlinx.coroutines.runBlocking
44
import org.junit.Assert.assertEquals
55
import org.junit.Assert.assertFalse
66
import org.junit.Assert.assertTrue
7+
import org.junit.Assert.assertNotNull
78
import org.junit.Test
89
import org.wordpress.android.fluxc.example.test.BuildConfig
910
import org.wordpress.android.fluxc.model.payments.inperson.WCPaymentAccountResult.WCPaymentAccountStatus
@@ -77,4 +78,37 @@ class ReleaseStack_InPersonPaymentsWCPayTest : ReleaseStack_WCBase() {
7778
assertEquals("9969", result.asWooResult().model?.paymentMethodDetails?.cardDetails?.last4)
7879
assertEquals(4500, result.asWooResult().model?.amount)
7980
}
81+
82+
@Test
83+
fun givenSiteHasWCPayWhenTransactionSummaryInvokedSummaryIsReturned() = runBlocking {
84+
val result = store.fetchTransactionsSummary(WOOCOMMERCE_PAYMENTS, sSite)
85+
86+
assertFalse(result.isError)
87+
88+
val data = result.result
89+
assertNotNull(data)
90+
assertNotNull(data!!.transactionsCount)
91+
assertNotNull(data.currency)
92+
assertNotNull(data.net)
93+
assertNotNull(data.fees)
94+
assertNotNull(data.total)
95+
}
96+
97+
@Test
98+
fun givenSiteHasWCPayWhenTransactionSummaryInvokedWithParameterSummaryIsReturned() =
99+
runBlocking {
100+
val dateAfter = "2021-07-01"
101+
102+
val result = store.fetchTransactionsSummary(WOOCOMMERCE_PAYMENTS, sSite, dateAfter)
103+
104+
assertFalse(result.isError)
105+
106+
val data = result.result
107+
assertNotNull(data)
108+
assertNotNull(data!!.transactionsCount)
109+
assertNotNull(data.currency)
110+
assertNotNull(data.net)
111+
assertNotNull(data.fees)
112+
assertNotNull(data.total)
113+
}
80114
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"data": {
3+
"count": 7,
4+
"currency": "usd",
5+
"total": 2231720,
6+
"fees": 58095,
7+
"net": 2173625,
8+
"store_currencies": [
9+
"usd"
10+
],
11+
"customer_currencies": [
12+
"usd"
13+
]
14+
}
15+
}

fluxc-processor/src/main/resources/wc-wp-api-endpoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454

5555
/payments/terminal/locations/store
5656

57+
/payments/transactions/summary
58+
5759
/wc_stripe/account/summary
5860
/wc_stripe/terminal/locations/store
5961
/wc_stripe/orders/<order_id>/create_customer
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.wordpress.android.fluxc.model.payments.inperson
2+
3+
import com.google.gson.annotations.SerializedName
4+
5+
data class WCPaymentTransactionsSummaryResult(
6+
@SerializedName("count") val transactionsCount: Int,
7+
val currency: String,
8+
val total: Int,
9+
val fees: Int,
10+
val net: Int,
11+
@SerializedName("store_currencies") val storeCurrencies: List<String>?,
12+
@SerializedName("customer_currencies") val customerCurrencies: List<String>?,
13+
)

plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/network/rest/wpcom/wc/payments/inperson/InPersonPaymentsRestClient.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import org.wordpress.android.fluxc.model.payments.inperson.WCPaymentChargeApiRes
1515
import org.wordpress.android.fluxc.model.payments.inperson.WCTerminalStoreLocationError
1616
import org.wordpress.android.fluxc.model.payments.inperson.WCTerminalStoreLocationErrorType
1717
import org.wordpress.android.fluxc.model.payments.inperson.WCTerminalStoreLocationResult
18+
import org.wordpress.android.fluxc.model.payments.inperson.WCPaymentTransactionsSummaryResult
1819
import org.wordpress.android.fluxc.model.payments.inperson.WCTerminalStoreLocationResult.StoreAddress
1920
import org.wordpress.android.fluxc.network.BaseRequest.GenericErrorType
2021
import org.wordpress.android.fluxc.network.rest.wpapi.WPAPINetworkError
@@ -172,6 +173,27 @@ class InPersonPaymentsRestClient @Inject constructor(private val wooNetwork: Woo
172173
return response.toWooPayload()
173174
}
174175

176+
suspend fun fetchTransactionsSummary(
177+
activePlugin: InPersonPaymentsPluginType,
178+
site: SiteModel,
179+
dateAfter: String? = null,
180+
): WooPayload<WCPaymentTransactionsSummaryResult> {
181+
val url = when (activePlugin) {
182+
WOOCOMMERCE_PAYMENTS -> WOOCOMMERCE.payments.transactions.summary.pathV3
183+
STRIPE -> error("Stripe does not support fetching transactions summary")
184+
}
185+
val params = mutableMapOf<String, String>()
186+
dateAfter?.let { params["date_after"] = it }
187+
val response = wooNetwork.executeGetGsonRequest(
188+
site = site,
189+
path = url,
190+
params = params,
191+
clazz = WCPaymentTransactionsSummaryResult::class.java
192+
)
193+
194+
return response.toWooPayload()
195+
}
196+
175197
private fun mapToCapturePaymentError(error: WPAPINetworkError?, message: String): WCCapturePaymentError {
176198
val type = when {
177199
error == null -> GENERIC_ERROR

plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/store/WCInPersonPaymentsStore.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import org.wordpress.android.fluxc.model.payments.inperson.WCCapturePaymentRespo
55
import org.wordpress.android.fluxc.model.payments.inperson.WCConnectionTokenResult
66
import org.wordpress.android.fluxc.model.payments.inperson.WCPaymentAccountResult
77
import org.wordpress.android.fluxc.model.payments.inperson.WCPaymentChargeApiResult
8+
import org.wordpress.android.fluxc.model.payments.inperson.WCPaymentTransactionsSummaryResult
89
import org.wordpress.android.fluxc.model.payments.inperson.WCTerminalStoreLocationResult
910
import org.wordpress.android.fluxc.network.BaseRequest.GenericErrorType.UNKNOWN
1011
import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooError
@@ -79,6 +80,24 @@ class WCInPersonPaymentsStore @Inject constructor(
7980
}
8081
}
8182

83+
/**
84+
* Fetches a summary of IPP transactions made in a given period of time.
85+
* The method is available for stores using WCPay plugin.
86+
*
87+
* @param activePlugin The active IPP plugin [InPersonPaymentsPluginType].
88+
* @param site The site to fetch the transactions for.
89+
* @param dateAfter The start date of the period to fetch the transactions for, in ISO 8601 format YYYY-mm-DD.
90+
*/
91+
suspend fun fetchTransactionsSummary(
92+
activePlugin: InPersonPaymentsPluginType,
93+
site: SiteModel,
94+
dateAfter: String? = null
95+
): WooPayload<WCPaymentTransactionsSummaryResult> {
96+
return coroutineEngine.withDefaultContext(AppLog.T.API, this, "fetchTransactionsSummary") {
97+
restClient.fetchTransactionsSummary(activePlugin, site, dateAfter)
98+
}
99+
}
100+
82101
enum class InPersonPaymentsPluginType {
83102
WOOCOMMERCE_PAYMENTS,
84103
STRIPE

0 commit comments

Comments
 (0)