Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update RefundRestClient to support partial refunds #3069

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,29 @@ import org.wordpress.android.fluxc.model.refunds.WCRefundModel.WCRefundFeeLine
import org.wordpress.android.fluxc.model.refunds.WCRefundModel.WCRefundShippingLine
import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooNetwork
import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooPayload
import org.wordpress.android.fluxc.utils.sumBy
import org.wordpress.android.fluxc.utils.toWooPayload
import javax.inject.Inject

class RefundRestClient @Inject constructor(private val wooNetwork: WooNetwork) {
suspend fun createRefundByAmount(
site: SiteModel,
orderId: Long,
amount: String,
reason: String,
automaticRefund: Boolean
): WooPayload<RefundResponse> {
val body = mapOf(
"amount" to amount,
"reason" to reason,
"api_refund" to automaticRefund.toString()
)
return createRefund(site, orderId, body)
}

@Suppress("LongParameterList")
suspend fun createRefundByItems(
suspend fun createRefund(
site: SiteModel,
orderId: Long,
amount: String,
reason: String,
automaticRefund: Boolean,
items: List<WCRefundModel.WCRefundItem>,
restockItems: Boolean
items: List<WCRefundModel.WCRefundItem>? = null,
restockItems: Boolean? = null
): WooPayload<RefundResponse> {
val body = mapOf(
"reason" to reason,
"amount" to items.sumBy { it.subtotal + it.totalTax }.toString(),
"api_refund" to automaticRefund.toString(),
"line_items" to items.associateBy { it.itemId },
"restock_items" to restockItems
val body = mutableMapOf<String, Any>(
"reason" to reason,
"amount" to amount,
"api_refund" to automaticRefund.toString()
)
items?.let { lineItems ->
body["line_items"] = lineItems.associateBy { it.itemId }
restockItems?.let { restock -> body["restock_items"] = restock }
}
return createRefund(site, orderId, body)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class WCRefundStore @Inject constructor(
autoRefund: Boolean = false
): WooResult<WCRefundModel> {
return coroutineEngine.withDefaultContext(AppLog.T.API, this, "createAmountRefund") {
val response = restClient.createRefundByAmount(
val response = restClient.createRefund(
site,
orderId,
amount.toString(),
Expand All @@ -53,19 +53,21 @@ class WCRefundStore @Inject constructor(
suspend fun createItemsRefund(
site: SiteModel,
orderId: Long,
amount: BigDecimal,
reason: String = "",
restockItems: Boolean = true,
autoRefund: Boolean = false,
items: List<WCRefundModel.WCRefundItem>
): WooResult<WCRefundModel> {
return coroutineEngine.withDefaultContext(AppLog.T.API, this, "createItemsRefund") {
val response = restClient.createRefundByItems(
site,
orderId,
reason,
autoRefund,
items,
restockItems
val response = restClient.createRefund(
site,
orderId,
amount.toString(),
reason,
autoRefund,
items,
restockItems
)
return@withDefaultContext when {
response.isError -> WooResult(response.error)
Expand Down
Loading