@@ -428,54 +428,63 @@ export class TransactionController {
428428 const refundData = this . refundList . get ( transaction . id ) ;
429429 if ( ! refundData ) throw new BadRequestException ( 'Request refund data first' ) ;
430430 if ( ! this . isRefundDataValid ( refundData ) ) throw new BadRequestException ( 'Refund data request invalid' ) ;
431+
432+ await this . executeRefund ( transaction , transaction . targetEntity , refundData , dto ) ;
433+
431434 this . refundList . delete ( transaction . id ) ;
435+ }
432436
437+ private async executeRefund (
438+ transaction : Transaction ,
439+ targetEntity : BuyCrypto | BuyFiat | BankTxReturn | undefined ,
440+ refundData : RefundDataDto ,
441+ dto : TransactionRefundDto ,
442+ ) : Promise < void > {
433443 const inputCurrency = await this . transactionHelper . getRefundActive ( transaction . refundTargetEntity ) ;
434444 if ( ! inputCurrency . refundEnabled ) throw new BadRequestException ( `Refund for ${ inputCurrency . name } not allowed` ) ;
435445
436446 const refundDto = { chargebackAmount : refundData . refundAmount , chargebackAllowedDateUser : new Date ( ) } ;
437447
438- if ( ! transaction . targetEntity ) {
448+ if ( ! targetEntity ) {
439449 transaction . bankTxReturn = await this . bankTxService
440450 . updateInternal ( transaction . bankTx , { type : BankTxType . BANK_TX_RETURN } )
441451 . then ( ( b ) => b . bankTxReturn ) ;
442452 }
443453
444454 const chargebackCurrency = refundData . refundAsset . name ;
445455
446- if ( transaction . targetEntity instanceof BankTxReturn ) {
456+ if ( targetEntity instanceof BankTxReturn ) {
447457 if ( ! dto . creditorData ) throw new BadRequestException ( 'Creditor data is required for bank refunds' ) ;
448458
449- return this . bankTxReturnService . refundBankTx ( transaction . targetEntity , {
459+ return this . bankTxReturnService . refundBankTx ( targetEntity , {
450460 refundIban : dto . refundTarget ?? refundData . refundTarget ,
451461 chargebackCurrency,
452462 creditorData : dto . creditorData ,
453463 ...refundDto ,
454464 } ) ;
455465 }
456466
457- if ( NotRefundableAmlReasons . includes ( transaction . targetEntity . amlReason ) )
467+ if ( NotRefundableAmlReasons . includes ( targetEntity . amlReason ) )
458468 throw new BadRequestException ( 'You cannot refund with this reason' ) ;
459469
460- if ( transaction . targetEntity instanceof BuyFiat )
461- return this . buyFiatService . refundBuyFiatInternal ( transaction . targetEntity , {
470+ if ( targetEntity instanceof BuyFiat )
471+ return this . buyFiatService . refundBuyFiatInternal ( targetEntity , {
462472 refundUserAddress : dto . refundTarget ,
463473 ...refundDto ,
464474 } ) ;
465475
466- if ( transaction . targetEntity . cryptoInput )
467- return this . buyCryptoService . refundCryptoInput ( transaction . targetEntity , {
476+ if ( targetEntity . cryptoInput )
477+ return this . buyCryptoService . refundCryptoInput ( targetEntity , {
468478 refundUserAddress : dto . refundTarget ,
469479 ...refundDto ,
470480 } ) ;
471481
472- if ( transaction . targetEntity . checkoutTx )
473- return this . buyCryptoService . refundCheckoutTx ( transaction . targetEntity , { ...refundDto } ) ;
482+ if ( targetEntity . checkoutTx ) return this . buyCryptoService . refundCheckoutTx ( targetEntity , { ...refundDto } ) ;
474483
475484 // BuyCrypto bank refund
476485 if ( ! dto . creditorData ) throw new BadRequestException ( 'Creditor data is required for bank refunds' ) ;
477486
478- return this . buyCryptoService . refundBankTx ( transaction . targetEntity , {
487+ return this . buyCryptoService . refundBankTx ( targetEntity , {
479488 refundIban : dto . refundTarget ?? refundData . refundTarget ,
480489 chargebackCurrency,
481490 creditorData : dto . creditorData ,
0 commit comments