[stripe] fix definitions files and tests

This commit is contained in:
Kamil Gałuszka
2017-07-10 11:49:10 +02:00
parent fc73cdd92b
commit 12469ce0d0
11 changed files with 336 additions and 245 deletions

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

@@ -0,0 +1,173 @@
// Type definitions for stripe-v2 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>
// Kamil Gałuszka <https://github.com/galuszkak>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare var Stripe: stripe.StripeStatic;
declare namespace stripe {
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,32 @@
declare function describe(desc: string, fn: () => void): void;
declare function it(desc: string, fn: () => void): void;
describe("Stripe", () => {
it("should excercise Stripe API", () => {
function success(card: stripe.StripeCard) {
console.log(card.brand && card.brand.toString());
}
const cardNumber = '4242424242424242';
const isValid = Stripe.validateCardNumber(cardNumber);
if (isValid) {
const tokenData: stripe.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

@@ -18,6 +18,6 @@
},
"files": [
"index.d.ts",
"stripe-tests.ts"
"stripe-v2-tests.ts"
]
}

View File

@@ -1,20 +1,30 @@
// Type definitions for stripe 3.0
// Type definitions for stripe-v3 3.0
// 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>
// Kamil Gałuszka <https://github.com/galuszkak>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
export function Stripe(stripePublicKey: string): stripe.StripeStatic;
declare var Stripe: stripe.StripeStatic;
export namespace stripe {
declare namespace stripe {
interface StripeStatic {
(publicKey: string): Stripe;
version: number;
}
interface Stripe {
elements(options?: elements.ElementsCreateOptions): elements.Elements;
createToken(element: elements.Element, options?: TokenOptions): Promise<TokenResponse>;
}
interface StripeOptions {
stripeAccount: string;
}
interface TokenOptions {
name?: string;
address_line1?: string;
@@ -153,7 +163,7 @@ export namespace stripe {
empty?: Style;
invalid?: Style;
};
value?: string | {[objectKey: string]: string; };
value?: string | { [objectKey: string]: string; };
}
interface Style extends StyleOptions {

View File

@@ -0,0 +1,113 @@
/// <reference types="stripe-v2" />
declare function describe(desc: string, fn: () => void): void;
declare function it(desc: string, fn: () => void): void;
describe("Stripe", () => {
it("should excercise all Stripe API", () => {
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);
});
});
});
describe("Stripe v2 & v3", () => {
it("should excercise all Stripe API", () => {
function success(card: stripe.StripeCard) {
console.log(card.brand && card.brand.toString());
}
const cardNumber = '4242424242424242';
const isValid = Stripe.validateCardNumber(cardNumber);
if (isValid) {
const tokenData: stripe.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

@@ -8,19 +8,16 @@
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../../",
"baseUrl": "../",
"typeRoots": [
"../../"
"../"
],
"types": [],
"paths": {
"stripe": ["stripe/v2"]
},
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"stripe-tests.ts"
"stripe-v3-tests.ts"
]
}

View File

@@ -1,39 +0,0 @@
import {stripe, Stripe} from 'stripe';
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

@@ -1,170 +0,0 @@
// 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

@@ -1,25 +0,0 @@
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);
}
});
}