From 308a714e425558ae4a15d6277f6d55a3c51f2e5c Mon Sep 17 00:00:00 2001 From: Siarhei Dudko Date: Wed, 3 Nov 2021 21:48:31 +0300 Subject: [PATCH 1/5] new methods --- .github/workflows/build-and-test.yml | 37 +++++++++++++++++++++++ CHANGELOG.md | 5 +++ README.md | 8 +++-- src/methods/addSourceToCustomer.ts | 33 ++++++++++++++++++++ src/methods/confirmPaymentIntentByCard.ts | 11 ++++--- src/methods/deleteSourceFromCustomer.ts | 34 +++++++++++++++++++++ src/methods/index.ts | 4 +++ src/utils/constants.ts | 3 +- 8 files changed, 126 insertions(+), 9 deletions(-) create mode 100644 .github/workflows/build-and-test.yml create mode 100644 src/methods/addSourceToCustomer.ts create mode 100644 src/methods/deleteSourceFromCustomer.ts diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml new file mode 100644 index 0000000..4ed32dd --- /dev/null +++ b/.github/workflows/build-and-test.yml @@ -0,0 +1,37 @@ +name: Build and deploy +on: + push: + branches: + - "!master" +jobs: + build: + runs-on: ubuntu-latest + timeout-minutes: 5 + env: + NODE_VERSION: 14 + steps: + - name: Сheckout repo + uses: actions/checkout@v2 + - name: Use Node.js ${{ env.NODE_VERSION }} + uses: actions/setup-node@v1 + with: + node-version: ${{ env.NODE_VERSION }} + - name: Cache node modules + uses: actions/cache@v1 + env: + cache-name: cache-node-modules + with: + path: ~/.npm + key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }} + restore-keys: | + ${{ runner.os }}-build-${{ env.cache-name }}- + ${{ runner.os }}-build- + ${{ runner.os }}- + - name: Install dependencies + run: npm ci + - name: Run linter + run: npm run lint + - name: Run builder + run: npm run build + - name: Run builder + run: npm run test diff --git a/CHANGELOG.md b/CHANGELOG.md index 53c8b71..ed2aa3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +### v0.0.3 + +- Added method `addSourceToCustomer` +- Added method `deleteSourceFromCustomer` + ### v0.0.2 - Updating documentation diff --git a/README.md b/README.md index cbfe34a..8aa6bd4 100644 --- a/README.md +++ b/README.md @@ -26,9 +26,11 @@ const stripe = await loadStripe("pk_test_TYooMQauvdEDq54NiTphI7jx"); ## Additional Methods -| Method | Arguments | Description | -| -------------------------- | -------------------------- | ---------------------------------------------------- | -| confirmPaymentIntentByCard | [client_secret], [card_id] | Confirm payment with the user's payment intent card. | +| 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')` | ## Scripts diff --git a/src/methods/addSourceToCustomer.ts b/src/methods/addSourceToCustomer.ts new file mode 100644 index 0000000..05e4754 --- /dev/null +++ b/src/methods/addSourceToCustomer.ts @@ -0,0 +1,33 @@ +import { Card } from "@stripe/stripe-js"; +import { responseHandler } from "../utils/handlers"; +import { stripeApiUrl, stripeApiVersion } from "../utils/constants"; +import { getApiKey } from "../utils/store"; + +/** + * Add payment method to customer (from source or token). + * + * @param source - source or token string (see: https://stripe.com/docs/api/sources/object) + * @param customerId - customer id (see: https://stripe.com/docs/api/customers/object#customer_object-id) + * @param customerEphemeralKey - customer ephemeral key + * @returns + */ +export const addSourceToCustomer = async ( + token: string, + customerId: string, + customerEphemeralKey: string +): Promise => { + const stripeApiKey = getApiKey() as string; + if (typeof stripeApiKey !== "string") + throw new Error("Initialization failed."); + + // make request + return fetch(`${stripeApiUrl}/customers/${customerId}/sources`, { + body: `source=${token}`, + headers: { + Authorization: `Bearer ${customerEphemeralKey}`, + "Content-Type": "application/x-www-form-urlencoded", + "Stripe-Version": `${stripeApiVersion}`, + }, + method: "POST", + }).then(responseHandler); +}; diff --git a/src/methods/confirmPaymentIntentByCard.ts b/src/methods/confirmPaymentIntentByCard.ts index bdec41a..eb360be 100644 --- a/src/methods/confirmPaymentIntentByCard.ts +++ b/src/methods/confirmPaymentIntentByCard.ts @@ -1,19 +1,19 @@ import { PaymentIntentResult } from "@stripe/stripe-js"; import { responseHandler } from "../utils/handlers"; -import { stripeApiUrl } from "../utils/constants"; +import { stripeApiUrl, stripeApiVersion } from "../utils/constants"; import { getApiKey } from "../utils/store"; /** * Confirm payment intent by customer's card * - * @param paymentIntentSecret - stripe payment intent secret - * @param paymentMethodId - stripe customer payment method id + * @param paymentIntentSecret - stripe payment intent secret (see: https://stripe.com/docs/api/payment_intents/object#payment_intent_object-client_secret) + * @param paymentMethodId - stripe customer payment method id (see: https://stripe.com/docs/api/cards/object#card_object-id) * @returns */ export const confirmPaymentIntentByCard = async ( paymentIntentSecret: string, paymentMethodId: string -): Promise => { +): Promise => { const stripeApiKey = getApiKey() as string; if (typeof stripeApiKey !== "string") throw new Error("Initialization failed."); @@ -21,12 +21,13 @@ export const confirmPaymentIntentByCard = async ( // make request const paymentIntentId = paymentIntentSecret.replace(/_secret_.+$/i, ""); return fetch( - `${stripeApiUrl}/v1/payment_intents/${paymentIntentId}/confirm?client_secret=${paymentIntentSecret}`, + `${stripeApiUrl}/payment_intents/${paymentIntentId}/confirm?client_secret=${paymentIntentSecret}`, { body: `payment_method=${paymentMethodId}`, headers: { Authorization: `Bearer ${stripeApiKey}`, "Content-Type": `application/x-www-form-urlencoded`, + "Stripe-Version": `${stripeApiVersion}`, }, method: `POST`, } diff --git a/src/methods/deleteSourceFromCustomer.ts b/src/methods/deleteSourceFromCustomer.ts new file mode 100644 index 0000000..d26665d --- /dev/null +++ b/src/methods/deleteSourceFromCustomer.ts @@ -0,0 +1,34 @@ +import { Card } from "@stripe/stripe-js"; +import { responseHandler } from "../utils/handlers"; +import { stripeApiUrl, stripeApiVersion } from "../utils/constants"; +import { getApiKey } from "../utils/store"; + +/** + * Delete payment method from customer. + * + * @param sourceId - source or card id (see: https://stripe.com/docs/api/sources/object#source_object-id) + * @param customerId - customer id (see: https://stripe.com/docs/api/customers/object#customer_object-id) + * @param customerEphemeralKey - customer ephemeral key + * @returns + */ +export const deleteSourceFromCustomer = async ( + sourceId: string, + customerId: string, + customerEphemeralKey: string +): Promise => { + const stripeApiKey = getApiKey() as string; + if (typeof stripeApiKey !== "string") + throw new Error("Initialization failed."); + + // make request + return await fetch( + `${stripeApiUrl}/customers/${customerId}/sources/${sourceId}`, + { + headers: { + Authorization: `Bearer ${customerEphemeralKey}`, + "Stripe-Version": stripeApiVersion, + }, + method: "DELETE", + } + ).then(responseHandler); +}; diff --git a/src/methods/index.ts b/src/methods/index.ts index de065b9..42fff59 100644 --- a/src/methods/index.ts +++ b/src/methods/index.ts @@ -1,8 +1,12 @@ import { confirmPaymentIntentByCard } from "./confirmPaymentIntentByCard"; +import { addSourceToCustomer } from "./addSourceToCustomer"; +import { deleteSourceFromCustomer } from "./deleteSourceFromCustomer"; /** * additional stripe methods */ export class additionalMethods { public confirmPaymentIntentByCard = confirmPaymentIntentByCard; + public addSourceToCustomer = addSourceToCustomer; + public deleteSourceFromCustomer = deleteSourceFromCustomer; } diff --git a/src/utils/constants.ts b/src/utils/constants.ts index 96659da..d4feff2 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -1 +1,2 @@ -export const stripeApiUrl = "https://api.stripe.com"; +export const stripeApiUrl = "https://api.stripe.com/v1"; +export const stripeApiVersion = "2020-08-27"; From 5c4a8ba269f1fc2bbdcc02f30484651298771a77 Mon Sep 17 00:00:00 2001 From: Siarhei Dudko Date: Wed, 3 Nov 2021 21:49:13 +0300 Subject: [PATCH 2/5] package version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index eeaa550..4e653bc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@remedyproduct/stripe-js", - "version": "0.0.2", + "version": "0.0.3", "description": "Additional methods for working with stripe-js", "main": "./lib/index.js", "scripts": { From db904246a8d61e62480457b601a480e3e0395778 Mon Sep 17 00:00:00 2001 From: Siarhei Dudko Date: Wed, 3 Nov 2021 21:53:09 +0300 Subject: [PATCH 3/5] - --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ed2aa3a..b574dbf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ - Added method `addSourceToCustomer` - Added method `deleteSourceFromCustomer` +- Updated ci/cd +- Updated docs ### v0.0.2 From 7fb9fc8cd50038ec038fbf036db96ace91795906 Mon Sep 17 00:00:00 2001 From: Siarhei Dudko Date: Wed, 3 Nov 2021 21:54:04 +0300 Subject: [PATCH 4/5] ci --- .github/workflows/build-and-test.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index 4ed32dd..4075f64 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -2,6 +2,7 @@ name: Build and deploy on: push: branches: + - "*" - "!master" jobs: build: From 1683c458d033df41a3c7e2f266f2a6fa06a297b4 Mon Sep 17 00:00:00 2001 From: Siarhei Dudko Date: Wed, 3 Nov 2021 21:54:50 +0300 Subject: [PATCH 5/5] fix ci --- .github/workflows/build-and-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index 4075f64..4bb5968 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -34,5 +34,5 @@ jobs: run: npm run lint - name: Run builder run: npm run build - - name: Run builder + - name: Run test run: npm run test