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(core-flows,utils,medusa): fix bug where payment collection across orders were getting updated #8401

Merged
merged 7 commits into from
Aug 2, 2024
Merged
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
185 changes: 185 additions & 0 deletions integration-tests/http/__tests__/fixtures/order.ts
Original file line number Diff line number Diff line change
@@ -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: "[email protected]",
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
}
75 changes: 68 additions & 7 deletions integration-tests/http/__tests__/payment/admin/payment.spec.ts
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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(
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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",
})
)
})
},
})
11 changes: 11 additions & 0 deletions integration-tests/http/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
getLocaleAmount,
getStylizedAmount,
} from "../../../../../lib/money-amount-helpers"
import { getOrderPaymentStatus } from "../../../../../lib/order-helpers"

type OrderPaymentSectionProps = {
order: HttpTypes.AdminOrder
Expand All @@ -46,7 +47,7 @@ export const OrderPaymentSection = ({ order }: OrderPaymentSectionProps) => {

return (
<Container className="divide-y divide-dashed p-0">
<Header />
<Header order={order} />

<PaymentBreakdown
order={order}
Expand All @@ -60,12 +61,17 @@ export const OrderPaymentSection = ({ order }: OrderPaymentSectionProps) => {
)
}

const Header = () => {
const Header = ({ order }) => {
const { t } = useTranslation()
const { label, color } = getOrderPaymentStatus(t, order.payment_status)

return (
<div className="flex items-center justify-between px-6 py-4">
<Heading level="h2">{t("orders.payment.title")}</Heading>

<StatusBadge color={color} className="text-nowrap">
{label}
</StatusBadge>
</div>
)
}
Expand Down
Loading
Loading