smartglocal_card.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "smartglocal/smartglocal_card.h"
  8. #include <QtCore/QRegularExpression>
  9. namespace SmartGlocal {
  10. Card::Card(
  11. QString type,
  12. QString network,
  13. QString maskedNumber)
  14. : _type(type)
  15. , _network(network)
  16. , _maskedNumber(maskedNumber) {
  17. }
  18. Card Card::Empty() {
  19. return Card(QString(), QString(), QString());
  20. }
  21. Card Card::DecodedObjectFromAPIResponse(QJsonObject object) {
  22. const auto string = [&](QStringView key) {
  23. return object.value(key).toString();
  24. };
  25. const auto type = string(u"card_type");
  26. const auto network = string(u"card_network");
  27. const auto maskedNumber = string(u"masked_card_number");
  28. if (type.isEmpty() || maskedNumber.isEmpty()) {
  29. return Card::Empty();
  30. }
  31. return Card(type, network, maskedNumber);
  32. }
  33. QString Card::type() const {
  34. return _type;
  35. }
  36. QString Card::network() const {
  37. return _network;
  38. }
  39. QString Card::maskedNumber() const {
  40. return _maskedNumber;
  41. }
  42. bool Card::empty() const {
  43. return _type.isEmpty() || _maskedNumber.isEmpty();
  44. }
  45. QString Last4(const Card &card) {
  46. static const auto RegExp = QRegularExpression("[^\\d]\\d*(\\d{4})$");
  47. const auto masked = card.maskedNumber();
  48. const auto m = RegExp.match(masked);
  49. return m.hasMatch() ? m.captured(1) : QString();
  50. }
  51. } // namespace SmartGlocal