2018-07-23 11:15:16 +02:00
|
|
|
Liberapay.stripe_init = function() {
|
|
|
|
var $form = $('form#stripe');
|
2024-06-04 09:27:07 +02:00
|
|
|
if ($form.length !== 1) return;
|
2018-07-23 11:15:16 +02:00
|
|
|
$('fieldset.hidden').prop('disabled', true);
|
|
|
|
$('button[data-modify]').click(function() {
|
|
|
|
var $btn = $(this);
|
|
|
|
$($btn.data('modify')).removeClass('hidden').prop('disabled', false);
|
|
|
|
$btn.parent().addClass('hidden');
|
|
|
|
});
|
|
|
|
|
2024-06-04 09:27:07 +02:00
|
|
|
var $errorElement = $('#stripe-errors');
|
|
|
|
var stripe = null;
|
|
|
|
if (window.Stripe) {
|
|
|
|
stripe = Stripe($form.data('stripe-pk'));
|
|
|
|
} else {
|
|
|
|
$errorElement.text($form.attr('data-msg-stripe-missing'));
|
|
|
|
$errorElement.hide().fadeIn()[0].scrollIntoView();
|
|
|
|
}
|
|
|
|
|
2018-09-11 17:09:03 +02:00
|
|
|
var $container = $('#stripe-element');
|
2024-05-23 16:48:33 +02:00
|
|
|
var $postal_address_alert = $form.find('.msg-postal-address-required');
|
|
|
|
var $postal_address_country = $form.find('select[name="postal_address.country"]');
|
|
|
|
var $postal_address_region = $form.find('input[name="postal_address.region"]');
|
|
|
|
var $postal_address_city = $form.find('input[name="postal_address.city"]');
|
|
|
|
var $postal_address_code = $form.find('input[name="postal_address.postal_code"]');
|
|
|
|
var $postal_address_local = $form.find('textarea[name="postal_address.local_address"]');
|
|
|
|
function is_postal_address_filled() {
|
|
|
|
return $postal_address_country.val() > '' &&
|
|
|
|
$postal_address_city.val() > '' &&
|
|
|
|
$postal_address_code.val() > '' &&
|
|
|
|
$postal_address_local.val() > '';
|
|
|
|
}
|
|
|
|
function is_postal_address_required() {
|
2025-04-15 21:00:23 +02:00
|
|
|
return /AD|AL|BL|CH|GB|GG|GI|IM|JE|MC|MD|ME|MK|NC|PF|PM|SM|TF|VA|WF/.test(
|
|
|
|
$container.data('country')
|
|
|
|
);
|
2024-05-23 16:48:33 +02:00
|
|
|
}
|
|
|
|
|
2024-06-04 09:27:07 +02:00
|
|
|
if ($container.length === 1) {
|
2024-09-10 10:59:07 +02:00
|
|
|
var elements = stripe.elements({
|
2024-09-12 13:15:53 +02:00
|
|
|
onBehalfOf: $form.data('stripe-on-behalf-of') || undefined,
|
2024-09-10 10:59:07 +02:00
|
|
|
});
|
2024-08-06 10:30:05 +02:00
|
|
|
var element_type = $container.data('type');
|
|
|
|
var options = {style: {
|
|
|
|
base: {
|
|
|
|
color: rgb_to_hex($container.css('color')),
|
|
|
|
fontFamily: $container.css('font-family'),
|
|
|
|
fontSize: $container.css('font-size'),
|
|
|
|
lineHeight: $container.css('line-height'),
|
2024-05-23 16:48:33 +02:00
|
|
|
}
|
2024-08-06 10:30:05 +02:00
|
|
|
}};
|
|
|
|
if (element_type == 'iban') {
|
|
|
|
options.supportedCountries = ['SEPA'];
|
2024-05-23 16:48:33 +02:00
|
|
|
}
|
2024-08-06 10:30:05 +02:00
|
|
|
var element = elements.create(element_type, options);
|
|
|
|
element.mount('#stripe-element');
|
|
|
|
element.addEventListener('change', function(event) {
|
|
|
|
if (event.error) {
|
|
|
|
$errorElement.text(event.error.message);
|
|
|
|
} else {
|
|
|
|
$errorElement.text('');
|
|
|
|
}
|
|
|
|
if (event.country) {
|
|
|
|
$container.data('country', event.country);
|
|
|
|
if (!is_postal_address_required()) {
|
|
|
|
$postal_address_alert.hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-07-23 11:15:16 +02:00
|
|
|
|
2024-06-04 09:27:07 +02:00
|
|
|
Liberapay.stripe_before_account_submit = async function() {
|
|
|
|
const response = await stripe.createToken('account', {
|
|
|
|
tos_shown_and_accepted: true,
|
|
|
|
});
|
|
|
|
if (response.token) {
|
|
|
|
$form.find('input[name="account_token"]').remove();
|
|
|
|
var $pm_id_input = $('<input type="hidden" name="account_token">');
|
|
|
|
$pm_id_input.val(response.token.id);
|
|
|
|
$pm_id_input.appendTo($form);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
$errorElement.text(response.error || response);
|
|
|
|
$errorElement.hide().fadeIn()[0].scrollIntoView();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Liberapay.stripe_before_element_submit = async function() {
|
2024-05-31 11:26:27 +02:00
|
|
|
// If the Payment Element is hidden, simply let the browser submit the form
|
2018-09-11 17:09:03 +02:00
|
|
|
if ($container.parents('.hidden').length > 0) {
|
2024-06-04 09:27:07 +02:00
|
|
|
console.debug("stripe_before_element_submit: ignoring hidden payment element");
|
2024-05-31 11:26:27 +02:00
|
|
|
return true;
|
2018-07-23 11:15:16 +02:00
|
|
|
}
|
2024-08-06 10:30:05 +02:00
|
|
|
// If Stripe.js is missing, stop the submission
|
|
|
|
if (!stripe) {
|
|
|
|
$errorElement.hide().fadeIn()[0].scrollIntoView();
|
|
|
|
return false;
|
|
|
|
}
|
2024-05-31 11:26:27 +02:00
|
|
|
// Create the PaymentMethod
|
2024-05-23 16:48:33 +02:00
|
|
|
var pmType = element_type;
|
|
|
|
if (element_type == 'iban') {
|
|
|
|
pmType = 'sepa_debit';
|
|
|
|
if (is_postal_address_required() && !is_postal_address_filled()) {
|
|
|
|
$postal_address_alert.removeClass('hidden').hide().fadeIn()[0].scrollIntoView();
|
2024-05-31 11:26:27 +02:00
|
|
|
return false;
|
2024-05-23 16:48:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
var local_address = $postal_address_local.val();
|
2024-05-31 11:26:27 +02:00
|
|
|
local_address = local_address ? local_address.split(/(?:\r\n?|\n)/g) : [undefined];
|
collect postal addresses for some payments (#2095)
On Monday (November 29) we received an email from Stripe informing us that starting on December 6 postal addresses will be required for SEPA Direct Debits from 16 non-EU territories: Andorra, French Polynesia, French Southern Territories, Gibraltar, United Kingdom, Guernsey, Isle of Man, Jersey, Monaco, New Caledonia, Saint Barthelemy, Saint Pierre and Miquelon, San Marino, Switzerland, Wallis and Futuna, Vatican City.
2021-12-09 09:49:42 +01:00
|
|
|
if (local_address.length === 1) {
|
2024-05-31 11:26:27 +02:00
|
|
|
local_address.push(undefined);
|
collect postal addresses for some payments (#2095)
On Monday (November 29) we received an email from Stripe informing us that starting on December 6 postal addresses will be required for SEPA Direct Debits from 16 non-EU territories: Andorra, French Polynesia, French Southern Territories, Gibraltar, United Kingdom, Guernsey, Isle of Man, Jersey, Monaco, New Caledonia, Saint Barthelemy, Saint Pierre and Miquelon, San Marino, Switzerland, Wallis and Futuna, Vatican City.
2021-12-09 09:49:42 +01:00
|
|
|
}
|
2024-08-22 16:38:53 +02:00
|
|
|
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(),
|
2024-05-31 11:26:27 +02:00
|
|
|
}
|
2024-08-22 16:38:53 +02:00
|
|
|
};
|
|
|
|
var result = await stripe.createPaymentMethod(pmType, element, pmData);
|
|
|
|
// If the PaymentMethod has been created, submit the form. Otherwise,
|
|
|
|
// display an error.
|
2024-10-11 19:06:08 +02:00
|
|
|
if (result.paymentMethod && result.paymentMethod.id) {
|
|
|
|
$form.find('input[name="route"]').remove();
|
|
|
|
$form.find('input[name="stripe_pm_id"]').remove();
|
|
|
|
var $pm_id_input = $('<input type="hidden" name="stripe_pm_id">');
|
|
|
|
$pm_id_input.val(result.paymentMethod.id);
|
|
|
|
$pm_id_input.appendTo($form);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
var msg = '' + (result.error ? result.error.message : result);
|
|
|
|
$errorElement.text(msg)[0].scrollIntoView();
|
2024-08-22 16:38:53 +02:00
|
|
|
return false;
|
2024-06-06 09:40:09 +02:00
|
|
|
}
|
2024-05-31 11:26:27 +02:00
|
|
|
};
|
2019-05-10 09:39:54 +02:00
|
|
|
};
|