Skip to content
This repository has been archived by the owner on Jan 6, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2 from remedyproduct/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
siarheidudko authored Nov 3, 2021
2 parents dd9df61 + 1683c45 commit 1f5f79f
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 10 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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 test
run: npm run test
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
### v0.0.3

- Added method `addSourceToCustomer`
- Added method `deleteSourceFromCustomer`
- Updated ci/cd
- Updated docs

### v0.0.2

- Updating documentation
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
33 changes: 33 additions & 0 deletions src/methods/addSourceToCustomer.ts
Original file line number Diff line number Diff line change
@@ -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<Card | undefined> => {
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);
};
11 changes: 6 additions & 5 deletions src/methods/confirmPaymentIntentByCard.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
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<PaymentIntentResult | undefined> => {
): Promise<PaymentIntentResult["paymentIntent"] | undefined> => {
const stripeApiKey = getApiKey() as string;
if (typeof stripeApiKey !== "string")
throw new Error("Initialization failed.");

// 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`,
}
Expand Down
34 changes: 34 additions & 0 deletions src/methods/deleteSourceFromCustomer.ts
Original file line number Diff line number Diff line change
@@ -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<Card | undefined> => {
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);
};
4 changes: 4 additions & 0 deletions src/methods/index.ts
Original file line number Diff line number Diff line change
@@ -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;
}
3 changes: 2 additions & 1 deletion src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const stripeApiUrl = "https://api.stripe.com";
export const stripeApiUrl = "https://api.stripe.com/v1";
export const stripeApiVersion = "2020-08-27";

0 comments on commit 1f5f79f

Please sign in to comment.