From ff64df758bd245470cf4f8f479f7650f04e0f6f8 Mon Sep 17 00:00:00 2001 From: "Valeriya(vchu)" Date: Fri, 31 Mar 2023 13:09:07 +0000 Subject: [PATCH] [IMP] payment_demo: add support partial capture support partial capture feature in demo payment task-2803352 closes odoo/odoo#117356 Signed-off-by: Antoine Vandevenne (anv) --- .../payment_demo/models/payment_provider.py | 2 +- .../models/payment_transaction.py | 10 ++++++---- .../tests/test_payment_transaction.py | 20 +++++++++++++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/addons/payment_demo/models/payment_provider.py b/addons/payment_demo/models/payment_provider.py index b7ab1eb971c..1220649e003 100644 --- a/addons/payment_demo/models/payment_provider.py +++ b/addons/payment_demo/models/payment_provider.py @@ -25,7 +25,7 @@ class PaymentProvider(models.Model): super()._compute_feature_support_fields() self.filtered(lambda p: p.code == 'demo').update({ 'support_fees': True, - 'support_manual_capture': 'full_only', + 'support_manual_capture': 'partial', 'support_refund': 'partial', 'support_tokenization': True, }) diff --git a/addons/payment_demo/models/payment_transaction.py b/addons/payment_demo/models/payment_transaction.py index 9cf6684dfc8..defeebc3a2a 100644 --- a/addons/payment_demo/models/payment_transaction.py +++ b/addons/payment_demo/models/payment_transaction.py @@ -103,12 +103,13 @@ class PaymentTransaction(models.Model): if self.provider_code != 'demo': return child_capture_tx + tx = child_capture_tx or self notification_data = { - 'reference': self.reference, + 'reference': tx.reference, 'simulated_state': 'done', 'manual_capture': True, # Distinguish manual captures from regular one-step captures. } - self._handle_notification_data('demo', notification_data) + tx._handle_notification_data('demo', notification_data) return child_capture_tx @@ -118,8 +119,9 @@ class PaymentTransaction(models.Model): if self.provider_code != 'demo': return child_void_tx - notification_data = {'reference': self.reference, 'simulated_state': 'cancel'} - self._handle_notification_data('demo', notification_data) + tx = child_void_tx or self + notification_data = {'reference': tx.reference, 'simulated_state': 'cancel'} + tx._handle_notification_data('demo', notification_data) return child_void_tx diff --git a/addons/payment_demo/tests/test_payment_transaction.py b/addons/payment_demo/tests/test_payment_transaction.py index 7be71f4f988..7f5b6eea82e 100644 --- a/addons/payment_demo/tests/test_payment_transaction.py +++ b/addons/payment_demo/tests/test_payment_transaction.py @@ -80,3 +80,23 @@ class TestPaymentTransaction(PaymentDemoCommon, PaymentHttpCommon): tx.token_id = self._create_token(demo_simulated_state=state) tx._send_payment_request() self.assertEqual(tx.state, tx.token_id.demo_simulated_state) + + def test_send_full_capture_request_does_not_create_capture_tx(self): + self.provider.capture_manually = True + source_tx = self._create_transaction(flow='direct', state='authorized') + child_tx = source_tx._send_capture_request() + self.assertFalse( + child_tx, msg="Full capture should not create a child transaction." + ) + + def test_send_partial_capture_request_creates_capture_tx(self): + self.provider.capture_manually = True + source_tx = self._create_transaction(flow='direct', state='authorized') + child_tx = source_tx._send_capture_request(amount_to_capture=10) + self.assertTrue( + source_tx.child_transaction_ids, + msg="Partial capture should create a child transaction and linked it to the source tx.", + ) + self.assertEqual( + child_tx.amount, 10, msg="Child transaction should have the requested amount." + )