# Encapsulation of logic used to convert a token generated by Stripe Elements # into a Stripe Customer + Card which can then be charged at a later point in time # Stores the generated customer & card ids against the local instance of Spree::CreditCard module Stripe class ProfileStorer def initialize(payment, provider) @payment = payment @provider = provider end def create_customer_from_token token = @payment.source.gateway_payment_profile_id response = @provider.store(token, options) if response.success? attrs = source_attrs_from(response) @payment.source.update_attributes!(attrs) else @payment.__send__(:gateway_error, response.message) end end private def options { email: @payment.order.email, login: Stripe.api_key, address: address_for(@payment) } end def address_for(payment) {}.tap do |hash| if address = payment.order.bill_address hash = { address1: address.address1, address2: address.address2, city: address.city, zip: address.zipcode } if address.country hash[:country] = address.country.name end if address.state hash[:state] = address.state.name end end end end def source_attrs_from(response) { cc_type: @payment.source.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 end end