Merge pull request #17168 from amritk/master

Added stripe v3 types, namespaced previous ones to v2
This commit is contained in:
Daniel Rosenwasser
2017-06-15 00:16:56 -07:00
committed by GitHub
8 changed files with 438 additions and 182 deletions

View File

@@ -3,7 +3,7 @@
// Definitions by: Chris Wrench <https://github.com/cgwrench>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="stripe"/>
/// <reference types="stripe/v2"/>
interface StripeCheckoutStatic {
configure(options: StripeCheckoutOptions): StripeCheckoutHandler;

View File

@@ -1,4 +1,4 @@
// Type definitions for stripe 2.x
// Type definitions for stripe 3.0
// Project: https://stripe.com/
// Definitions by: Andy Hawkins <https://github.com/a904guy/,http://a904guy.com>
// Eric J. Smith <https://github.com/ejsmith/>
@@ -7,164 +7,184 @@
// Justin Leider <https://github.com/jleider>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare const Stripe: StripeStatic;
export function Stripe(stripePublicKey: string): stripe.StripeStatic;
interface StripeStatic {
applePay: StripeApplePay;
setPublishableKey(key: string): void;
validateCardNumber(cardNumber: string): boolean;
validateExpiry(month: string, year: string): boolean;
validateCVC(cardCVC: string): boolean;
cardType(cardNumber: string): StripeCardDataBrand;
getToken(token: string, responseHandler: (status: number, response: StripeCardTokenResponse) => void): void;
card: StripeCard;
createToken(data: StripeCardTokenData, responseHandler: (status: number, response: StripeCardTokenResponse) => void): void;
bankAccount: StripeBankAccount;
}
export namespace stripe {
interface StripeStatic {
elements(options?: elements.ElementsCreateOptions): elements.Elements;
createToken(element: elements.Element, options?: TokenOptions): Promise<TokenResponse>;
}
interface StripeCardTokenData {
number: string;
exp_month?: number;
exp_year?: number;
exp?: string;
cvc?: string;
name?: string;
address_line1?: string;
address_line2?: string;
address_city?: string;
address_state?: string;
address_zip?: string;
address_country?: string;
}
interface TokenOptions {
name?: string;
address_line1?: string;
address_line2?: string;
address_city?: string;
address_state?: string;
address_zip?: string;
address_country?: string;
currency?: string;
}
interface StripeTokenResponse {
id: string;
client_ip: string;
created: number;
livemode: boolean;
object: string;
type: string;
used: boolean;
error?: StripeError;
}
interface StripeCardTokenResponse extends StripeTokenResponse {
card: StripeCard;
}
interface StripeError {
type: string;
code: string;
message: string;
param?: string;
}
type StripeCardDataBrand = 'Visa' | 'American Express' | 'MasterCard' | 'Discover' | 'JCB' | 'Diners Club' | 'Unknown';
type StripeCardDataFunding = 'credit' | 'debit' | 'prepaid' | 'unknown';
interface StripeCard {
object: string;
last4: string;
exp_month: number;
exp_year: number;
country?: string;
name?: string;
address_line1?: string;
address_line2?: string;
address_city?: string;
address_state?: string;
address_zip?: string;
address_country?: string;
brand?: StripeCardDataBrand;
funding?: StripeCardDataFunding;
createToken(data: StripeCardTokenData, responseHandler: (status: number, response: StripeCardTokenResponse) => void): void;
validateCardNumber(cardNumber: string): boolean;
validateExpiry(month: string, year: string): boolean;
validateCVC(cardCVC: string): boolean;
}
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 {
country: string;
currency: string;
account_number: number | string;
routing_number?: number | string;
account_holder_name: string;
account_holder_type: string;
}
interface StripeBankTokenResponse extends StripeTokenResponse {
bank_account: {
country: string;
bank_name: string;
last4: number;
validated: boolean;
interface Token {
id: string;
object: string;
};
bank_account?: BankAccount;
card?: Card;
client_ip: string;
created: number;
livemode: boolean;
type: string;
used: boolean;
}
interface TokenResponse {
token?: Token;
error?: Error;
}
interface Error {
type: string;
charge: string;
message?: string;
code?: string;
declined_code?: string;
param?: string;
}
type statusType = 'new' | 'validated' | 'verified' | 'verification_failed' | 'errored';
interface BankAccount {
id: string;
object: string;
account_holder_name: string;
account_holder_type: string;
bank_name: string;
country: string;
currency: string;
fingerprint: string;
last4: string;
routing_number: string;
status: statusType;
}
type brandType = 'Visa' | 'American Express' | 'MasterCard' | 'Discover' | 'JCB' | 'Diners Club' | 'Unknown';
type checkType = 'pass' | 'fail' | 'unavailable' | 'unchecked';
type fundingType = 'credit' | 'debit' | 'prepaid' | 'unknown';
type tokenizationType = 'apple_pay' | 'android_pay';
interface Card {
id: string;
object: string;
address_city?: string;
address_country?: string;
address_line1?: string;
address_line1_check?: checkType;
address_line2?: string;
address_state?: string;
address_zip?: string;
address_zip_check?: checkType;
brand: brandType;
country: string;
currency?: string;
cvc_check?: checkType;
dynamic_last4: string;
exp_month: number;
exp_year: number;
fingerprint: string;
funding: fundingType;
last4: string;
metadata: any;
name?: string;
tokenization_method?: tokenizationType;
}
// Container for all elements related types
namespace elements {
interface ElementsCreateOptions {
fonts?: elements.Font[];
locale?: string;
}
type handler = (response?: ElementChangeResponse) => void;
type eventTypes = 'blur' | 'change' | 'focus' | 'ready';
interface Element {
// HTMLElement keeps giving this error for some reason:
// Cannot find name 'HTMLElement'
mount(domElement: string | any): void;
on(event: eventTypes, handler: handler): void;
blur(): void;
clear(): void;
unmount(): void;
update(options: ElementsOptions): void;
}
interface ElementChangeResponse {
brand: string;
complete: boolean;
empty: boolean;
value?: { postalCode: string | number };
error?: Error;
}
interface ElementOptions {
fonts?: elements.Font[];
locale?: string;
}
type elementsType = 'card' | 'cardNumber' | 'cardExpiry' | 'cardCvc' | 'postalCode';
interface Elements {
create(type: elementsType, options: ElementsOptions): Element;
}
interface ElementsOptions {
classes?: {
base?: string;
complete?: string;
empty?: string;
focus?: string;
invalid?: string;
webkitAutofill?: string;
};
hidePostalCode?: boolean;
hideIcon?: boolean;
iconStyle?: 'solid' | 'default';
style?: {
base?: Style;
complete?: Style;
empty?: Style;
invalid?: Style;
};
value?: string | {[objectKey: string]: string; };
}
interface Style extends StyleOptions {
':hover'?: StyleOptions;
':focus'?: StyleOptions;
'::placeholder'?: StyleOptions;
'::selection'?: StyleOptions;
':-webkit-autofill'?: StyleOptions;
}
interface Font {
family?: string;
src?: string;
style?: string;
unicodeRange?: string;
weight?: string;
}
interface StyleOptions {
color?: string;
fontFamily?: string;
fontSize?: string;
fontSmoothing?: string;
fontStyle?: string;
fontVariant?: string;
iconColor?: string;
lineHeight?: string;
letterSpacing?: string;
textDecoration?: string;
textShadow?: string;
textTransform?: string;
}
}
}
interface StripeApplePay {
checkAvailability(resopnseHandler: (result: boolean) => void): void;
buildSession(data: StripeApplePayPaymentRequest,
onSuccessHandler: (result: StripeApplePaySessionResult, completion: ((value: any) => void)) => void,
onErrorHanlder: (error: { message: string }) => void): any;
}
type StripeApplePayBillingContactField = 'postalAddress' | 'name';
type StripeApplePayShippingContactField = StripeApplePayBillingContactField | 'phone' | 'email';
type StripeApplePayShipping = 'shipping' | 'delivery' | 'storePickup' | 'servicePickup';
interface StripeApplePayPaymentRequest {
billingContact: StripeApplePayPaymentContact;
countryCode: string;
currencyCode: string;
total: StripeApplePayLineItem;
lineItems?: StripeApplePayLineItem[];
requiredBillingContactFields?: StripeApplePayBillingContactField[];
requiredShippingContactFields?: StripeApplePayShippingContactField[];
shippingContact?: StripeApplePayPaymentContact;
shippingMethods?: StripeApplePayShippingMethod[];
shippingType?: StripeApplePayShipping[];
}
// https://developer.apple.com/reference/applepayjs/1916082-applepay_js_data_types
interface StripeApplePayLineItem {
type: 'pending' | 'final';
label: string;
amount: number;
}
interface StripeApplePaySessionResult {
token: StripeCardTokenResponse;
shippingContact?: StripeApplePayPaymentContact;
shippingMethod?: StripeApplePayShippingMethod;
}
interface StripeApplePayShippingMethod {
label: string;
detail: string;
amount: number;
identifier: string;
}
interface StripeApplePayPaymentContact {
emailAddress: string;
phoneNumber: string;
givenName: string;
familyName: string;
addressLines: string[];
locality: string;
administrativeArea: string;
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.

View File

@@ -1,25 +1,39 @@
function success(card: StripeCard) {
console.log(card.brand && card.brand.toString());
}
import {stripe, Stripe} from 'stripe';
const cardNumber = '4242424242424242';
const isValid = Stripe.validateCardNumber(cardNumber);
if (isValid) {
const tokenData: StripeCardTokenData = {
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);
const stripe = Stripe('public-key');
const elements = stripe.elements();
const style = {
base: {
color: '#32325d',
lineHeight: '24px',
fontFamily: 'Roboto, "Helvetica Neue", sans-serif',
fontSmoothing: 'antialiased',
fontSize: '16px',
'::placeholder': {
color: '#aab7c4'
}
});
}
},
invalid: {
color: '#B71C1C',
iconColor: '#B71C1C'
}
};
const card = elements.create('card', {hidePostalCode: true, style});
card.mount(document.createElement('div'));
card.on('ready', () => {
console.log('ready');
});
card.on('change', (resp: stripe.elements.ElementChangeResponse) => {
console.log(resp.brand);
});
stripe.createToken(card, {
name: 'Jimmy',
address_city: 'Toronto',
address_country: 'Canada'
})
.then((result: stripe.TokenResponse) => {
console.log(result.token);
},
(error: stripe.Error) => {
console.error(error);
});

View File

@@ -20,4 +20,4 @@
"index.d.ts",
"stripe-tests.ts"
]
}
}

170
types/stripe/v2/index.d.ts vendored Normal file
View File

@@ -0,0 +1,170 @@
// Type definitions for stripe 2.x
// Project: https://stripe.com/
// Definitions by: Andy Hawkins <https://github.com/a904guy/,http://a904guy.com>
// Eric J. Smith <https://github.com/ejsmith/>
// Amrit Kahlon <https://github.com/amritk/>
// Adam Cmiel <https://github.com/adamcmiel>
// Justin Leider <https://github.com/jleider>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare const Stripe: StripeStatic;
interface StripeStatic {
applePay: StripeApplePay;
setPublishableKey(key: string): void;
validateCardNumber(cardNumber: string): boolean;
validateExpiry(month: string, year: string): boolean;
validateCVC(cardCVC: string): boolean;
cardType(cardNumber: string): StripeCardDataBrand;
getToken(token: string, responseHandler: (status: number, response: StripeCardTokenResponse) => void): void;
card: StripeCard;
createToken(data: StripeCardTokenData, responseHandler: (status: number, response: StripeCardTokenResponse) => void): void;
bankAccount: StripeBankAccount;
}
interface StripeCardTokenData {
number: string;
exp_month?: number;
exp_year?: number;
exp?: string;
cvc?: string;
name?: string;
address_line1?: string;
address_line2?: string;
address_city?: string;
address_state?: string;
address_zip?: string;
address_country?: string;
}
interface StripeTokenResponse {
id: string;
client_ip: string;
created: number;
livemode: boolean;
object: string;
type: string;
used: boolean;
error?: StripeError;
}
interface StripeCardTokenResponse extends StripeTokenResponse {
card: StripeCard;
}
interface StripeError {
type: string;
code: string;
message: string;
param?: string;
}
type StripeCardDataBrand = 'Visa' | 'American Express' | 'MasterCard' | 'Discover' | 'JCB' | 'Diners Club' | 'Unknown';
type StripeCardDataFunding = 'credit' | 'debit' | 'prepaid' | 'unknown';
interface StripeCard {
object: string;
last4: string;
exp_month: number;
exp_year: number;
country?: string;
name?: string;
address_line1?: string;
address_line2?: string;
address_city?: string;
address_state?: string;
address_zip?: string;
address_country?: string;
brand?: StripeCardDataBrand;
funding?: StripeCardDataFunding;
createToken(data: StripeCardTokenData, responseHandler: (status: number, response: StripeCardTokenResponse) => void): void;
validateCardNumber(cardNumber: string): boolean;
validateExpiry(month: string, year: string): boolean;
validateCVC(cardCVC: string): boolean;
}
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 {
country: string;
currency: string;
account_number: number | string;
routing_number?: number | string;
account_holder_name: string;
account_holder_type: string;
}
interface StripeBankTokenResponse extends StripeTokenResponse {
bank_account: {
country: string;
bank_name: string;
last4: number;
validated: boolean;
object: string;
};
}
interface StripeApplePay {
checkAvailability(resopnseHandler: (result: boolean) => void): void;
buildSession(data: StripeApplePayPaymentRequest,
onSuccessHandler: (result: StripeApplePaySessionResult, completion: ((value: any) => void)) => void,
onErrorHanlder: (error: { message: string }) => void): any;
}
type StripeApplePayBillingContactField = 'postalAddress' | 'name';
type StripeApplePayShippingContactField = StripeApplePayBillingContactField | 'phone' | 'email';
type StripeApplePayShipping = 'shipping' | 'delivery' | 'storePickup' | 'servicePickup';
interface StripeApplePayPaymentRequest {
billingContact: StripeApplePayPaymentContact;
countryCode: string;
currencyCode: string;
total: StripeApplePayLineItem;
lineItems?: StripeApplePayLineItem[];
requiredBillingContactFields?: StripeApplePayBillingContactField[];
requiredShippingContactFields?: StripeApplePayShippingContactField[];
shippingContact?: StripeApplePayPaymentContact;
shippingMethods?: StripeApplePayShippingMethod[];
shippingType?: StripeApplePayShipping[];
}
// https://developer.apple.com/reference/applepayjs/1916082-applepay_js_data_types
interface StripeApplePayLineItem {
type: 'pending' | 'final';
label: string;
amount: number;
}
interface StripeApplePaySessionResult {
token: StripeCardTokenResponse;
shippingContact?: StripeApplePayPaymentContact;
shippingMethod?: StripeApplePayShippingMethod;
}
interface StripeApplePayShippingMethod {
label: string;
detail: string;
amount: number;
identifier: string;
}
interface StripeApplePayPaymentContact {
emailAddress: string;
phoneNumber: string;
givenName: string;
familyName: string;
addressLines: string[];
locality: string;
administrativeArea: string;
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.

View File

@@ -0,0 +1,25 @@
function success(card: StripeCard) {
console.log(card.brand && card.brand.toString());
}
const cardNumber = '4242424242424242';
const isValid = Stripe.validateCardNumber(cardNumber);
if (isValid) {
const tokenData: StripeCardTokenData = {
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);
}
});
}

View File

@@ -0,0 +1,26 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"dom"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../../",
"typeRoots": [
"../../"
],
"types": [],
"paths": {
"stripe": ["stripe/v2"]
},
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"stripe-tests.ts"
]
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }