From 9eba56b4cd3e44f45571fba1fc5fbdb1be0d08b2 Mon Sep 17 00:00:00 2001 From: Changaco Date: Thu, 6 Jun 2024 09:40:09 +0200 Subject: [PATCH] switch back to Sources API for SEPA Direct Debit --- js/stripe.js | 80 ++++++++++++------- www/%username/giving/pay/stripe/%payin_id.spt | 4 +- 2 files changed, 56 insertions(+), 28 deletions(-) diff --git a/js/stripe.js b/js/stripe.js index 60777d81c..f024f583c 100644 --- a/js/stripe.js +++ b/js/stripe.js @@ -99,33 +99,59 @@ Liberapay.stripe_form_init = function($form) { if (local_address.length === 1) { local_address.push(null); } - var pmData = { - billing_details: { - address: { - city: $postal_address_city.val(), - country: $postal_address_country.val(), - line1: local_address[0], - line2: local_address[1], - postal_code: $postal_address_code.val(), - state: $postal_address_region.val(), - }, - email: $form.find('input[name="owner.email"]').val(), - name: $form.find('input[name="owner.name"]').val(), - } - }; - stripe.createPaymentMethod(pmType, element, pmData).then(Liberapay.wrap(function(result) { - if (result.error) { - $errorElement.text(result.error.message); - } else { - submitting = true; - $form.find('input[name="route"]').remove(); - $form.find('input[name="stripe_pm_id"]').remove(); - var $hidden_input = $(''); - $hidden_input.val(result.paymentMethod.id); - $form.append($hidden_input); - $form.submit(); - } - })); + if (element_type == 'iban') { + var tokenData = { + currency: 'EUR', + account_holder_name: $form.find('input[name="owner.name"]').val(), + address_country: $postal_address_country.val(), + address_state: $postal_address_region.val(), + address_city: $postal_address_city.val(), + address_zip: $postal_address_code.val(), + address_line1: local_address[0], + address_line2: local_address[1], + }; + stripe.createToken(element, tokenData).then(Liberapay.wrap(function(result) { + if (result.error) { + $errorElement.text(result.error.message); + } else { + submitting = true; + $form.find('input[name="route"]').remove(); + $form.find('input[name="token"]').remove(); + var $hidden_input = $(''); + $hidden_input.val(result.token.id); + $form.append($hidden_input); + $form.submit(); + } + })); + } else { + var pmData = { + billing_details: { + address: { + city: $postal_address_city.val(), + country: $postal_address_country.val(), + line1: local_address[0], + line2: local_address[1], + postal_code: $postal_address_code.val(), + state: $postal_address_region.val(), + }, + email: $form.find('input[name="owner.email"]').val(), + name: $form.find('input[name="owner.name"]').val(), + } + }; + stripe.createPaymentMethod(pmType, element, pmData).then(Liberapay.wrap(function(result) { + if (result.error) { + $errorElement.text(result.error.message); + } else { + submitting = true; + $form.find('input[name="route"]').remove(); + $form.find('input[name="stripe_pm_id"]').remove(); + var $hidden_input = $(''); + $hidden_input.val(result.paymentMethod.id); + $form.append($hidden_input); + $form.submit(); + } + })); + } })); $form.attr('action', ''); }; diff --git a/www/%username/giving/pay/stripe/%payin_id.spt b/www/%username/giving/pay/stripe/%payin_id.spt index 5e9d02191..53c7d7c1a 100644 --- a/www/%username/giving/pay/stripe/%payin_id.spt +++ b/www/%username/giving/pay/stripe/%payin_id.spt @@ -205,6 +205,7 @@ if tippees: raise response.redirect(payer.path( 'giving/pay?redirect_reason=unaccepted_currency' )) + route_address_pattern = 'src_%' if payment_type == 'sdd' and len(tips) == 1 else '%' routes = website.db.all(""" SELECT r FROM exchange_routes r @@ -212,11 +213,12 @@ if tippees: AND r.status = 'chargeable' AND r.network::text LIKE %s AND (r.one_off IS FALSE OR r.ctime > (current_timestamp - interval '6 hours')) + AND r.address LIKE %s ORDER BY r.is_default_for = %s DESC NULLS LAST , r.is_default DESC NULLS LAST , r.network = 'stripe-sdd' DESC , r.id DESC - """, (payer.id, 'stripe-' + (payment_type or '%'), payment.currency)) + """, (payer.id, 'stripe-' + (payment_type or '%'), route_address_pattern, payment.currency)) if routes: route = None while routes: