diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index 4bb5968..b4010e3 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -1,4 +1,4 @@ -name: Build and deploy +name: Build and test on: push: branches: diff --git a/CHANGELOG.md b/CHANGELOG.md index b574dbf..212d747 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +### v0.0.4 + +- Added method `getAllCards` +- Added method `getCustomer` +- Added method `setDefaultCard` +- Updated docs + ### v0.0.3 - Added method `addSourceToCustomer` diff --git a/README.md b/README.md index 8aa6bd4..fea7227 100644 --- a/README.md +++ b/README.md @@ -26,11 +26,53 @@ const stripe = await loadStripe("pk_test_TYooMQauvdEDq54NiTphI7jx"); ## Additional Methods -| Method | Arguments | Description | Example | -| -------------------------- | ------------------------------------------------- | ------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| confirmPaymentIntentByCard | [client_secret], [card_id] | Confirm payment with the user's payment intent card. | `stripe.confirmPaymentIntentByCard('pi_3Jrk80HdlMaZle3e1tGtSxiH_secret_mWdWNlqJfkYEoYOml1GqRPyPm', 'card_1JrMi8HdlMaZle3eSPPOvapJ')` | -| addSourceToCustomer | [source or token], [customer_id], [ephemeral_key] | Add payment method to customer (from source or token). | `stripe.addSourceToCustomer('tok_visa', 'cus_KO9SkBdMeHoMXR', 'ek_test_YWNjdF8xSFhSd0xIZGxNYVpsZTNlLENrVUxKWWNjZExxSDJDb1VKa1YwaXU5VDZVcmVmQXQ_00drAg7pBQ')` | -| deleteSourceFromCustomer | [source_id], [customer_id], [ephemeral_key] | Delete payment method from customer. | `stripe.deleteSourceFromCustomer('card_1JroRSHdlMaZle3e4EIGOZuv', 'cus_KO9SkBdMeHoMXR', 'ek_test_YWNjdF8xSFhSd0xIZGxNYVpsZTNlLENrVUxKWWNjZExxSDJDb1VKa1YwaXU5VDZVcmVmQXQ_00drAg7pBQ')` | +| Method | Arguments | Description | +| -------------------------- | ------------------------------------------------- | ------------------------------------------------------ | +| confirmPaymentIntentByCard | [client_secret], [card_id] | Confirm payment with the user's payment intent card. | +| addSourceToCustomer | [source or token], [customer_id], [ephemeral_key] | Add payment method to customer (from source or token). | +| deleteSourceFromCustomer | [source_id], [customer_id], [ephemeral_key] | Delete payment method from customer. | +| getAllCards | [ephemeral_key], [customer_id] | Get all cards from customer. | +| getCustomer | [ephemeral_key], [customer_id] | Get customer. +| +| setDefaultCard | [defaultCardId], [ephemeral_key], [customer_id] | Set default card. +| + +## Examples + +``` +stripe.confirmPaymentIntentByCard( + 'pi_3Jrk80HdlMaZle3e1tGtSxiH_secret_mWdWNlqJfkYEoYOml1GqRPyPm', + 'card_1JrMi8HdlMaZle3eSPPOvapJ' +); + +stripe.addSourceToCustomer( + 'tok_visa', + 'cus_KO9SkBdMeHoMXR', + 'ek_test_YWNjdF8xSFhSd0xIZGxNYVpsZTNlLENrVUxKWWNjZExxSDJDb1VKa1YwaXU5VDZVcmVmQXQ_00drAg7pBQ' +); + +stripe.deleteSourceFromCustomer( + 'card_1JroRSHdlMaZle3e4EIGOZuv', + 'cus_KO9SkBdMeHoMXR', + 'ek_test_YWNjdF8xSFhSd0xIZGxNYVpsZTNlLENrVUxKWWNjZExxSDJDb1VKa1YwaXU5VDZVcmVmQXQ_00drAg7pBQ' +); + +stripe.getAllCards( + 'ek_test_YWNjdF8xSFhSd0xIZGxNYVpsZTNlLENrVUxKWWNjZExxSDJDb1VKa1YwaXU5VDZVcmVmQXQ_00drAg7pBQ', + 'cus_KO9SkBdMeHoMXR' +); + +stripe.getCustomer( + 'ek_test_YWNjdF8xSFhSd0xIZGxNYVpsZTNlLENrVUxKWWNjZExxSDJDb1VKa1YwaXU5VDZVcmVmQXQ_00drAg7pBQ', + 'cus_KO9SkBdMeHoMXR' +); + +stripe.setDefaultCard( + 'card_1JrMi8HdlMaZle3eSPPOvapJ', + 'ek_test_YWNjdF8xSFhSd0xIZGxNYVpsZTNlLENrVUxKWWNjZExxSDJDb1VKa1YwaXU5VDZVcmVmQXQ_00drAg7pBQ', + 'cus_KO9SkBdMeHoMXR' +); +``` ## Scripts diff --git a/package-lock.json b/package-lock.json index e608475..b454992 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@remedyproduct/stripe-js", - "version": "0.0.1", + "version": "0.0.4", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@remedyproduct/stripe-js", - "version": "0.0.1", + "version": "0.0.4", "license": "MIT", "dependencies": { "@stripe/stripe-js": "^1.21.0" @@ -75,21 +75,6 @@ "url": "https://opencollective.com/babel" } }, - "node_modules/@babel/core/node_modules/json5": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.0.tgz", - "integrity": "sha512-f+8cldu7X/y7RAJurMEJmdoKXGB/X550w2Nr3tTbezL6RwEE/iMcm+tZnXeoZtKuOq6ft8+CqzEkrIgx1fPoQA==", - "dev": true, - "dependencies": { - "minimist": "^1.2.5" - }, - "bin": { - "json5": "lib/cli.js" - }, - "engines": { - "node": ">=6" - } - }, "node_modules/@babel/core/node_modules/semver": { "version": "6.3.0", "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz", @@ -690,9 +675,9 @@ } }, "node_modules/@stripe/stripe-js": { - "version": "1.21.0", - "resolved": "https://registry.npmjs.org/@stripe/stripe-js/-/stripe-js-1.21.0.tgz", - "integrity": "sha512-6C3XsUXZm81G8NFcTqRIBVCYSMau3a2hUkoKSSYIK8NNbzwWFM8eV6BiTUOjMmNsSg/VBn97VmKM0pHVlCAtJA==" + "version": "1.21.1", + "resolved": "https://registry.npmjs.org/@stripe/stripe-js/-/stripe-js-1.21.1.tgz", + "integrity": "sha512-/HhRol0Bia/6L7JstXUOpg3m0U3nBW8c2tvaBE6QdonN+OMusYT9AIqCMR/PyzoF3ROifFJ2kbWT7xQjbKN3tw==" }, "node_modules/@types/json-schema": { "version": "7.0.9", @@ -1156,9 +1141,9 @@ } }, "node_modules/caniuse-lite": { - "version": "1.0.30001275", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001275.tgz", - "integrity": "sha512-ihJVvj8RX0kn9GgP43HKhb5q9s2XQn4nEQhdldEJvZhCsuiB2XOq6fAMYQZaN6FPWfsr2qU0cdL0CSbETwbJAg==", + "version": "1.0.30001278", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001278.tgz", + "integrity": "sha512-mpF9KeH8u5cMoEmIic/cr7PNS+F5LWBk0t2ekGT60lFf0Wq+n9LspAj0g3P+o7DQhD3sUdlMln4YFAWhFYn9jg==", "dev": true, "funding": { "type": "opencollective", @@ -1369,15 +1354,6 @@ "node": ">=8" } }, - "node_modules/default-require-extensions/node_modules/strip-bom": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-4.0.0.tgz", - "integrity": "sha512-3xurFv5tEgii33Zi8Jtp55wEIILR9eh34FAW00PZf+JnSsTmV/ioewSgQl97JHvgjoRGwPShsWm+IdrxB35d0w==", - "dev": true, - "engines": { - "node": ">=8" - } - }, "node_modules/define-properties": { "version": "1.1.3", "resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.1.3.tgz", @@ -1424,9 +1400,9 @@ } }, "node_modules/electron-to-chromium": { - "version": "1.3.887", - "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.3.887.tgz", - "integrity": "sha512-QQUumrEjFDKSVYVdaeBmFdyQGoaV+fCSMyWHvfx/u22bRHSTeBQYt6P4jMY+gFd4kgKB9nqk7RMtWkDB49OYPA==", + "version": "1.3.889", + "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.3.889.tgz", + "integrity": "sha512-suEUoPTD1mExjL9TdmH7cvEiWJVM2oEiAi+Y1p0QKxI2HcRlT44qDTP2c1aZmVwRemIPYOpxmV7CxQCOWcm4XQ==", "dev": true }, "node_modules/emoji-regex": { @@ -2205,9 +2181,9 @@ } }, "node_modules/glob": { - "version": "7.2.0", - "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.0.tgz", - "integrity": "sha512-lmLf6gtyrPq8tTjSmrO94wBeQbFR3HbLHbuyD69wuyQkImp2hWqMGB47OX65FBkPffO641IP9jWa1z4ivqG26Q==", + "version": "7.1.7", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.7.tgz", + "integrity": "sha512-OvD9ENzPLbegENnYP5UUfJIirTg4+XwMWGaQfQTY0JenxNvvIKP3U3/tAQSPIu/lHxXYSZmpXlUHeqAIdKzBLQ==", "dev": true, "dependencies": { "fs.realpath": "^1.0.0", @@ -2878,15 +2854,18 @@ "dev": true }, "node_modules/json5": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/json5/-/json5-1.0.1.tgz", - "integrity": "sha512-aKS4WQjPenRxiQsC93MNfjx+nbF4PAdYzmd/1JIj8HYzqfbu86beTuNgXDzPknWk0n0uARlyewZo4s++ES36Ow==", + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.0.tgz", + "integrity": "sha512-f+8cldu7X/y7RAJurMEJmdoKXGB/X550w2Nr3tTbezL6RwEE/iMcm+tZnXeoZtKuOq6ft8+CqzEkrIgx1fPoQA==", "dev": true, "dependencies": { - "minimist": "^1.2.0" + "minimist": "^1.2.5" }, "bin": { "json5": "lib/cli.js" + }, + "engines": { + "node": ">=6" } }, "node_modules/jsonc-parser": { @@ -3102,26 +3081,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/mocha/node_modules/glob": { - "version": "7.1.7", - "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.7.tgz", - "integrity": "sha512-OvD9ENzPLbegENnYP5UUfJIirTg4+XwMWGaQfQTY0JenxNvvIKP3U3/tAQSPIu/lHxXYSZmpXlUHeqAIdKzBLQ==", - "dev": true, - "dependencies": { - "fs.realpath": "^1.0.0", - "inflight": "^1.0.4", - "inherits": "2", - "minimatch": "^3.0.4", - "once": "^1.3.0", - "path-is-absolute": "^1.0.0" - }, - "engines": { - "node": "*" - }, - "funding": { - "url": "https://github.com/sponsors/isaacs" - } - }, "node_modules/mocha/node_modules/locate-path": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-6.0.0.tgz", @@ -4061,12 +4020,12 @@ } }, "node_modules/strip-bom": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-3.0.0.tgz", - "integrity": "sha1-IzTBjpx1n3vdVv3vfprj1YjmjtM=", + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-4.0.0.tgz", + "integrity": "sha512-3xurFv5tEgii33Zi8Jtp55wEIILR9eh34FAW00PZf+JnSsTmV/ioewSgQl97JHvgjoRGwPShsWm+IdrxB35d0w==", "dev": true, "engines": { - "node": ">=4" + "node": ">=8" } }, "node_modules/strip-json-comments": { @@ -4146,6 +4105,27 @@ "strip-bom": "^3.0.0" } }, + "node_modules/tsconfig-paths/node_modules/json5": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/json5/-/json5-1.0.1.tgz", + "integrity": "sha512-aKS4WQjPenRxiQsC93MNfjx+nbF4PAdYzmd/1JIj8HYzqfbu86beTuNgXDzPknWk0n0uARlyewZo4s++ES36Ow==", + "dev": true, + "dependencies": { + "minimist": "^1.2.0" + }, + "bin": { + "json5": "lib/cli.js" + } + }, + "node_modules/tsconfig-paths/node_modules/strip-bom": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-3.0.0.tgz", + "integrity": "sha1-IzTBjpx1n3vdVv3vfprj1YjmjtM=", + "dev": true, + "engines": { + "node": ">=4" + } + }, "node_modules/tslib": { "version": "1.14.1", "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz", @@ -4231,6 +4211,26 @@ "typescript": "4.0.x || 4.1.x || 4.2.x || 4.3.x || 4.4.x" } }, + "node_modules/typedoc/node_modules/glob": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.0.tgz", + "integrity": "sha512-lmLf6gtyrPq8tTjSmrO94wBeQbFR3HbLHbuyD69wuyQkImp2hWqMGB47OX65FBkPffO641IP9jWa1z4ivqG26Q==", + "dev": true, + "dependencies": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.0.4", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + }, + "engines": { + "node": "*" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, "node_modules/typescript": { "version": "4.4.4", "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.4.4.tgz", @@ -4510,15 +4510,6 @@ "source-map": "^0.5.0" }, "dependencies": { - "json5": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.0.tgz", - "integrity": "sha512-f+8cldu7X/y7RAJurMEJmdoKXGB/X550w2Nr3tTbezL6RwEE/iMcm+tZnXeoZtKuOq6ft8+CqzEkrIgx1fPoQA==", - "dev": true, - "requires": { - "minimist": "^1.2.5" - } - }, "semver": { "version": "6.3.0", "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz", @@ -4987,9 +4978,9 @@ } }, "@stripe/stripe-js": { - "version": "1.21.0", - "resolved": "https://registry.npmjs.org/@stripe/stripe-js/-/stripe-js-1.21.0.tgz", - "integrity": "sha512-6C3XsUXZm81G8NFcTqRIBVCYSMau3a2hUkoKSSYIK8NNbzwWFM8eV6BiTUOjMmNsSg/VBn97VmKM0pHVlCAtJA==" + "version": "1.21.1", + "resolved": "https://registry.npmjs.org/@stripe/stripe-js/-/stripe-js-1.21.1.tgz", + "integrity": "sha512-/HhRol0Bia/6L7JstXUOpg3m0U3nBW8c2tvaBE6QdonN+OMusYT9AIqCMR/PyzoF3ROifFJ2kbWT7xQjbKN3tw==" }, "@types/json-schema": { "version": "7.0.9", @@ -5300,9 +5291,9 @@ "dev": true }, "caniuse-lite": { - "version": "1.0.30001275", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001275.tgz", - "integrity": "sha512-ihJVvj8RX0kn9GgP43HKhb5q9s2XQn4nEQhdldEJvZhCsuiB2XOq6fAMYQZaN6FPWfsr2qU0cdL0CSbETwbJAg==", + "version": "1.0.30001278", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001278.tgz", + "integrity": "sha512-mpF9KeH8u5cMoEmIic/cr7PNS+F5LWBk0t2ekGT60lFf0Wq+n9LspAj0g3P+o7DQhD3sUdlMln4YFAWhFYn9jg==", "dev": true }, "chai": { @@ -5463,14 +5454,6 @@ "dev": true, "requires": { "strip-bom": "^4.0.0" - }, - "dependencies": { - "strip-bom": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-4.0.0.tgz", - "integrity": "sha512-3xurFv5tEgii33Zi8Jtp55wEIILR9eh34FAW00PZf+JnSsTmV/ioewSgQl97JHvgjoRGwPShsWm+IdrxB35d0w==", - "dev": true - } } }, "define-properties": { @@ -5507,9 +5490,9 @@ } }, "electron-to-chromium": { - "version": "1.3.887", - "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.3.887.tgz", - "integrity": "sha512-QQUumrEjFDKSVYVdaeBmFdyQGoaV+fCSMyWHvfx/u22bRHSTeBQYt6P4jMY+gFd4kgKB9nqk7RMtWkDB49OYPA==", + "version": "1.3.889", + "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.3.889.tgz", + "integrity": "sha512-suEUoPTD1mExjL9TdmH7cvEiWJVM2oEiAi+Y1p0QKxI2HcRlT44qDTP2c1aZmVwRemIPYOpxmV7CxQCOWcm4XQ==", "dev": true }, "emoji-regex": { @@ -6105,9 +6088,9 @@ } }, "glob": { - "version": "7.2.0", - "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.0.tgz", - "integrity": "sha512-lmLf6gtyrPq8tTjSmrO94wBeQbFR3HbLHbuyD69wuyQkImp2hWqMGB47OX65FBkPffO641IP9jWa1z4ivqG26Q==", + "version": "7.1.7", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.7.tgz", + "integrity": "sha512-OvD9ENzPLbegENnYP5UUfJIirTg4+XwMWGaQfQTY0JenxNvvIKP3U3/tAQSPIu/lHxXYSZmpXlUHeqAIdKzBLQ==", "dev": true, "requires": { "fs.realpath": "^1.0.0", @@ -6580,12 +6563,12 @@ "dev": true }, "json5": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/json5/-/json5-1.0.1.tgz", - "integrity": "sha512-aKS4WQjPenRxiQsC93MNfjx+nbF4PAdYzmd/1JIj8HYzqfbu86beTuNgXDzPknWk0n0uARlyewZo4s++ES36Ow==", + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.0.tgz", + "integrity": "sha512-f+8cldu7X/y7RAJurMEJmdoKXGB/X550w2Nr3tTbezL6RwEE/iMcm+tZnXeoZtKuOq6ft8+CqzEkrIgx1fPoQA==", "dev": true, "requires": { - "minimist": "^1.2.0" + "minimist": "^1.2.5" } }, "jsonc-parser": { @@ -6747,20 +6730,6 @@ "path-exists": "^4.0.0" } }, - "glob": { - "version": "7.1.7", - "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.7.tgz", - "integrity": "sha512-OvD9ENzPLbegENnYP5UUfJIirTg4+XwMWGaQfQTY0JenxNvvIKP3U3/tAQSPIu/lHxXYSZmpXlUHeqAIdKzBLQ==", - "dev": true, - "requires": { - "fs.realpath": "^1.0.0", - "inflight": "^1.0.4", - "inherits": "2", - "minimatch": "^3.0.4", - "once": "^1.3.0", - "path-is-absolute": "^1.0.0" - } - }, "locate-path": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-6.0.0.tgz", @@ -7458,9 +7427,9 @@ } }, "strip-bom": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-3.0.0.tgz", - "integrity": "sha1-IzTBjpx1n3vdVv3vfprj1YjmjtM=", + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-4.0.0.tgz", + "integrity": "sha512-3xurFv5tEgii33Zi8Jtp55wEIILR9eh34FAW00PZf+JnSsTmV/ioewSgQl97JHvgjoRGwPShsWm+IdrxB35d0w==", "dev": true }, "strip-json-comments": { @@ -7520,6 +7489,23 @@ "json5": "^1.0.1", "minimist": "^1.2.0", "strip-bom": "^3.0.0" + }, + "dependencies": { + "json5": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/json5/-/json5-1.0.1.tgz", + "integrity": "sha512-aKS4WQjPenRxiQsC93MNfjx+nbF4PAdYzmd/1JIj8HYzqfbu86beTuNgXDzPknWk0n0uARlyewZo4s++ES36Ow==", + "dev": true, + "requires": { + "minimist": "^1.2.0" + } + }, + "strip-bom": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-3.0.0.tgz", + "integrity": "sha1-IzTBjpx1n3vdVv3vfprj1YjmjtM=", + "dev": true + } } }, "tslib": { @@ -7578,6 +7564,22 @@ "marked": "^3.0.8", "minimatch": "^3.0.4", "shiki": "^0.9.12" + }, + "dependencies": { + "glob": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.0.tgz", + "integrity": "sha512-lmLf6gtyrPq8tTjSmrO94wBeQbFR3HbLHbuyD69wuyQkImp2hWqMGB47OX65FBkPffO641IP9jWa1z4ivqG26Q==", + "dev": true, + "requires": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.0.4", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + } + } } }, "typescript": { diff --git a/package.json b/package.json index 4e653bc..9b53ba4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@remedyproduct/stripe-js", - "version": "0.0.3", + "version": "0.0.4", "description": "Additional methods for working with stripe-js", "main": "./lib/index.js", "scripts": { diff --git a/src/index.ts b/src/index.ts index 0f1fc74..fd3f96a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,13 +3,12 @@ import { StripeConstructorOptions, Stripe as StripeDefault, } from "@stripe/stripe-js"; -import { additionalMethods } from "./methods"; -import { setApiKey } from "./utils/store"; +import { RemedyProductStripe } from "./methods/index"; /** * Stripe default interface */ -interface StripeDefaultWithInternal extends StripeDefault { +export interface StripeDefaultWithInternal extends StripeDefault { /** * Stripe api key after initialization, like pk_... */ @@ -17,9 +16,9 @@ interface StripeDefaultWithInternal extends StripeDefault { } /** - * Stripe patched interface + * Stripe patched library */ -export interface Stripe extends StripeDefaultWithInternal, additionalMethods {} +export interface Stripe extends RemedyProductStripe, StripeDefault {} /** * Initialize stripe @@ -41,15 +40,9 @@ export const loadStripe = async ( typeof (stripeDefault as StripeDefaultWithInternal)?._apiKey !== "string" ) throw new Error("Initialization error."); - const apiKey = (stripeDefault as StripeDefaultWithInternal)._apiKey; - setApiKey(apiKey); - const methods = new additionalMethods(); - - const stripe: Stripe = { - _apiKey: apiKey, - ...stripeDefault, - ...methods, - }; - + const remedyProductStripe = new RemedyProductStripe( + (stripeDefault as StripeDefaultWithInternal)._apiKey + ); + const stripe: Stripe = Object.assign(remedyProductStripe, stripeDefault); return stripe; }; diff --git a/src/methods/addSourceToCustomer.ts b/src/methods/addSourceToCustomer.ts index 05e4754..fdc0f08 100644 --- a/src/methods/addSourceToCustomer.ts +++ b/src/methods/addSourceToCustomer.ts @@ -1,7 +1,7 @@ import { Card } from "@stripe/stripe-js"; import { responseHandler } from "../utils/handlers"; import { stripeApiUrl, stripeApiVersion } from "../utils/constants"; -import { getApiKey } from "../utils/store"; +import { RemedyProductStripe } from "./index"; /** * Add payment method to customer (from source or token). @@ -11,12 +11,15 @@ import { getApiKey } from "../utils/store"; * @param customerEphemeralKey - customer ephemeral key * @returns */ -export const addSourceToCustomer = async ( +export const addSourceToCustomer = async function ( + this: RemedyProductStripe, token: string, customerId: string, customerEphemeralKey: string -): Promise => { - const stripeApiKey = getApiKey() as string; +): Promise { + /* eslint-disable */ + const stripeApiKey = this._apiKey; + /* eslint-enable */ if (typeof stripeApiKey !== "string") throw new Error("Initialization failed."); diff --git a/src/methods/confirmPaymentIntentByCard.ts b/src/methods/confirmPaymentIntentByCard.ts index eb360be..71b548e 100644 --- a/src/methods/confirmPaymentIntentByCard.ts +++ b/src/methods/confirmPaymentIntentByCard.ts @@ -1,7 +1,7 @@ import { PaymentIntentResult } from "@stripe/stripe-js"; import { responseHandler } from "../utils/handlers"; import { stripeApiUrl, stripeApiVersion } from "../utils/constants"; -import { getApiKey } from "../utils/store"; +import { RemedyProductStripe } from "./index"; /** * Confirm payment intent by customer's card @@ -10,11 +10,14 @@ import { getApiKey } from "../utils/store"; * @param paymentMethodId - stripe customer payment method id (see: https://stripe.com/docs/api/cards/object#card_object-id) * @returns */ -export const confirmPaymentIntentByCard = async ( +export const confirmPaymentIntentByCard = async function ( + this: RemedyProductStripe, paymentIntentSecret: string, paymentMethodId: string -): Promise => { - const stripeApiKey = getApiKey() as string; +): Promise { + /* eslint-disable */ + const stripeApiKey = this._apiKey; + /* eslint-enable */ if (typeof stripeApiKey !== "string") throw new Error("Initialization failed."); diff --git a/src/methods/deleteSourceFromCustomer.ts b/src/methods/deleteSourceFromCustomer.ts index d26665d..9c04f28 100644 --- a/src/methods/deleteSourceFromCustomer.ts +++ b/src/methods/deleteSourceFromCustomer.ts @@ -1,7 +1,7 @@ import { Card } from "@stripe/stripe-js"; import { responseHandler } from "../utils/handlers"; import { stripeApiUrl, stripeApiVersion } from "../utils/constants"; -import { getApiKey } from "../utils/store"; +import { RemedyProductStripe } from "./index"; /** * Delete payment method from customer. @@ -11,12 +11,15 @@ import { getApiKey } from "../utils/store"; * @param customerEphemeralKey - customer ephemeral key * @returns */ -export const deleteSourceFromCustomer = async ( +export const deleteSourceFromCustomer = async function ( + this: RemedyProductStripe, sourceId: string, customerId: string, customerEphemeralKey: string -): Promise => { - const stripeApiKey = getApiKey() as string; +): Promise { + /* eslint-disable */ + const stripeApiKey = this._apiKey; + /* eslint-enable */ if (typeof stripeApiKey !== "string") throw new Error("Initialization failed."); diff --git a/src/methods/getAllCards.ts b/src/methods/getAllCards.ts new file mode 100644 index 0000000..78bd646 --- /dev/null +++ b/src/methods/getAllCards.ts @@ -0,0 +1,35 @@ +import { responseHandler } from "../utils/handlers"; +import { stripeApiUrl, stripeApiVersion } from "../utils/constants"; +import { RemedyProductStripe } from "."; + +/** + * Confirm payment intent by customer's card + * + * @param customerKey - customer ephemeral key + * @param customerId - customer id (see: https://stripe.com/docs/api/customers/object#customer_object-id) + * @returns + */ + +export const getAllCards = async function ( + this: RemedyProductStripe, + customerKey: string, + customerId: string +) { + /* eslint-disable */ + const stripeApiKey = this._apiKey; + /* eslint-enable */ + if (typeof stripeApiKey !== "string") + throw new Error("Initialization failed."); + + // make request + return await fetch( + `${stripeApiUrl}/payment_methods?customer=${customerId}&type=card&limit=100`, + { + headers: { + Authorization: `Bearer ${customerKey}`, + "Stripe-Version": stripeApiVersion, + }, + method: "GET", + } + ).then(responseHandler); +}; diff --git a/src/methods/getCustomer.ts b/src/methods/getCustomer.ts new file mode 100644 index 0000000..be0cdbe --- /dev/null +++ b/src/methods/getCustomer.ts @@ -0,0 +1,31 @@ +import { responseHandler } from "../utils/handlers"; +import { stripeApiUrl, stripeApiVersion } from "../utils/constants"; +import { RemedyProductStripe } from "."; + +/** + * Confirm payment intent by customer's card + * + * @param customerKey - customer ephemeral key + * @param customerId - customer id (see: https://stripe.com/docs/api/customers/object#customer_object-id) + * @returns + */ + +export const getCustomer = async function ( + this: RemedyProductStripe, + customerKey: string, + customerId: string +) { + /* eslint-disable */ + const stripeApiKey = this._apiKey; + /* eslint-enable */ + if (typeof stripeApiKey !== "string") + throw new Error("Initialization failed."); + + // make request + return await fetch(`${stripeApiUrl}/customers/${customerId}`, { + headers: { + Authorization: `Bearer ${customerKey}`, + "Stripe-Version": stripeApiVersion, + }, + }).then(responseHandler); +}; diff --git a/src/methods/index.ts b/src/methods/index.ts index 42fff59..f3c888a 100644 --- a/src/methods/index.ts +++ b/src/methods/index.ts @@ -1,12 +1,22 @@ import { confirmPaymentIntentByCard } from "./confirmPaymentIntentByCard"; import { addSourceToCustomer } from "./addSourceToCustomer"; import { deleteSourceFromCustomer } from "./deleteSourceFromCustomer"; +import { getAllCards } from "./getAllCards"; +import { getCustomer } from "./getCustomer"; +import { setDefaultCard } from "./setDefaultCard"; /** * additional stripe methods */ -export class additionalMethods { +export class RemedyProductStripe { + constructor(apiKey: string) { + this._apiKey = apiKey; + } + public _apiKey: string; public confirmPaymentIntentByCard = confirmPaymentIntentByCard; public addSourceToCustomer = addSourceToCustomer; public deleteSourceFromCustomer = deleteSourceFromCustomer; + public getAllCards = getAllCards; + public getCustomer = getCustomer; + public setDefaultCard = setDefaultCard; } diff --git a/src/methods/setDefaultCard.ts b/src/methods/setDefaultCard.ts new file mode 100644 index 0000000..6f3b539 --- /dev/null +++ b/src/methods/setDefaultCard.ts @@ -0,0 +1,36 @@ +import { responseHandler } from "../utils/handlers"; +import { stripeApiUrl, stripeApiVersion } from "../utils/constants"; +import { RemedyProductStripe } from "."; + +/** + * Confirm payment intent by customer's card + * + * @param defaultCardId customer default card id + * @param customerKey - customer ephemeral key + * @param customerId - customer id (see: https://stripe.com/docs/api/customers/object#customer_object-id) + * @returns + */ + +export const setDefaultCard = async function ( + this: RemedyProductStripe, + defaultCardId: string, + customerKey: string, + customerId: string +) { + /* eslint-disable */ + const stripeApiKey = this._apiKey; + /* eslint-enable */ + if (typeof stripeApiKey !== "string") + throw new Error("Initialization failed."); + + // make request + return await fetch(`${stripeApiUrl}/customers/${customerId}`, { + body: `default_source=${defaultCardId}`, + headers: { + Authorization: `Bearer ${customerKey}`, + "Content-Type": "application/x-www-form-urlencoded", + "Stripe-Version": stripeApiVersion, + }, + method: "POST", + }).then(responseHandler); +}; diff --git a/src/utils/store.ts b/src/utils/store.ts deleted file mode 100644 index 8698487..0000000 --- a/src/utils/store.ts +++ /dev/null @@ -1,6 +0,0 @@ -const store: { - apiKey?: string; -} = {}; - -export const setApiKey = (apiKey: string) => (store.apiKey = apiKey); -export const getApiKey = () => store.apiKey;