feat(dashboard,core-flows,js-sdk,types): ability to mark payment as paid (#8679)

* feat(core-flows): create or update payment collections in RMA flows

* chore: change ui to pick payment link from unpaid payment collection

* Apply suggestions from code review

Co-authored-by: Carlos R. L. Rodrigues <37986729+carlos-r-l-rodrigues@users.noreply.github.com>

* chore: fix mathbn

* feat(dashboard,core-flows,js-sdk,types): ability to mark payment as paid

* chore: add captured bt

---------

Co-authored-by: Carlos R. L. Rodrigues <37986729+carlos-r-l-rodrigues@users.noreply.github.com>
This commit is contained in:
Riqwan Thamir
2024-08-20 22:58:28 +02:00
committed by GitHub
parent 99eca64c20
commit 8bd284779e
12 changed files with 273 additions and 6 deletions

View File

@@ -41,6 +41,7 @@ export * from "./exchange/update-exchange-add-item"
export * from "./exchange/update-exchange-shipping-method"
export * from "./get-order-detail"
export * from "./get-orders-list"
export * from "./mark-payment-collection-as-paid"
export * from "./order-edit/begin-order-edit"
export * from "./order-edit/cancel-begin-order-edit"
export * from "./order-edit/confirm-order-edit-request"

View File

@@ -0,0 +1,79 @@
import { PaymentCollectionDTO } from "@medusajs/types"
import { MedusaError } from "@medusajs/utils"
import {
WorkflowData,
WorkflowResponse,
createStep,
createWorkflow,
} from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../common"
import { createPaymentSessionsWorkflow } from "../../definition"
import {
authorizePaymentSessionStep,
capturePaymentWorkflow,
} from "../../payment"
/**
* This step validates that the payment collection is not_paid
*/
export const throwUnlessPaymentCollectionNotPaid = createStep(
"validate-existing-payment-collection",
({ paymentCollection }: { paymentCollection: PaymentCollectionDTO }) => {
if (paymentCollection.status !== "not_paid") {
throw new MedusaError(
MedusaError.Types.NOT_ALLOWED,
`Can only mark 'not_paid' payment collection as paid`
)
}
}
)
const systemPaymentProviderId = "pp_system_default"
export const markPaymentCollectionAsPaidId = "mark-payment-collection-as-paid"
/**
* This workflow marks a payment collection for an order as paid.
*/
export const markPaymentCollectionAsPaid = createWorkflow(
markPaymentCollectionAsPaidId,
(
input: WorkflowData<{
payment_collection_id: string
order_id: string
captured_by?: string
}>
) => {
const paymentCollection = useRemoteQueryStep({
entry_point: "payment_collection",
fields: ["id", "status", "amount"],
variables: { id: input.payment_collection_id },
throw_if_key_not_found: true,
list: false,
})
throwUnlessPaymentCollectionNotPaid({ paymentCollection })
const paymentSession = createPaymentSessionsWorkflow.runAsStep({
input: {
payment_collection_id: paymentCollection.id,
provider_id: systemPaymentProviderId,
data: {},
context: {},
},
})
const payment = authorizePaymentSessionStep({
id: paymentSession.id,
context: { order_id: input.order_id },
})
capturePaymentWorkflow.runAsStep({
input: {
payment_id: payment.id,
captured_by: input.captured_by,
amount: paymentCollection.amount,
},
})
return new WorkflowResponse(payment)
}
)