From 9175aa9240925b2333c86d1019d7c6b05b778fe7 Mon Sep 17 00:00:00 2001 From: Changaco Date: Sat, 4 Dec 2021 12:39:52 +0100 Subject: [PATCH] collect postal addresses for some payments --- js/stripe.js | 23 +++++--- liberapay/constants.py | 3 -- liberapay/models/exchange_route.py | 27 ++++++++++ templates/macros/postal-addresses.html | 4 +- www/%username/giving/pay/stripe/%payin_id.spt | 54 ++++++++++++++++--- www/%username/identity.spt | 4 -- www/%username/routes/add.spt | 26 +++++---- 7 files changed, 110 insertions(+), 31 deletions(-) diff --git a/js/stripe.js b/js/stripe.js index 413c793092..5752cb41ed 100644 --- a/js/stripe.js +++ b/js/stripe.js @@ -64,10 +64,21 @@ Liberapay.stripe_form_init = function($form) { $form.submit(); return; } + var local_address = $form.find('input[name="postal_address.local_address"]').val(); + local_address = !!local_address ? local_address.split(/(?:\r\n?|\n)/g) : [null]; + if (local_address.length === 1) { + local_address.push(null); + } if (element_type == 'iban') { var tokenData = {}; tokenData.currency = 'EUR'; tokenData.account_holder_name = $form.find('input[name="owner.name"]').val(); + tokenData.address_country = $form.find('input[name="postal_address.country"]').val(); + tokenData.address_state = $form.find('input[name="postal_address.region"]').val(); + tokenData.address_city = $form.find('input[name="postal_address.city"]').val(); + tokenData.address_zip = $form.find('input[name="postal_address.postal_code"]').val(); + tokenData.address_line1 = local_address[0]; + tokenData.address_line2 = local_address[1]; stripe.createToken(element, tokenData).then(Liberapay.wrap(function(result) { if (result.error) { $errorElement.text(result.error.message); @@ -85,12 +96,12 @@ Liberapay.stripe_form_init = function($form) { var pmData = { billing_details: { address: { - city: $form.find('input[name="owner.address.city"]').val(), - country: $form.find('input[name="owner.address.country"]').val(), - line1: $form.find('input[name="owner.address.line1"]').val(), - line2: $form.find('input[name="owner.address.line2"]').val(), - postal_code: $form.find('input[name="owner.address.postal_code"]').val(), - state: $form.find('input[name="owner.address.state"]').val(), + city: $form.find('input[name="postal_address.city"]').val(), + country: $form.find('input[name="postal_address.country"]').val(), + line1: local_address[0], + line2: local_address[1], + postal_code: $form.find('input[name="postal_address.postal_code"]').val(), + state: $form.find('input[name="postal_address.region"]').val(), }, email: $form.find('input[name="owner.email"]').val(), name: $form.find('input[name="owner.name"]').val(), diff --git a/liberapay/constants.py b/liberapay/constants.py index 306e389bee..902f996c2d 100644 --- a/liberapay/constants.py +++ b/liberapay/constants.py @@ -378,9 +378,6 @@ def __missing__(self, currency): POSTAL_ADDRESS_KEYS_LIBERAPAY = ( 'country', 'region', 'city', 'postal_code', 'local_address' ) -POSTAL_ADDRESS_KEYS_STRIPE = ( - 'line1', 'line2', 'city', 'state', 'postal_code', 'country' -) PRIVACY_FIELDS = OrderedDict([ ('hide_giving', (_("Hide total giving from others."), False)), diff --git a/liberapay/models/exchange_route.py b/liberapay/models/exchange_route.py index 1376a0c4d0..70a34cbef0 100644 --- a/liberapay/models/exchange_route.py +++ b/liberapay/models/exchange_route.py @@ -324,6 +324,33 @@ def get_partial_number(self): else: raise NotImplementedError(self.network) + def get_postal_address(self): + if self.network.startswith('stripe-'): + if self.address.startswith('pm_'): + return self.stripe_payment_method.billing_details.address + else: + return self.stripe_source.owner.address + else: + raise NotImplementedError(self.network) + + def set_postal_address(self, addr): + if self.network.startswith('stripe-'): + addr = addr.copy() + addr['state'] = addr.pop('region') + addr['line1'], addr['line2'], *_ignore = addr.pop('local_address').splitlines() + if self.address.startswith('pm_'): + self.stripe_payment_method = stripe.PaymentMethod.modify( + self.address, + billing_details={'address': addr}, + ) + else: + self.stripe_source = stripe.Source.modify( + self.address, + owner={'address': addr}, + ) + else: + raise NotImplementedError(self.network) + def has_been_charged_successfully(self): return bool(self.db.one(""" SELECT 1 diff --git a/templates/macros/postal-addresses.html b/templates/macros/postal-addresses.html index f5a72b4ec8..e359034625 100644 --- a/templates/macros/postal-addresses.html +++ b/templates/macros/postal-addresses.html @@ -1,6 +1,6 @@ % from "templates/macros/icons.html" import glyphicon -% macro postal_address_form_v2(prefix='postal_address', saved=None, country=None, required=True) +% macro postal_address_form_v2(prefix='postal_address', saved=None, required=True) % set required = 'required' if required else '' @@ -8,7 +8,7 @@ {{ _("Country") }}