Skip to content
This repository has been archived by the owner on Dec 11, 2022. It is now read-only.

Commit

Permalink
Update Stripe library and update library version to 2020-03-02.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff committed Apr 25, 2020
1 parent ad2df2c commit 8e35cc4
Show file tree
Hide file tree
Showing 30 changed files with 880 additions and 516 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Stripe API version to test against.
STRIPE_API_VERSION=2018-05-21
STRIPE_API_VERSION=2020-03-02

# Stripe test API key for an account to run the tests as.
STRIPE_TEST_SECRET_KEY=
Expand Down
16 changes: 4 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stripe-stateful-mock",
"version": "0.0.12",
"version": "0.0.13",
"description": "A half-baked, stateful Stripe mock server",
"main": "dist/index.js",
"scripts": {
Expand Down Expand Up @@ -49,7 +49,6 @@
"@types/express": "^4.17.6",
"@types/loglevel": "^1.6.3",
"@types/mocha": "^7.0.2",
"@types/stripe": "^7.10.3",
"@typescript-eslint/eslint-plugin": "^2.29.0",
"@typescript-eslint/parser": "^2.29.0",
"chai": "^4.2.0",
Expand All @@ -58,7 +57,7 @@
"eslint": "^6.8.0",
"mocha": "^7.1.1",
"rimraf": "^3.0.2",
"stripe": "^7.9.0",
"stripe": "^8.47.0",
"ts-node": "^8.9.0",
"typescript": "^3.8.3"
}
Expand Down
24 changes: 24 additions & 0 deletions src/api/RestError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Stripe from "stripe";

export class RestError extends Error {

constructor(public statusCode: number, public error: RestError.StripeErrorJson) {
super(error.message);
}
}

export namespace RestError {
export interface StripeErrorJson {
message: string;
type: "card_error" | "invalid_request_error" | "api_error" | "authentication_error" | "rate_limit_error" | "idempotency_error" | "invalid_grant";
readonly code?: string;
doc_url?: string;
param?: string;
charge?: string;
decline_code?: string;
payment_intent?: Stripe.PaymentIntent;
payment_method?: Stripe.PaymentMethod;
setup_intent?: Stripe.SetupIntent;
source?: Stripe.Source;
}
}
16 changes: 0 additions & 16 deletions src/api/StripeError.ts

This file was deleted.

61 changes: 32 additions & 29 deletions src/api/accounts.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
import * as stripe from "stripe";
import {applyListOptions, generateId} from "./utils";
import {StripeError} from "./StripeError";
import Stripe from "stripe";
import {applyListOptions, generateId, stringifyMetadata} from "./utils";
import {RestError} from "./RestError";
import {verify} from "./verify";
import log = require("loglevel");

export namespace accounts {

const accounts: { [accountId: string]: stripe.accounts.IAccount } = {};
const accounts: { [accountId: string]: Stripe.Account } = {};

export function create(accountId: string, params: stripe.accounts.IAccountCreationOptions): stripe.accounts.IAccount {
export function create(accountId: string, params: Stripe.AccountCreateParams): Stripe.Account {
log.debug("accounts.create", accountId, params);

if (accountId !== "acct_default") {
throw new StripeError(400, {
throw new RestError(400, {
message: "You can only create new accounts if you've signed up for Connect, which you can learn how to do at https://stripe.com/docs/connect.",
type: "invalid_request_error"
});
}
verify.requiredParams(params, ["type"]);

const connectedAccountId = (params as any).id || `acct_${generateId(16)}`;
const account: stripe.accounts.IAccount & any = { // The d.ts is out of date on this object and I don't want to bother.
const account: Stripe.Account = {
id: connectedAccountId,
object: "account",
business_profile: {
mcc: (params.business_profile && params.business_profile.mcc) || null,
name: (params.business_profile && params.business_profile.name) || "Stripe.com",
product_description: (params.business_profile && params.business_profile.product_description) || null,
support_address: (params.business_profile && params.business_profile.support_address) || null,
support_email: (params.business_profile && params.business_profile.support_email) || null,
support_phone: (params.business_profile && params.business_profile.support_phone) || null,
support_url: (params.business_profile && params.business_profile.support_url) || null,
url: (params.business_profile && params.business_profile.url) || null
mcc: params.business_profile?.mcc || null,
name: params.business_profile?.name || "Stripe.com",
product_description: params.business_profile?.product_description || null,
support_address: null, // meh
support_email: params.business_profile?.support_email || null,
support_phone: params.business_profile?.support_phone || null,
support_url: params.business_profile?.support_url || null,
url: params.business_profile?.url || null
},
business_type: params.business_type || null,
capabilities: {},
Expand All @@ -45,10 +45,9 @@ export namespace accounts {
object: "list",
data: [],
has_more: false,
total_count: 0,
url: `/v1/accounts/${connectedAccountId}/external_accounts`
},
metadata: params.metadata || {},
metadata: stringifyMetadata(params.metadata),
payouts_enabled: false,
requirements: {
current_deadline: null,
Expand All @@ -70,6 +69,7 @@ export namespace accounts {
"tos_acceptance.ip"
],
disabled_reason: "requirements.past_due",
errors: [],
eventually_due: [
"business_url",
"product_description",
Expand All @@ -82,9 +82,10 @@ export namespace accounts {
},
settings: {
branding: {
icon: (params.settings && params.settings.branding && params.settings.branding.icon) || null,
logo: (params.settings && params.settings.branding && params.settings.branding.logo) || null,
primary_color: (params.settings && params.settings.branding && params.settings.branding.primary_color) || null
icon: params?.settings?.branding?.icon || null,
logo: params?.settings?.branding?.logo || null,
primary_color: params?.settings?.branding?.primary_color || null,
secondary_color: params?.settings?.branding?.secondary_color || null,
},
card_payments: {
decline_on: {
Expand Down Expand Up @@ -112,9 +113,9 @@ export namespace accounts {
}
},
tos_acceptance: {
date: (params.tos_acceptance && params.tos_acceptance.date) || null,
ip: (params.tos_acceptance && params.tos_acceptance.ip) || null,
user_agent: (params.tos_acceptance && params.tos_acceptance.user_agent) || null
date: params.tos_acceptance?.date || null,
ip: params.tos_acceptance?.ip || null,
user_agent: params.tos_acceptance?.user_agent || null
},
type: params.type
};
Expand All @@ -141,17 +142,17 @@ export namespace accounts {
return account;
}

export function retrieve(accountId: string, connectedAccountId: string, censoredAccessToken: string): stripe.accounts.IAccount {
export function retrieve(accountId: string, connectedAccountId: string, censoredAccessToken: string): Stripe.Account {
log.debug("accounts.retrieve", accountId, connectedAccountId);

if (accountId !== "acct_default" && accountId !== connectedAccountId) {
throw new StripeError(400, {
throw new RestError(400, {
message: "The account specified in the path of /v1/accounts/:account does not match the account specified in the Stripe-Account header.",
type: "invalid_request_error"
});
}
if (!accounts[connectedAccountId]) {
throw new StripeError(403, {
throw new RestError(403, {
code: "account_invalid",
doc_url: "https://stripe.com/docs/error-codes/account-invalid",
message: `The provided key '${censoredAccessToken}' does not have access to account '${connectedAccountId}' (or that account does not exist). Application access may have been revoked.`,
Expand All @@ -161,16 +162,18 @@ export namespace accounts {
return accounts[connectedAccountId];
}

export function list(accountId: string, params: stripe.IListOptions): stripe.IList<stripe.accounts.IAccount> {
export function list(accountId: string, params: Stripe.PaginationParams): Stripe.ApiList<Stripe.Account> {
log.debug("accounts.list", accountId, params);

const data = Object.values(accounts);
return applyListOptions(data, params, (id, paramName) => retrieve(accountId, id, paramName));
}

export function del(accountId: string, connectedAccountId: string, censoredAccessToken: string): stripe.IDeleteConfirmation {
export function del(accountId: string, connectedAccountId: string, censoredAccessToken: string): Stripe.DeletedAccount {
log.debug("accounts.delete", accountId, connectedAccountId);

if (!accounts[connectedAccountId]) {
throw new StripeError(403, {
throw new RestError(403, {
code: "account_invalid",
doc_url: "https://stripe.com/docs/error-codes/account-invalid",
message: `The provided key '${censoredAccessToken}' does not have access to account '${connectedAccountId}' (or that account does not exist). Application access may have been revoked.`,
Expand Down
13 changes: 6 additions & 7 deletions src/api/cards.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import * as stripe from "stripe";
import log = require("loglevel");
import Stripe from "stripe";
import {generateId} from "./utils";
import log = require("loglevel");

export namespace cards {

export interface CardExtra {
sourceToken: string;
}

const cardExtras: {[cardId: string]: CardExtra} = {};
const cardExtras: { [cardId: string]: CardExtra } = {};

export function createFromSource(token: string): stripe.cards.ICard {
export function createFromSource(token: string): Stripe.Card {
log.debug("cards.createFromSource", token);

let saveCard = true;
const cardId = `card_${generateId(24)}`;
const now = new Date();
const card: stripe.cards.ICard = {
const card: Stripe.Card = {
id: cardId,
object: "card",
address_city: null,
Expand All @@ -37,8 +37,7 @@ export namespace cards {
fingerprint: generateId(16),
funding: "credit",
last4: "XXXX",
metadata: {
},
metadata: {},
name: null,
tokenization_method: null
};
Expand Down
Loading

0 comments on commit 8e35cc4

Please sign in to comment.