Spree : oauth : Create Shipping/Billing address along with user

If you already have billing/shipping address and payment info of the store in the OAuth provider [eg. some app with Doorkeeper]; and you don’t want to let your users go through  the hassle to fill the form all over again, then, you can reuse the data. For this, you need to return the info along with user info from the OAuth provider in the response.

def from_omniauth(auth_data)
  spree_user = get_or_set_user(auth_data)
  address = set_shipping_address(spree_user, auth_data) if spree_user.shipping_address.nil?
  spree_user.update(bill_address_id: address.try(:id))
  set_roles(spree_user, auth_data)
  spree_user
end

# @return [Spree::Address]
# #
def set_shipping_address(user, auth_data)
  raw_info = auth_data.extra.raw_info
  country = Spree::Country.find_by(iso: 'US')
  state = country.states.find_by_abbr(raw_info.payment_source.billing_state)
  shipping_address = Spree::Address.create(
      {
          firstname: raw_info.first_name,
          lastname: raw_info.last_name,
          address1: raw_info.payment_source.billing_address,
          address2: raw_info.payment_source.billing_apt_or_suite,
          city: raw_info.payment_source.billing_city,
          zipcode: raw_info.payment_source.billing_zip,
          phone: raw_info.phone,
          state_name: state.name,
          alternative_phone: nil,
          company: nil,
          state_id: state.id,
          country_id: country.id
      })
end

One thought on “Spree : oauth : Create Shipping/Billing address along with user

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s