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

Commit

Permalink
Release 0.0.16. Updated to Stripe 8.125.0 and added support for Charg…
Browse files Browse the repository at this point in the history
…e.amount_captured.
  • Loading branch information
jeff committed Nov 20, 2020
1 parent 06cdbbd commit bc12230
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 321 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
STRIPE_TEST_SECRET_KEY=

# A Stripe account that is already connected to the account above.
# You can create a new test connected account from the "connected accounts"
# section of the dashboard. Create type "standard" and follow the link to skip
# all verification steps.
STRIPE_CONNECTED_ACCOUNT_ID=
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ Simulates a stateful Stripe server for local unit testing. Makes Stripe calls 5
Supported features:
- charges: create with the most common [test tokens](https://stripe.com/docs/testing) or customer card, retrieve, list, update, capture
- refunds: create, retrieve, list
- customers: create, retrieve, list, update, create card, retrieve card, delete card
- customers: create, retrieve, update, list, create card, retrieve card, delete card
- products: create, retrieve, list
- plans: create, retrieve, list
- prices: create, retrieve, update, list
- subscriptions: create, retrieve, list
- tax plans: create, retrieve, list, update
- connect accounts: create and delete
- tax plans: create, retrieve, update, list
- connect accounts: create, delete
- idempotency

Correctness of this test server is not guaranteed! Set up unit testing to work against either the Stripe server with a test account or this mock server with a flag to switch between them. Test against the official Stripe server occasionally to ensure correctness on the fine details.
Expand Down
466 changes: 171 additions & 295 deletions package-lock.json

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stripe-stateful-mock",
"version": "0.0.15",
"version": "0.0.16",
"description": "A half-baked, stateful Stripe mock server",
"main": "dist/index.js",
"scripts": {
Expand Down Expand Up @@ -43,23 +43,23 @@
"devDependencies": {
"@types/basic-auth": "^1.1.3",
"@types/body-parser": "^1.19.0",
"@types/chai": "^4.2.13",
"@types/chai": "^4.2.14",
"@types/chai-as-promised": "^7.1.3",
"@types/deep-equal": "^1.0.1",
"@types/dotenv-safe": "^8.1.1",
"@types/express": "^4.17.8",
"@types/express": "^4.17.9",
"@types/loglevel": "^1.6.3",
"@types/mocha": "^8.0.3",
"@typescript-eslint/eslint-plugin": "^4.4.0",
"@typescript-eslint/parser": "^4.4.0",
"@types/mocha": "^8.0.4",
"@typescript-eslint/eslint-plugin": "^4.8.1",
"@typescript-eslint/parser": "^4.8.1",
"chai": "^4.2.0",
"chai-exclude": "^2.0.2",
"dotenv-safe": "^8.2.0",
"eslint": "^7.10.0",
"mocha": "^8.1.3",
"eslint": "^7.13.0",
"mocha": "^8.2.1",
"rimraf": "^3.0.2",
"stripe": "^8.92.0",
"stripe": "^8.125.0",
"ts-node": "^9.0.0",
"typescript": "^4.0.3"
"typescript": "^4.1.2"
}
}
13 changes: 8 additions & 5 deletions src/api/charges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,17 +236,20 @@ export namespace charges {
} else {
charge.captured = true;
}
charge.amount_captured += captureAmount;
charge.balance_transaction = "txn_" + generateId(24);

return charge;
}

function getChargeFromCard(params: Stripe.ChargeCreateParams, source: Stripe.Card): Stripe.Charge {
const chargeId = "ch_" + generateId();
const captured = params.capture as any !== "false";
return {
id: chargeId,
object: "charge",
amount: +params.amount,
amount_captured: captured ? +params.amount : 0,
amount_refunded: 0,
application: null,
application_fee: null,
Expand All @@ -266,7 +269,7 @@ export namespace charges {
phone: null
},
calculated_statement_descriptor: null,
captured: params.capture as any !== "false",
captured: captured,
created: (Date.now() / 1000) | 0,
currency: params.currency.toLowerCase(),
customer: null,
Expand Down Expand Up @@ -525,8 +528,8 @@ export namespace charges {
}
}

export function getShippingFromParams(params: Stripe.ChargeUpdateParams.Shipping | null): Stripe.Charge.Shipping | null {
if (params == null) {
export function getShippingFromParams(params: Stripe.ChargeUpdateParams.Shipping | "" | null): Stripe.Charge.Shipping | null {
if (params == null || params === "") {
return null;
}

Expand All @@ -539,8 +542,8 @@ export namespace charges {
};
}

export function getAddressFromParams(params: Stripe.AddressParam | null): Stripe.Address | null {
if (params == null) {
export function getAddressFromParams(params: Stripe.AddressParam | "" | null): Stripe.Address | null {
if (params == null || params === "") {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/api/customers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export namespace customers {
createCard(accountId, customer, {source: params.source});
}
if (params.tax_exempt !== undefined) {
customer.tax_exempt = params.tax_exempt;
customer.tax_exempt = params.tax_exempt || null;
}

return expandObject(
Expand Down
6 changes: 3 additions & 3 deletions src/api/subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export namespace subscriptions {
days_until_due: +params.days_until_due || null,
default_payment_method: null,
default_source: default_source || null,
default_tax_rates: params.default_tax_rates?.map(t => taxRates.retrieve(accountId, t, "default_tax_rate")),
default_tax_rates: (params.default_tax_rates || null)?.map(t => taxRates.retrieve(accountId, t, "default_tax_rate")),
discount: null,
ended_at: null,
items: {
Expand Down Expand Up @@ -132,15 +132,15 @@ export namespace subscriptions {
const subscriptionItem: Stripe.SubscriptionItem = {
object: "subscription_item",
id: subItemId,
billing_thresholds: item.billing_thresholds ?? null,
billing_thresholds: item.billing_thresholds || null,
created: Math.floor(Date.now() / 1000),
deleted: undefined,
metadata: stringifyMetadata(item.metadata),
plan: getOrCreatePlan(accountId, item.plan), // isn't in the documentation, deprecated?
price: item.price ? prices.retrieve(accountId, item.price, "price") : null,
quantity: +item.quantity || 1,
subscription: subscriptionId,
tax_rates: item.tax_rates?.map(r => taxRates.retrieve(accountId, r, "tax_rate"))
tax_rates: (item.tax_rates || null)?.map(r => taxRates.retrieve(accountId, r, "tax_rate"))
};
accountSubscriptionItems.put(accountId, subscriptionItem);

Expand Down
2 changes: 1 addition & 1 deletion src/api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export function generateId(length: number = 20): string {
return new Array(length).fill("5").map(() => chars[(Math.random() * chars.length) | 0]).join("");
}

export function stringifyMetadata(metadata?: { [key: string]: string | number }): { [key: string]: string } {
export function stringifyMetadata(metadata?: { [key: string]: string | number } | ""): { [key: string]: string } {
if (!metadata) {
return {};
}
Expand Down
12 changes: 9 additions & 3 deletions test/charges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,18 @@ describe("charges", () => {
}
));

it("supports on_behalf_of", buildChargeParityTest({
amount: 3500,
currency: "usd",
on_behalf_of: process.env["STRIPE_CONNECTED_ACCOUNT_ID"],
source: "tok_visa",
transfer_group: "xfer"
}));

it("supports misc additional params", buildChargeParityTest({
amount: 3500,
description: "this is a description",
currency: "usd",
on_behalf_of: process.env["STRIPE_CONNECTED_ACCOUNT_ID"],
receipt_email: "[email protected]",
shipping: {
address: {
Expand All @@ -217,8 +224,7 @@ describe("charges", () => {
tracking_number: "abc123"
},
source: "tok_visa",
statement_descriptor: "ccc",
transfer_group: "ddd"
statement_descriptor: "hello world",
}));

it("supports upper case currency", buildStripeParityTest(
Expand Down
1 change: 1 addition & 0 deletions test/stripeAssert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export function assertChargesAreBasicallyEqual(actual: Stripe.Charge, expected:
assertEqualOnKeys(actual, expected, [
"object",
"amount",
"amount_captured",
"amount_refunded",
"application_fee",
"application_fee_amount",
Expand Down

0 comments on commit bc12230

Please sign in to comment.