<commit_before>module Spree
  class Gateway::StripeElementsGateway < Gateway::StripeGateway
    def method_type
      'stripe_elements'
    end
  end
end<commit_msg>[SD-852] implement payment intents<commit_after>module Spree
  class Gateway::StripeElementsGateway < Gateway::StripeGateway
    preference :intents, :boolean

    def method_type
      'stripe_elements'
    end

    def gateway_class
      if intents?
        ActiveMerchant::Billing::StripePaymentIntentsGateway
      else
        ActiveMerchant::Billing::StripeGateway
      end
    end

    def create_profile(payment)
      return unless payment.source.gateway_customer_profile_id.nil?
      options = {
        email: payment.order.email,
        login: preferred_secret_key,
      }.merge! address_for(payment)

      source = update_source!(payment.source)
      if source.gateway_payment_profile_id.present?
        if intents?
          creditcard = ActiveMerchant::Billing::StripeGateway::StripePaymentToken.new('id' => source.gateway_payment_profile_id)
        else
          creditcard = source.gateway_payment_profile_id
        end
      else
        creditcard = source
      end

      response = provider.store(creditcard, options)
      if response.success?
        cc_type=payment.source.cc_type
        response_cc_type = response.params['sources']['data'].first['brand']
        cc_type = CARD_TYPE_MAPPING[response_cc_type] if CARD_TYPE_MAPPING.include?(response_cc_type)

        if intents?
          payment.source.update!(
            cc_type: payment.source.cc_type,
            gateway_customer_profile_id: response.params['customer'],
            gateway_payment_profile_id: response.params['id']
          )
        else
          payment.source.update!({
            cc_type: cc_type, # side-effect of update_source!
            gateway_customer_profile_id: response.params['id'],
            gateway_payment_profile_id: response.params['default_source'] || response.params['default_card']
          })
        end
      else
        payment.send(:gateway_error, response.message)
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.08262108262108261, 'fraction_numerical': 0.0014245014245014246, 'mean_word_length': 2.8170289855072466, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5101842', 'n_tokens_mistral': 616, 'n_tokens_neox': 606, 'n_words': 104}