All files / src/controllers/components stripeIntegrations.ts

66.66% Statements 12/18
37.5% Branches 3/8
50% Functions 2/4
66.66% Lines 12/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53  1x     1x   1x     1x 1x 1x               1x 1x       1x 1x         1x                   1x                        
 
import Stripe from 'stripe';
import {currentUserType} from '../types';
 
const stripe = new Stripe(process.env.STRIPE_KEY || "", {apiVersion: '2020-08-27'});
 
export default class StripeIntegrations {
    constructor() {
    }
    public paymentIntents = async (payments: any, user: currentUserType): Promise<any> => {
        let methodName = "paymentIntents"
        let payload = {
            amount: payments.amountPaid * 100,
            currency: "USD",
            description: "Saw Buyer Broker-initial fee",
            payment_method: payments.paymentID,
            confirm: true,
            metadata: {name: user.name || "", id: user.UID, email: user.email || "", phone: user.phone}
        }
        try {
            let paymentResponse = await stripe.paymentIntents.create(payload)
            return {error: null, data: paymentResponse}
 
        } catch (error: any) {
            let errorObj = {code: error.code, message: error.message}
            return {error: errorObj, data: null, methodName}
        }
    }
 
    // list of webhooks
    public webhookList = async (): Promise<any> => {
        let data: any = await stripe.webhookEndpoints.list();
        // isArray(data.data) && data.data.forEach((element: any) => {
        //     stripe.webhookEndpoints.del(
        //         element.id
        //     );
        // });
        return data
 
    }
    public createWebHook = async (): Promise<any> => {
        const url: string = process.env.API_URL || ""
        const payload: Stripe.WebhookEndpointCreateParams = {
            url: url + '/stripPaymentUpdate',
            enabled_events: [
                'charge.refunded',
                'charge.refund.updated',
            ],
        }
        return await stripe.webhookEndpoints.create(payload);
    }
}