33namespace  Fintecture \Payment \Gateway ;
44
55use  Fintecture \Payment \Gateway \Config \Config ;
6+ use  Fintecture \Payment \Gateway \Http \Sdk ;
67use  Fintecture \Payment \Helper \Fintecture  as  FintectureHelper ;
78use  Fintecture \Payment \Logger \Logger ;
89use  Magento \Framework \DB \Transaction ;
@@ -60,6 +61,9 @@ class HandlePayment
6061    /** @var InvoiceRepositoryInterface */ 
6162    protected  $ invoiceRepository ;
6263
64+     /** @var Sdk */ 
65+     protected  $ sdk ;
66+ 
6367    public  function  __construct (
6468        Logger   $ fintectureLogger ,
6569        Config   $ config ,
@@ -73,7 +77,8 @@ public function __construct(
7377        OrderSender   $ orderSender ,
7478        InvoiceSender   $ invoiceSender ,
7579        InvoiceService   $ invoiceService ,
76-         InvoiceRepositoryInterface   $ invoiceRepository
80+         InvoiceRepositoryInterface   $ invoiceRepository ,
81+         Sdk   $ sdk
7782    ) {
7883        $ this  ->fintectureLogger  = $ fintectureLogger ;
7984        $ this  ->config  = $ config ;
@@ -88,6 +93,7 @@ public function __construct(
8893        $ this  ->invoiceService  = $ invoiceService ;
8994        $ this  ->invoiceSender  = $ invoiceSender ;
9095        $ this  ->invoiceRepository  = $ invoiceRepository ;
96+         $ this  ->sdk  = $ sdk ;
9197    }
9298
9399    public  function  create (
@@ -140,7 +146,7 @@ public function create(
140146            ->setOrder ($ order )
141147            ->setTransactionId ($ order ->getIncrementId () . '- '  . time ())
142148            ->setAdditionalInformation ([
143-                 \ Magento \ Sales \ Model \ Order \ Payment \Transaction::RAW_DETAILS  => [
149+                 Payment \Transaction::RAW_DETAILS  => [
144150                    'amount '  => (string ) $ lastTransactionAmount  . ' € ' ,
145151                    'status '  => $ params ['status ' ],
146152                    'sessionId '  => $ params ['sessionId ' ],
@@ -204,6 +210,7 @@ public function sendInvoice(Order $order, array $params): void
204210            $ invoice  = $ this  ->invoiceService ->prepareInvoice ($ order );
205211            $ invoice ->setTransactionId ($ params ['sessionId ' ]);
206212            $ invoice ->register ();
213+             $ invoice ->pay ();
207214            $ this  ->invoiceRepository ->save ($ invoice );
208215            $ transactionSave  = $ this  ->transaction 
209216                ->addObject ($ invoice )
@@ -214,6 +221,35 @@ public function sendInvoice(Order $order, array $params): void
214221
215222            $ order ->setIsCustomerNotified (true );
216223            $ this  ->orderRepository ->save ($ order );
224+ 
225+             if  ($ params ['status ' ] === 'order_created ' ) {
226+                 // Update invoice_reference field for BNPL orders 
227+                 $ data  = [
228+                     'data '  => [
229+                         'attributes '  => [
230+                             'invoice_reference '  => '# '  . $ invoice ->getIncrementId (),
231+                         ],
232+                     ],
233+                 ];
234+ 
235+                 $ pisToken  = $ this  ->sdk ->pisClient ->token ->generate ();
236+                 if  (!$ pisToken ->error ) {
237+                     $ this  ->sdk ->pisClient ->setAccessToken ($ pisToken ); // set token of PIS client 
238+                 } else  {
239+                     $ this  ->fintectureLogger ->error ("Can't update invoice_reference field " , [
240+                         'message '  => $ pisToken ->errorMsg ,
241+                         'incrementOrderId '  => $ order ->getIncrementId (),
242+                     ]);
243+                 }
244+ 
245+                 $ apiResponse  = $ this  ->sdk ->pisClient ->payment ->update ($ params ['sessionId ' ], $ data );
246+                 if  ($ apiResponse ->error ) {
247+                     $ this  ->fintectureLogger ->error ("Can't update invoice_reference field " , [
248+                         'message '  => $ apiResponse ->errorMsg ,
249+                         'incrementOrderId '  => $ order ->getIncrementId (),
250+                     ]);
251+                 }
252+             }
217253        }
218254    }
219255
0 commit comments