Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
silenaker committed Aug 29, 2024
1 parent 710ff38 commit 86395c1
Show file tree
Hide file tree
Showing 8 changed files with 290 additions and 270 deletions.
25 changes: 10 additions & 15 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,11 +202,6 @@ export interface CreateRefundDTO {
*/
amount?: BigNumberInput

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

/**
* Who refunded the payment. For example,
* a user's ID.
Expand Down Expand Up @@ -243,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 Down Expand Up @@ -284,6 +269,16 @@ export interface UpdatePaymentSessionDTO {
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
91 changes: 45 additions & 46 deletions packages/core/types/src/payment/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,36 +118,51 @@ export type UpdatePaymentProviderSession = {
* The ISO 3 character code of the payment session.
*/
currency_code: string

/*
* The payment method token
*/
token?: string
}

/**
* @interface
*
* The response of operations on a payment.
* The attributes to authorize a payment related to a payment session in a provider.
*/
export type PaymentProviderSessionResponse = {
export type AuthorizePaymentProviderSession = {
/**
* The data to be stored in the `data` field of the Payment Session to be created.
* The `data` field is useful to hold any data required by the third-party provider to process the payment or retrieve its details at a later point.
* A payment's context.
*/
context: PaymentProviderContext

/**
* The `data` field of the payment session.
*/
data: Record<string, unknown>

/*
* The payment method token
*/
token: string
}

/**
* @interface
*
* The successful result of authorizing a payment session using a payment provider.
* The response of operations on a payment.
*/
export type PaymentProviderAuthorizeResponse = {
export type PaymentProviderSessionResponse = {
/**
* The status of the payment, which will be stored in the payment session's `status` field.
*/
status: PaymentSessionStatus

/**
* The `data` to be stored in the payment session's `data` field.
* The data to be stored in the `data` field of the Payment Session to be created.
* The `data` field is useful to hold any data required by the third-party provider to process the payment or retrieve its details at a later point.
*/
data: PaymentProviderSessionResponse["data"]
data: Record<string, unknown>
}

/**
Expand Down Expand Up @@ -253,8 +268,7 @@ export interface IPaymentProvider {
* For example, in the Stripe provider, this method is used to create a Payment Intent for the customer.
*
* @param {CreatePaymentProviderSession} data - The data necessary to initiate the payment.
* @returns {Promise<PaymentProviderError | PaymentProviderSessionResponse>} Either the payment's data, which is stored in the `data` field
* of the payment session, or an error object.
* @returns {Promise<PaymentProviderError | PaymentProviderSessionResponse>} Either the payment's status and data or an error object.
*/
initiatePayment(
data: CreatePaymentProviderSession
Expand All @@ -263,22 +277,22 @@ export interface IPaymentProvider {
/**
* This method is used to update a payment associated with a session in the third-party provider.
*
* @param {UpdatePaymentProviderSession} context - The data related to the update.
* @returns {Promise<PaymentProviderError | PaymentProviderSessionResponse | void>} Either the payment's data or an error object.
* @param {UpdatePaymentProviderSession} data - The data related to the update.
* @returns {Promise<PaymentProviderError | PaymentProviderSessionResponse>} Either the payment's status and data or an error object.
*/
updatePayment(
context: UpdatePaymentProviderSession
data: UpdatePaymentProviderSession
): Promise<PaymentProviderError | PaymentProviderSessionResponse>

/**
* This method is called before a payment session is deleted. It's used to perform any actions necessary before the deletion.
*
* @param {Record<string, unknown>} paymentSessionData - The `data` field of the Payment Session.
* @returns {Promise<PaymentProviderError | PaymentProviderSessionResponse["data"]>} Either an error object or an empty object.
* @returns {Promise<PaymentProviderError | void>} Either an error object or null if successful.
*/
deletePayment(
paymentSessionData: Record<string, unknown>
): Promise<PaymentProviderError | PaymentProviderSessionResponse["data"]>
): Promise<PaymentProviderError | void>

/**
* This method is called when a payment session should be authorized.
Expand All @@ -287,15 +301,12 @@ export interface IPaymentProvider {
* Refer to [this guide](https://docs.medusajs.com/experimental/payment/payment-flow/#3-authorize-payment-session)
* to learn more about how this fits into the payment flow and how to handle required actions.
*
* @param {Record<string, unknown>} paymentSessionData - The `data` field of the payment session.
* @param {Record<string, unknown>} context - The context of the authorization.
* @returns {Promise<PaymentProviderError | PaymentProviderAuthorizeResponse>} The authorization details or an error object. If
* the authorization details are returned, the `data` and `status` field are set in the associated payment session.
* @param {AuthorizePaymentProviderSession} data - The data related to authorize.
* @returns {Promise<PaymentProviderError | PaymentProviderSessionResponse>} Either the payment's status and data or an error object.
*/
authorizePayment(
paymentSessionData: Record<string, unknown>,
context: Record<string, unknown>
): Promise<PaymentProviderError | PaymentProviderAuthorizeResponse>
data: AuthorizePaymentProviderSession
): Promise<PaymentProviderError | PaymentProviderSessionResponse>

/**
* This method is called when a payment should be captured. The payment is captured in one of the following scenarios:
Expand All @@ -306,64 +317,52 @@ export interface IPaymentProvider {
*
* In this method, you can interact with the third-party provider and perform any actions necessary to capture the payment.
*
* @param {Record<string, unknown>} paymentSessionData - The `data` field of the payment.
* @param {Record<string, unknown>} paymentSessionData - The `data` field of the Payment Session.
* @param {BigNumberInput} captureAmount - The amount to capture.
* @returns {Promise<PaymentProviderError | PaymentProviderSessionResponse["data"]>} Either an error object or a value that's stored in the `data` field of the payment capture.
* @returns {Promise<PaymentProviderError | PaymentProviderSessionResponse>} Either the payment's status and data or an error object.
*/
capturePayment(
paymentSessionData: Record<string, unknown>,
captureAmount?: BigNumberInput
): Promise<PaymentProviderError | PaymentProviderSessionResponse["data"]>
): Promise<PaymentProviderError | PaymentProviderSessionResponse>

/**
* This method is called when a payment should be refunded. This is typically triggered manually by the merchant.
*
* In this method, you can interact with the third-party provider and perform any actions necessary to refund the payment.
*
* @param {Record<string, unknown>} paymentSessionData - The `data` field of a Payment.
* @param {Record<string, unknown>} paymentSessionData - The `data` field of the Payment Session.
* @param {BigNumberInput} refundAmount - The amount to refund.
* @returns {Promise<PaymentProviderError | PaymentProviderSessionResponse["data"]>} Either an error object or an object that's stored in the `data` field of the payment refund.
* @returns {Promise<PaymentProviderError | PaymentProviderSessionResponse>} Either the payment's status and data or an error object.
*/
refundPayment(
paymentSessionData: Record<string, unknown>,
refundAmount: BigNumberInput
): Promise<PaymentProviderError | PaymentProviderSessionResponse["data"]>
): Promise<PaymentProviderError | PaymentProviderSessionResponse>

/**
* This method is used to provide a uniform way of retrieving the payment information from the third-party provider.
*
* For example, in Stripe’s payment provider this method is used to retrieve the payment intent details from Stripe.
*
* @param {Record<string, unknown>} paymentSessionData -
* The `data` field of a payment session. Make sure to store in the `data` field any necessary data that would allow you to retrieve the payment data from the third-party provider.
* @returns {Promise<PaymentProviderError | PaymentProviderSessionResponse["session_data"]>} Either an error object or the payment's data retrieved from a third-party provider.
* @param {Record<string, unknown>} paymentSessionData - The `data` field of the Payment Session.
* @returns {Promise<PaymentProviderError | PaymentProviderSessionResponse>} Either the payment's status and data or an error object.
*/
retrievePayment(
paymentSessionData: Record<string, unknown>
): Promise<PaymentProviderError | PaymentProviderSessionResponse["data"]>
): Promise<PaymentProviderError | PaymentProviderSessionResponse>

/**
* This method is called when a payment is canceled.
*
* In this method, you can interact with the third-party provider and perform any actions necessary to cancel the payment.
*
* @param {Record<string, unknown>} paymentSessionData - The `data` field of the payment.
* @returns {Promise<PaymentProviderError | PaymentProviderSessionResponse["data"]>} Either an error object or a value that's stored in the `data` field of the payment.
* @param {Record<string, unknown>} paymentSessionData - The `data` field of the Payment Session.
* @returns {Promise<PaymentProviderError | PaymentProviderSessionResponse>} Either the payment's status and data or an error object.
*/
cancelPayment(
paymentSessionData: Record<string, unknown>
): Promise<PaymentProviderError | PaymentProviderSessionResponse["data"]>

/**
* This method is used to get the status of a payment or a payment session.
*
* @param {Record<string, unknown>} paymentSessionData -
* The `data` field of a payment as a parameter. You can use this data to interact with the third-party provider to check the status of the payment if necessary.
* @returns {Promise<PaymentSessionStatus>} The status of the payment or payment session.
*/
getPaymentStatus(
paymentSessionData: Record<string, unknown>
): Promise<PaymentSessionStatus>
): Promise<PaymentProviderError | PaymentProviderSessionResponse>

/**
* The method is called when a webhook event is received for this provider.
Expand Down
24 changes: 12 additions & 12 deletions packages/core/types/src/payment/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
RefundDTO,
} from "./common"
import {
AuthorizePaymentSessionDTO,
CreateCaptureDTO,
CreatePaymentCollectionDTO,
CreatePaymentSessionDTO,
Expand Down Expand Up @@ -424,7 +425,7 @@ export interface IPaymentModuleService extends IModuleService {
* provider_id: "stripe",
* currency_code: "usd",
* amount: 3000,
* data: {},
* context: {},
* }
* )
*/
Expand Down Expand Up @@ -473,7 +474,7 @@ export interface IPaymentModuleService extends IModuleService {
* Learn more about the payment flow in [this guide](https://docs.medusajs.com/experimental/payment/payment-flow/)
*
* @param {string} id - The payment session's ID.
* @param {Record<string, unknown>} context - Context data to pass to the associated payment provider.
* @param {AuthorizePaymentSessionDTO} data - The attributes to authorize in a payment session.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<PaymentDTO>} The created payment.
*
Expand All @@ -486,7 +487,7 @@ export interface IPaymentModuleService extends IModuleService {
*/
authorizePaymentSession(
id: string,
context: Record<string, unknown>,
data?: AuthorizePaymentSessionDTO,
sharedContext?: Context
): Promise<PaymentDTO>

Expand Down Expand Up @@ -622,35 +623,34 @@ export interface IPaymentModuleService extends IModuleService {
*
* Learn more about the payment flow in [this guide](https://docs.medusajs.com/experimental/payment/payment-flow/)
*
* @param {string} paymentId - The ID of the payment to create the capture for.
* @param {CreateCaptureDTO} data - The payment capture to be created.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<PaymentDTO>} The payment's details.
*
* @example
* const payment = await paymentModuleService.capturePayment({
* payment_id: "pay_123",
* })
* const payment = await paymentModuleService.capturePayment("pay_123")
*/
capturePayment(
data: CreateCaptureDTO,
paymentId: string,
data?: CreateCaptureDTO,
sharedContext?: Context
): Promise<PaymentDTO>

/**
* This method refunds a payment using its associated payment provider. An amount can only be refunded if it has been captured first.
*
* @param {string} paymentId - The ID of the payment to create the refund for.
* @param {CreateRefundDTO} data - The refund to be created.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<PaymentDTO>} The payment's details.
*
* @example
* const payment = await paymentModuleService.refundPayment({
* payment_id: "pay_123",
* amount: 300,
* })
* const payment = await paymentModuleService.refundPayment("pay_123", { amount: 300 })
*/
refundPayment(
data: CreateRefundDTO,
paymentId: string,
data?: CreateRefundDTO,
sharedContext?: Context
): Promise<PaymentDTO>

Expand Down
31 changes: 10 additions & 21 deletions packages/core/utils/src/payment/abstract-payment-provider.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {
AuthorizePaymentProviderSession,
BigNumberInput,
CreatePaymentProviderSession,
IPaymentProvider,
MedusaContainer,
PaymentProviderError,
PaymentProviderSessionResponse,
PaymentSessionStatus,
ProviderWebhookPayload,
UpdatePaymentProviderSession,
WebhookActionResult,
Expand Down Expand Up @@ -218,46 +218,35 @@ export abstract class AbstractPaymentProvider<TConfig = Record<string, unknown>>
abstract capturePayment(
paymentSessionData: Record<string, unknown>,
captureAmount?: BigNumberInput
): Promise<PaymentProviderError | PaymentProviderSessionResponse["data"]>
): Promise<PaymentProviderError | PaymentProviderSessionResponse>

abstract authorizePayment(
paymentSessionData: Record<string, unknown>,
context: Record<string, unknown>
): Promise<
| PaymentProviderError
| {
status: PaymentSessionStatus
data: PaymentProviderSessionResponse["data"]
}
>
data: AuthorizePaymentProviderSession
): Promise<PaymentProviderError | PaymentProviderSessionResponse>

abstract cancelPayment(
paymentSessionData: Record<string, unknown>
): Promise<PaymentProviderError | PaymentProviderSessionResponse["data"]>
): Promise<PaymentProviderError | PaymentProviderSessionResponse>

abstract initiatePayment(
context: CreatePaymentProviderSession
data: CreatePaymentProviderSession
): Promise<PaymentProviderError | PaymentProviderSessionResponse>

abstract deletePayment(
paymentSessionData: Record<string, unknown>
): Promise<PaymentProviderError | PaymentProviderSessionResponse["data"]>

abstract getPaymentStatus(
paymentSessionData: Record<string, unknown>
): Promise<PaymentSessionStatus>
): Promise<PaymentProviderError | void>

abstract refundPayment(
paymentSessionData: Record<string, unknown>,
refundAmount: BigNumberInput
): Promise<PaymentProviderError | PaymentProviderSessionResponse["data"]>
): Promise<PaymentProviderError | PaymentProviderSessionResponse>

abstract retrievePayment(
paymentSessionData: Record<string, unknown>
): Promise<PaymentProviderError | PaymentProviderSessionResponse["data"]>
): Promise<PaymentProviderError | PaymentProviderSessionResponse>

abstract updatePayment(
context: UpdatePaymentProviderSession
data: UpdatePaymentProviderSession
): Promise<PaymentProviderError | PaymentProviderSessionResponse>

abstract getWebhookActionAndData(
Expand Down
Loading

0 comments on commit 86395c1

Please sign in to comment.