From 6dc418fe88786f8089c7495ee19b93a7379973eb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Fri, 17 Jan 2020 14:06:04 +0100
Subject: [PATCH] lingo: add transaction_amount in pay_invoice payload (#39062)

---
 combo/apps/lingo/models.py       | 11 ++++++-----
 tests/test_lingo_remote_regie.py | 24 +++++++++++++++++++-----
 2 files changed, 25 insertions(+), 10 deletions(-)

diff --git a/combo/apps/lingo/models.py b/combo/apps/lingo/models.py
index 204530b..291dfb6 100644
--- a/combo/apps/lingo/models.py
+++ b/combo/apps/lingo/models.py
@@ -194,12 +194,13 @@ class Regie(models.Model):
             return requests.get(url, user=user, remote_service='auto', cache_duration=0)
         raise PermissionDenied
 
-    def pay_invoice(self, invoice_id, transaction_id, transaction_date):
+    def pay_invoice(self, invoice_id, transaction):
         url = self.webservice_url + '/invoice/%s/pay/' % invoice_id
-        data = {'transaction_id': transaction_id,
-                'transaction_date': transaction_date.strftime('%Y-%m-%dT%H:%M:%S')}
+        data = {'transaction_id': transaction.order_id,
+                'transaction_date': transaction.end_date.strftime('%Y-%m-%dT%H:%M:%S'),
+                'transaction_amount': str(transaction.amount)}
         headers = {'content-type': 'application/json'}
-        return requests.post(url, remote_service='auto',
+        return requests.post(url, user=transaction.user, remote_service='auto',
                              data=json.dumps(data), headers=headers).json()
 
     def as_api_dict(self):
@@ -486,7 +487,7 @@ class Transaction(models.Model):
         for item_id in items.split(','):
             try:
                 remote_item = regie.get_invoice(user=self.user, invoice_id=item_id)
-                regie.pay_invoice(item_id, self.order_id, self.end_date)
+                regie.pay_invoice(item_id, self)
             except Exception:
                 to_be_paid_remote_items.append(item_id)
                 logger.exception(u'unable to notify payment for remote item %s from transaction %s',
diff --git a/tests/test_lingo_remote_regie.py b/tests/test_lingo_remote_regie.py
index d4e2c08..8787a5c 100644
--- a/tests/test_lingo_remote_regie.py
+++ b/tests/test_lingo_remote_regie.py
@@ -173,15 +173,18 @@ def test_remote_regie_past_invoices_cell(mock_send, remote_regie):
     content = cell.render(context)
     assert 'No items yet' in content
 
-@mock.patch('combo.apps.lingo.models.Regie.pay_invoice')
+
+@mock.patch('combo.apps.lingo.models.requests.post')
 @mock.patch('combo.apps.lingo.models.requests.get')
-def test_anonymous_successful_item_payment(mock_get, mock_pay_invoice, app, remote_regie):
-    assert remote_regie.is_remote() == True
+def test_anonymous_successful_item_payment(mock_get, mock_post, app, remote_regie):
+    assert remote_regie.is_remote() is True
     encrypt_id = aes_hex_encrypt(settings.SECRET_KEY, 'F201601')
     mock_json = mock.Mock()
     mock_json.json.return_value = {'err': 0, 'data': INVOICES[0]}
     mock_get.return_value = mock_json
-    mock_pay_invoice.return_value = mock.Mock(status_code=200)
+    mock_post_json = mock.Mock()
+    mock_post_json.json.return_value = {"err": 0, "data": True}
+    mock_post.return_value = mock_post_json
     resp = app.get('/lingo/item/%s/%s/' % (remote_regie.id, encrypt_id))
     form = resp.form
 
@@ -215,9 +218,9 @@ def test_anonymous_successful_item_payment(mock_get, mock_pay_invoice, app, remo
     assert urlparse.urlparse(resp.url).path == '/'
     # simulate successful call to callback URL
     resp = app.get(reverse('lingo-callback', kwargs={'regie_pk': remote_regie.id}), params=args)
+
     trans = Transaction.objects.all()
     b_item = BasketItem.objects.all()
-
     assert trans
     assert b_item
     trans = trans[0]
@@ -228,6 +231,17 @@ def test_anonymous_successful_item_payment(mock_get, mock_pay_invoice, app, remo
 
     assert resp.status_code == 200
 
+    assert len(mock_post.call_args_list) == 1
+    call_args = mock_post.call_args_list[0]
+    assert call_args[0][0] == 'http://example.org/regie/invoice/F201601/pay/'
+    call_data = json.loads(call_args[1]['data'])
+    assert call_data == {
+        'transaction_id': trans.order_id,
+        'transaction_date': trans.end_date.strftime('%Y-%m-%dT%H:%M:%S'),
+        'transaction_amount': str(trans.amount),
+    }
+
+
 @mock.patch('combo.apps.lingo.models.requests.get')
 def test_anonymous_item_payment_email_error(mock_get, app, remote_regie):
     assert remote_regie.is_remote() == True
-- 
2.20.1

