changeset cfeb36a39207 in modules/account_payment_braintree:default
details:
https://hg.tryton.org/modules/account_payment_braintree?cmd=changeset;node=cfeb36a39207
description:
Update Braintree Customer
The Braintree Customer is updated each time the party is modified in
such way
that the parameters are also changed. We do not manage any retry on the
update
because it is just a best effort.
issue9888
review302701002
diffstat:
CHANGELOG | 4 ++-
account.py | 34 +++++++++++++++++++++++----
party.py | 20 +++++++++++++++-
tests/scenario_account_payment_braintree.rst | 11 +++++++++
4 files changed, 62 insertions(+), 7 deletions(-)
diffs (118 lines):
diff -r 9f75a8437ffa -r cfeb36a39207 CHANGELOG
--- a/CHANGELOG Sat Dec 19 17:08:44 2020 +0100
+++ b/CHANGELOG Sat Dec 26 23:55:43 2020 +0100
@@ -1,2 +1,4 @@
+* Update Braintree Customer
+
Version 5.8.0 - 2020-11-02
-* Initial release
\ No newline at end of file
+* Initial release
diff -r 9f75a8437ffa -r cfeb36a39207 account.py
--- a/account.py Sat Dec 19 17:08:44 2020 +0100
+++ b/account.py Sat Dec 26 23:55:43 2020 +0100
@@ -1033,11 +1033,8 @@
gateway = customer.braintree_account.gateway()
try:
if not customer.braintree_customer_id:
- result = gateway.customer.create({
- 'email': customer.party.email,
- 'phone': customer.party.phone,
- 'payment_method_nonce': customer.braintree_nonce,
- })
+ result = gateway.customer.create(
+ customer._customer_parameters())
else:
result = gateway.payment_method.create({
'customer_id': customer.braintree_customer_id,
@@ -1066,6 +1063,33 @@
cls._payment_methods_cache.clear()
Transaction().commit()
+ def _customer_parameters(self):
+ params = {
+ 'email': self.party.email,
+ 'fax': self.party.fax[:255],
+ 'last_name': self.party.name[:255],
+ 'phone': self.party.phone[:255],
+ 'website': self.party.website[:255],
+ }
+ if self.braintree_nonce:
+ params['payment_method_nonce'] = self.braintree_nonce
+ return params
+
+ @classmethod
+ def braintree_update(cls, customers):
+ for customer in customers:
+ gateway = customer.braintree_account.gateway()
+ try:
+ gateway.customer.update(
+ customer.braintree_customer_id,
+ customer._customer_parameters())
+ except TooManyRequestsError as e:
+ logger.warning(str(e))
+ except Exception:
+ logger.error(
+ "Error when updating customer %d", customer.id,
+ exc_info=True)
+
@classmethod
def braintree_delete(cls, customers=None):
"""Deletes braintree customers
diff -r 9f75a8437ffa -r cfeb36a39207 party.py
--- a/party.py Sat Dec 19 17:08:44 2020 +0100
+++ b/party.py Sat Dec 26 23:55:43 2020 +0100
@@ -1,6 +1,6 @@
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
-from trytond.pool import PoolMeta
+from trytond.pool import PoolMeta, Pool
from trytond.model import fields
@@ -10,6 +10,24 @@
braintree_customers = fields.One2Many(
'account.payment.braintree.customer', 'party', "Braintree Customers")
+ @classmethod
+ def write(cls, *args):
+ pool = Pool()
+ Customer = pool.get('account.payment.braintree.customer')
+
+ parties = sum(args[0:None:2], [])
+ customers = sum((p.braintree_customers for p in parties), ())
+ customer2params = {c: c._customer_parameters() for c in customers}
+
+ super().write(*args)
+
+ to_update = []
+ for customer, params in customer2params.items():
+ if customer._customer_parameters() != params:
+ to_update.append(customer)
+ if to_update:
+ Customer.__queue__.braintree_update(to_update)
+
class Replace(metaclass=PoolMeta):
__name__ = 'party.replace'
diff -r 9f75a8437ffa -r cfeb36a39207
tests/scenario_account_payment_braintree.rst
--- a/tests/scenario_account_payment_braintree.rst Sat Dec 19 17:08:44
2020 +0100
+++ b/tests/scenario_account_payment_braintree.rst Sat Dec 26 23:55:43
2020 +0100
@@ -155,6 +155,17 @@
>>> bool(braintree_customer.braintree_customer_id)
True
+Update customer::
+
+ >>> contact = customer.contact_mechanisms.new()
+ >>> contact.type = 'email'
+ >>> contact.value = '[email protected]'
+ >>> customer.save()
+
+ >>> cus = gateway.customer.find(braintree_customer.braintree_customer_id)
+ >>> cus.email
+ '[email protected]'
+
Make payment with customer::
>>> payment, = payment.duplicate()