From dfefe9d88b6eb070616606f01fc140db48311658 Mon Sep 17 00:00:00 2001 From: tintou Date: Sun, 21 May 2017 23:45:03 +0200 Subject: [PATCH] Allow putting a slash for the date (#298) --- src/Dialogs/StripeDialog.vala | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Dialogs/StripeDialog.vala b/src/Dialogs/StripeDialog.vala index aa07ce3b4..02860ada6 100644 --- a/src/Dialogs/StripeDialog.vala +++ b/src/Dialogs/StripeDialog.vala @@ -95,7 +95,7 @@ public class AppCenter.Widgets.StripeDialog : Gtk.Dialog { card_expiration_entry = new Gtk.Entry (); card_expiration_entry.hexpand = true; - card_expiration_entry.max_length = 4; + card_expiration_entry.max_length = 5; /// TRANSLATORS: Don't change the order, only transliterate card_expiration_entry.placeholder_text = _("MM / YY"); card_expiration_entry.primary_icon_name = "office-calendar-symbolic"; @@ -105,6 +105,14 @@ public class AppCenter.Widgets.StripeDialog : Gtk.Dialog { validate (2, card_expiration_entry.text); }); + card_expiration_entry.focus_out_event.connect (() => { + var expiration_text = card_expiration_entry.text; + if (!("/" in expiration_text) && expiration_text.char_count () > 2) { + int position = 2; + card_expiration_entry.insert_text ("/", 1, ref position); + } + }); + card_cvc_entry = new Gtk.Entry (); card_cvc_entry.hexpand = true; card_cvc_entry.input_purpose = Gtk.InputPurpose.DIGITS; @@ -291,10 +299,10 @@ public class AppCenter.Widgets.StripeDialog : Gtk.Dialog { card_valid = is_card_valid (new_text); break; case 2: - if (new_text.length != 4) { + if (new_text.length < 4) { expiration_valid = false; } else { - var regex = new Regex ("""[0-9]{4}"""); + var regex = new Regex ("""^[0-9]{2}\/?[0-9]{2}$"""); expiration_valid = regex.match (new_text); } break; @@ -364,9 +372,10 @@ public class AppCenter.Widgets.StripeDialog : Gtk.Dialog { private void on_pay_clicked () { new Thread (null, () => { - var year = (int.parse (card_expiration_entry.text[2:4]) + 2000).to_string (); + string expiration_dateyear = card_expiration_entry.text.replace("/", ""); + var year = (int.parse (expiration_dateyear[2:4]) + 2000).to_string (); - var data = get_stripe_data (stripe_key, email_entry.text, (amount * 100).to_string (), card_number_entry.text, card_expiration_entry.text[0:2], year, card_cvc_entry.text); + var data = get_stripe_data (stripe_key, email_entry.text, (amount * 100).to_string (), card_number_entry.text, expiration_dateyear[0:2], year, card_cvc_entry.text); debug ("Stripe data:%s", data); string? error = null; try {