[braintree] fix search api to be a stream instead of a promise (#39253)

* [braintree] change search function to use streams
prettier formatting

* [braintree] modify tests to reflect the new types
update version in the comments

* [braintree] Fix linting problems (versions and spaces)
This commit is contained in:
Mohamed Elsharnouby
2019-10-22 16:29:32 -04:00
committed by Wesley Wigham
parent 690829caea
commit eb54fa643f
2 changed files with 157 additions and 65 deletions

View File

@@ -1,15 +1,26 @@
import braintree = require('braintree');
import { BraintreeGateway, Address, AddressCreateRequest,
CreditCard, Customer, PayPalAccount, ApplePayCard, AndroidPayCard,
VisaCheckoutCard, SamsungPayCard, MasterpassCard, PaymentMethod,
PaymentMethodNonce, Transaction
import {
BraintreeGateway,
Address,
AddressCreateRequest,
CreditCard,
Customer,
PayPalAccount,
ApplePayCard,
AndroidPayCard,
VisaCheckoutCard,
SamsungPayCard,
MasterpassCard,
PaymentMethod,
PaymentMethodNonce,
Transaction,
} from 'braintree';
/**
* Gateway
*/
const gateway: BraintreeGateway = new braintree.BraintreeGateway({
environment: 'Sandbox',
environment: braintree.Environment.Sandbox,
merchantId: 'abc123',
publicKey: 'def456',
privateKey: 'xyz789',
@@ -23,7 +34,8 @@ const gateway: BraintreeGateway = new braintree.BraintreeGateway({
customerId: '123456',
streetAddress: '222 Oak Street',
};
const response = await gateway.address.create(addressRequest);
const response = await gateway.address.create(addressRequest).catch(console.error);
if (!response) return;
const { id, customerId }: Address = response.address;
})();
@@ -32,13 +44,15 @@ const gateway: BraintreeGateway = new braintree.BraintreeGateway({
cardholderName: 'Johnny Dogood',
cvv: '123',
};
const response = await gateway.creditCard.update('abcdef', creditCardRequest);
const response = await gateway.creditCard.update('abcdef', creditCardRequest).catch(console.error);
if (!response) return;
const { bin, maskedNumber, last4 }: CreditCard = response.creditCard;
})();
(async () => {
const response: Customer = await gateway.customer.find('abcdef');
const { id, paymentMethods } = response;
const response = await gateway.customer.find('abcdef').catch(console.error);
if (!response) return;
const { id, paymentMethods }: Customer = response;
})();
(async () => {
@@ -46,7 +60,8 @@ const gateway: BraintreeGateway = new braintree.BraintreeGateway({
customerId: '123456',
paymentMethodNonce: 'i-am-a-nonce',
};
const response = await gateway.paymentMethod.create(paymentMethodRequest);
const response = await gateway.paymentMethod.create(paymentMethodRequest).catch(console.error);
if (!response) return;
const { token }: PaymentMethod = response.paymentMethod;
const applePayCard = <ApplePayCard> response.paymentMethod;
const paypalAccount = <PayPalAccount> response.paymentMethod;
@@ -59,21 +74,24 @@ const gateway: BraintreeGateway = new braintree.BraintreeGateway({
})();
(async () => {
const response = await gateway.paymentMethodNonce.create('token');
const nonce = response.paymentMethodNonce.nonce;
const response = await gateway.paymentMethodNonce.create('token').catch(console.error);
if (!response) return;
const nonce: PaymentMethodNonce = response.paymentMethodNonce;
})();
(async () => {
const transactionRequest = {
amount: '128.00',
};
const response = await gateway.transaction.sale(transactionRequest);
const response = await gateway.transaction.sale(transactionRequest).catch(console.error);
if (!response) return;
const { amount, billing, id }: Transaction = response.transaction;
// Cannot assign to var
await gateway.transaction.cloneTransaction(id, { amount: '100.00' , options: {submitForSettlement: true }});
await gateway.transaction
.cloneTransaction(id, { amount: '100.00', options: { submitForSettlement: true } })
.catch(console.error);
const transactions: Transaction[] = await gateway.transaction.search(() => {
return;
});
const transactions: Transaction[] = [];
gateway.transaction.search(() => true).on('data', transactions.push);
})();

View File

@@ -1,11 +1,15 @@
// Type definitions for braintree 2.16
// Type definitions for braintree 2.20
// Project: https://github.com/braintree/braintree_node
// Definitions by: Sam Rubin <https://github.com/smrubin>
// Definitions by: Sam Rubin <https://github.com/smrubin>,
// Mohamed Elsharnouby <https://github.com/sharno>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
// TypeScript Version: 3.6
/// <reference types="node" />
export = braintree;
export as namespace braintree;
import * as stream from 'stream';
declare namespace braintree {
/**
@@ -16,11 +20,11 @@ declare namespace braintree {
Development = 'Development',
Production = 'Production',
Qa = 'Qa',
Sandbox = 'Sandbox'
Sandbox = 'Sandbox',
}
export interface GatewayConfig {
environment: any;
environment: Environment;
merchantId: string;
publicKey: string;
privateKey: string;
@@ -78,7 +82,11 @@ declare namespace braintree {
create(request: AddressCreateRequest): Promise<ValidatedResponse<Address>>;
delete(customerId: string, addressId: string): Promise<void>;
find(customerId: string, addressId: string): Promise<Address>;
update(customerId: string, addressId: string, updates: AddressUpdateRequest): Promise<ValidatedResponse<Address>>;
update(
customerId: string,
addressId: string,
updates: AddressUpdateRequest,
): Promise<ValidatedResponse<Address>>;
}
interface ClientTokenGateway {
@@ -94,14 +102,14 @@ declare namespace braintree {
}
interface CreditCardVerificationGateway {
search(searchFn: any): Promise<CreditCardVerification[]>;
search(searchFn: any): stream.Readable;
}
interface CustomerGateway {
create(request: CustomerCreateRequest): Promise<ValidatedResponse<Customer>>;
delete(customerId: string): Promise<void>;
find(customerId: string): Promise<Customer>;
search(searchFn: any): Promise<Customer[]>;
search(searchFn: any): stream.Readable;
update(customerId: string, updates: CustomerUpdateRequest): Promise<ValidatedResponse<Customer>>;
}
@@ -111,19 +119,28 @@ declare namespace braintree {
interface DisputeGateway {
accept(disputeId: string): Promise<ValidatedResponse<Dispute>>;
addFileEvidence(disputeId: string, evidence: { documentId: string, category?: string }): Promise<ValidatedResponse<Evidence>>;
addTextEvidence(disputeId: string, evidence: { content: string, category?: string}): Promise<ValidatedResponse<Evidence>>;
addFileEvidence(
disputeId: string,
evidence: { documentId: string; category?: string },
): Promise<ValidatedResponse<Evidence>>;
addTextEvidence(
disputeId: string,
evidence: { content: string; category?: string },
): Promise<ValidatedResponse<Evidence>>;
finalize(disputeId: string): Promise<ValidatedResponse<Dispute>>;
find(disputeId: string): Promise<Dispute>;
removeEvidence(disputeId: string, evidenceId: string): Promise<ValidatedResponse<Dispute>>;
search(searchFn: any): Promise<Dispute[]>;
search(searchFn: any): stream.Readable;
}
interface MerchantAccountGateway {
all(): Promise<MerchantAccount[]>;
create(request: MerchantAccountCreateRequest): Promise<ValidatedResponse<MerchantAccount>>;
createForCurrency(currency: string, id?: string): Promise<ValidatedResponse<MerchantAccount>>;
update(merchantAccountId: string, updates: MerchantAccountUpdateRequest): Promise<ValidatedResponse<MerchantAccount>>;
update(
merchantAccountId: string,
updates: MerchantAccountUpdateRequest,
): Promise<ValidatedResponse<MerchantAccount>>;
find(merchantAccountId: string): Promise<MerchantAccount>;
}
@@ -131,34 +148,41 @@ declare namespace braintree {
create(request: PaymentMethodCreateRequest): Promise<ValidatedResponse<PaymentMethod>>;
delete(token: string): Promise<void>;
find(token: string): Promise<PaymentMethod>;
grant(sharedPaymentMethodToken: string, options: {allowVaulting?: boolean, includeBillingPostalCode?: boolean, revokeAfter?: Date }): Promise<Readonly<string>>;
grant(
sharedPaymentMethodToken: string,
options: { allowVaulting?: boolean; includeBillingPostalCode?: boolean; revokeAfter?: Date },
): Promise<Readonly<string>>;
revoke(sharedPaymentMethodToken: string): Promise<void>;
update(token: string, updates: PaymentMethodUpdateRequest): Promise<ValidatedResponse<PaymentMethod>>;
}
interface PaymentMethodNonceGateway {
interface PaymentMethodNonceGateway {
create(paymentMethodToken: string): Promise<ValidatedResponse<PaymentMethodNonce>>;
find(paymentMethodNonce: string): Promise<PaymentMethodNonce>;
}
interface PlanGateway {
interface PlanGateway {
all(): Promise<Plan[]>;
}
interface SettlementBatchSummaryGateway {
generate(request: {settlementDate: string, groupByCustomField?: string}): Promise<SettlementBatchSummary>;
interface SettlementBatchSummaryGateway {
generate(request: { settlementDate: string; groupByCustomField?: string }): Promise<SettlementBatchSummary>;
}
interface SubscriptionGateway {
interface SubscriptionGateway {
cancel(subscriptionId: string): Promise<void>;
create(request: SubscriptionRequest): Promise<ValidatedResponse<Subscription>>;
find(subscriptionId: string): Promise<Subscription>;
retryCharge(subscriptionId: string, amount?: string, submitForSettlement?: boolean): Promise<ValidatedResponse<Subscription>>;
search(searchFn: any): Promise<Subscription[]>;
retryCharge(
subscriptionId: string,
amount?: string,
submitForSettlement?: boolean,
): Promise<ValidatedResponse<Subscription>>;
search(searchFn: any): stream.Readable;
update(subscriptionId: string, updates: SubscriptionRequest): Promise<ValidatedResponse<Subscription>>;
}
interface TestingGateway {
interface TestingGateway {
settle(transactionId: string): Promise<ValidatedResponse<Transaction>>;
settlementConfirm(transactionId: string): Promise<ValidatedResponse<Transaction>>;
settlementDecline(transactionId: string): Promise<ValidatedResponse<Transaction>>;
@@ -166,21 +190,27 @@ declare namespace braintree {
settlementPending(transactionId: string): Promise<ValidatedResponse<Transaction>>;
}
interface TransactionGateway {
interface TransactionGateway {
cancelRelease(transactionId: string): Promise<void>;
cloneTransaction(transactionId: string, options: {amount: string, options: {submitForSettlement: boolean}}): Promise<void>;
cloneTransaction(
transactionId: string,
options: { amount: string; options: { submitForSettlement: boolean } },
): Promise<void>;
find(transactionId: string): Promise<Transaction>;
holdInEscrow(transactionId: string): Promise<Transaction>;
refund(transactionId: string, amount?: string): Promise<ValidatedResponse<Transaction>>;
releaseFromEscrow(transactionId: string): Promise<Transaction>;
sale(request: TransactionRequest): Promise<ValidatedResponse<Transaction>>;
search(searchFn: any): Promise<Transaction[]>;
submitForPartialSettlement(authorizedTransactionId: string, amount: string): Promise<ValidatedResponse<Transaction>>;
search(searchFn: any): stream.Readable;
submitForPartialSettlement(
authorizedTransactionId: string,
amount: string,
): Promise<ValidatedResponse<Transaction>>;
submitForSettlement(transactionId: string, amount?: string): Promise<ValidatedResponse<Transaction>>;
void(transactionId: string): Promise<ValidatedResponse<Transaction>>;
}
interface TransactionLineItemGateway {
interface TransactionLineItemGateway {
findAll(transactionId: string): Promise<TransactionLineItem[]>;
}
@@ -375,7 +405,7 @@ declare namespace braintree {
streetAddress?: string;
options?: {
updateExisting?: boolean;
}
};
};
cardholderName?: string;
cvv?: string;
@@ -665,8 +695,15 @@ declare namespace braintree {
*/
// Payment method is an instance of one of these types
export type PaymentMethod = AndroidPayCard | ApplePayCard | PayPalAccount | CreditCard | SamsungPayCard |
VenmoAccount | VisaCheckoutCard | MasterpassCard;
export type PaymentMethod =
| AndroidPayCard
| ApplePayCard
| PayPalAccount
| CreditCard
| SamsungPayCard
| VenmoAccount
| VisaCheckoutCard
| MasterpassCard;
export interface PaymentMethodCreateRequest {
billingAddress?: {
@@ -717,8 +754,8 @@ declare namespace braintree {
region?: string;
streetAddress?: string;
options?: {
updateExisting?: boolean
}
updateExisting?: boolean;
};
};
billingAddressId?: string;
cardholderName?: string;
@@ -768,7 +805,16 @@ declare namespace braintree {
lastTwo?: string;
}
export type PaymentMethodType = 'AndroidPayCard' | 'ApplePayCard' | 'CreditCard' |'MasterpassCard' | 'PayPalAccount' | 'UsBankAccount' | 'VenmoAccount' | 'VisaCheckoutCard' | 'SamsungPayCard';
export type PaymentMethodType =
| 'AndroidPayCard'
| 'ApplePayCard'
| 'CreditCard'
| 'MasterpassCard'
| 'PayPalAccount'
| 'UsBankAccount'
| 'VenmoAccount'
| 'VisaCheckoutCard'
| 'SamsungPayCard';
/**
* Plan
@@ -859,7 +905,7 @@ declare namespace braintree {
doNotInheritAddOnsOrDiscounts?: boolean;
paypal?: {
description?: string;
}
};
startImmediately?: boolean;
};
paymentMethodNonce?: string;
@@ -892,7 +938,7 @@ declare namespace braintree {
amount: string;
androidPayCard?: {
bin: string;
commercial: Commercial
commercial: Commercial;
countryOfIssuance: string;
debit: Debit;
durbinRegulated: DurbinRegulated;
@@ -915,7 +961,7 @@ declare namespace braintree {
bin: string;
cardType: string;
cardholderName: string;
commercial: Commercial
commercial: Commercial;
countryOfIssuance: string;
debit: Debit;
durbinRegulated: DurbinRegulated;
@@ -945,7 +991,7 @@ declare namespace braintree {
countryName?: string;
extendedAddress?: string;
firstName?: string;
id?: string
id?: string;
lastName?: string;
locality?: string;
postalCode?: string;
@@ -1094,7 +1140,7 @@ declare namespace braintree {
countryName?: string;
extendedAddress?: string;
firstName?: string;
id?: string
id?: string;
lastName?: string;
locality?: string;
postalCode?: string;
@@ -1149,7 +1195,7 @@ declare namespace braintree {
}
interface ClientToken {
clientToken: string;
clientToken: string;
}
export interface TransactionRequest {
@@ -1177,7 +1223,7 @@ declare namespace braintree {
expirationMonth?: string;
expirationYear?: string;
number?: string;
token?: string
token?: string;
};
customer?: {
company?: string;
@@ -1208,7 +1254,7 @@ declare namespace braintree {
paypal?: {
customField?: string;
description?: string;
}
};
skipAdvancedFraudChecking?: boolean;
skipAvs?: boolean;
skipCvv?: boolean;
@@ -1217,11 +1263,11 @@ declare namespace braintree {
storeShippingAddressInVault?: boolean;
submitForSettlement?: boolean;
threeDSecure?: {
required?: boolean
}
required?: boolean;
};
venmo?: {
profileId?: string;
}
};
};
orderId?: string;
paymentMethodNonce?: string;
@@ -1301,14 +1347,31 @@ declare namespace braintree {
sourcePaymentMethodToken: string;
}
export type GatewayRejectionReason = 'application_incomplete' | 'avs' | 'avs_and_cvv' | 'cvv' | 'duplicate' | 'fraud' | 'risk_threshold' | 'three_d_secure' | 'token_issuance';
export type GatewayRejectionReason =
| 'application_incomplete'
| 'avs'
| 'avs_and_cvv'
| 'cvv'
| 'duplicate'
| 'fraud'
| 'risk_threshold'
| 'three_d_secure'
| 'token_issuance';
export type PaymentInstrumentType = 'android_pay_card' | 'apple_pay_card' | 'credit_card' | 'masterpass_card' | 'paypal_account' | 'samsung_pay_card' | 'venmo_account' | 'visa_checkout_card';
export type PaymentInstrumentType =
| 'android_pay_card'
| 'apple_pay_card'
| 'credit_card'
| 'masterpass_card'
| 'paypal_account'
| 'samsung_pay_card'
| 'venmo_account'
| 'visa_checkout_card';
export type TransactionProcessorResponseType = 'approved' | 'soft_declined' | 'hard_declined';
export enum TransactionRequestSource {
recurring = 'recurring',
recurring = 'recurring',
unscheduled = 'unscheduled',
recurring_first = 'recurring_first',
moto = 'moto',
@@ -1321,8 +1384,19 @@ declare namespace braintree {
id: string;
}
export type TransactionStatus = 'authorization_expired' | 'authorized' | 'authorizing' | 'settlement_pending' | 'settlement_declined' |
'failed' | 'gateway_rejected' | 'processor_declined' | 'settled' | 'settling' | 'submitted_for_settlement' | 'voided';
export type TransactionStatus =
| 'authorization_expired'
| 'authorized'
| 'authorizing'
| 'settlement_pending'
| 'settlement_declined'
| 'failed'
| 'gateway_rejected'
| 'processor_declined'
| 'settled'
| 'settling'
| 'submitted_for_settlement'
| 'voided';
export interface TransactionStatusHistory {
amount: string;