src/payment-intents/payment-intents.service.ts
Methods |
|
constructor(stripeService: StripeService)
|
||||||
Parameters :
|
Async confirmPaymentsIntent | ||||||
confirmPaymentsIntent(id: string)
|
||||||
Parameters :
Returns :
Promise<PaymentIntentResponse>
|
Async createPaymentsIntent | ||||||
createPaymentsIntent(paymentsIntent: PaymentIntentsDto)
|
||||||
Parameters :
Returns :
Promise<PaymentIntentResponse>
|
Async retrievePaymentIntents | ||||||
retrievePaymentIntents(id: string)
|
||||||
Parameters :
Returns :
Promise<PaymentIntentResponse>
|
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',
};
}