@@ -76,14 +76,45 @@ public function __construct(
7676 $ this ->consultInstallments = $ consultInstallments ;
7777 }
7878
79+ /**
80+ * Fetch interest from PagBank for a given bin, installment and amount.
81+ *
82+ * @param int $storeId
83+ * @param string $bin
84+ * @param float $amount
85+ * @param int $installment
86+ *
87+ * @return float
88+ */
89+ private function fetchInterest (int $ storeId , string $ bin , float $ amount , int $ installment ): float
90+ {
91+ if (!$ bin || $ installment < 2 ) {
92+ return 0.0 ;
93+ }
94+
95+ $ pagBankInterests = $ this ->consultInstallments ->getPagBankInstallments (
96+ $ storeId ,
97+ $ bin ,
98+ $ this ->configBase ->formatPrice ($ amount )
99+ );
100+
101+ if (isset ($ pagBankInterests [$ installment - 1 ]['amount ' ]['fees ' ])) {
102+ return (float ) $ pagBankInterests [$ installment - 1 ]['amount ' ]['fees ' ]['buyer ' ]['interest ' ]['total ' ] / 100 ;
103+ }
104+
105+ return 0.0 ;
106+ }
107+
79108 /**
80109 * Generate List Installments.
81110 *
82- * @param int $cartId
83- * @param \PagBank\PaymentMagento\Api\Data\CreditCardBinInterface $creditCardBin
84- * @param \PagBank\PaymentMagento\Api\Data\InstallmentSelectedInterface $installmentSelected
85- * @param \PagBank\PaymentMagento\Api\Data\CustomAmountInterface|null $customAmount
86- * @param \PagBank\PaymentMagento\Api\Data\CardIndexInterface|null $cardIndex
111+ * @param int $cartId
112+ * @param \PagBank\PaymentMagento\Api\Data\CreditCardBinInterface $creditCardBin
113+ * @param \PagBank\PaymentMagento\Api\Data\InstallmentSelectedInterface $installmentSelected
114+ * @param \PagBank\PaymentMagento\Api\Data\CustomAmountInterface|null $customAmount
115+ * @param \PagBank\PaymentMagento\Api\Data\CardIndexInterface|null $cardIndex
116+ * @param \PagBank\PaymentMagento\Api\Data\CreditCardBinInterface|null $creditCardBinCard1
117+ * @param \PagBank\PaymentMagento\Api\Data\InstallmentSelectedInterface|null $installmentSelectedCard1
87118 *
88119 * @throws CouldNotSaveException
89120 * @throws NoSuchEntityException
@@ -99,7 +130,9 @@ public function generatePagBankInterest(
99130 CreditCardBinInterface $ creditCardBin ,
100131 InstallmentSelectedInterface $ installmentSelected ,
101132 ?CustomAmountInterface $ customAmount = null ,
102- ?CardIndexInterface $ cardIndex = null
133+ ?CardIndexInterface $ cardIndex = null ,
134+ ?CreditCardBinInterface $ creditCardBinCard1 = null ,
135+ ?InstallmentSelectedInterface $ installmentSelectedCard1 = null
103136 ) {
104137 $ interest = 0 ;
105138 $ cardIndexValue = null ;
@@ -111,63 +144,63 @@ public function generatePagBankInterest(
111144
112145 $ quoteTotal = $ this ->quoteTotalRepository ->get ($ cartId );
113146
114- $ creditCardBin = $ creditCardBin ->getCreditCardBin ();
115-
116- $ installmentSelected = $ installmentSelected ->getInstallmentSelected ();
117-
147+ $ creditCardBinValue = $ creditCardBin ->getCreditCardBin ();
148+ $ installmentSelectedValue = $ installmentSelected ->getInstallmentSelected ();
118149 $ storeId = $ quote ->getData (QuoteCartInterface::KEY_STORE_ID );
119-
120150 $ amount = $ quoteTotal ->getBaseGrandTotal ();
121- $ currentInterest = $ quote ->getData (InstallmentSelectedInterface::PAGBANK_INTEREST_AMOUNT );
122-
123- if ($ installmentSelected === 0 ) {
124151
152+ if ($ installmentSelectedValue === 0 ) {
125153 if (!$ cardIndex ) {
126154 $ quote ->setData (InstallmentSelectedInterface::PAGBANK_INTEREST_AMOUNT , 0 );
127155 $ quote ->setData (InstallmentSelectedInterface::BASE_PAGBANK_INTEREST_AMOUNT , 0 );
128156 $ this ->quoteRepository ->save ($ quote );
129157 return $ this ->quoteTotalRepository ->get ($ cartId );
130158 }
131159
132- if ($ cardIndex !== null ) {
133- $ cardIndexValue = $ cardIndex ->getCardIndex ();
134- if ($ cardIndexValue === 1 ) {
135- $ quote ->setData (InstallmentSelectedInterface::PAGBANK_INTEREST_AMOUNT , 0 );
136- $ quote ->setData (InstallmentSelectedInterface::BASE_PAGBANK_INTEREST_AMOUNT , 0 );
137- $ this ->quoteRepository ->save ($ quote );
138- return $ this ->quoteTotalRepository ->get ($ cartId );
139- }
160+ $ cardIndexValue = $ cardIndex ->getCardIndex ();
161+ if ($ cardIndexValue === 1 ) {
162+ $ quote ->setData (InstallmentSelectedInterface::PAGBANK_INTEREST_AMOUNT , 0 );
163+ $ quote ->setData (InstallmentSelectedInterface::BASE_PAGBANK_INTEREST_AMOUNT , 0 );
164+ $ this ->quoteRepository ->save ($ quote );
165+ return $ this ->quoteTotalRepository ->get ($ cartId );
140166 }
141167 }
142168
143169 if ($ cardIndex !== null ) {
144170 $ cardIndexValue = $ cardIndex ->getCardIndex ();
171+ $ customAmountValue = $ customAmount ? (float ) $ customAmount ->getCustomAmount () : 0.0 ;
172+
145173 if ($ cardIndexValue === 1 ) {
146- $ customAmountValue = $ customAmount ->getCustomAmount ();
147174 $ amount = $ customAmountValue ;
148175 }
149176
150177 if ($ cardIndexValue === 2 ) {
151- $ customAmountValue = $ customAmount ->getCustomAmount ();
178+ $ card1Interest = 0.0 ;
179+ if ($ creditCardBinCard1 && $ installmentSelectedCard1 ) {
180+ $ card1Interest = $ this ->fetchInterest (
181+ $ storeId ,
182+ (string ) $ creditCardBinCard1 ->getCreditCardBin (),
183+ $ customAmountValue ,
184+ (int ) $ installmentSelectedCard1 ->getInstallmentSelected ()
185+ );
186+ }
187+
152188 $ amount -= $ customAmountValue ;
153- $ amount -= $ currentInterest ;
189+ $ amount -= $ card1Interest ;
154190 }
155- } else {
156- $ amount -= $ currentInterest ;
157191 }
158192
159-
160193 $ amount = $ this ->configBase ->formatPrice ($ amount );
161194
162- if ($ creditCardBin ) {
195+ if ($ creditCardBinValue ) {
163196 $ pagBankInterests = $ this ->consultInstallments ->getPagBankInstallments (
164197 $ storeId ,
165- $ creditCardBin ,
198+ $ creditCardBinValue ,
166199 $ amount
167200 );
168201
169- if (isset ($ pagBankInterests [$ installmentSelected - 1 ])) {
170- $ installment = $ pagBankInterests [$ installmentSelected - 1 ];
202+ if (isset ($ pagBankInterests [$ installmentSelectedValue - 1 ])) {
203+ $ installment = $ pagBankInterests [$ installmentSelectedValue - 1 ];
171204 if (isset ($ installment ['amount ' ]['fees ' ])) {
172205 $ interest = $ installment ['amount ' ]['fees ' ]['buyer ' ]['interest ' ]['total ' ];
173206 }
@@ -178,7 +211,7 @@ public function generatePagBankInterest(
178211
179212 try {
180213 if ($ cardIndexValue === 2 ) {
181- $ interest += $ currentInterest ;
214+ $ interest += $ card1Interest ;
182215 }
183216
184217 $ quote ->setData (InstallmentSelectedInterface::PAGBANK_INTEREST_AMOUNT , $ interest );
@@ -190,4 +223,4 @@ public function generatePagBankInterest(
190223
191224 return $ this ->quoteTotalRepository ->get ($ cartId );
192225 }
193- }
226+ }
0 commit comments