From 25ecee95138cb451fd66f25d730dac76e4e0d61a Mon Sep 17 00:00:00 2001 From: Aluan Haddad Date: Wed, 4 Jan 2017 12:20:13 -0500 Subject: [PATCH] Added the brand property to the StripeCardData and fix style issues, lint (#13084) * Add the "brand" field to StipeCardData Added the StipeCardDataBrand type. Added the `brand` property to the `StripeCardData` declaration with the type `'Visa' | 'American Express' | 'MasterCard' | 'Discover JCB' | 'Diners Club' | 'Unknown'` Reference: https://stripe.com/docs/api#card_object * Run linter, fix issues. set --strictNullChecks. Unsure about ambient declaration: Removed as it seemed to be in error: 1. case was not correct 2. the export was the type of the global which is unconditionally defined. 3. this module does not seem a suitable candidate for a UMD declaration. * Use header version of 0.0; explain Stripe versioning in comments * Restore global * Added tests --- stripe/index.d.ts | 45 +++++++++++++++++++----------------------- stripe/stripe-tests.ts | 25 +++++++++++++++++++++++ stripe/tsconfig.json | 7 ++++--- stripe/tslint.json | 3 +++ 4 files changed, 52 insertions(+), 28 deletions(-) create mode 100644 stripe/stripe-tests.ts create mode 100644 stripe/tslint.json diff --git a/stripe/index.d.ts b/stripe/index.d.ts index 2f8c895298..fdfa1db96e 100644 --- a/stripe/index.d.ts +++ b/stripe/index.d.ts @@ -1,8 +1,10 @@ -// Type definitions for stripe +// Type definitions for stripe 0.0 // Project: https://stripe.com/ // Definitions by: Andy Hawkins , Eric J. Smith , Amrit Kahlon , Adam Cmiel // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +declare const Stripe: StripeStatic; + interface StripeStatic { applePay: StripeApplePay; setPublishableKey(key: string): void; @@ -49,6 +51,8 @@ interface StripeError { param?: string; } +type StripeCardDataBrand = 'Visa' | 'American Express' | 'MasterCard' | 'Discover JCB' | 'Diners Club' | 'Unknown'; + interface StripeCardData { object: string; last4: string; @@ -62,18 +66,17 @@ interface StripeCardData { address_state?: string; address_zip?: string; address_country?: string; + brand?: StripeCardDataBrand; createToken(data: StripeTokenData, responseHandler: (status: number, response: StripeTokenResponse) => void): void; } -interface StripeBankAccount -{ - createToken(params: StripeBankTokenParams, stripeResponseHandler: (status:number, response: StripeBankTokenResponse) => void): void; +interface StripeBankAccount { + createToken(params: StripeBankTokenParams, stripeResponseHandler: (status: number, response: StripeBankTokenResponse) => void): void; validateRoutingNumber(routingNumber: number | string, countryCode: string): boolean; validateAccountNumber(accountNumber: number | string, countryCode: string): boolean; } -interface StripeBankTokenParams -{ +interface StripeBankTokenParams { country: string; currency: string; account_number: number | string; @@ -82,8 +85,7 @@ interface StripeBankTokenParams account_holder_type: string; } -interface StripeBankTokenResponse -{ +interface StripeBankTokenResponse { id: string; bank_account: { country: string; @@ -100,13 +102,7 @@ interface StripeBankTokenResponse error?: StripeError; } -declare var Stripe: StripeStatic; -declare module "Stripe" { - export = StripeStatic; -} - -interface StripeApplePay -{ +interface StripeApplePay { checkAvailability(resopnseHandler: (result: boolean) => void): void; buildSession(data: StripeApplePayPaymentRequest, onSuccessHandler: (result: StripeApplePaySessionResult, completion: ((value: any) => void)) => void, @@ -117,8 +113,7 @@ type StripeApplePayBillingContactField = 'postalAddress' | 'name'; type StripeApplePayShippingContactField = StripeApplePayBillingContactField | 'phone' | 'email'; type StripeApplePayShipping = 'shipping' | 'delivery' | 'storePickup' | 'servicePickup'; -interface StripeApplePayPaymentRequest -{ +interface StripeApplePayPaymentRequest { billingContact: StripeApplePayPaymentContact; countryCode: string; currencyCode: string; @@ -132,30 +127,26 @@ interface StripeApplePayPaymentRequest } // https://developer.apple.com/reference/applepayjs/1916082-applepay_js_data_types -interface StripeApplePayLineItem -{ +interface StripeApplePayLineItem { type: 'pending' | 'final'; label: string; amount: number; } -interface StripeApplePaySessionResult -{ +interface StripeApplePaySessionResult { token: StripeTokenResponse; shippingContact?: StripeApplePayPaymentContact; shippingMethod?: StripeApplePayShippingMethod; } -interface StripeApplePayShippingMethod -{ +interface StripeApplePayShippingMethod { label: string; detail: string; amount: number; identifier: string; } -interface StripeApplePayPaymentContact -{ +interface StripeApplePayPaymentContact { emailAddress: string; phoneNumber: string; givenName: string; @@ -166,3 +157,7 @@ interface StripeApplePayPaymentContact postalCode: string; countryCode: string; } + +// The Stripe client side APIs are not made available to package managers for direct installation. +// As explained compliance reasons. Source: https://github.com/stripe/stripe-node/blob/master/README.md#these-are-serverside-bindings-only +// A release date versioning schema is used to version these APIs. diff --git a/stripe/stripe-tests.ts b/stripe/stripe-tests.ts new file mode 100644 index 0000000000..44aa09d216 --- /dev/null +++ b/stripe/stripe-tests.ts @@ -0,0 +1,25 @@ +function success(card: StripeCardData) { + console.log(card.brand && card.brand.toString()); +} + +const cardNumber = '4242424242424242'; + +const isValid = Stripe.validateCardNumber(cardNumber); +if (isValid) { + const tokenData: StripeTokenData = { + number: cardNumber, + exp_month: 1, + exp_year: 2100, + cvc: '111' + }; + Stripe.card.createToken(tokenData, (status, response) => { + if (response.error) { + console.error(response.error.message); + if (response.error.param) { + console.error(response.error.param); + } + } else { + success(response.card); + } + }); +} diff --git a/stripe/tsconfig.json b/stripe/tsconfig.json index 79b9eb6b27..265882db3c 100644 --- a/stripe/tsconfig.json +++ b/stripe/tsconfig.json @@ -3,7 +3,7 @@ "module": "commonjs", "target": "es6", "noImplicitAny": true, - "strictNullChecks": false, + "strictNullChecks": true, "baseUrl": "../", "typeRoots": [ "../" @@ -13,6 +13,7 @@ "forceConsistentCasingInFileNames": true }, "files": [ - "index.d.ts" + "index.d.ts", + "stripe-tests.ts" ] -} \ No newline at end of file +} diff --git a/stripe/tslint.json b/stripe/tslint.json new file mode 100644 index 0000000000..f9e30021f4 --- /dev/null +++ b/stripe/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "../tslint.json" +}