fix: allow swaps with total < 0 (#143)

This commit is contained in:
Sebastian Rindom authored and GitHub committed 2020-12-02 17:37:46 +01:00
1 parent 454504cdf4
commit 304431e7c3
5 files changed
+83 -8

No files matched your search

@@ -477,7 +477,7 @@ class BrightpearlService extends BaseService {
const soId = const soId =
fromSwap.metadata && fromSwap.metadata.brightpearl_sales_order_id fromSwap.metadata && fromSwap.metadata.brightpearl_sales_order_id
if (!soId) { if (!soId || fromSwap.amount_paid <= 0) {
return return
} }
@@ -558,6 +558,10 @@ class BrightpearlService extends BaseService {
const order = await client.orders.retrieve(salesOrderId) const order = await client.orders.retrieve(salesOrderId)
await client.warehouses.createReservation(order, this.options.warehouse) await client.warehouses.createReservation(order, this.options.warehouse)
const total = order.rows.reduce((acc, next) => {
return acc + parseFloat(next.net) + parseFloat(next.tax)
}, 0)
const paymentMethod = fromOrder.payment_method const paymentMethod = fromOrder.payment_method
const paymentType = "RECEIPT" const paymentType = "RECEIPT"
const payment = { const payment = {
@@ -566,7 +570,7 @@ class BrightpearlService extends BaseService {
paymentMethodCode: this.options.payment_method_code || "1220", paymentMethodCode: this.options.payment_method_code || "1220",
orderId: salesOrderId, orderId: salesOrderId,
currencyIsoCode: fromOrder.currency_code, currencyIsoCode: fromOrder.currency_code,
amountPaid: fromSwap.return.refund_amount, amountPaid: total,
paymentDate: new Date(), paymentDate: new Date(),
paymentType, paymentType,
} }
@@ -128,8 +128,8 @@ export default app => {
* Captures the payment associated with a swap * Captures the payment associated with a swap
*/ */
route.post( route.post(
"/:id/swaps/:swap_id/capture", "/:id/swaps/:swap_id/process-payment",
middlewares.wrap(require("./capture-swap").default) middlewares.wrap(require("./process-swap-payment").default)
) )
/** /**
@@ -7,7 +7,7 @@ export default async (req, res) => {
const order = await orderService.retrieve(id) const order = await orderService.retrieve(id)
await swapService.capturePayment(swap_id) await swapService.processDifference(swap_id)
// Decorate the order // Decorate the order
const data = await orderService.decorate( const data = await orderService.decorate(
+1
View File
@@ -25,6 +25,7 @@ class SwapModel extends BaseModel {
amount_paid: { type: Number }, amount_paid: { type: Number },
region_id: { type: String }, region_id: { type: String },
currency_code: { type: String }, currency_code: { type: String },
order_payment: { type: PaymentMethodSchema },
order_id: { type: String }, order_id: { type: String },
cart_id: { type: String }, cart_id: { type: String },
created: { type: String, default: Date.now }, created: { type: String, default: Date.now },
+73 -3
View File
@@ -12,6 +12,8 @@ class SwapService extends BaseService {
PAYMENT_COMPLETED: "swap.payment_completed", PAYMENT_COMPLETED: "swap.payment_completed",
PAYMENT_CAPTURED: "swap.payment_captured", PAYMENT_CAPTURED: "swap.payment_captured",
PAYMENT_CAPTURE_FAILED: "swap.payment_capture_failed", PAYMENT_CAPTURE_FAILED: "swap.payment_capture_failed",
PROCESS_REFUND_FAILED: "swap.process_refund_failed",
REFUND_PROCESSED: "swap.refund_processed",
} }
constructor({ constructor({
@@ -189,6 +191,7 @@ class SwapService extends BaseService {
return this.swapModel_.create({ return this.swapModel_.create({
order_id: order._id, order_id: order._id,
order_payment: order.payment_method,
region_id: order.region_id, region_id: order.region_id,
currency_code: order.currency_code, currency_code: order.currency_code,
return_items: validatedReturnItems, return_items: validatedReturnItems,
@@ -197,6 +200,70 @@ class SwapService extends BaseService {
}) })
} }
async processDifference(swapId) {
const swap = await this.retrieve(swapId)
if (!swap.is_paid) {
throw new MedusaError(
MedusaError.Types.NOT_ALLOWED,
"Cannot process a swap that hasn't been confirmed by the customer"
)
}
if (swap.amount_paid < 0) {
const { provider_id, data } = swap.order_payment
const paymentProvider = this.paymentProviderService_.retrieveProvider(
provider_id
)
try {
await paymentProvider.refundPayment(data, -1 * swap.amount_paid)
} catch (err) {
return this.swapModel_
.updateOne(
{
_id: swapId,
},
{
$set: { payment_status: "requires_action" },
}
)
.then(result => {
this.eventBus_.emit(
SwapService.Events.PROCESS_REFUND_FAILED,
result
)
return result
})
}
return this.swapModel_
.updateOne(
{
_id: swapId,
},
{
$set: { payment_status: "difference_refunded" },
}
)
.then(result => {
this.eventBus_.emit(SwapService.Events.REFUND_PROCESSED, result)
return result
})
} else if (swap.amount_paid === 0) {
return this.swapModel_.updateOne(
{
_id: swapId,
},
{
$set: { payment_status: "difference_refunded" },
}
)
}
return capturePayment(swapId)
}
async capturePayment(swapId) { async capturePayment(swapId) {
const swap = await this.retrieve(swapId) const swap = await this.retrieve(swapId)
@@ -351,6 +418,11 @@ class SwapService extends BaseService {
) )
} }
const total = await this.totalsService_.getTotal(cart)
let payment = {}
if (total > 0) {
let paymentSession = {} let paymentSession = {}
let paymentData = {} let paymentData = {}
const { payment_method, payment_sessions } = cart const { payment_method, payment_sessions } = cart
@@ -396,15 +468,13 @@ class SwapService extends BaseService {
paymentData = await paymentProvider.retrievePayment(paymentSession.data) paymentData = await paymentProvider.retrievePayment(paymentSession.data)
let payment = {}
if (paymentSession.provider_id) { if (paymentSession.provider_id) {
payment = { payment = {
provider_id: paymentSession.provider_id, provider_id: paymentSession.provider_id,
data: paymentData, data: paymentData,
} }
} }
}
const total = await this.totalsService_.getTotal(cart)
return this.swapModel_ return this.swapModel_
.updateOne( .updateOne(