Skip to content

Commit

Permalink
Merge pull request #2284 from wordpress-mobile/woo/simple-payments-cl…
Browse files Browse the repository at this point in the history
…eanup-p2

Woo/simple payments cleanup p2
  • Loading branch information
hichamboushaba authored Feb 11, 2022
2 parents 553be24 + 1344ccf commit 39d1d0c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -552,17 +552,6 @@ class MockedStack_WCOrdersTest : MockedStack_Base() {
assertEquals(payload.error.type, OrderErrorType.INVALID_RESPONSE)
}

@Test
fun testPostSimplePayment() = runBlocking {
interceptor.respondWith("wc-fetch-order-response-success.json")
val response = orderRestClient.postSimplePayment(siteModel, "10.00", isTaxable = true)

with(response) {
assertNull(error)
assertNotNull(order)
}
}

@Suppress("unused")
@Subscribe
fun onAction(action: Action<*>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ package org.wordpress.android.fluxc.network.rest.wpcom.wc.order

import android.content.Context
import com.android.volley.RequestQueue
import com.google.gson.JsonArray
import com.google.gson.JsonElement
import com.google.gson.JsonObject
import com.google.gson.reflect.TypeToken
import org.wordpress.android.fluxc.Dispatcher
import org.wordpress.android.fluxc.action.WCOrderAction
import org.wordpress.android.fluxc.generated.WCOrderActionBuilder
import org.wordpress.android.fluxc.generated.endpoint.WOOCOMMERCE
import org.wordpress.android.fluxc.model.LocalOrRemoteId.LocalId
import org.wordpress.android.fluxc.model.LocalOrRemoteId.RemoteId
import org.wordpress.android.fluxc.model.SiteModel
import org.wordpress.android.fluxc.model.WCOrderListDescriptor
Expand All @@ -21,7 +18,6 @@ import org.wordpress.android.fluxc.model.WCOrderShipmentProviderModel
import org.wordpress.android.fluxc.model.WCOrderShipmentTrackingModel
import org.wordpress.android.fluxc.model.WCOrderStatusModel
import org.wordpress.android.fluxc.model.WCOrderSummaryModel
import org.wordpress.android.fluxc.model.order.FeeLineTaxStatus
import org.wordpress.android.fluxc.model.order.UpdateOrderRequest
import org.wordpress.android.fluxc.network.BaseRequest.GenericErrorType
import org.wordpress.android.fluxc.network.UserAgent
Expand Down Expand Up @@ -483,51 +479,6 @@ class OrderRestClient @Inject constructor(
mapOf("shipping" to shipping, "billing" to billing)
)

/**
* @Deprecated("Use OrderUpdateStore.createSimplePayment instead")
* This function can be dropped once WCAndroid switches to OrderUpdateStore.createSimplePayment
*/
suspend fun postSimplePayment(site: SiteModel, amount: String, isTaxable: Boolean): RemoteOrderPayload {
val params = mapOf(
"fee_lines" to generateSimplePaymentFeeLineJson(amount, isTaxable),
"_fields" to ORDER_FIELDS
)

val url = WOOCOMMERCE.orders.pathV3
val response = jetpackTunnelGsonRequestBuilder.syncPostRequest(
this,
site,
url,
params,
OrderDto::class.java
)

return when (response) {
is JetpackSuccess -> {
response.data?.let {
val newModel = it.toDomainModel(localSiteId = site.localId())
RemoteOrderPayload(newModel, site)
} ?: RemoteOrderPayload(
OrderError(type = GENERIC_ERROR, message = "Success response with empty data"),
// We should update `RemoteOrderPayload` signature or change return type. This is a quick fix
// added for successful merge
WCOrderModel(localSiteId = LocalId(-1), remoteOrderId = RemoteId(-1)),
site
)
}
is JetpackError -> {
val orderError = networkErrorToOrderError(response.error)
RemoteOrderPayload(
orderError,
// We should update `RemoteOrderPayload` signature or change return type. This is a quick fix
// added for successful merge
WCOrderModel(localSiteId = LocalId(-1), remoteOrderId = RemoteId(-1)),
site
)
}
}
}

/**
* Makes a GET call to `/wc/v3/orders/<id>/notes` via the Jetpack tunnel (see [JetpackTunnelGsonRequest]),
* retrieving a list of notes for the given WooCommerce [SiteModel] and [WCOrderModel].
Expand Down Expand Up @@ -1024,22 +975,5 @@ class OrderRestClient @Inject constructor(
"tracking_number",
"tracking_provider"
).joinToString(separator = ",")

const val SIMPLE_PAYMENT_FEELINE_NAME = "Simple Payment"

fun generateSimplePaymentFeeLineJson(amount: String, isTaxable: Boolean, feeId: Long? = null): JsonArray {
val jsonFee = JsonObject().also { json ->
feeId?.let {
json.addProperty("id", it)
}
json.addProperty("name", SIMPLE_PAYMENT_FEELINE_NAME)
json.addProperty("total", amount)
json.addProperty(
"tax_status",
if (isTaxable) FeeLineTaxStatus.Taxable.value else FeeLineTaxStatus.None.value
)
}
return JsonArray().also { it.add(jsonFee) }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.wordpress.android.fluxc.store

import com.google.gson.JsonArray
import com.google.gson.JsonObject
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.FlowCollector
import org.wordpress.android.fluxc.model.LocalOrRemoteId.LocalId
Expand Down Expand Up @@ -154,7 +156,7 @@ class OrderUpdateStore @Inject internal constructor(
copy(
customerNote = customerNote,
billingEmail = billingEmail,
feeLines = OrderRestClient.generateSimplePaymentFeeLineJson(amount, isTaxable, feeId).toString()
feeLines = generateSimplePaymentFeeLineJson(amount, isTaxable, feeId).toString()
)
}
emit(UpdateOrderResult.OptimisticUpdateResult(OnOrderChanged()))
Expand Down Expand Up @@ -200,7 +202,7 @@ class OrderUpdateStore @Inject internal constructor(
* the passed information. Pass null for the feeId if this is a new fee line item, otherwise
* pass the id of an existing fee line item to replace it.
*/
fun generateSimplePaymentFeeLineList(
private fun generateSimplePaymentFeeLineList(
amount: String,
isTaxable: Boolean,
feeId: Long? = null
Expand All @@ -209,7 +211,7 @@ class OrderUpdateStore @Inject internal constructor(
feeId?.let {
feeLine.id = it
}
feeLine.name = OrderRestClient.SIMPLE_PAYMENT_FEELINE_NAME
feeLine.name = SIMPLE_PAYMENT_FEELINE_NAME
feeLine.total = amount
feeLine.taxStatus = if (isTaxable) FeeLineTaxStatus.Taxable else FeeLineTaxStatus.None
return listOf(feeLine)
Expand Down Expand Up @@ -377,4 +379,23 @@ class OrderUpdateStore @Inject internal constructor(
OnOrderChanged(orderError = OrderError(message = message))
))
}

companion object {
private const val SIMPLE_PAYMENT_FEELINE_NAME = "Simple Payment"

fun generateSimplePaymentFeeLineJson(amount: String, isTaxable: Boolean, feeId: Long? = null): JsonArray {
val jsonFee = JsonObject().also { json ->
feeId?.let {
json.addProperty("id", it)
}
json.addProperty("name", SIMPLE_PAYMENT_FEELINE_NAME)
json.addProperty("total", amount)
json.addProperty(
"tax_status",
if (isTaxable) FeeLineTaxStatus.Taxable.value else FeeLineTaxStatus.None.value
)
}
return JsonArray().also { it.add(jsonFee) }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -534,27 +534,6 @@ class WCOrderStore @Inject constructor(
}
}

/**
* @deprecated This function can be removed once the client is updated to use postSimplePayment
*/
@Deprecated("Use postSimplePayment instead")
suspend fun postQuickOrder(site: SiteModel, amount: String): OnQuickOrderResult {
return postSimplePayment(site, amount, false)
}

suspend fun postSimplePayment(site: SiteModel, amount: String, isTaxable: Boolean): OnQuickOrderResult {
return coroutineEngine.withDefaultContext(T.API, this, "postSimplePayment") {
val result = wcOrderRestClient.postSimplePayment(site, amount, isTaxable)

return@withDefaultContext if (result.isError) {
OnQuickOrderResult().also { it.error = result.error }
} else {
ordersDao.insertOrUpdateOrder(result.order)
OnQuickOrderResult(result.order)
}
}
}

suspend fun updateOrderStatus(
remoteOrderId: RemoteId,
site: SiteModel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ class OrderUpdateStoreTest {
fun `should create simple payment with correct amount and tax status`(): Unit = runBlocking {
// given
val newOrder = initialOrder.copy(
feeLines = OrderRestClient.generateSimplePaymentFeeLineJson(
feeLines = OrderUpdateStore.generateSimplePaymentFeeLineJson(
SIMPLE_PAYMENT_AMOUNT,
SIMPLE_PAYMENT_IS_TAXABLE,
SIMPLE_PAYMENT_FEE_ID
Expand Down Expand Up @@ -446,7 +446,7 @@ class OrderUpdateStoreTest {
val updatedOrder = initialOrder.copy(
customerNote = SIMPLE_PAYMENT_CUSTOMER_NOTE,
billingEmail = SIMPLE_PAYMENT_BILLING_EMAIL,
feeLines = OrderRestClient.generateSimplePaymentFeeLineJson(
feeLines = OrderUpdateStore.generateSimplePaymentFeeLineJson(
SIMPLE_PAYMENT_AMOUNT,
SIMPLE_PAYMENT_IS_TAXABLE,
SIMPLE_PAYMENT_FEE_ID
Expand Down

0 comments on commit 39d1d0c

Please sign in to comment.