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

fix(js-sdk, medusa): Allow users to change their mind during the checkout process #10671

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
28 changes: 13 additions & 15 deletions packages/core/js-sdk/src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -960,22 +960,20 @@ export class Store {
query?: SelectParams,
headers?: ClientHeaders
) => {
let paymentCollectionId = (cart as any).payment_collection?.id
if (!paymentCollectionId) {
const collectionBody = {
cart_id: cart.id,
}
paymentCollectionId = (
await this.client.fetch<HttpTypes.StorePaymentCollectionResponse>(
`/store/payment-collections`,
{
method: "POST",
headers,
body: collectionBody,
}
)
).payment_collection.id
const collectionBody = {
cart_id: cart.id,
}
const paymentCollectionId = (
await this.client.fetch<HttpTypes.StorePaymentCollectionResponse>(
`/store/payment-collections`,
{
method: "POST",
headers,
body: collectionBody,
}
)
).payment_collection.id
Comment on lines +967 to +975
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sugg: you can return this function directly and remove the Get from underneath

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are they equivalent though? One is a payment collection the other is a payment session.

Before my changes they were still called together but only when the payment collection id was not defined.



return this.client.fetch<HttpTypes.StorePaymentCollectionResponse>(
`/store/payment-collections/${paymentCollectionId}/payment-sessions`,
Expand Down
41 changes: 21 additions & 20 deletions packages/medusa/src/api/store/payment-collections/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createPaymentCollectionForCartWorkflow } from "@medusajs/core-flows"
import { createPaymentCollectionForCartWorkflow, refreshPaymentCollectionForCartWorkflow } from "@medusajs/core-flows"
import {
ContainerRegistrationKeys,
remoteQueryObjectFromString,
Expand All @@ -9,41 +9,42 @@ import {
} from "@medusajs/framework/http"
import { HttpTypes } from "@medusajs/framework/types"

export const POST = async (
req: AuthenticatedMedusaRequest<HttpTypes.StoreCreatePaymentCollection>,
res: MedusaResponse<HttpTypes.StorePaymentCollectionResponse>
) => {
const getPaymentCollectionForCart = async (req: AuthenticatedMedusaRequest<HttpTypes.StoreCreatePaymentCollection>) => {
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
const { cart_id } = req.body

// We can potentially refactor the workflow to behave more like an upsert and return an existing collection if there is one.
const [cartCollectionRelation] = await remoteQuery(
remoteQueryObjectFromString({
entryPoint: "cart_payment_collection",
variables: { filters: { cart_id } },
fields: req.remoteQueryConfig.fields.map(
(f) => `payment_collection.${f}`
),
) as any,
})
)
let paymentCollection = cartCollectionRelation?.payment_collection
return cartCollectionRelation?.payment_collection
}

export const POST = async (
req: AuthenticatedMedusaRequest<HttpTypes.StoreCreatePaymentCollection>,
res: MedusaResponse<HttpTypes.StorePaymentCollectionResponse>
) => {
const { cart_id } = req.body

// We can potentially refactor the workflow to behave more like an upsert and return an existing collection if there is one.

let paymentCollection = await getPaymentCollectionForCart(req)
if (!paymentCollection) {
await createPaymentCollectionForCartWorkflow(req.scope).run({
input: req.body,
})

const [cartCollectionRelation] = await remoteQuery(
remoteQueryObjectFromString({
entryPoint: "cart_payment_collection",
variables: { filters: { cart_id } },
fields: req.remoteQueryConfig.fields.map(
(f) => `payment_collection.${f}`
),
})
)
paymentCollection = cartCollectionRelation?.payment_collection
paymentCollection = await getPaymentCollectionForCart(req)
} else {
await refreshPaymentCollectionForCartWorkflow(req.scope).run({
input: { cart_id },
})
paymentCollection = await getPaymentCollectionForCart(req)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sugg: can you remove this function here and add this on line 48 instead:

const paymentCollection = await refetchEntity(
    "payment_collection",
    cartCollectionRelation?.payment_collection?.id,
    req.scope,
    req.remoteQueryConfig.fields
  )

}

res.status(200).json({ payment_collection: paymentCollection })
}
}