fix(core-flows,utils,medusa): fix bug where payment collection across orders were getting updated (#8401)
This took embarrassingly long to debug. :| what: - fixes a bug where the payment collection of other orders were getting updated - adds order status to payments section <img width="1069" alt="Screenshot 2024-08-02 at 08 37 38" src="https://github.com/user-attachments/assets/31776bd3-e6b9-4d23-8be6-f972f7316cf3"> <img width="1072" alt="Screenshot 2024-08-02 at 08 37 48" src="https://github.com/user-attachments/assets/38cdd8a1-9f31-4920-91bf-a3554e298960">
This commit is contained in:
@@ -0,0 +1,185 @@
|
||||
import { adminHeaders } from "../../../helpers/create-admin-user"
|
||||
|
||||
export async function createOrderSeeder({ api }) {
|
||||
const region = (
|
||||
await api.post(
|
||||
"/admin/regions",
|
||||
{ name: "Test region", currency_code: "usd" },
|
||||
adminHeaders
|
||||
)
|
||||
).data.region
|
||||
|
||||
const salesChannel = (
|
||||
await api.post(
|
||||
"/admin/sales-channels",
|
||||
{ name: "first channel", description: "channel" },
|
||||
adminHeaders
|
||||
)
|
||||
).data.sales_channel
|
||||
|
||||
const stockLocation = (
|
||||
await api.post(
|
||||
`/admin/stock-locations`,
|
||||
{ name: "test location" },
|
||||
adminHeaders
|
||||
)
|
||||
).data.stock_location
|
||||
|
||||
const inventoryItem = (
|
||||
await api.post(
|
||||
`/admin/inventory-items`,
|
||||
{
|
||||
sku: `12345-${stockLocation.id}`,
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.inventory_item
|
||||
|
||||
await api.post(
|
||||
`/admin/inventory-items/${inventoryItem.id}/location-levels`,
|
||||
{
|
||||
location_id: stockLocation.id,
|
||||
stocked_quantity: 10,
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
await api.post(
|
||||
`/admin/stock-locations/${stockLocation.id}/sales-channels`,
|
||||
{ add: [salesChannel.id] },
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
const shippingProfile = (
|
||||
await api.post(
|
||||
`/admin/shipping-profiles`,
|
||||
{ name: `test-${stockLocation.id}`, type: "default" },
|
||||
adminHeaders
|
||||
)
|
||||
).data.shipping_profile
|
||||
|
||||
const product = (
|
||||
await api.post(
|
||||
"/admin/products",
|
||||
{
|
||||
title: `Test fixture ${shippingProfile.id}`,
|
||||
options: [
|
||||
{ title: "size", values: ["large", "small"] },
|
||||
{ title: "color", values: ["green"] },
|
||||
],
|
||||
variants: [
|
||||
{
|
||||
title: "Test variant",
|
||||
inventory_items: [
|
||||
{
|
||||
inventory_item_id: inventoryItem.id,
|
||||
required_quantity: 1,
|
||||
},
|
||||
],
|
||||
prices: [
|
||||
{
|
||||
currency_code: "usd",
|
||||
amount: 100,
|
||||
},
|
||||
],
|
||||
options: {
|
||||
size: "large",
|
||||
color: "green",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.product
|
||||
|
||||
const fulfillmentSets = (
|
||||
await api.post(
|
||||
`/admin/stock-locations/${stockLocation.id}/fulfillment-sets?fields=*fulfillment_sets`,
|
||||
{
|
||||
name: `Test-${shippingProfile.id}`,
|
||||
type: "test-type",
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.stock_location.fulfillment_sets
|
||||
|
||||
const fulfillmentSet = (
|
||||
await api.post(
|
||||
`/admin/fulfillment-sets/${fulfillmentSets[0].id}/service-zones`,
|
||||
{
|
||||
name: `Test-${shippingProfile.id}`,
|
||||
geo_zones: [{ type: "country", country_code: "us" }],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.fulfillment_set
|
||||
|
||||
await api.post(
|
||||
`/admin/stock-locations/${stockLocation.id}/fulfillment-providers`,
|
||||
{ add: ["manual_test-provider"] },
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
const shippingOption = (
|
||||
await api.post(
|
||||
`/admin/shipping-options`,
|
||||
{
|
||||
name: `Test shipping option ${fulfillmentSet.id}`,
|
||||
service_zone_id: fulfillmentSet.service_zones[0].id,
|
||||
shipping_profile_id: shippingProfile.id,
|
||||
provider_id: "manual_test-provider",
|
||||
price_type: "flat",
|
||||
type: {
|
||||
label: "Test type",
|
||||
description: "Test description",
|
||||
code: "test-code",
|
||||
},
|
||||
prices: [
|
||||
{ currency_code: "usd", amount: 1000 },
|
||||
{ region_id: region.id, amount: 1100 },
|
||||
],
|
||||
rules: [],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.shipping_option
|
||||
|
||||
const cart = (
|
||||
await api.post(`/store/carts`, {
|
||||
currency_code: "usd",
|
||||
email: "tony@stark-industries.com",
|
||||
shipping_address: {
|
||||
address_1: "test address 1",
|
||||
address_2: "test address 2",
|
||||
city: "ny",
|
||||
country_code: "us",
|
||||
province: "ny",
|
||||
postal_code: "94016",
|
||||
},
|
||||
sales_channel_id: salesChannel.id,
|
||||
items: [{ quantity: 1, variant_id: product.variants[0].id }],
|
||||
})
|
||||
).data.cart
|
||||
|
||||
const paymentCollection = (
|
||||
await api.post(`/store/payment-collections`, {
|
||||
cart_id: cart.id,
|
||||
region_id: region.id,
|
||||
currency_code: region.currency_code,
|
||||
amount: cart.total,
|
||||
})
|
||||
).data.payment_collection
|
||||
|
||||
await api.post(
|
||||
`/store/payment-collections/${paymentCollection.id}/payment-sessions`,
|
||||
{ provider_id: "pp_system_default" }
|
||||
)
|
||||
|
||||
let order = (await api.post(`/store/carts/${cart.id}/complete`, {})).data
|
||||
.order
|
||||
|
||||
order = (await api.get(`/admin/orders/${order.id}`, adminHeaders)).data.order
|
||||
|
||||
return order
|
||||
}
|
||||
@@ -1,9 +1,12 @@
|
||||
import { IPaymentModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
import { adminHeaders } from "../../../../helpers/create-admin-user"
|
||||
import { IPaymentModuleService } from "@medusajs/types"
|
||||
import { seedStorefrontDefaults } from "../../../../helpers/seed-storefront-defaults"
|
||||
import { setupTaxStructure } from "../../../../modules/__tests__/fixtures"
|
||||
|
||||
const { medusaIntegrationTestRunner } = require("medusa-test-utils")
|
||||
const { createAdminUser } = require("../../../../helpers/create-admin-user")
|
||||
import { medusaIntegrationTestRunner } from "medusa-test-utils"
|
||||
import { createAdminUser } from "../../../../helpers/create-admin-user"
|
||||
import { createOrderSeeder } from "../../fixtures/order"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
@@ -12,10 +15,12 @@ medusaIntegrationTestRunner({
|
||||
let paymentModule: IPaymentModuleService
|
||||
let paymentCollection
|
||||
let payment
|
||||
let container
|
||||
|
||||
beforeEach(async () => {
|
||||
paymentModule = getContainer().resolve(ModuleRegistrationName.PAYMENT)
|
||||
await createAdminUser(dbConnection, adminHeaders, getContainer())
|
||||
container = getContainer()
|
||||
paymentModule = container.resolve(ModuleRegistrationName.PAYMENT)
|
||||
await createAdminUser(dbConnection, adminHeaders, container)
|
||||
|
||||
const collection = (
|
||||
await api.post(
|
||||
@@ -51,7 +56,7 @@ medusaIntegrationTestRunner({
|
||||
payment = payments[0]
|
||||
})
|
||||
|
||||
it("Captures an authorized payment", async () => {
|
||||
it("should capture an authorized payment", async () => {
|
||||
const response = await api.post(
|
||||
`/admin/payments/${payment.id}/capture`,
|
||||
undefined,
|
||||
@@ -75,7 +80,7 @@ medusaIntegrationTestRunner({
|
||||
expect(response.status).toEqual(200)
|
||||
})
|
||||
|
||||
it("Refunds an captured payment", async () => {
|
||||
it("should refund a captured payment", async () => {
|
||||
await api.post(
|
||||
`/admin/payments/${payment.id}/capture`,
|
||||
undefined,
|
||||
@@ -116,5 +121,61 @@ medusaIntegrationTestRunner({
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should not update payment collection of other orders", async () => {
|
||||
await setupTaxStructure(container.resolve(ModuleRegistrationName.TAX))
|
||||
await seedStorefrontDefaults(container, "dkk")
|
||||
|
||||
let order1 = await createOrderSeeder({ api })
|
||||
|
||||
expect(order1).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
payment_status: "authorized",
|
||||
})
|
||||
)
|
||||
|
||||
const order1Payment = order1.payment_collections[0].payments[0]
|
||||
|
||||
const result = await api.post(
|
||||
`/admin/payments/${order1Payment.id}/capture?fields=*payment_collection`,
|
||||
{
|
||||
amount: order1Payment.amount,
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
order1 = (await api.get(`/admin/orders/${order1.id}`, adminHeaders)).data
|
||||
.order
|
||||
|
||||
expect(order1).toEqual(
|
||||
expect.objectContaining({
|
||||
id: order1.id,
|
||||
payment_status: "captured",
|
||||
})
|
||||
)
|
||||
|
||||
let order2 = await createOrderSeeder({ api })
|
||||
|
||||
order2 = (await api.get(`/admin/orders/${order2.id}`, adminHeaders)).data
|
||||
.order
|
||||
|
||||
expect(order2).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
payment_status: "authorized",
|
||||
})
|
||||
)
|
||||
|
||||
order1 = (await api.get(`/admin/orders/${order1.id}`, adminHeaders)).data
|
||||
.order
|
||||
|
||||
expect(order1).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
payment_status: "captured",
|
||||
})
|
||||
)
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
@@ -4,6 +4,17 @@ module.exports = {
|
||||
testEnvironment: `node`,
|
||||
rootDir: "./",
|
||||
transformIgnorePatterns: ["/dist", "/node_modules/"],
|
||||
testPathIgnorePatterns: [
|
||||
`/examples/`,
|
||||
`/www/`,
|
||||
`/dist/`,
|
||||
`/node_modules/`,
|
||||
`<rootDir>/node_modules/`,
|
||||
`__tests__/fixtures`,
|
||||
`__testfixtures__`,
|
||||
`.cache`,
|
||||
"__fixtures__",
|
||||
],
|
||||
transform: {
|
||||
"^.+\\.[jt]s$": [
|
||||
"@swc/jest",
|
||||
|
||||
Reference in New Issue
Block a user