File: payments_util.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (60 lines) | stat: -rw-r--r-- 2,403 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/autofill/core/browser/payments/payments_util.h"

#include <algorithm>
#include <string_view>

#include "base/check_op.h"
#include "base/strings/string_number_conversions.h"
#include "components/autofill/core/browser/data_manager/payments/payments_data_manager.h"
#include "components/autofill/core/browser/payments/payments_customer_data.h"
#include "components/autofill/core/common/credit_card_number_validation.h"

namespace autofill::payments {

namespace {
constexpr int kCustomerHasNoBillingCustomerNumber = 0;
}

int64_t GetBillingCustomerId(const PaymentsDataManager& payments_data_manager) {
  // Get billing customer ID from the synced PaymentsCustomerData.
  const PaymentsCustomerData* const customer_data =
      payments_data_manager.GetPaymentsCustomerData();
  if (customer_data && !customer_data->customer_id.empty()) {
    int64_t billing_customer_id = 0;
    if (base::StringToInt64(customer_data->customer_id, &billing_customer_id)) {
      return billing_customer_id;
    }
  }
  return kCustomerHasNoBillingCustomerNumber;
}

bool HasGooglePaymentsAccount(
    const PaymentsDataManager& payments_data_manager) {
  return GetBillingCustomerId(payments_data_manager) !=
         kCustomerHasNoBillingCustomerNumber;
}

bool IsCreditCardNumberSupported(
    const std::u16string& card_number,
    const std::vector<std::pair<int, int>>& supported_card_bin_ranges) {
  std::u16string stripped_number = StripCardNumberSeparators(card_number);
  return std::ranges::any_of(supported_card_bin_ranges, [&](const auto& p) {
    auto& [bin_low, bin_high] = p;
    unsigned long range_num_of_digits = base::NumberToString(bin_low).size();
    DCHECK_EQ(range_num_of_digits, base::NumberToString(bin_high).size());
    // The first n digits of credit card number, where n is the number of
    // digits in range's starting/ending number.
    int first_digits_start, first_digits_end;
    base::StringToInt(stripped_number.substr(0, range_num_of_digits),
                      &first_digits_start);
    base::StringToInt(stripped_number.substr(0, range_num_of_digits),
                      &first_digits_end);
    return first_digits_start >= bin_low && first_digits_end <= bin_high;
  });
}

}  // namespace autofill::payments