|
1
|
import crypto from "crypto" |
|
2
|
|
|
3
|
import { |
|
4
|
AuthorizePaymentInput, |
|
5
|
AuthorizePaymentOutput, |
|
6
|
CancelPaymentInput, |
|
7
|
CancelPaymentOutput, |
|
8
|
CapturePaymentInput, |
|
9
|
CapturePaymentOutput, |
|
10
|
CreateAccountHolderInput, |
|
11
|
CreateAccountHolderOutput, |
|
12
|
DeleteAccountHolderInput, |
|
13
|
DeleteAccountHolderOutput, |
|
14
|
DeletePaymentInput, |
|
15
|
DeletePaymentOutput, |
|
16
|
GetPaymentStatusInput, |
|
17
|
GetPaymentStatusOutput, |
|
18
|
InitiatePaymentInput, |
|
19
|
InitiatePaymentOutput, |
|
20
|
ProviderWebhookPayload, |
|
21
|
RefundPaymentInput, |
|
22
|
RefundPaymentOutput, |
|
23
|
RetrieveAccountHolderInput, |
|
24
|
RetrieveAccountHolderOutput, |
|
25
|
RetrievePaymentInput, |
|
26
|
RetrievePaymentOutput, |
|
27
|
UpdatePaymentInput, |
|
28
|
UpdatePaymentOutput, |
|
29
|
WebhookActionResult, |
|
30
|
} from "@medusajs/framework/types" |
|
31
|
import { |
|
32
|
AbstractPaymentProvider, |
|
33
|
MedusaError, |
|
34
|
PaymentActions, |
|
35
|
PaymentSessionStatus, |
|
36
|
} from "@medusajs/framework/utils" |
|
37
|
|
|
38
|
/** |
|
39
|
* Test payment provider used to exercise the account-holder flow in |
|
40
|
* createPaymentSessionsWorkflow. |
|
41
|
* |
|
42
|
* - createAccountHolder is idempotent: it returns the customer id, so repeated |
|
43
|
* inits for the same customer resolve to the same (provider_id, external_id), |
|
44
|
* mirroring a real provider (e.g. Stripe) whose create is keyed on the |
|
45
|
* customer. |
|
46
|
* - initiatePayment fails on demand (when the session data carries `fail: |
|
47
|
* true`), so a test can drive a workflow rollback after the account holder |
|
48
|
* has been created and linked. |
|
49
|
*/ |
|
50
|
export class AccountHolderPaymentProvider extends AbstractPaymentProvider { |
|
51
|
static identifier = "account-holder" |
|
52
|
|
|
53
|
constructor( |
|
54
|
cradle: Record<string, unknown>, |
|
55
|
config: Record<string, unknown> = {} |
|
56
|
) { |
|
57
|
super(cradle, config) |
|
58
|
} |
|
59
|
|
|
60
|
async getStatus(_): Promise<string> { |
|
61
|
return "authorized" |
|
62
|
} |
|
63
|
|
|
64
|
async getPaymentData(_): Promise<Record<string, unknown>> { |
|
65
|
return {} |
|
66
|
} |
|
67
|
|
|
68
|
async initiatePayment( |
|
69
|
input: InitiatePaymentInput |
|
70
|
): Promise<InitiatePaymentOutput> { |
|
71
|
if ((input.data as { fail?: boolean })?.fail) { |
|
72
|
throw new MedusaError( |
|
73
|
MedusaError.Types.UNEXPECTED_STATE, |
|
74
|
"Simulated payment session failure" |
|
75
|
) |
|
76
|
} |
|
77
|
|
|
78
|
return { data: {}, id: crypto.randomUUID() } |
|
79
|
} |
|
80
|
|
|
81
|
async getPaymentStatus( |
|
82
|
input: GetPaymentStatusInput |
|
83
|
): Promise<GetPaymentStatusOutput> { |
|
84
|
return { status: PaymentSessionStatus.AUTHORIZED } |
|
85
|
} |
|
86
|
|
|
87
|
async retrievePayment( |
|
88
|
input: RetrievePaymentInput |
|
89
|
): Promise<RetrievePaymentOutput> { |
|
90
|
return {} |
|
91
|
} |
|
92
|
|
|
93
|
async authorizePayment( |
|
94
|
input: AuthorizePaymentInput |
|
95
|
): Promise<AuthorizePaymentOutput> { |
|
96
|
return { data: {}, status: PaymentSessionStatus.AUTHORIZED } |
|
97
|
} |
|
98
|
|
|
99
|
async updatePayment(input: UpdatePaymentInput): Promise<UpdatePaymentOutput> { |
|
100
|
return { data: {} } |
|
101
|
} |
|
102
|
|
|
103
|
async deletePayment(input: DeletePaymentInput): Promise<DeletePaymentOutput> { |
|
104
|
return { data: {} } |
|
105
|
} |
|
106
|
|
|
107
|
async capturePayment( |
|
108
|
input: CapturePaymentInput |
|
109
|
): Promise<CapturePaymentOutput> { |
|
110
|
return { data: {} } |
|
111
|
} |
|
112
|
|
|
113
|
async retrieveAccountHolder( |
|
114
|
input: RetrieveAccountHolderInput |
|
115
|
): Promise<RetrieveAccountHolderOutput> { |
|
116
|
return { id: input.id } |
|
117
|
} |
|
118
|
|
|
119
|
async createAccountHolder( |
|
120
|
input: CreateAccountHolderInput |
|
121
|
): Promise<CreateAccountHolderOutput> { |
|
122
|
return { id: input.context.customer.id } |
|
123
|
} |
|
124
|
|
|
125
|
async deleteAccountHolder( |
|
126
|
input: DeleteAccountHolderInput |
|
127
|
): Promise<DeleteAccountHolderOutput> { |
|
128
|
return { data: {} } |
|
129
|
} |
|
130
|
|
|
131
|
async refundPayment(input: RefundPaymentInput): Promise<RefundPaymentOutput> { |
|
132
|
return { data: {} } |
|
133
|
} |
|
134
|
|
|
135
|
async cancelPayment(input: CancelPaymentInput): Promise<CancelPaymentOutput> { |
|
136
|
return { data: {} } |
|
137
|
} |
|
138
|
|
|
139
|
async getWebhookActionAndData( |
|
140
|
data: ProviderWebhookPayload["payload"] |
|
141
|
): Promise<WebhookActionResult> { |
|
142
|
return { action: PaymentActions.NOT_SUPPORTED } |
|
143
|
} |
|
144
|
} |