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

Commit

Permalink
update caller instance
Browse files Browse the repository at this point in the history
  • Loading branch information
siarheidudko committed Nov 5, 2021
1 parent d6f8eb8 commit 0ce61ba
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 41 deletions.
30 changes: 8 additions & 22 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,22 @@ 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_...
*/
_apiKey: string;
}

/**
* Stripe patched interface
* Stripe patched library
*/
export interface Stripe extends StripeDefaultWithInternal, AdditionalMethods {}

/**
* Stripe liblary
*/
let stripe: Stripe;
export interface Stripe extends RemedyProductStripe, StripeDefault {}

/**
* Initialize stripe
Expand All @@ -37,8 +31,6 @@ export const loadStripe = async (
publishableKey: string,
options: StripeConstructorOptions | undefined
) => {
if (stripe) throw new Error("Already initialized.");

const stripeDefault: StripeDefault | null = await loadStripeDefault(
publishableKey,
options
Expand All @@ -48,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();

stripe = {
_apiKey: apiKey,
...stripeDefault,
...methods,
};

const remedyProductStripe = new RemedyProductStripe(
(stripeDefault as StripeDefaultWithInternal)._apiKey
);
const stripe: Stripe = Object.assign(remedyProductStripe, stripeDefault);
return stripe;
};
11 changes: 7 additions & 4 deletions src/methods/addSourceToCustomer.ts
Original file line number Diff line number Diff line change
@@ -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).
Expand All @@ -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<Card | undefined> => {
const stripeApiKey = getApiKey() as string;
): Promise<Card | undefined> {
/* eslint-disable */
const stripeApiKey = this._apiKey;
/* eslint-enable */
if (typeof stripeApiKey !== "string")
throw new Error("Initialization failed.");

Expand Down
11 changes: 7 additions & 4 deletions src/methods/confirmPaymentIntentByCard.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<PaymentIntentResult["paymentIntent"] | undefined> => {
const stripeApiKey = getApiKey() as string;
): Promise<PaymentIntentResult["paymentIntent"] | undefined> {
/* eslint-disable */
const stripeApiKey = this._apiKey;
/* eslint-enable */
if (typeof stripeApiKey !== "string")
throw new Error("Initialization failed.");

Expand Down
11 changes: 7 additions & 4 deletions src/methods/deleteSourceFromCustomer.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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<Card | undefined> => {
const stripeApiKey = getApiKey() as string;
): Promise<Card | undefined> {
/* eslint-disable */
const stripeApiKey = this._apiKey;
/* eslint-enable */
if (typeof stripeApiKey !== "string")
throw new Error("Initialization failed.");

Expand Down
6 changes: 5 additions & 1 deletion src/methods/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { deleteSourceFromCustomer } from "./deleteSourceFromCustomer";
/**
* 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;
Expand Down
6 changes: 0 additions & 6 deletions src/utils/store.ts

This file was deleted.

0 comments on commit 0ce61ba

Please sign in to comment.