File

src/payment-intents/payment-intents.service.ts

Index

Methods

Constructor

constructor(stripeService: StripeService)
Parameters :
Name Type Optional
stripeService StripeService No

Methods

Async confirmPaymentsIntent
confirmPaymentsIntent(id: string)
Parameters :
Name Type Optional
id string No
Async createPaymentsIntent
createPaymentsIntent(paymentsIntent: PaymentIntentsDto)
Parameters :
Name Type Optional
paymentsIntent PaymentIntentsDto No
Async retrievePaymentIntents
retrievePaymentIntents(id: string)
Parameters :
Name Type Optional
id string No
import { Injectable } from '@nestjs/common';
import { StripeService } from '../core/stripe/stripe.service';
import { Stripe } from '../core/stripe/stripe.iterface';
import { PaymentIntentsDto } from './payment-intents.dto';
import { PaymentIntentResponse } from './payment-intents.interface';

@Injectable()
export class PaymentIntentsService {
  constructor(private readonly stripeService: StripeService) {}

  async createPaymentsIntent(
    paymentsIntent: PaymentIntentsDto
  ): Promise<PaymentIntentResponse> {
    const {
      amount,
      paymentMethodID,
      statementDescriptor,
      returnUrl,
    } = paymentsIntent;

    const paymentIntents = await this.stripeService.createPaymentIntents(
      amount,
      paymentMethodID,
      statementDescriptor,
      returnUrl
    );

    return generatePaymentResponse(paymentIntents);
  }

  async retrievePaymentIntents(id: string): Promise<PaymentIntentResponse> {
    const paymentIntents = await this.stripeService.retrievePaymentIntents(id);

    return generatePaymentResponse(paymentIntents);
  }

  async confirmPaymentsIntent(id: string): Promise<PaymentIntentResponse> {
    const paymentIntents = await this.stripeService.confirmPaymentIntents(id);

    return generatePaymentResponse(paymentIntents);
  }
}

function generatePaymentResponse(
  intent: Stripe.PaymentIntents
): PaymentIntentResponse {
  console.log(`:: intent`, intent);
  if (intent.status === 'requires_action') {
    if (intent.next_action.type === 'use_stripe_sdk') {
      return {
        payment_intent_id: intent.id,
        requires_action: true,
        payment_intent_client_secret: intent.client_secret,
      };
    }

    return {
      payment_intent_id: intent.id,
      requires_action: true,
      next_action: intent.next_action,
    };
  }

  if (intent.status === 'requires_confirmation') {
    return {
      payment_intent_id: intent.id,
      requires_action: true,
      next_action: {
        type: 'requires_confirmation',
      },
    };
  }

  if (intent.status === 'succeeded') {
    return {
      success: true,
    };
  }

  return {
    error: 'Invalid PaymentIntent status',
  };
}

results matching ""

    No results matching ""