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
This commit is contained in:
Cédric Krier committed 2020-12-26 23:55:43 +01:00
1 parent 2eb1c710e5
commit f42d92f0e0
4 files changed
+62 -7

No files matched your search

+3 -1
View File
@@ -1,2 +1,4 @@
* Update Braintree Customer
Version 5.8.0 - 2020-11-02
* Initial release
* Initial release
+29 -5
View File
@@ -1033,11 +1033,8 @@ class PaymentBraintreeCustomer(
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 @@ class PaymentBraintreeCustomer(
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
+19 -1
View File
@@ -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 @@ class Party(metaclass=PoolMeta):
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'
@@ -155,6 +155,17 @@ Run cron::
>>> bool(braintree_customer.braintree_customer_id)
True
Update customer::
>>> contact = customer.contact_mechanisms.new()
>>> contact.type = 'email'
>>> contact.value = 'customer@example.com'
>>> customer.save()
>>> cus = gateway.customer.find(braintree_customer.braintree_customer_id)
>>> cus.email
'customer@example.com'
Make payment with customer::
>>> payment, = payment.duplicate()