Skip to content

Commit

Permalink
🤖 Merge PR DefinitelyTyped#59034 Update BraintreeNode: Stronger types…
Browse files Browse the repository at this point in the history
… for search transactions function by @siquel

* Patch Transaction search

* Run prettier
  • Loading branch information
siquel authored Mar 22, 2022
1 parent 4e2a54f commit b7adfa4
Show file tree
Hide file tree
Showing 2 changed files with 195 additions and 4 deletions.
69 changes: 69 additions & 0 deletions types/braintree/braintree-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,75 @@ const gateway: BraintreeGateway = new braintree.BraintreeGateway({
escrowStatus === Transaction.EscrowStatus.Released;
escrowStatus === Transaction.EscrowStatus.Refunded;

// Assert overlap between source and static field
'Api' === Transaction.Source.Api;
'Recurring' === Transaction.Source.Recurring;
'ControlPanel' === Transaction.Source.ControlPanel;

// Assert overlap between created using and static field
'token' === Transaction.CreatedUsing.Token;
'full_information' === Transaction.CreatedUsing.FullInformation;

gateway.transaction.search(search => {
search.id().is('foo');
search.type().in([Transaction.Type.Sale, Transaction.Type.Credit]);
search.type().in(Transaction.Type.All());
search.createdUsing().is(Transaction.CreatedUsing.Token);
search.createdUsing().in([Transaction.CreatedUsing.Token, Transaction.CreatedUsing.FullInformation]);
search.source().is(Transaction.Source.Api);
search.source().in([
Transaction.Source.Api,
Transaction.Source.ControlPanel
]);

// Credit card
search.creditCardCardholderName().is("Patrick Smith");
search.creditCardExpirationDate().is("05/2012");
search.creditCardNumber().endsWith("1111");

// customer details
search.customerCompany().is("Braintree");
search.customerEmail().is("jen@example.com");
search.customerEmail().endsWith("example.com");
search.customerPhone().is("312.555.1234");
search.customerFax().is("614.555.5678");
search.customerWebsite().is("www.example.com");

// billing address
search.billingFirstName().is("Paul");
search.billingLastName().is("Smith");
search.billingCompany().is("Braintree");
search.billingStreetAddress().is("123 Main St");
search.billingExtendedAddress().is("Suite 123");
search.billingLocality().is("Chicago");
search.billingRegion().is("Illinois");
search.billingPostalCode().is("12345");
search.billingCountryName().is("United States of America");

search.orderId().is("myorder");
search.processorAuthorizationCode().is("123456");
search.paymentMethodToken().is("theToken");

search.creditCardCustomerLocation().is('US');
search.creditCardCustomerLocation().in(['US', 'International']);

search.creditcardCardType().in(CreditCard.CardType.All());
search.creditcardCardType().is(CreditCard.CardType.AmEx);

search.status().is(Transaction.Status.Authorized);
search.status().in([Transaction.Status.Authorized, Transaction.Status.Settled]);
search.status().in(Transaction.Status.All());

search.createdAt().min(new Date());
search.processorDeclinedAt().between(new Date(), new Date());
search.disputeDate().min(new Date());
search.voidedAt().max(new Date());
search.amount().between('100.00', '200.00');

search.refund().is(false);
search.refund().is(true);
});

// Cannot assign to var
await gateway.transaction
.cloneTransaction(id, { amount: '100.00', options: { submitForSettlement: true } })
Expand Down
130 changes: 126 additions & 4 deletions types/braintree/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,117 @@ declare namespace braintree {
Sandbox = 'Sandbox',
}

export type TextFieldSearchFn = () => {
is: (input: string) => void;
isNot: (input: string) => void;
startsWith: (input: string) => void;
endsWith: (input: string) => void;
contains: (input: string) => void;
};

export type MultiValueSearchFn<T> = () => {
is: (input: T) => void;
in: (input: T[]) => void;
};

export type RangeFieldSearchFn<T> = () => {
is: (input: T) => void;
/** Inclusive */
between: (lowerBound: T, upperBoundIncl: T) => void;
min: (minimum: T) => void;
max: (maximum: T) => void;
};

export type EqualitySearchFn<T> = () => {
is: (input: T) => void;
isNot: (input: T) => void;
};

export type PartialMatchSearchFn<T> = () => {
startsWith: (input: T) => void;
endsWith: (input: T) => void;
};

export type KeyValueSearchFn<T> = () => {
is: (input: T) => void;
};

export type TransactionSearchFn = (search: {
// text fields https://github.com/braintree/braintree_node/blob/master/lib/braintree/transaction_search.js#L9
billingCompany: TextFieldSearchFn;
billingCountryName: TextFieldSearchFn;
billingExtendedAddress: TextFieldSearchFn;
billingFirstName: TextFieldSearchFn;
billingLastName: TextFieldSearchFn;
billingLocality: TextFieldSearchFn;
billingPostalCode: TextFieldSearchFn;
billingRegion: TextFieldSearchFn;
billingStreetAddress: TextFieldSearchFn;
creditCardCardholderName: TextFieldSearchFn;
creditCardUniqueIdentifier: TextFieldSearchFn;
currency: TextFieldSearchFn;
customerCompany: TextFieldSearchFn;
customerEmail: TextFieldSearchFn;
customerFax: TextFieldSearchFn;
customerFirstName: TextFieldSearchFn;
customerId: TextFieldSearchFn;
customerLastName: TextFieldSearchFn;
customerPhone: TextFieldSearchFn;
customerWebsite: TextFieldSearchFn;
id: TextFieldSearchFn;
orderId: TextFieldSearchFn;
paymentMethodToken: TextFieldSearchFn;
paypalPayerEmail: TextFieldSearchFn;
paypalPaymentId: TextFieldSearchFn;
paypalAuthorizationId: TextFieldSearchFn;
processorAuthorizationCode: TextFieldSearchFn;
settlementBatchId: TextFieldSearchFn;
shippingCompany: TextFieldSearchFn;
shippingCountryName: TextFieldSearchFn;
shippingExtendedAddress: TextFieldSearchFn;
shippingFirstName: TextFieldSearchFn;
shippingLastName: TextFieldSearchFn;
shippingLocality: TextFieldSearchFn;
shippingPostalCode: TextFieldSearchFn;
shippingRegion: TextFieldSearchFn;
shippingStreetAddress: TextFieldSearchFn;
storeId: TextFieldSearchFn;

creditCardExpirationDate: EqualitySearchFn<string>;
creditCardNumber: PartialMatchSearchFn<string>;

createdUsing: MultiValueSearchFn<typeof Transaction.CreatedUsing[keyof typeof Transaction.CreatedUsing]>;
creditcardCardType: MultiValueSearchFn<
typeof CreditCard.CardType[keyof Omit<typeof CreditCard.CardType, 'All'>]
>;
creditCardCustomerLocation: MultiValueSearchFn<CustomerLocation>;

ids: MultiValueSearchFn<string>;
user: MultiValueSearchFn<string>;
paymentInstrumentType: MultiValueSearchFn<string>;
merchantAccountId: MultiValueSearchFn<string>;
status: MultiValueSearchFn<TransactionStatus>;
source: MultiValueSearchFn<TransactionSource | string>;
type: MultiValueSearchFn<typeof Transaction.Type[keyof Omit<typeof Transaction.Type, 'All'>]>;
storeIds: MultiValueSearchFn<string>;

refund: KeyValueSearchFn<boolean>;

// range fields
amount: RangeFieldSearchFn<string>;
authorizationExpiredAt: RangeFieldSearchFn<Date>;
authorizedAt: RangeFieldSearchFn<Date>;
createdAt: RangeFieldSearchFn<Date>;
disbursementDate: RangeFieldSearchFn<Date>;
disputeDate: RangeFieldSearchFn<Date>;
failedAt: RangeFieldSearchFn<Date>;
gatewayRejectedAt: RangeFieldSearchFn<Date>;
processorDeclinedAt: RangeFieldSearchFn<Date>;
settledAt: RangeFieldSearchFn<Date>;
submittedForSettlementAt: RangeFieldSearchFn<Date>;
voidedAt: RangeFieldSearchFn<Date>;
}) => void;

export type GatewayConfig = KeyGatewayConfig | ClientGatewayConfig | AccessTokenGatewayConfig;

export interface KeyGatewayConfig {
Expand Down Expand Up @@ -235,7 +346,7 @@ declare namespace braintree {
refund(transactionId: string, amount?: string): Promise<ValidatedResponse<Transaction>>;
releaseFromEscrow(transactionId: string): Promise<Transaction>;
sale(request: TransactionRequest): Promise<ValidatedResponse<Transaction>>;
search(searchFn: any): stream.Readable;
search(searchFn: TransactionSearchFn): stream.Readable;
submitForPartialSettlement(
authorizedTransactionId: string,
amount: string,
Expand Down Expand Up @@ -385,7 +496,7 @@ declare namespace braintree {
Switch: 'Switch';
Visa: 'Visa';
Unknown: 'Unknown';
All: () => string[];
All: () => Array<typeof CreditCard.CardType[keyof Omit<typeof CreditCard.CardType, 'All'>]>;
};

static CustomerLocation: {
Expand Down Expand Up @@ -1266,7 +1377,18 @@ declare namespace braintree {
static Type: {
Credit: 'credit';
Sale: 'sale';
All: () => string[];
All: () => Array<typeof Transaction.Type[keyof Omit<typeof Transaction.Type, 'All'>]>;
};

static Source: {
Api: 'Api';
ControlPanel: 'ControlPanel';
Recurring: 'Recurring';
};

static CreatedUsing: {
Token: 'token';
FullInformation: 'full_information';
};

static GatewayRejectionReason: {
Expand Down Expand Up @@ -1295,7 +1417,7 @@ declare namespace braintree {
SettlementPending: 'settlement_pending';
SubmittedForSettlement: 'submitted_for_settlement';
Voided: 'voided';
All: () => string[];
All: () => Array<typeof Transaction.Status[keyof Omit<typeof Transaction.Status, 'All'>]>;
};

addOns?: AddOn[] | undefined;
Expand Down

0 comments on commit b7adfa4

Please sign in to comment.