Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit 73a2501

Browse files
authored
feat!: Constrained types for Payment endpoint. (#721)
BREAKING CHANGE: Types for data being passed to gateway.Order.Payment have been constrained further to help make it more clear what options are required by which payment gateway in the devs IDE. Each payment type is now part of a discriminated union the key property being `gateway`. This pass was done using the current documentation as the source of truth if anything is missing let us know.
1 parent 7dee771 commit 73a2501

File tree

2 files changed

+158
-8
lines changed

2 files changed

+158
-8
lines changed

src/types/order.d.ts

Lines changed: 149 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
} from './core'
1515
import { FormattedPrice, Price } from './price'
1616
import { ProductComponents } from './pcm'
17+
import { XOR } from './util'
1718

1819
export interface OrderAddressBase {
1920
first_name: string
@@ -191,14 +192,158 @@ export interface OrderItem extends Identifiable, OrderItemBase {
191192
catalog_source?: 'pim'
192193
}
193194

195+
export type PurchasePaymentMethod = 'purchase'
196+
export type AuthorizePaymentMethod = 'authorize'
197+
export type CapturePaymentMethod = 'capture'
198+
export type RefundPaymentMethod = 'refund'
199+
200+
export type PaymentMethod =
201+
| PurchasePaymentMethod
202+
| AuthorizePaymentMethod
203+
| CapturePaymentMethod
204+
| RefundPaymentMethod
205+
206+
interface PaymentBase {
207+
payment: string
208+
}
209+
210+
export interface AdyenPayment extends PaymentBase {
211+
method: PurchasePaymentMethod | AuthorizePaymentMethod | CapturePaymentMethod
212+
gateway: 'adyen'
213+
options: {
214+
shopper_reference: string
215+
recurring_processing_model?: string
216+
}
217+
}
218+
219+
export interface AuthorizeNetPayment extends PaymentBase {
220+
method: PurchasePaymentMethod | AuthorizePaymentMethod | CapturePaymentMethod
221+
gateway: 'authorize_net'
222+
options: {
223+
customer_payment_profile_id: string
224+
}
225+
}
226+
227+
/** Braintree Payment **/
228+
229+
type BraintreePaymentOptions = XOR<
230+
{ payment_method_nonce: true },
231+
{ payment_method_token: true }
232+
> & {
233+
custom_fields?: Record<string, string>
234+
}
235+
236+
export interface BraintreePayment extends PaymentBase {
237+
method: PurchasePaymentMethod | AuthorizePaymentMethod
238+
gateway: 'braintree'
239+
options?: BraintreePaymentOptions
240+
}
241+
242+
export interface CardConnectPayment extends PaymentBase {
243+
method:
244+
| PurchasePaymentMethod
245+
| AuthorizePaymentMethod
246+
| CapturePaymentMethod
247+
| RefundPaymentMethod
248+
gateway: 'card_connect'
249+
}
250+
251+
export interface CyberSourcePayment extends PaymentBase {
252+
method: PurchasePaymentMethod | AuthorizePaymentMethod
253+
gateway: 'cyber_source'
254+
options?: Record<string, string>
255+
}
256+
257+
export interface PayPalExpressCheckoutPayment extends PaymentBase {
258+
method: PurchasePaymentMethod | AuthorizePaymentMethod
259+
gateway: 'paypal_express_checkout'
260+
options?: {
261+
description?: string
262+
soft_descriptor?: string
263+
application_context?: {
264+
return_url?: string
265+
cancel_url?: string
266+
shipping_preference?: string
267+
landing_page?: 'LOGIN' | 'BILLING' | 'NO_PREFERENCE'
268+
locale?: string
269+
brand_name?: string
270+
}
271+
}
272+
}
273+
274+
/**
275+
* Stripe Payments
276+
*/
277+
278+
export type StripePaymentOptionBase = {
279+
idempotency_key?: string
280+
receipt_email?: string
281+
customer?: string
282+
}
283+
284+
export interface StripePaymentBase extends PaymentBase {
285+
amount?: number
286+
options?: StripePaymentOptionBase
287+
}
288+
289+
export interface StripePayment extends StripePaymentBase {
290+
method: PurchasePaymentMethod | AuthorizePaymentMethod | CapturePaymentMethod
291+
gateway: 'stripe'
292+
options?: StripePaymentOptionBase & {
293+
destination?: string
294+
}
295+
}
296+
297+
export interface StripeConnectPayment extends StripePaymentBase {
298+
method: PurchasePaymentMethod | AuthorizePaymentMethod
299+
gateway: 'stripe_connect'
300+
}
301+
302+
export interface StripeIntentsPayment extends StripePaymentBase {
303+
method: PurchasePaymentMethod | AuthorizePaymentMethod
304+
gateway: 'stripe_payment_intents'
305+
}
306+
307+
export interface ElasticPathStripePayment extends StripePaymentBase {
308+
method: PurchasePaymentMethod | AuthorizePaymentMethod
309+
gateway: 'elastic_path_payments_stripe'
310+
}
311+
312+
/**
313+
* Manual Payments
314+
*/
315+
316+
export interface ManualPayment extends PaymentBase {
317+
method: PurchasePaymentMethod | AuthorizePaymentMethod
318+
gateway: 'manual'
319+
amount?: number
320+
paymentmethod_meta?: {
321+
name?: string
322+
custom_reference?: string
323+
}
324+
}
325+
326+
export type PaymentRequestBody =
327+
| ManualPayment
328+
| ElasticPathStripePayment
329+
| StripeIntentsPayment
330+
| StripeConnectPayment
331+
| StripePayment
332+
| PayPalExpressCheckoutPayment
333+
| CyberSourcePayment
334+
| CardConnectPayment
335+
| BraintreePayment
336+
| AuthorizeNetPayment
337+
| AdyenPayment
338+
194339
export interface ConfirmPaymentBody {
195340
method: string
196341
gateway: string
197342
payment: string
198343
options?: {
199-
customer: string
200-
idempotency_key: string
201-
receipt_email: string
344+
customer?: string
345+
idempotency_key?: string
346+
receipt_email?: string
202347
}
203348
}
204349

@@ -314,7 +459,7 @@ export interface OrdersEndpoint
314459
* @param id - The UUID of the order that you want to authorize payment for.
315460
* @param body - The body of the order
316461
*/
317-
Payment(id: string, body: ConfirmPaymentBody): Promise<ConfirmPaymentResponse>
462+
Payment(id: string, body: PaymentRequestBody): Promise<ConfirmPaymentResponse>
318463

319464
/**
320465
* Update an Order
@@ -330,10 +475,6 @@ export interface OrdersEndpoint
330475
* anonymize an Order
331476
* Description: Anonymize order with the list of the ids.
332477
* DOCS: https://documentation.elasticpath.com/commerce-cloud/docs/api
333-
* @param id
334-
* @param body
335-
* @constructor
336478
*/
337-
338479
anonymize(ids: AnonymizeOrder): Promise<AnonymizeOrderResponse>
339480
}

src/types/util.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,12 @@ export type WithOptional<T, K extends keyof T> = Omit<T, K> &
33

44
export type WithRequired<T, K extends keyof T> = Pick<T, K> &
55
Partial<Omit<T, K>>
6+
7+
/**
8+
* XOR using conditional types
9+
* https://github.com/Microsoft/TypeScript/issues/14094#issuecomment-373782604
10+
*/
11+
export type Without<T, U> = { [P in Exclude<keyof T, keyof U>]?: never }
12+
export type XOR<T, U> = T | U extends object
13+
? (Without<T, U> & U) | (Without<U, T> & T)
14+
: T | U

0 commit comments

Comments
 (0)