@commercetools/connect-payments-sdk 0.27.0 → 0.27.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @commercetools/connect-payments-sdk
2
2
 
3
+ ## 0.27.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 7bfd391: Fix issue calling the recurring payment jobs API
8
+
3
9
  ## 0.27.0
4
10
 
5
11
  ### Minor Changes
@@ -1,15 +1,19 @@
1
1
  import { Logger } from '../..';
2
+ import { AuthorizationService } from '../types/authorization.type';
2
3
  import { RecurringPaymentJob, RecurringPaymentJobDraft, RecurringPaymentJobService } from '../types/recurring-payment-job.type';
3
4
  import { DefaultCartService } from './ct-cart.service';
4
5
  /**
5
6
  * Default implementation of the RecurringPaymentJobService interface.
6
7
  */
7
8
  export declare class DefaultRecurringPaymentJobService implements RecurringPaymentJobService {
9
+ private authorizationService;
8
10
  private ctCartService;
9
11
  private checkoutUrl;
10
12
  private projectKey;
11
13
  private logger;
14
+ private token;
12
15
  constructor(opts: {
16
+ authorizationService: AuthorizationService;
13
17
  ctCartService: DefaultCartService;
14
18
  checkoutUrl: string;
15
19
  projectKey: string;
@@ -51,5 +55,9 @@ export declare class DefaultRecurringPaymentJobService implements RecurringPayme
51
55
  * Internal method that performs the HTTP request to create a recurring payment job.
52
56
  */
53
57
  private internalCreateRecurringPaymentJob;
58
+ /**
59
+ * Performs a fetch request with automatic token refresh on 401/403 errors.
60
+ */
61
+ private fetchWithTokenRetry;
54
62
  private safeParseErrorResponse;
55
63
  }
@@ -6,11 +6,14 @@ const errorx_1 = require("../../errorx/errorx");
6
6
  * Default implementation of the RecurringPaymentJobService interface.
7
7
  */
8
8
  class DefaultRecurringPaymentJobService {
9
+ authorizationService;
9
10
  ctCartService;
10
11
  checkoutUrl;
11
12
  projectKey;
12
13
  logger;
14
+ token;
13
15
  constructor(opts) {
16
+ this.authorizationService = opts.authorizationService;
14
17
  this.ctCartService = opts.ctCartService;
15
18
  this.checkoutUrl = opts.checkoutUrl;
16
19
  this.projectKey = opts.projectKey;
@@ -75,11 +78,8 @@ class DefaultRecurringPaymentJobService {
75
78
  state: 'Initial',
76
79
  },
77
80
  };
78
- const response = await fetch(url, {
81
+ const response = await this.fetchWithTokenRetry(url, {
79
82
  method: 'POST',
80
- headers: {
81
- 'Content-Type': 'application/json',
82
- },
83
83
  body: JSON.stringify(payload),
84
84
  });
85
85
  if (!response.ok) {
@@ -94,6 +94,35 @@ class DefaultRecurringPaymentJobService {
94
94
  }
95
95
  return (await response.json());
96
96
  }
97
+ /**
98
+ * Performs a fetch request with automatic token refresh on 401/403 errors.
99
+ */
100
+ async fetchWithTokenRetry(url, options) {
101
+ if (!this.token) {
102
+ this.token = await this.authorizationService.getAccessToken();
103
+ }
104
+ let response = await fetch(url, {
105
+ ...options,
106
+ headers: {
107
+ 'Content-Type': 'application/json',
108
+ Authorization: `Bearer ${this.token.access_token}`,
109
+ ...options.headers,
110
+ },
111
+ });
112
+ // If commercetools oauth token is expired, get a new one and retry once
113
+ if (response.status === 401 || response.status === 403) {
114
+ this.token = await this.authorizationService.getAccessToken();
115
+ response = await fetch(url, {
116
+ ...options,
117
+ headers: {
118
+ 'Content-Type': 'application/json',
119
+ Authorization: `Bearer ${this.token.access_token}`,
120
+ ...options.headers,
121
+ },
122
+ });
123
+ }
124
+ return response;
125
+ }
97
126
  async safeParseErrorResponse(response) {
98
127
  try {
99
128
  return await response.json();
package/dist/index.js CHANGED
@@ -71,6 +71,7 @@ const setupPaymentSDK = (opts) => {
71
71
  const ctPaymentMethodService = new ct_payment_method_service_1.DefaultPaymentMethodService({ ctAPI, logger });
72
72
  const ctCustomTypeService = new ct_custom_type_service_1.DefaultCustomTypeService({ ctAPI, logger });
73
73
  const ctRecurringPaymentJobService = new ct_recurring_payment_job_service_1.DefaultRecurringPaymentJobService({
74
+ authorizationService: ctAuthorizationService,
74
75
  ctCartService,
75
76
  checkoutUrl: opts.checkoutUrl,
76
77
  projectKey: opts.projectKey,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@commercetools/connect-payments-sdk",
3
- "version": "0.27.0",
3
+ "version": "0.27.1",
4
4
  "description": "Payment SDK for commercetools payment connectors",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",