@storecraft/payments-paypal 1.0.9 → 1.0.11
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 +6 -12
- package/adapter.html.js +6 -2
- package/adapter.js +52 -44
- package/adapter.utils.js +3 -4
- package/package.json +1 -1
- package/types.public.d.ts +12 -5
package/README.md
CHANGED
@@ -19,13 +19,13 @@ npm i @storecraft/payments-paypal
|
|
19
19
|
|
20
20
|
## Howto
|
21
21
|
|
22
|
-
```
|
23
|
-
import { PayPal } from '@storecraft/payments-paypal';
|
22
|
+
```ts
|
23
|
+
import { PayPal, Config } from '@storecraft/payments-paypal';
|
24
24
|
|
25
|
-
const config = {
|
25
|
+
const config: Config = {
|
26
26
|
env: 'prod',
|
27
|
-
client_id:
|
28
|
-
secret:
|
27
|
+
client_id: process.env.PAYPAL_CLIENT_ID,
|
28
|
+
secret: env.process.PAYPAL_SECRET,
|
29
29
|
currency_code: 'USD',
|
30
30
|
intent_on_checkout: 'AUTHORIZE'
|
31
31
|
}
|
@@ -48,13 +48,7 @@ const app = new App(config)
|
|
48
48
|
.withStorage(new GoogleStorage())
|
49
49
|
.withPaymentGateways(
|
50
50
|
{
|
51
|
-
'paypal_standard_prod': new Paypal(
|
52
|
-
{
|
53
|
-
client_id: process.env.PAYPAL_CLIENT_ID,
|
54
|
-
secret: process.env.PAYPAL_SECRET,
|
55
|
-
env: 'prod'
|
56
|
-
}
|
57
|
-
),
|
51
|
+
'paypal_standard_prod': new Paypal() // config can be inferred from env variables
|
58
52
|
}
|
59
53
|
);
|
60
54
|
|
package/adapter.html.js
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
/**
|
2
|
+
* @import { Config } from './types.public.js'
|
3
|
+
* @import { OrderData } from '@storecraft/core/api'
|
4
|
+
*/
|
1
5
|
/**
|
2
6
|
*
|
3
7
|
* @description Official PayPal UI integration with `storecraft`.
|
@@ -10,8 +14,8 @@
|
|
10
14
|
* - Expiry date: 12/2027
|
11
15
|
* - CVC code: 897
|
12
16
|
*
|
13
|
-
* @param {
|
14
|
-
* @param {Partial<
|
17
|
+
* @param {Config} config
|
18
|
+
* @param {Partial<OrderData>} order_data
|
15
19
|
*/
|
16
20
|
export default function html_buy_ui(config, order_data) {
|
17
21
|
const orderData = order_data?.payment_gateway?.on_checkout_create;
|
package/adapter.js
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
/**
|
2
|
+
* @import { Config } from './types.public.js'
|
3
|
+
* @import { ENV } from '@storecraft/core';
|
4
|
+
* @import { OrderData, PaymentGatewayStatus } from '@storecraft/core/api'
|
5
|
+
* @import { payment_gateway } from '@storecraft/core/payments'
|
6
|
+
* @import { paypal_order, paypal_order_request } from './types.private.js'
|
7
|
+
*/
|
1
8
|
import {
|
2
9
|
CheckoutStatusEnum, PaymentOptionsEnum
|
3
10
|
} from '@storecraft/core/api/types.api.enums.js';
|
@@ -6,44 +13,58 @@ import { StorecraftError } from '@storecraft/core/api/utils.func.js';
|
|
6
13
|
import html_buy_ui from './adapter.html.js';
|
7
14
|
|
8
15
|
/**
|
9
|
-
* @typedef {
|
10
|
-
* @typedef {
|
11
|
-
* @typedef {import('@storecraft/core/api').CheckoutStatusEnum} CheckoutStatusOptions
|
12
|
-
* @typedef {import('@storecraft/core/api').OrderData} OrderData
|
13
|
-
* @typedef {import('./types.public.d.ts').Config} Config
|
14
|
-
* @typedef {import('@storecraft/core/payments').payment_gateway<Config, CreateResult>} payment_gateway
|
16
|
+
* @typedef {paypal_order} CreateResult
|
17
|
+
* @typedef {payment_gateway<Config, CreateResult>} Impl
|
15
18
|
*/
|
16
19
|
|
20
|
+
|
17
21
|
/**
|
18
|
-
* @implements {payment_gateway}
|
19
|
-
*
|
20
22
|
* @description **Paypal Payment** gateway (https://developer.paypal.com/docs/checkout/)
|
21
|
-
|
23
|
+
*
|
24
|
+
* @implements {Impl}
|
25
|
+
*/
|
22
26
|
export class Paypal {
|
23
27
|
|
24
|
-
/** @
|
28
|
+
/** @satisfies {ENV<Config>} */
|
29
|
+
static EnvConfigProd = /** @type{const} */ ({
|
30
|
+
client_id: `PAYPAL_CLIENT_ID_PROD`,
|
31
|
+
secret: 'PAYPAL_SECRET_PROD',
|
32
|
+
});
|
33
|
+
|
34
|
+
/** @satisfies {ENV<Config>} */
|
35
|
+
static EnvConfigTest = /** @type{const} */ ({
|
36
|
+
client_id: `PAYPAL_CLIENT_ID_TEST`,
|
37
|
+
secret: 'PAYPAL_SECRET_TEST',
|
38
|
+
});
|
25
39
|
|
26
|
-
/**
|
27
|
-
*
|
28
|
-
* @param {Config} config
|
29
|
-
*/
|
30
|
-
constructor(config) {
|
31
|
-
this.#_config = this.#validate_and_resolve_config(config);
|
32
|
-
}
|
40
|
+
/** @type {Config} */ #_config;
|
33
41
|
|
34
42
|
/**
|
35
43
|
*
|
36
44
|
* @param {Config} config
|
37
45
|
*/
|
38
|
-
|
39
|
-
|
46
|
+
constructor(config={}) {
|
47
|
+
// this.#_config = this.#validate_and_resolve_config(config);
|
48
|
+
this.#_config = {
|
40
49
|
default_currency_code: 'USD',
|
41
50
|
env: 'prod',
|
42
51
|
intent_on_checkout: 'AUTHORIZE',
|
43
52
|
...config,
|
44
53
|
}
|
54
|
+
}
|
45
55
|
|
46
|
-
|
56
|
+
/** @type {Impl["onInit"]} */
|
57
|
+
onInit = (app) => {
|
58
|
+
const is_prod = Boolean(this.config.env==='prod');
|
59
|
+
this.config.client_id ??= app.platform.env[
|
60
|
+
is_prod ? Paypal.EnvConfigProd.client_id : Paypal.EnvConfigTest.client_id
|
61
|
+
] ?? 'PAYPAL_CLIENT_ID';
|
62
|
+
|
63
|
+
this.config.secret ??= app.platform.env[
|
64
|
+
is_prod ? Paypal.EnvConfigProd.secret : Paypal.EnvConfigTest.secret
|
65
|
+
] ?? 'PAYPAL_SECRET';
|
66
|
+
|
67
|
+
const is_valid = this.config.client_id && this.config.secret;
|
47
68
|
|
48
69
|
if(!is_valid) {
|
49
70
|
throw new StorecraftError(
|
@@ -51,8 +72,6 @@ export class Paypal {
|
|
51
72
|
Missing client_id or secret`
|
52
73
|
)
|
53
74
|
}
|
54
|
-
|
55
|
-
return config;
|
56
75
|
}
|
57
76
|
|
58
77
|
get info() {
|
@@ -91,7 +110,7 @@ export class Paypal {
|
|
91
110
|
|
92
111
|
/**
|
93
112
|
*
|
94
|
-
* @type {
|
113
|
+
* @type {Impl["invokeAction"]}
|
95
114
|
*/
|
96
115
|
invokeAction(action_handle) {
|
97
116
|
switch (action_handle) {
|
@@ -108,11 +127,7 @@ export class Paypal {
|
|
108
127
|
}
|
109
128
|
|
110
129
|
/**
|
111
|
-
* @
|
112
|
-
*
|
113
|
-
* @param {Partial<OrderData>} order
|
114
|
-
*
|
115
|
-
* @return {Promise<string>} html
|
130
|
+
* @type {Impl["onBuyLinkHtml"]}
|
116
131
|
*/
|
117
132
|
async onBuyLinkHtml(order) {
|
118
133
|
|
@@ -124,14 +139,12 @@ export class Paypal {
|
|
124
139
|
/**
|
125
140
|
* @description TODO: the user prefers to capture intent instead
|
126
141
|
*
|
127
|
-
* @
|
128
|
-
*
|
129
|
-
* @return {Promise<CreateResult>}
|
142
|
+
* @type {Impl["onCheckoutCreate"]}
|
130
143
|
*/
|
131
144
|
async onCheckoutCreate(order) {
|
132
145
|
const { default_currency_code: currency_code, intent_on_checkout } = this.config;
|
133
146
|
|
134
|
-
/** @type {
|
147
|
+
/** @type {paypal_order_request} */
|
135
148
|
const body = {
|
136
149
|
intent: intent_on_checkout==='AUTHORIZE' ? 'AUTHORIZE' : 'CAPTURE',
|
137
150
|
purchase_units: [
|
@@ -163,9 +176,7 @@ export class Paypal {
|
|
163
176
|
/**
|
164
177
|
* @description todo: logic for if user wanted capture at approval
|
165
178
|
*
|
166
|
-
* @
|
167
|
-
*
|
168
|
-
* @return {ReturnType<payment_gateway["onCheckoutComplete"]>}
|
179
|
+
* @type {Impl["onCheckoutComplete"]}
|
169
180
|
*/
|
170
181
|
async onCheckoutComplete(create_result) {
|
171
182
|
// the url based on authorize or capture intent
|
@@ -179,7 +190,7 @@ export class Paypal {
|
|
179
190
|
|
180
191
|
await throw_bad_response(response);
|
181
192
|
|
182
|
-
/** @type {
|
193
|
+
/** @type {paypal_order} */
|
183
194
|
const payload = await response.json();
|
184
195
|
|
185
196
|
let status;
|
@@ -211,11 +222,7 @@ export class Paypal {
|
|
211
222
|
/**
|
212
223
|
* @description Fetch the order and analyze it's status
|
213
224
|
*
|
214
|
-
*
|
215
|
-
* @param {CreateResult} create_result
|
216
|
-
*
|
217
|
-
*
|
218
|
-
* @returns {Promise<PaymentGatewayStatus>}
|
225
|
+
* @type {Impl["status"]}
|
219
226
|
*/
|
220
227
|
async status(create_result) {
|
221
228
|
const o = await this.retrieve_order(create_result);
|
@@ -286,9 +293,10 @@ export class Paypal {
|
|
286
293
|
/**
|
287
294
|
* @description [https://developer.paypal.com/api/rest/webhooks/rest/](https://developer.paypal.com/api/rest/webhooks/rest/)
|
288
295
|
*
|
289
|
-
* @
|
296
|
+
* @type {Impl["webhook"]}
|
290
297
|
*/
|
291
298
|
async webhook(request) {
|
299
|
+
throw new Error('Paypal:: webhook - not supported yet !');
|
292
300
|
return null;
|
293
301
|
}
|
294
302
|
|
@@ -297,7 +305,7 @@ export class Paypal {
|
|
297
305
|
*
|
298
306
|
* @param {CreateResult} create_result first create result, holds paypal id
|
299
307
|
*
|
300
|
-
* @return {Promise<
|
308
|
+
* @return {Promise<paypal_order>}
|
301
309
|
*/
|
302
310
|
retrieve_order = async (create_result) => {
|
303
311
|
const response = await fetch_with_auth(
|
@@ -308,7 +316,7 @@ export class Paypal {
|
|
308
316
|
|
309
317
|
await throw_bad_response(response);
|
310
318
|
|
311
|
-
/** @type {
|
319
|
+
/** @type {paypal_order} */
|
312
320
|
const jsonData = await response.json();
|
313
321
|
return jsonData;
|
314
322
|
}
|
package/adapter.utils.js
CHANGED
@@ -1,13 +1,12 @@
|
|
1
|
+
/**
|
2
|
+
* @import { Config } from './types.public.js'
|
3
|
+
*/
|
1
4
|
|
2
5
|
export const endpoints = {
|
3
6
|
test: 'https://api-m.sandbox.paypal.com',
|
4
7
|
prod: 'https://api-m.paypal.com'
|
5
8
|
}
|
6
9
|
|
7
|
-
/**
|
8
|
-
* @typedef {import("./types.public.d.ts").Config} Config
|
9
|
-
*/
|
10
|
-
|
11
10
|
/**
|
12
11
|
* @param {Config} config
|
13
12
|
*/
|
package/package.json
CHANGED
package/types.public.d.ts
CHANGED
@@ -6,26 +6,33 @@ export { Paypal } from './adapter.js';
|
|
6
6
|
export type Config = {
|
7
7
|
/**
|
8
8
|
* @description default currency code
|
9
|
+
* @default 'USD'
|
9
10
|
*/
|
10
11
|
default_currency_code?: string;
|
11
12
|
|
12
13
|
/**
|
13
14
|
* @description the environment
|
15
|
+
* @default 'prod'
|
14
16
|
*/
|
15
|
-
env
|
17
|
+
env?: 'prod' | 'test';
|
16
18
|
|
17
19
|
/**
|
18
|
-
* @description private client id
|
20
|
+
* @description private client id. If missing,
|
21
|
+
* - if `env===prod` -> will be inferred from env variable `PAYPAL_CLIENT_ID_PROD` or `PAYPAL_CLIENT_ID`
|
22
|
+
* - if `env===test` -> will be inferred from env variable `PAYPAL_CLIENT_ID_TEST` or `PAYPAL_CLIENT_ID`
|
19
23
|
*/
|
20
|
-
client_id
|
24
|
+
client_id?: string;
|
21
25
|
|
22
26
|
/**
|
23
|
-
* @description private secret
|
27
|
+
* @description private secret. If missing,
|
28
|
+
* - if `env===prod` -> will be inferred from env variable `PAYPAL_SECRET_PROD` or `PAYPAL_SECRET`
|
29
|
+
* - if `env===test` -> will be inferred from env variable `PAYPAL_SECRET_TEST` or `PAYPAL_SECRET`
|
24
30
|
*/
|
25
|
-
secret
|
31
|
+
secret?: string;
|
26
32
|
|
27
33
|
/**
|
28
34
|
* @description default intent to `authorize` or `capture` on order creation
|
35
|
+
* @default 'AUTHORIZE'
|
29
36
|
*/
|
30
37
|
intent_on_checkout?: 'AUTHORIZE' | 'CAPTURE';
|
31
38
|
}
|