-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(payments): Refactor core Payment related
- Loading branch information
Showing
19 changed files
with
984 additions
and
492 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
export { default as BaseService } from "./base-service" | ||
export { default as FileService } from "./file-service" | ||
export { default as PaymentService } from "./payment-service" | ||
export { default as FulfillmentService } from "./fulfillment-service" | ||
export { default as FileService } from "./file-service" | ||
export { default as NotificationService } from "./notification-service" | ||
export { default as OauthService } from "./oauth-service" | ||
export { default as PaymentService } from "./payment-service" | ||
export { default as SearchService } from "./search-service" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { TransactionBaseService } from "./transaction-base-service" | ||
import { | ||
Cart, | ||
Customer, | ||
Payment, | ||
PaymentSession, | ||
PaymentSessionStatus, | ||
} from "../models" | ||
import { PaymentService } from "medusa-interfaces" | ||
|
||
export type Data = Record<string, unknown> | ||
export type PaymentData = Data | ||
export type PaymentSessionData = Data | ||
|
||
export interface PaymentService<T extends TransactionBaseService<never>> | ||
extends TransactionBaseService<T> { | ||
getIdentifier(): string | ||
|
||
getPaymentData(paymentSession: PaymentSession): Promise<PaymentData> | ||
|
||
updatePaymentData( | ||
paymentSessionData: PaymentSessionData, | ||
data: Data | ||
): Promise<PaymentSessionData> | ||
|
||
createPayment(cart: Cart): Promise<PaymentSessionData> | ||
|
||
retrievePayment(paymentData: PaymentData): Promise<Data> | ||
|
||
updatePayment( | ||
paymentSessionData: PaymentSessionData, | ||
cart: Cart | ||
): Promise<PaymentSessionData> | ||
|
||
authorizePayment( | ||
paymentSession: PaymentSession, | ||
context: Data | ||
): Promise<{ data: PaymentSessionData; status: PaymentSessionStatus }> | ||
|
||
capturePayment(payment: Payment): Promise<PaymentData> | ||
|
||
refundPayment(payment: Payment, refundAmount: number): Promise<PaymentData> | ||
|
||
cancelPayment(payment: Payment): Promise<PaymentData> | ||
|
||
deletePayment(paymentSession: PaymentSession): Promise<void> | ||
|
||
retrieveSavedMethods(customer: Customer): Promise<Data[]> | ||
|
||
getStatus( | ||
paymentSessionData: PaymentSessionData | ||
): Promise<PaymentSessionStatus> | ||
} | ||
|
||
export abstract class AbstractPaymentService< | ||
T extends TransactionBaseService<never> | ||
> | ||
extends TransactionBaseService<T> | ||
implements PaymentService<T> | ||
{ | ||
protected constructor(container: unknown, config?: Record<string, unknown>) { | ||
super(container, config) | ||
} | ||
|
||
protected static identifier: string | ||
|
||
public getIdentifier(): string { | ||
if (!(<typeof AbstractPaymentService>this.constructor).identifier) { | ||
throw new Error('Missing static property "identifier".') | ||
} | ||
return (<typeof AbstractPaymentService>this.constructor).identifier | ||
} | ||
|
||
public abstract getPaymentData( | ||
paymentSession: PaymentSession | ||
): Promise<PaymentData> | ||
|
||
public abstract updatePaymentData( | ||
paymentSessionData: PaymentSessionData, | ||
data: Data | ||
): Promise<PaymentSessionData> | ||
|
||
public abstract createPayment(cart: Cart): Promise<PaymentSessionData> | ||
|
||
public abstract retrievePayment(paymentData: PaymentData): Promise<Data> | ||
|
||
public abstract updatePayment( | ||
paymentSessionData: PaymentSessionData, | ||
cart: Cart | ||
): Promise<PaymentSessionData> | ||
|
||
public abstract authorizePayment( | ||
paymentSession: PaymentSession, | ||
context: Data | ||
): Promise<{ data: PaymentSessionData; status: PaymentSessionStatus }> | ||
|
||
public abstract capturePayment(payment: Payment): Promise<PaymentData> | ||
|
||
public abstract refundPayment( | ||
payment: Payment, | ||
refundAmount: number | ||
): Promise<PaymentData> | ||
|
||
public abstract cancelPayment(payment: Payment): Promise<PaymentData> | ||
|
||
public abstract deletePayment(paymentSession: PaymentSession): Promise<void> | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
public retrieveSavedMethods(customer: Customer): Promise<Data[]> { | ||
return Promise.resolve([]) | ||
} | ||
|
||
public abstract getStatus( | ||
paymentSessionData: PaymentSessionData | ||
): Promise<PaymentSessionStatus> | ||
} | ||
|
||
export function isPaymentService(obj: unknown): boolean { | ||
return obj instanceof AbstractPaymentService || obj instanceof PaymentService | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
export const testPayServiceMock = { | ||
identifier: "test-pay", | ||
getIdentifier: "test-pay", | ||
getStatus: jest.fn().mockResolvedValue(Promise.resolve("authorised")), | ||
retrieveSavedMethods: jest.fn().mockResolvedValue(Promise.resolve([])), | ||
getPaymentData: jest.fn().mockResolvedValue(Promise.resolve({})), | ||
createPayment: jest.fn().mockImplementation(() => { | ||
return {} | ||
}), | ||
retrievePayment: jest.fn().mockImplementation(() => { | ||
return {} | ||
}), | ||
updatePayment: jest.fn().mockImplementation(() => { | ||
return {} | ||
}), | ||
deletePayment: jest.fn().mockImplementation(() => { | ||
return {} | ||
}), | ||
authorizePayment: jest.fn().mockImplementation(() => { | ||
return {} | ||
}), | ||
updatePaymentData: jest.fn().mockImplementation(() => { | ||
return {} | ||
}), | ||
cancelPayment: jest.fn().mockImplementation(() => { | ||
return {} | ||
}), | ||
capturePayment: jest.fn().mockImplementation(() => { | ||
return {} | ||
}), | ||
refundPayment: jest.fn().mockImplementation(() => { | ||
return {} | ||
}) | ||
} | ||
|
||
const mock = jest.fn().mockImplementation(() => { | ||
return testPayServiceMock | ||
}) | ||
|
||
export default mock |
Oops, something went wrong.