@apexara/stripe 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -4,6 +4,7 @@ A typed Stripe service layer for Node.js. Wraps the official Stripe SDK into foc
4
4
 
5
5
  ## Contents
6
6
 
7
+ - [Changes since 1.1.0](#changes-since-110)
7
8
  - [Changes since 1.0.5](#changes-since-105)
8
9
  - [Installation](#installation)
9
10
  - [Quick Start](#quick-start)
@@ -26,6 +27,20 @@ A typed Stripe service layer for Node.js. Wraps the official Stripe SDK into foc
26
27
 
27
28
  ---
28
29
 
30
+ ## Changes since 1.1.0
31
+
32
+ These changes ship as 1.2.0. Nothing an existing call does changes.
33
+
34
+ ### New
35
+
36
+ - **`HttpError` carries `code` and `declineCode`.** A refusal that came from Stripe keeps Stripe's error code and the
37
+ bank's decline code, so an app can word or log it its own way: `payPastDueInvoice`'s declined card carries
38
+ `code: 'card_declined'` and the bank's `declineCode` (such as `insufficient_funds`), and its bank-check refusal
39
+ `code: 'invoice_payment_intent_requires_action'`. Both are `null` on the library's own refusals. `HttpError` takes them
40
+ as an optional fourth argument, `{ code, declineCode }` (`HttpErrorReason`).
41
+
42
+ ---
43
+
29
44
  ## Changes since 1.0.5
30
45
 
31
46
  These changes ship as 1.1.0, and some change what an existing call does (see "Behaves differently"). An app on a `^1`
@@ -484,8 +499,8 @@ Charges a `past_due` subscription's latest invoice now, with the card the subscr
484
499
  their own schedule after the customer saves a new card, so call this right after setting the new default card, the way
485
500
  app stores charge at once on a card update. Returns the invoice once it is paid, also when a retry paid it meanwhile
486
501
  and the subscription reads `active` again. Returns `null` for a subscription in any other status, one with no invoice,
487
- a draft or void invoice, and an `active` subscription's open invoice, which are never charged here. A declined card throws `HttpError` 402 with the card's reason, and a payment that waits for the
488
- customer's bank check (3D Secure) throws 402 "Your bank needs you to confirm this payment.". After a decline Stripe
502
+ a draft or void invoice, and an `active` subscription's open invoice, which are never charged here. A declined card throws `HttpError` 402 with the card's reason, Stripe's `code` and the bank's `declineCode`, and a payment that waits for the
503
+ customer's bank check (3D Secure) throws 402 "Your bank needs you to confirm this payment." with `code` `invoice_payment_intent_requires_action`. After a decline Stripe
489
504
  keeps retrying as before; a payment that waits for the bank check waits for the customer to confirm it
490
505
  (`findConfirmPaymentUrl`). Any other refusal from Stripe is thrown as it is.
491
506
 
@@ -496,7 +511,7 @@ try {
496
511
  const paid = await stripe.invoices.payPastDueInvoice(subscription);
497
512
  // → the paid invoice, or null when there was nothing to pay
498
513
  } catch (err) {
499
- // err.status === 402: err.message is the reason to show the customer
514
+ // err.status === 402: err.message is the reason to show the customer; err.code and err.declineCode say why
500
515
  }
501
516
  ```
502
517
 
@@ -1,8 +1,11 @@
1
+ import { HttpErrorReason } from './../models/stripe.model';
1
2
  export declare class HttpError extends Error {
2
3
  readonly status: number;
3
4
  readonly message: string;
4
5
  readonly shouldTryLater: boolean;
5
- constructor(status: number, message: string, shouldTryLater?: boolean);
6
+ constructor(status: number, message: string, shouldTryLater?: boolean, reason?: HttpErrorReason);
6
7
  readonly shouldSendEmail?: boolean;
8
+ readonly code: string | null;
9
+ readonly declineCode: string | null;
7
10
  }
8
11
  //# sourceMappingURL=http-error.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"http-error.d.ts","sourceRoot":"","sources":["../../src/errors/http-error.ts"],"names":[],"mappings":"AAAA,qBAAa,SAAU,SAAQ,KAAK;aAIhB,MAAM,EAAE,MAAM;aACd,OAAO,EAAE,MAAM;aACf,cAAc;gBAFd,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,MAAM,EACf,cAAc,UAAQ;IASxC,SAAgB,eAAe,CAAC,EAAE,OAAO,CAAC;CAE3C"}
1
+ {"version":3,"file":"http-error.d.ts","sourceRoot":"","sources":["../../src/errors/http-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAE3D,qBAAa,SAAU,SAAQ,KAAK;aAKhB,MAAM,EAAE,MAAM;aACd,OAAO,EAAE,MAAM;aACf,cAAc;gBAFd,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,MAAM,EACf,cAAc,UAAQ,EACtC,MAAM,GAAE,eAAoB;IAY9B,SAAgB,eAAe,CAAC,EAAE,OAAO,CAAC;IAG1C,SAAgB,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAGpC,SAAgB,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;CAE5C"}
@@ -3,11 +3,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.HttpError = void 0;
4
4
  class HttpError extends Error {
5
5
  // shouldTryLater marks a 4xx that asks for the same request again later, so markRefusalsDone gives its event back.
6
- constructor(status, message, shouldTryLater = false) {
6
+ // reason is Stripe's code and decline code for a refusal that came from Stripe, such as a declined card.
7
+ constructor(status, message, shouldTryLater = false, reason = {}) {
7
8
  super(message);
8
9
  this.status = status;
9
10
  this.message = message;
10
11
  this.shouldTryLater = shouldTryLater;
12
+ this.code = reason.code ?? null;
13
+ this.declineCode = reason.declineCode ?? null;
11
14
  if (status < 500) {
12
15
  this.shouldSendEmail = false;
13
16
  }
@@ -79,4 +79,8 @@ export interface PendingActivationInvoice {
79
79
  export interface IdempotencyOptions {
80
80
  idempotencyKey?: string;
81
81
  }
82
+ export interface HttpErrorReason {
83
+ code?: string | null;
84
+ declineCode?: string | null;
85
+ }
82
86
  //# sourceMappingURL=stripe.model.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"stripe.model.d.ts","sourceRoot":"","sources":["../../src/models/stripe.model.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,MAAM,WAAW,sBAAsB;IACrC,MAAM,EAAE,MAAM,CAAC,YAAY,CAAC,MAAM,CAAC;IACnC,iBAAiB,EAAE,OAAO,CAAC;IAE3B,QAAQ,EAAE,IAAI,GAAG,IAAI,CAAC;IAEtB,QAAQ,EAAE,OAAO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C,YAAY,CAAC,EAAE,MAAM,CAAC,mBAAmB,CAAC,eAAe,CAAC,CAAC;CAC5D;AAED,MAAM,WAAW,wBAAwB;IACvC,cAAc,EAAE,MAAM,CAAC;IACvB,gBAAgB,EAAE,MAAM,GAAG,IAAI,CAAC;CACjC;AAKD,MAAM,WAAW,kBAAkB;IACjC,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAGD,MAAM,WAAW,eAAe;IAE9B,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAErB,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAC7B"}
@@ -95,10 +95,10 @@ class InvoiceService {
95
95
  return after.status === 'paid' ? after : null;
96
96
  }
97
97
  if (err?.code === REQUIRES_ACTION_CODE) {
98
- throw new http_error_1.HttpError(402, 'Your bank needs you to confirm this payment.');
98
+ throw new http_error_1.HttpError(402, 'Your bank needs you to confirm this payment.', false, { code: REQUIRES_ACTION_CODE });
99
99
  }
100
100
  if (err?.type === 'StripeCardError') {
101
- throw new http_error_1.HttpError(402, err.message || 'The payment did not go through.');
101
+ throw new http_error_1.HttpError(402, err.message || 'The payment did not go through.', false, { code: err.code, declineCode: err.decline_code });
102
102
  }
103
103
  throw err;
104
104
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@apexara/stripe",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Stripe service layer and webhook dispatcher",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",