Skip to content

Commit

Permalink
feat: refetch from stripe.charges
Browse files Browse the repository at this point in the history
  • Loading branch information
KenLSM committed Sep 20, 2023
1 parent b718e23 commit f9242f0
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/app/modules/payments/stripe.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1129,15 +1129,36 @@ const _getPaymentProofPresignedS3Url = (
)
}

const generatePaymentInvoiceAsPdf = (
const _retrieveReceiptUrlFromStripe = (
payment: IPaymentSchema,
): ResultAsync<string, StripeFetchError> => {
return ResultAsync.fromPromise(
stripe.paymentIntents.retrieve(
payment.paymentIntentId,
{ expand: ['latest_charge'] },
{ stripeAccount: payment.targetAccountId },
),
(error) => new StripeFetchError(String(error)),
).andThen((paymentIntent) => {
const receiptUrl = (paymentIntent.latest_charge as Stripe.Charge)
.receipt_url
if (!receiptUrl) {
return errAsync(new StripeFetchError('Receipt url not found'))
}
return ok(receiptUrl)
})
}

const _generatePaymentInvoiceAsPdf = (
payment: IPaymentSchema,
populatedForm: IPopulatedEncryptedForm,
receiptUrl: string,
): ResultAsync<Buffer, StripeFetchError> => {
if (!payment.completedPayment?.receiptUrl) {
return errAsync(new StripeFetchError('Receipt url not ready'))
}
return ResultAsync.fromPromise(
axios.get<string>(payment.completedPayment.receiptUrl),
axios.get<string>(receiptUrl),
(error) => new StripeFetchError(String(error)),
).andThen((receiptUrlResponse) => {
// retrieve receiptURL as html
Expand Down Expand Up @@ -1193,7 +1214,10 @@ export const generatePaymentInvoiceUrl = (
StripeFetchError | PaymentProofUploadS3Error | PaymentProofPresignS3Error
> => {
if (!payment.completedPayment?.hasS3ReceiptUrl) {
return generatePaymentInvoiceAsPdf(payment, populatedForm)
return _retrieveReceiptUrlFromStripe(payment)
.andThen((receiptUrl) =>
_generatePaymentInvoiceAsPdf(payment, populatedForm, receiptUrl),
)
.andThen((pdfBuffer) => _storePaymentProofInS3(payment, pdfBuffer))
.andThen(() => _getPaymentProofPresignedS3Url(payment))
}
Expand Down

0 comments on commit f9242f0

Please sign in to comment.