Skip to content

Commit

Permalink
feat: payment module optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
silenaker committed Sep 12, 2024
1 parent 2d17d42 commit 1129c87
Show file tree
Hide file tree
Showing 27 changed files with 1,629 additions and 1,109 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ build/**
**/stats
.favorites.json
.vscode
.history
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from "./create-carts"
export * from "./create-line-item-adjustments"
export * from "./create-line-items"
export * from "./create-shipping-method-adjustments"
export * from "./create-payment-collection"
export * from "./find-one-or-any-region"
export * from "./find-or-create-customer"
export * from "./find-sales-channel"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ interface StepInput {
provider_id: string
amount: BigNumberInput
currency_code: string
provider_token?: string
context?: PaymentProviderContext
data?: Record<string, unknown>
}

export const createPaymentSessionStepId = "create-payment-session"
Expand All @@ -27,9 +27,9 @@ export const createPaymentSessionStep = createStep(
input.payment_collection_id,
{
provider_id: input.provider_id,
provider_token: input.provider_token,
currency_code: input.currency_code,
amount: input.amount,
data: input.data ?? {},
context: input.context,
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ export const deletePaymentSessionsStep = createStep(
provider_id: paymentSession.provider_id,
currency_code: paymentSession.currency_code,
amount: paymentSession.amount,
data: paymentSession.data ?? {},
context: paymentSession.context,
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import { PaymentProviderContext, PaymentSessionDTO } from "@medusajs/types"
import {
BigNumberInput,
PaymentProviderContext,
PaymentSessionDTO,
} from "@medusajs/types"
import { MathBN } from "@medusajs/utils"
import {
WorkflowData,
WorkflowResponse,
createWorkflow,
parallelize,
transform,
} from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../common"
import { createPaymentSessionStep } from "../steps"
import { deletePaymentSessionsWorkflow } from "./delete-payment-sessions"

interface WorkflowInput {
payment_collection_id: string
provider_id: string
provider_token?: string
data?: Record<string, unknown>
context?: PaymentProviderContext
amount?: BigNumberInput
}

export const createPaymentSessionsWorkflowId = "create-payment-sessions"
Expand All @@ -23,46 +28,36 @@ export const createPaymentSessionsWorkflow = createWorkflow(
(input: WorkflowData<WorkflowInput>): WorkflowResponse<PaymentSessionDTO> => {
const paymentCollection = useRemoteQueryStep({
entry_point: "payment_collection",
fields: ["id", "amount", "currency_code", "payment_sessions.*"],
fields: [
"id",
"raw_amount",
"raw_authorized_amount",
"currency_code",
"payment_sessions.*",
],
variables: { id: input.payment_collection_id },
list: false,
})

const paymentSessionInput = transform(
{ paymentCollection, input },
(data) => {
const balance = MathBN.sub(
data.paymentCollection.raw_amount,
data.paymentCollection.raw_authorized_amount || 0
)
return {
payment_collection_id: data.input.payment_collection_id,
provider_id: data.input.provider_id,
provider_token: data.input.provider_token,
data: data.input.data,
context: data.input.context,
amount: data.paymentCollection.amount,
amount: MathBN.min(data.input.amount || balance, balance),
currency_code: data.paymentCollection.currency_code,
}
}
)

const deletePaymentSessionInput = transform(
{ paymentCollection },
(data) => {
return {
ids:
data.paymentCollection?.payment_sessions?.map((ps) => ps.id) || [],
}
}
)

// Note: We are deleting an existing active session before creating a new one
// for a payment collection as we don't support split payments at the moment.
// When we are ready to accept split payments, this along with other workflows
// need to be handled correctly
const [created] = parallelize(
createPaymentSessionStep(paymentSessionInput),
deletePaymentSessionsWorkflow.runAsStep({
input: deletePaymentSessionInput,
})
)

return new WorkflowResponse(created)
return new WorkflowResponse(createPaymentSessionStep(paymentSessionInput))
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ export const capturePaymentStep = createStep(
ModuleRegistrationName.PAYMENT
)

const payment = await paymentModule.capturePayment(input)
const payment = await paymentModule.capturePayment(input.payment_id, {
amount: input.amount,
captured_by: input.captured_by,
})

return new StepResponse(payment)
}
Expand Down
7 changes: 5 additions & 2 deletions packages/core/core-flows/src/payment/steps/refund-payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { StepResponse, createStep } from "@medusajs/workflows-sdk"

type StepInput = {
payment_id: string
created_by?: string
refunded_by?: string
amount?: BigNumberInput
}

Expand All @@ -16,7 +16,10 @@ export const refundPaymentStep = createStep(
ModuleRegistrationName.PAYMENT
)

const payment = await paymentModule.refundPayment(input)
const payment = await paymentModule.refundPayment(input.payment_id, {
amount: input.amount,
refunded_by: input.refunded_by,
})

return new StepResponse(payment)
}
Expand Down
16 changes: 16 additions & 0 deletions packages/core/js-sdk/src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,22 @@ export class Store {
query,
})
},

addPaymentSession: async (
paymentCollectionId: string,
body: Record<string, any>,
query?: SelectParams,
headers?: ClientHeaders
) => {
return this.client.fetch<{
payment_collection: HttpTypes.StorePaymentCollection
}>(`/store/payment-collections/${paymentCollectionId}/payment-sessions`, {
method: "POST",
headers,
body,
query,
})
},
}

public order = {
Expand Down
13 changes: 9 additions & 4 deletions packages/core/types/src/payment/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@ import { BigNumberValue } from "../totals"
/* ********** PAYMENT COLLECTION ********** */

export type PaymentCollectionStatus =
| "not_paid"
| "awaiting"
| "pending"
| "paid"
| "partially_paid"
| "authorized"
| "partially_authorized"
| "canceled"
| "refunded"
| "partially_refunded"

export type PaymentSessionStatus =
| "authorized"
| "captured"
| "partially_captured"
| "refunded"
| "partially_refunded"
| "pending"
| "requires_more"
| "error"
| "canceled"
| "processing"

/**
* The payment collection details.
Expand Down
43 changes: 19 additions & 24 deletions packages/core/types/src/payment/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,6 @@ export interface CreateCaptureDTO {
*/
amount?: BigNumberInput

/**
* The associated payment's ID.
*/
payment_id: string

/**
* Who captured the payment. For example,
* a user's ID.
Expand All @@ -207,16 +202,11 @@ export interface CreateRefundDTO {
*/
amount?: BigNumberInput

/**
* The associated payment's ID.
*/
payment_id: string

/**
* Who refunded the payment. For example,
* a user's ID.
*/
created_by?: string
refunded_by?: string
}

/**
Expand All @@ -228,6 +218,11 @@ export interface CreatePaymentSessionDTO {
*/
provider_id: string

/**
* The provider's payment method token
*/
provider_token?: string

/**
* The ISO 3 character currency code of the payment session.
*/
Expand All @@ -238,11 +233,6 @@ export interface CreatePaymentSessionDTO {
*/
amount: BigNumberInput

/**
* Necessary data for the associated payment provider to process the payment.
*/
data: Record<string, unknown>

/**
* Necessary context data for the associated payment provider.
*/
Expand All @@ -259,26 +249,31 @@ export interface UpdatePaymentSessionDTO {
id: string

/**
* Necessary data for the associated payment provider to process the payment.
* The provider's payment method token
*/
data: Record<string, unknown>

/**
* The ISO 3 character currency code.
*/
currency_code: string
provider_token?: string

/**
* The amount to be authorized.
*/
amount: BigNumberInput
amount?: BigNumberInput

/**
* Necessary context data for the associated payment provider.
*/
context?: PaymentProviderContext
}

/**
* The attributes to authorize in a payment session.
*/
export interface AuthorizePaymentSessionDTO {
/**
* The provider token to authorize payment session
*/
provider_token?: string
}

/**
* The payment provider to be created.
*/
Expand Down
Loading

0 comments on commit 1129c87

Please sign in to comment.