@kiva/kv-shop 1.1.3 → 1.1.4

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/dist/index.cjs ADDED
@@ -0,0 +1,411 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // src/index.ts
30
+ var src_exports = {};
31
+ __export(src_exports, {
32
+ ShopError: () => ShopError,
33
+ checkSubscriptionStatus: () => checkSubscriptionStatus,
34
+ defaultPaymentTypes: () => defaultPaymentTypes,
35
+ executeNewSubscriptionCheckout: () => executeNewSubscriptionCheckout,
36
+ executeOneTimeCheckout: () => executeOneTimeCheckout,
37
+ getBasketID: () => getBasketID,
38
+ getClientToken: () => getClientToken,
39
+ getCookieValue: () => getCookieValue,
40
+ parseShopError: () => parseShopError,
41
+ setBasketID: () => setBasketID,
42
+ setTipDonation: () => setTipDonation,
43
+ useBraintreeDropIn: () => useBraintreeDropIn,
44
+ waitOnTransaction: () => waitOnTransaction
45
+ });
46
+ module.exports = __toCommonJS(src_exports);
47
+
48
+ // src/basket.ts
49
+ var getCookieValue = (name) => {
50
+ if (typeof document !== void 0) {
51
+ return decodeURIComponent(document.cookie.match(`(^|;)\\s*${name}\\s*=\\s*([^;]+)`)?.pop() || "");
52
+ }
53
+ };
54
+ function getBasketID() {
55
+ return getCookieValue("kvbskt");
56
+ }
57
+ function setBasketID(basketId) {
58
+ }
59
+
60
+ // src/basketItems.ts
61
+ var import_core = require("@apollo/client/core");
62
+ var import_numeral = __toESM(require("numeral"), 1);
63
+
64
+ // src/shopError.ts
65
+ var ShopError = class extends Error {
66
+ constructor({ code, original }, ...params) {
67
+ super(...params);
68
+ if (Error.captureStackTrace) {
69
+ Error.captureStackTrace(this, ShopError);
70
+ }
71
+ this.name = "ShopError";
72
+ this.code = code;
73
+ this.original = original;
74
+ }
75
+ };
76
+ function parseShopError(error) {
77
+ const errorCode = error?.code ?? error?.name ?? "";
78
+ const errorMessage = typeof error === "string" ? error : error?.message ?? "";
79
+ if (errorCode === "invalidMethodParameter" && errorMessage.includes("paymentMethod.create")) {
80
+ return new ShopError({
81
+ code: "paymentMethod.create.invalidMethodParameter",
82
+ original: error
83
+ }, "There was a problem validating your payment information. Please double-check the details and try again.");
84
+ }
85
+ return new ShopError({
86
+ code: "shop.unknown",
87
+ original: error
88
+ }, "An unknown error occurred.");
89
+ }
90
+
91
+ // src/basketItems.ts
92
+ async function setTipDonation({ amount, apollo }) {
93
+ let data;
94
+ let error;
95
+ try {
96
+ const result = await apollo.mutate({
97
+ mutation: import_core.gql`mutation setTipDonation($price: Money!, $basketId: String) {
98
+ shop (basketId: $basketId) {
99
+ id
100
+ updateDonation (donation: {
101
+ price: $price,
102
+ isTip: true
103
+ })
104
+ {
105
+ id
106
+ price
107
+ isTip
108
+ }
109
+ }
110
+ }`,
111
+ variables: {
112
+ price: (0, import_numeral.default)(amount).format("0.00"),
113
+ basketId: getBasketID()
114
+ }
115
+ });
116
+ if (result.error || result.errors.length) {
117
+ error = result.error ?? result.errors[0];
118
+ } else {
119
+ data = result.data;
120
+ }
121
+ } catch (e) {
122
+ error = e;
123
+ }
124
+ if (error) {
125
+ throw parseShopError(error);
126
+ }
127
+ return data?.shop?.updateDonation;
128
+ }
129
+
130
+ // src/oneTimeCheckout.ts
131
+ var import_core2 = require("@apollo/client/core");
132
+ async function executeOneTimeCheckout({ apollo }) {
133
+ }
134
+ async function waitOnTransaction({ apollo, transactionId }) {
135
+ const result = await apollo.query({
136
+ query: import_core2.gql`
137
+ query checkoutStatus($transactionId: String!, $visitorId: string) {
138
+ checkoutStatus(transactionId: $transactionId, visitorId: $visitorId) {
139
+ errorCode
140
+ errorMessage
141
+ status
142
+ transactionId
143
+ }
144
+ }
145
+ `,
146
+ variables: {
147
+ transactionId
148
+ }
149
+ });
150
+ }
151
+
152
+ // src/subscriptionCheckout.ts
153
+ var import_core3 = require("@apollo/client/core");
154
+ async function checkSubscriptionStatus(apollo) {
155
+ const { data: subsData } = await apollo.query({
156
+ query: import_core3.gql`query subscriptionStatus{
157
+ my {
158
+ id
159
+ subscriptions {
160
+ totalCount
161
+ }
162
+ autoDeposit {
163
+ id
164
+ isSubscriber
165
+ }
166
+ }
167
+ }`
168
+ });
169
+ if (!subsData?.my?.id) {
170
+ throw new ShopError({ code: "api.authenticationRequired" }, "You must be logged in to continue.");
171
+ }
172
+ if ((subsData?.my?.subscriptions?.totalCount ?? 0) > 0 || subsData?.my?.autoDeposit?.isSubscriber) {
173
+ throw new ShopError({ code: "shop.subscriptionExists" }, "You already have an existing Monthly Good subscription. Changes can be made in your subscription settings.");
174
+ }
175
+ if (subsData?.my?.autoDeposit?.id) {
176
+ throw new ShopError({ code: "shop.autoDepositExists" }, "You already have existing Auto Deposit settings. Changes can be made in your subscription settings.");
177
+ }
178
+ return true;
179
+ }
180
+ async function executeNewSubscriptionCheckout({
181
+ amount,
182
+ apollo,
183
+ dayOfMonth = (/* @__PURE__ */ new Date()).getDate(),
184
+ donateAmount,
185
+ paymentMethod
186
+ }) {
187
+ const amountRegex = new RegExp(/^\d+\.\d{2}$/);
188
+ if (!amountRegex.test(amount) || !amountRegex.test(donateAmount)) {
189
+ throw new ShopError({
190
+ code: "api.invalidMethodParameter"
191
+ }, "Please check that the amount is correct and try again.");
192
+ }
193
+ await checkSubscriptionStatus(apollo);
194
+ const { deviceData, nonce } = paymentMethod;
195
+ let data;
196
+ let error;
197
+ try {
198
+ const result = await apollo.query({
199
+ variables: {
200
+ dayOfMonth,
201
+ deviceData,
202
+ nonce
203
+ },
204
+ query: import_core3.gql`mutation createAutoDepositSubscription(
205
+ $nonce: String!,
206
+ $deviceData: String,
207
+ $amount: Money!,
208
+ $donateAmount: Money!,
209
+ $dayOfMonth: Int!
210
+ ) {
211
+ my {
212
+ createAutoDeposit (
213
+ autoDeposit: {
214
+ amount: $amount,
215
+ donateAmount: $donateAmount,
216
+ dayOfMonth: $dayOfMonth,
217
+ },
218
+ deviceData: $deviceData,
219
+ paymentMethodNonce: $nonce
220
+ ) {
221
+ id amount donateAmount dayOfMonth status
222
+ }
223
+ }
224
+ }`
225
+ });
226
+ if (result.error || result.errors.length) {
227
+ error = result.error ?? result.errors[0];
228
+ } else {
229
+ data = result.data;
230
+ }
231
+ } catch (e) {
232
+ error = e;
233
+ }
234
+ if (error) {
235
+ const parsed = parseShopError(error);
236
+ if (parsed.code === "shop.unknown") {
237
+ throw new ShopError({
238
+ code: "shop.createAutoDepositError",
239
+ original: parsed
240
+ }, "There was a problem trying to setup your monthly deposit.");
241
+ }
242
+ throw parsed;
243
+ }
244
+ return data?.my?.createAutoDeposit;
245
+ }
246
+
247
+ // src/useBraintreeDropIn.ts
248
+ var import_core4 = require("@apollo/client/core");
249
+ var import_numeral2 = __toESM(require("numeral"), 1);
250
+ var import_vue_demi = require("vue-demi");
251
+ var defaultPaymentTypes = ["paypal", "card", "applePay", "googlePay"];
252
+ async function getClientToken(apollo) {
253
+ const { data, error, errors } = await apollo.query({
254
+ query: import_core4.gql`query getClientToken {
255
+ shop {
256
+ id
257
+ getClientToken(useCustomerId: true)
258
+ }
259
+ }`
260
+ });
261
+ if (error || errors.length) {
262
+ throw parseShopError(error ?? errors[0]);
263
+ }
264
+ return data?.shop?.getClientToken;
265
+ }
266
+ function useBraintreeDropIn() {
267
+ let instance;
268
+ let formattedAmount = "";
269
+ const paymentMethodRequestable = (0, import_vue_demi.ref)(false);
270
+ function getApplePaymentRequest(amount) {
271
+ return {
272
+ countryCode: "US",
273
+ currencyCode: "USD",
274
+ // merchantCapabilities: ['supports3DS'], // TODO: confirm/update
275
+ requiredBillingContactFields: ["postalAddress"],
276
+ // supportedNetworks: ['amex', 'discover', 'interac', 'jcb', 'masterCard', 'visa'], // TODO: confirm/update
277
+ total: {
278
+ label: "Kiva",
279
+ amount
280
+ }
281
+ };
282
+ }
283
+ function getGoogleTransactionInfo(amount) {
284
+ return {
285
+ totalPriceStatus: "FINAL",
286
+ totalPrice: amount,
287
+ currencyCode: "USD",
288
+ countryCode: "US"
289
+ };
290
+ }
291
+ function initDropInActions() {
292
+ if (instance.isPaymentMethodRequestable()) {
293
+ paymentMethodRequestable.value = true;
294
+ }
295
+ instance.on("paymentMethodRequestable", (event) => {
296
+ paymentMethodRequestable.value = true;
297
+ });
298
+ instance.on("noPaymentMethodRequestable", () => {
299
+ paymentMethodRequestable.value = false;
300
+ });
301
+ }
302
+ async function initDropIn({
303
+ amount,
304
+ authToken,
305
+ container,
306
+ googlePayMerchantId,
307
+ paymentTypes = [...defaultPaymentTypes],
308
+ preselectVaultedPaymentMethod = true,
309
+ paypalFlow = "checkout"
310
+ }) {
311
+ formattedAmount = (0, import_numeral2.default)(amount).format("0.00");
312
+ const { default: DropIn } = await import("braintree-web-drop-in");
313
+ try {
314
+ instance = await DropIn.create({
315
+ authorization: authToken,
316
+ container,
317
+ dataCollector: {
318
+ kount: true
319
+ // Required if Kount fraud data collection is enabled
320
+ },
321
+ // vaultManager: true, - Useful for testing and removing payment methods easily.
322
+ paymentOptionPriority: paymentTypes,
323
+ preselectVaultedPaymentMethod,
324
+ card: {
325
+ vault: {
326
+ allowVaultCardOverride: true
327
+ }
328
+ },
329
+ paypal: {
330
+ flow: paypalFlow,
331
+ amount: formattedAmount,
332
+ currency: "USD",
333
+ buttonStyle: {
334
+ // @ts-ignore
335
+ color: "gold",
336
+ // @ts-ignore
337
+ shape: "rect",
338
+ // @ts-ignore
339
+ size: "responsive"
340
+ }
341
+ },
342
+ googlePay: {
343
+ googlePayVersion: 2,
344
+ merchantId: googlePayMerchantId,
345
+ transactionInfo: getGoogleTransactionInfo(formattedAmount),
346
+ button: {
347
+ allowedPaymentMethods: [{
348
+ type: "CARD",
349
+ // @ts-ignore
350
+ parameters: {
351
+ // allowedAuthMethods: ['PAN_ONLY', 'CRYPTOGRAM_3DS'], // TODO: confirm/update
352
+ // allowedCardNetworks: ['AMEX', 'DISCOVER', 'INTERAC', 'JCB', 'MASTERCARD', 'VISA'], // TODO: confirm/update
353
+ billingAddressRequired: true,
354
+ billingAddressParameters: {
355
+ format: "FULL"
356
+ }
357
+ }
358
+ }]
359
+ }
360
+ },
361
+ applePay: {
362
+ displayName: "Kiva",
363
+ paymentRequest: getApplePaymentRequest(formattedAmount)
364
+ }
365
+ });
366
+ initDropInActions();
367
+ } catch (e) {
368
+ throw new ShopError({
369
+ code: "shop.braintreeDropinInitError",
370
+ original: e
371
+ }, "An Error has occured. Please refresh the page and try again.");
372
+ }
373
+ return instance;
374
+ }
375
+ async function requestPaymentMethod() {
376
+ if (instance.isPaymentMethodRequestable()) {
377
+ return instance.requestPaymentMethod();
378
+ }
379
+ }
380
+ function updateAmount(amount) {
381
+ const newAmount = (0, import_numeral2.default)(amount).format("0.00");
382
+ if (newAmount !== formattedAmount) {
383
+ formattedAmount = newAmount;
384
+ instance?.updateConfiguration("paypal", "amount", formattedAmount);
385
+ instance?.updateConfiguration("googlePay", "transactionInfo", getGoogleTransactionInfo(formattedAmount));
386
+ instance?.updateConfiguration?.("applePay", "paymentRequest", getApplePaymentRequest(formattedAmount));
387
+ }
388
+ }
389
+ return {
390
+ initDropIn,
391
+ paymentMethodRequestable,
392
+ requestPaymentMethod,
393
+ updateAmount
394
+ };
395
+ }
396
+ // Annotate the CommonJS export names for ESM import in node:
397
+ 0 && (module.exports = {
398
+ ShopError,
399
+ checkSubscriptionStatus,
400
+ defaultPaymentTypes,
401
+ executeNewSubscriptionCheckout,
402
+ executeOneTimeCheckout,
403
+ getBasketID,
404
+ getClientToken,
405
+ getCookieValue,
406
+ parseShopError,
407
+ setBasketID,
408
+ setTipDonation,
409
+ useBraintreeDropIn,
410
+ waitOnTransaction
411
+ });
@@ -0,0 +1,8 @@
1
+ export { getBasketID, getCookieValue, setBasketID } from './basket.js';
2
+ export { SetTipDonationOptions, setTipDonation } from './basketItems.js';
3
+ export { OneTimeCheckoutOptions, WaitOnTransactionOptions, executeOneTimeCheckout, waitOnTransaction } from './oneTimeCheckout.js';
4
+ export { ShopError, ShopErrorOptions, parseShopError } from './shopError.js';
5
+ export { SubscriptionCheckoutOptions, checkSubscriptionStatus, executeNewSubscriptionCheckout } from './subscriptionCheckout.js';
6
+ export { DropInInitOptions, PayPalFlowType, PaymentType, defaultPaymentTypes, getClientToken, default as useBraintreeDropIn } from './useBraintreeDropIn.js';
7
+ import 'braintree-web-drop-in';
8
+ import '@vue/composition-api';
package/dist/index.js ADDED
@@ -0,0 +1,40 @@
1
+ import {
2
+ setTipDonation
3
+ } from "./chunk-KSEC5YWO.js";
4
+ import {
5
+ getBasketID,
6
+ getCookieValue,
7
+ setBasketID
8
+ } from "./chunk-F6QCMBHV.js";
9
+ import {
10
+ executeOneTimeCheckout,
11
+ waitOnTransaction
12
+ } from "./chunk-M4CJOCIQ.js";
13
+ import {
14
+ checkSubscriptionStatus,
15
+ executeNewSubscriptionCheckout
16
+ } from "./chunk-V2I7V4QJ.js";
17
+ import {
18
+ defaultPaymentTypes,
19
+ getClientToken,
20
+ useBraintreeDropIn
21
+ } from "./chunk-R27YZQKP.js";
22
+ import {
23
+ ShopError,
24
+ parseShopError
25
+ } from "./chunk-H35VQXDR.js";
26
+ export {
27
+ ShopError,
28
+ checkSubscriptionStatus,
29
+ defaultPaymentTypes,
30
+ executeNewSubscriptionCheckout,
31
+ executeOneTimeCheckout,
32
+ getBasketID,
33
+ getClientToken,
34
+ getCookieValue,
35
+ parseShopError,
36
+ setBasketID,
37
+ setTipDonation,
38
+ useBraintreeDropIn,
39
+ waitOnTransaction
40
+ };
@@ -0,0 +1,50 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/oneTimeCheckout.ts
20
+ var oneTimeCheckout_exports = {};
21
+ __export(oneTimeCheckout_exports, {
22
+ executeOneTimeCheckout: () => executeOneTimeCheckout,
23
+ waitOnTransaction: () => waitOnTransaction
24
+ });
25
+ module.exports = __toCommonJS(oneTimeCheckout_exports);
26
+ var import_core = require("@apollo/client/core");
27
+ async function executeOneTimeCheckout({ apollo }) {
28
+ }
29
+ async function waitOnTransaction({ apollo, transactionId }) {
30
+ const result = await apollo.query({
31
+ query: import_core.gql`
32
+ query checkoutStatus($transactionId: String!, $visitorId: string) {
33
+ checkoutStatus(transactionId: $transactionId, visitorId: $visitorId) {
34
+ errorCode
35
+ errorMessage
36
+ status
37
+ transactionId
38
+ }
39
+ }
40
+ `,
41
+ variables: {
42
+ transactionId
43
+ }
44
+ });
45
+ }
46
+ // Annotate the CommonJS export names for ESM import in node:
47
+ 0 && (module.exports = {
48
+ executeOneTimeCheckout,
49
+ waitOnTransaction
50
+ });
@@ -0,0 +1,13 @@
1
+ interface OneTimeCheckoutOptions {
2
+ apollo: any;
3
+ basketId: string;
4
+ visitorId: string | null | undefined;
5
+ }
6
+ declare function executeOneTimeCheckout({ apollo }: OneTimeCheckoutOptions): Promise<void>;
7
+ interface WaitOnTransactionOptions {
8
+ apollo: any;
9
+ transactionId: string;
10
+ }
11
+ declare function waitOnTransaction({ apollo, transactionId }: WaitOnTransactionOptions): Promise<void>;
12
+
13
+ export { OneTimeCheckoutOptions, WaitOnTransactionOptions, executeOneTimeCheckout, waitOnTransaction };
@@ -0,0 +1,8 @@
1
+ import {
2
+ executeOneTimeCheckout,
3
+ waitOnTransaction
4
+ } from "./chunk-M4CJOCIQ.js";
5
+ export {
6
+ executeOneTimeCheckout,
7
+ waitOnTransaction
8
+ };
@@ -0,0 +1,55 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/shopError.ts
20
+ var shopError_exports = {};
21
+ __export(shopError_exports, {
22
+ ShopError: () => ShopError,
23
+ parseShopError: () => parseShopError
24
+ });
25
+ module.exports = __toCommonJS(shopError_exports);
26
+ var ShopError = class extends Error {
27
+ constructor({ code, original }, ...params) {
28
+ super(...params);
29
+ if (Error.captureStackTrace) {
30
+ Error.captureStackTrace(this, ShopError);
31
+ }
32
+ this.name = "ShopError";
33
+ this.code = code;
34
+ this.original = original;
35
+ }
36
+ };
37
+ function parseShopError(error) {
38
+ const errorCode = error?.code ?? error?.name ?? "";
39
+ const errorMessage = typeof error === "string" ? error : error?.message ?? "";
40
+ if (errorCode === "invalidMethodParameter" && errorMessage.includes("paymentMethod.create")) {
41
+ return new ShopError({
42
+ code: "paymentMethod.create.invalidMethodParameter",
43
+ original: error
44
+ }, "There was a problem validating your payment information. Please double-check the details and try again.");
45
+ }
46
+ return new ShopError({
47
+ code: "shop.unknown",
48
+ original: error
49
+ }, "An unknown error occurred.");
50
+ }
51
+ // Annotate the CommonJS export names for ESM import in node:
52
+ 0 && (module.exports = {
53
+ ShopError,
54
+ parseShopError
55
+ });
@@ -0,0 +1,12 @@
1
+ interface ShopErrorOptions {
2
+ code: string;
3
+ original?: object;
4
+ }
5
+ declare class ShopError extends Error {
6
+ code: string;
7
+ original?: object;
8
+ constructor({ code, original }: ShopErrorOptions, ...params: any[]);
9
+ }
10
+ declare function parseShopError(error: any): ShopError;
11
+
12
+ export { ShopError, ShopErrorOptions, parseShopError };
@@ -0,0 +1,8 @@
1
+ import {
2
+ ShopError,
3
+ parseShopError
4
+ } from "./chunk-H35VQXDR.js";
5
+ export {
6
+ ShopError,
7
+ parseShopError
8
+ };