buckaroo-it / Magento2

Repository containing the Magento2 plugin by Buckaroo
MIT License
28 stars 35 forks source link

[BP-601] Save payment after skip_push fails #62

Closed evs-xsarus closed 3 years ago

evs-xsarus commented 3 years ago

After selecting a creditcard as payment method, no Buckaroo site is shown, but instead an error is thrown:

[2020-12-10 08:17:36] main.CRITICAL: An exception occurred on 'sales_model_service_quote_submit_failure' event: Rolled back transaction has not been completed correctly. {"exception":"[object] (Exception(code: 0): An exception occurred on 'sales_model_service_quote_submit_failure' event: Rolled back transaction has not been completed correctly. at /home/svc-mcf_prod/www.mag.nl/vendor/magento/module-quote/Model/QuoteManagement.php:703, Zend_Db_Statement_Exception(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (mcf_prod.sales_order_payment, CONSTRAINT SALES_ORDER_PAYMENT_PARENT_ID_SALES_ORDER_ENTITY_ID FOREIGN KEY (parent_id) REFERENCES sales_order (entity_id) ON DELETE CASCADE), query was: INSERT INTO sales_order_payment (base_shipping_amount, shipping_amount, base_amount_ordered, amount_ordered, additional_data, cc_exp_month, cc_ss_start_year, method, cc_last_4, cc_ss_start_month, cc_owner, cc_type, po_number, cc_exp_year, cc_ss_issue, cc_number_enc, additional_information) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) at /home/svc-mcf_prod/www.mag.nl/vendor/magento/framework/DB/Statement/Pdo/Mysql.php:110, PDOException(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (mcf_prod.sales_order_payment, CONSTRAINT SALES_ORDER_PAYMENT_PARENT_ID_SALES_ORDER_ENTITY_ID FOREIGN KEY (parent_id) REFERENCES sales_order (entity_id) ON DELETE CASCADE) at /home/svc-mcf_prod/www.mag.nl/vendor/magento/framework/DB/Statement/Pdo/Mysql.php:91)"} []

I've managed to pinpoint this due to the 'skip_push' implementation. This is also the reason why I have no problems with iDeal as the skip_push isn't applied to this payment method.

See:

Only if skip_push is > 0 , the payment is saved. But at this time, the order hasn't been saved yet, thus parent_id stay's empty.

Could you look into this. For now, I've applied this patch:

--- vendor/buckaroo/magento2/Model/Method/AbstractMethod.php
+++ vendor/buckaroo/magento2/Model/Method/AbstractMethod.php
@@ -1226,7 +1226,11 @@
              */
             if ($skipFirstPush > 0) {
                 $payment->setAdditionalInformation('skip_push', $skipFirstPush - 1);
-                $payment->save();
+                if (!empty($payment->getOrder()) && !empty($payment->getOrder()->getId())) {
+                    // Only save payment if order is already saved, this to avoid foreign key constraint error
+                    // on table sales_order_payment, column parent_id.
+                    $payment->save();
+                }
             }
         }
Buckaroo-Rens commented 3 years ago

Issue will be fixed in our next release.

Buckaroo-Rens commented 3 years ago

Released.