Sha256: 488fd6f29c7edeb365b8a772611cc3b037a8119be5d4dfebde984bd6872352da

Contents?: true

Size: 1.38 KB

Versions: 10

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module SolidusBolt
  module Users
    class SyncPaymentSourcesService < SolidusBolt::BaseService
      attr_reader :user, :access_token, :bolt_payment_method

      def initialize(user:, access_token:)
        @user = user
        @access_token = access_token
        @bolt_payment_method = SolidusBolt::PaymentMethod.last
        super
      end

      def call
        return if user.nil? || access_token.nil? || bolt_payment_method.nil?

        payment_methods.each do |payment_method|
          add_payment_source(payment_method)
        end
      end

      private

      def user_info
        @user_info ||= Accounts::DetailService.call(access_token: access_token)
      end

      def payment_methods
        user_info['payment_methods']
      end

      def spree_wallet
        @spree_wallet ||= Spree::Wallet.new(user)
      end

      def add_payment_source(payment_method)
        # format of card_expiration: '2022-04'
        card_expiration = "#{payment_method['exp_year']}-#{payment_method['exp_month'].to_s.rjust(2, '0')}"
        payment_source = SolidusBolt::PaymentSource.find_or_create_by!(
          payment_method_id: bolt_payment_method.id,
          card_last4: payment_method['last4'],
          card_expiration: card_expiration,
          card_id: payment_method['id']
        )
        spree_wallet.add(payment_source)
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
solidus_bolt-0.7.2 app/services/solidus_bolt/users/sync_payment_sources_service.rb
solidus_bolt-0.7.1 app/services/solidus_bolt/users/sync_payment_sources_service.rb
solidus_bolt-0.7.0 app/services/solidus_bolt/users/sync_payment_sources_service.rb
solidus_bolt-0.6.0 app/services/solidus_bolt/users/sync_payment_sources_service.rb
solidus_bolt-0.5.0 app/services/solidus_bolt/users/sync_payment_sources_service.rb
solidus_bolt-0.4.0 app/services/solidus_bolt/users/sync_payment_sources_service.rb
solidus_bolt-0.3.0 app/services/solidus_bolt/users/sync_payment_sources_service.rb
solidus_bolt-0.2.0 app/services/solidus_bolt/users/sync_payment_sources_service.rb
solidus_bolt-0.1.0 app/services/solidus_bolt/users/sync_payment_sources_service.rb
solidus_bolt-0.0.1 app/services/solidus_bolt/users/sync_payment_sources_service.rb