Welcome to mirror list, hosted at ThFree Co, Russian Federation.

upsert_credit_card_validation_service.rb « users « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e0f8197194414f1faf6e6098203c8003adc0ffab (plain)
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
61
62
63
64
65
66
67
68
69
# frozen_string_literal: true

module Users
  class UpsertCreditCardValidationService < BaseService
    attr_reader :params

    def initialize(params)
      @params = params.to_h.with_indifferent_access
    end

    def execute
      credit_card = Users::CreditCardValidation.find_or_initialize_by_user(user_id)

      credit_card_params = {
        credit_card_validated_at: credit_card_validated_at,
        last_digits: last_digits,
        holder_name: holder_name,
        network: network,
        expiration_date: expiration_date
      }

      credit_card.update(credit_card_params)

      success
    rescue ActiveRecord::InvalidForeignKey, ActiveRecord::NotNullViolation
      error
    rescue StandardError => e
      Gitlab::ErrorTracking.track_exception(e)
      error
    end

    private

    def user_id
      params.fetch(:user_id)
    end

    def credit_card_validated_at
      params.fetch(:credit_card_validated_at)
    end

    def last_digits
      Integer(params.fetch(:credit_card_mask_number), 10)
    end

    def holder_name
      params.fetch(:credit_card_holder_name)
    end

    def network
      params.fetch(:credit_card_type)
    end

    def expiration_date
      year = params.fetch(:credit_card_expiration_year)
      month = params.fetch(:credit_card_expiration_month)

      Date.new(year, month, -1) # last day of the month
    end

    def success
      ServiceResponse.success(message: _('Credit card validation record saved'))
    end

    def error
      ServiceResponse.error(message: _('Error saving credit card validation record'))
    end
  end
end