class ActiveMerchant::Billing::StripePaymentIntentsGateway

This gateway uses the current Stripe Payment Intents API. For the legacy API, see the Stripe gateway

Constants

ALLOWED_CANCELLATION_REASONS
ALLOWED_METHOD_STATES
CONFIRM_INTENT_ATTRIBUTES
CREATE_INTENT_ATTRIBUTES
DEFAULT_API_VERSION
UPDATE_INTENT_ATTRIBUTES

Public Instance Methods

authorize(money, payment_method, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/stripe_payment_intents.rb, line 84
def authorize(money, payment_method, options = {})
  create_intent(money, payment_method, options.merge!(confirm: true, capture_method: 'manual'))
end
capture(money, intent_id, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/stripe_payment_intents.rb, line 92
def capture(money, intent_id, options = {})
  post = {}
  post[:amount_to_capture] = money
  add_connected_account(post, options)
  commit(:post, "payment_intents/#{intent_id}/capture", post, options)
end
confirm_intent(intent_id, payment_method, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/stripe_payment_intents.rb, line 43
def confirm_intent(intent_id, payment_method, options = {})
  post = {}
  add_payment_method_token(post, payment_method, options)
  CONFIRM_INTENT_ATTRIBUTES.each do |attribute|
    add_whitelisted_attribute(post, options, attribute)
  end

  commit(:post, "payment_intents/#{intent_id}/confirm", post, options)
end
create_intent(money, payment_method, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/stripe_payment_intents.rb, line 18
def create_intent(money, payment_method, options = {})
  post = {}
  add_amount(post, money, options, true)
  add_capture_method(post, options)
  add_confirmation_method(post, options)
  add_customer(post, options)
  add_payment_method_token(post, payment_method, options)
  add_metadata(post, options)
  add_return_url(post, options)
  add_connected_account(post, options)
  add_shipping_address(post, options)
  setup_future_usage(post, options)
  add_exemption(post, options)

  CREATE_INTENT_ATTRIBUTES.each do |attribute|
    add_whitelisted_attribute(post, options, attribute)
  end

  commit(:post, 'payment_intents', post, options)
end
create_payment_method(payment_method, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/stripe_payment_intents.rb, line 53
def create_payment_method(payment_method, options = {})
  post = {}
  post[:type] = 'card'
  post[:card] = {}
  post[:card][:number] = payment_method.number
  post[:card][:exp_month] = payment_method.month
  post[:card][:exp_year] = payment_method.year
  post[:card][:cvc] = payment_method.verification_value if payment_method.verification_value
  add_billing_address(post, options)

  commit(:post, 'payment_methods', post, options)
end
purchase(money, payment_method, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/stripe_payment_intents.rb, line 88
def purchase(money, payment_method, options = {})
  create_intent(money, payment_method, options.merge!(confirm: true, capture_method: 'automatic'))
end
refund(money, intent_id, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/stripe_payment_intents.rb, line 105
def refund(money, intent_id, options = {})
  intent = commit(:get, "payment_intents/#{intent_id}", nil, options)
  charge_id = intent.params.dig('charges', 'data')[0].dig('id')
  super(money, charge_id, options)
end
show_intent(intent_id, options) click to toggle source
# File lib/active_merchant/billing/gateways/stripe_payment_intents.rb, line 39
def show_intent(intent_id, options)
  commit(:get, "payment_intents/#{intent_id}", nil, options)
end
store(payment_method, options = {}) click to toggle source

Note: Not all payment methods are currently supported by the Payment Methods API Current implementation will create a PaymentMethod object if the method is a token or credit card All other types will default to legacy Stripe store

# File lib/active_merchant/billing/gateways/stripe_payment_intents.rb, line 114
def store(payment_method, options = {})
  params = {}
  post = {}

  # If customer option is provided, create a payment method and attach to customer id
  # Otherwise, create a customer, then attach
  if payment_method.is_a?(StripePaymentToken) || payment_method.is_a?(ActiveMerchant::Billing::CreditCard)
    add_payment_method_token(params, payment_method, options)
    if options[:customer]
      customer_id = options[:customer]
    else
      post[:validate] = options[:validate] unless options[:validate].nil?
      post[:description] = options[:description] if options[:description]
      post[:email] = options[:email] if options[:email]
      customer = commit(:post, 'customers', post, options)
      customer_id = customer.params['id']
    end
    commit(:post, "payment_methods/#{params[:payment_method]}/attach", { customer: customer_id }, options)
  else
    super(payment, options)
  end
end
unstore(identification, options = {}, deprecated_options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/stripe_payment_intents.rb, line 137
def unstore(identification, options = {}, deprecated_options = {})
  if identification.include?('pm_')
    _, payment_method = identification.split('|')
    commit(:post, "payment_methods/#{payment_method}/detach", nil, options)
  else
    super(identification, options, deprecated_options)
  end
end
update_intent(money, intent_id, payment_method, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/stripe_payment_intents.rb, line 66
def update_intent(money, intent_id, payment_method, options = {})
  post = {}
  post[:amount] = money if money

  add_payment_method_token(post, payment_method, options)
  add_payment_method_types(post, options)
  add_customer(post, options)
  add_metadata(post, options)
  add_shipping_address(post, options)
  add_connected_account(post, options)

  UPDATE_INTENT_ATTRIBUTES.each do |attribute|
    add_whitelisted_attribute(post, options, attribute)
  end

  commit(:post, "payment_intents/#{intent_id}", post, options)
end
void(intent_id, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/stripe_payment_intents.rb, line 99
def void(intent_id, options = {})
  post = {}
  post[:cancellation_reason] = options[:cancellation_reason] if ALLOWED_CANCELLATION_REASONS.include?(options[:cancellation_reason])
  commit(:post, "payment_intents/#{intent_id}/cancel", post, options)
end

Private Instance Methods

add_billing_address(post, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/stripe_payment_intents.rb, line 232
def add_billing_address(post, options = {})
  return unless billing = options[:billing_address] || options[:address]
  post[:billing_details] = {}
  post[:billing_details][:address] = {}
  post[:billing_details][:address][:city] = billing[:city] if billing[:city]
  post[:billing_details][:address][:country] = billing[:country] if billing[:country]
  post[:billing_details][:address][:line1] = billing[:address1] if billing[:address1]
  post[:billing_details][:address][:line2] = billing[:address2] if billing[:address2]
  post[:billing_details][:address][:postal_code] = billing[:zip] if billing[:zip]
  post[:billing_details][:address][:state] = billing[:state] if billing[:state]
  post[:billing_details][:email] = billing[:email] if billing[:email]
  post[:billing_details][:name] = billing[:name] if billing[:name]
  post[:billing_details][:phone] = billing[:phone] if billing[:phone]
  post
end
add_capture_method(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/stripe_payment_intents.rb, line 153
def add_capture_method(post, options)
  capture_method = options[:capture_method].to_s
  post[:capture_method] = capture_method if ALLOWED_METHOD_STATES.include?(capture_method)
  post
end
add_confirmation_method(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/stripe_payment_intents.rb, line 159
def add_confirmation_method(post, options)
  confirmation_method = options[:confirmation_method].to_s
  post[:confirmation_method] = confirmation_method if ALLOWED_METHOD_STATES.include?(confirmation_method)
  post
end
add_connected_account(post, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/stripe_payment_intents.rb, line 221
def add_connected_account(post, options = {})
  return unless options[:transfer_destination]
  post[:transfer_data] = {}
  post[:transfer_data][:destination] = options[:transfer_destination]
  post[:transfer_data][:amount] = options[:transfer_amount] if options[:transfer_amount]
  post[:on_behalf_of] = options[:on_behalf_of] if options[:on_behalf_of]
  post[:transfer_group] = options[:transfer_group] if options[:transfer_group]
  post[:application_fee_amount] = options[:application_fee] if options[:application_fee]
  post
end
add_customer(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/stripe_payment_intents.rb, line 165
def add_customer(post, options)
  customer = options[:customer].to_s
  post[:customer] = customer if customer.start_with?('cus_')
  post
end
add_exemption(post, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/stripe_payment_intents.rb, line 208
def add_exemption(post, options = {})
  return unless options[:confirm]
  post[:payment_method_options] ||= {}
  post[:payment_method_options][:card] ||= {}
  post[:payment_method_options][:card][:moto] = true if options[:moto]
end
add_payment_method_token(post, payment_method, options) click to toggle source
# File lib/active_merchant/billing/gateways/stripe_payment_intents.rb, line 178
def add_payment_method_token(post, payment_method, options)
  return if payment_method.nil?

  if payment_method.is_a?(ActiveMerchant::Billing::CreditCard)
    p = create_payment_method(payment_method, options)
    payment_method = p.params['id']
  end

  if payment_method.is_a?(StripePaymentToken)
    post[:payment_method] = payment_method.payment_data['id']
  elsif payment_method.is_a?(String)
    if payment_method.include?('|')
      customer_id, payment_method_id = payment_method.split('|')
      token = payment_method_id
      post[:customer] = customer_id
    else
      token = payment_method
    end
    post[:payment_method] = token
  end
end
add_payment_method_types(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/stripe_payment_intents.rb, line 200
def add_payment_method_types(post, options)
  payment_method_types = options[:payment_method_types] if options[:payment_method_types]
  return if payment_method_types.nil?

  post[:payment_method_types] = Array(payment_method_types)
  post
end
add_return_url(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/stripe_payment_intents.rb, line 171
def add_return_url(post, options)
  return unless options[:confirm]
  post[:confirm] = options[:confirm]
  post[:return_url] = options[:return_url] if options[:return_url]
  post
end
add_shipping_address(post, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/stripe_payment_intents.rb, line 248
def add_shipping_address(post, options = {})
  return unless shipping = options[:shipping]
  post[:shipping] = {}
  post[:shipping][:address] = {}
  post[:shipping][:address][:line1] = shipping[:address][:line1]
  post[:shipping][:address][:city] = shipping[:address][:city] if shipping[:address][:city]
  post[:shipping][:address][:country] = shipping[:address][:country] if shipping[:address][:country]
  post[:shipping][:address][:line2] = shipping[:address][:line2] if shipping[:address][:line2]
  post[:shipping][:address][:postal_code] = shipping[:address][:postal_code] if shipping[:address][:postal_code]
  post[:shipping][:address][:state] = shipping[:address][:state] if shipping[:address][:state]

  post[:shipping][:name] = shipping[:name]
  post[:shipping][:carrier] = shipping[:carrier] if shipping[:carrier]
  post[:shipping][:phone] = shipping[:phone] if shipping[:phone]
  post[:shipping][:tracking_number] = shipping[:tracking_number] if shipping[:tracking_number]
  post
end
add_whitelisted_attribute(post, options, attribute) click to toggle source
# File lib/active_merchant/billing/gateways/stripe_payment_intents.rb, line 148
def add_whitelisted_attribute(post, options, attribute)
  post[attribute] = options[attribute] if options[attribute]
  post
end
setup_future_usage(post, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/stripe_payment_intents.rb, line 215
def setup_future_usage(post, options = {})
  post[:setup_future_usage] = options[:setup_future_usage] if %w( on_session off_session ).include?(options[:setup_future_usage])
  post[:off_session] = options[:off_session] if options[:off_session] && options[:confirm] == true
  post
end