-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
feat(dashboard,core-flows,js-sdk,types): ability to mark payment as paid #8679
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ae82591
feat(core-flows): create or update payment collections in RMA flows
riqwan 9fc3de0
Merge branch 'develop' into feat/create-or-update-payment-collection
riqwan 5f5297b
chore: change ui to pick payment link from unpaid payment collection
riqwan 7cbe480
Apply suggestions from code review
riqwan 71eb52a
chore: fix mathbn
riqwan 9ff69d9
feat(dashboard,core-flows,js-sdk,types): ability to mark payment as paid
riqwan 4ba7988
chore: add captured bt
riqwan 97ad6f9
chore: merge with the latest
riqwan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
packages/core/core-flows/src/order/workflows/mark-payment-collection-as-paid.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/medusa/src/api/admin/payment-collections/[id]/mark-as-paid/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { markPaymentCollectionAsPaid } from "@medusajs/core-flows" | ||
import { HttpTypes } from "@medusajs/types" | ||
import { | ||
AuthenticatedMedusaRequest, | ||
MedusaResponse, | ||
} from "../../../../../types/routing" | ||
import { refetchEntity } from "../../../../utils/refetch-entity" | ||
import { AdminMarkPaymentCollectionPaidType } from "../../validators" | ||
|
||
export const POST = async ( | ||
req: AuthenticatedMedusaRequest<AdminMarkPaymentCollectionPaidType>, | ||
res: MedusaResponse<HttpTypes.AdminPaymentCollectionResponse> | ||
) => { | ||
const { id } = req.params | ||
|
||
await markPaymentCollectionAsPaid(req.scope).run({ | ||
input: { | ||
...req.body, | ||
payment_collection_id: id, | ||
captured_by: req.auth_context.actor_id, | ||
}, | ||
}) | ||
|
||
const paymentCollection = await refetchEntity( | ||
"payment_collection", | ||
id, | ||
req.scope, | ||
req.remoteQueryConfig.fields | ||
) | ||
|
||
res.status(200).json({ payment_collection: paymentCollection }) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: Can we rely on this check? couldn't there be multiple unpaid collections?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no way to get in this situation through the UI as of yet. So not possible right now. But when we link the payment collection to the order change, we can get rid of this check