Copy an order using Bootic’s Ruby client

The following guide illustrates the order creation flow using Bootic’s official Ruby API client.

This example shows how to use an existing closed order to create a copy with a public URL where a user can complete and pay it using their prefered payment method.

1. Configure the client.

Read how to configure the Ruby client library to get the library ready.

2. Instantiate the client

The following creates a Bootic client instance for server-to-server scripts.

client = BooticClient.client(:client_credentials, scope: 'admin')

This example uses a client authorized by a shop owner, for example in a Ruby on Rails application.

client = BooticClient.client(:authorized, scope: 'admin', access_token: session[:access_token]) do |new_token|
  session[:access_token] = new_token
end

3. Load the root resource.

root = client.root

The root resource includes information on your current scope and the shops you own. From there you can navigate to other resources and take actions such as creating orders.

4. Find an existing order

We’ll first find an existing closed order so we can use it as a template to create a new one in the checkout state. A human user will then be able to complete and pay it using Bootic’s checkout web interface.

# Our account's shop
shop = root.shops.first

# Find the last closed order
orders = shop.orders(sort: 'updated_on.desc', status: 'closed', per_page: 1)
order = orders.items.first

5. Copy attributes and create new order

# Create a new order using this order's product, contact and address information
payload = {
  contact: {email: order.contact.email},
  address: {id: order.address.id},
  line_items: []
}

# Copy line items over to new order
# (you can change the units, or add new line items)
order.line_items.each do |item|
  payload[:line_items] << {variant_id: item.variant_id, units: item.units}
end

# Create the new order
new_order = shop.create_order(payload)

# Check for errors
if new_order.errors
  # Handle errors
else
  # New Order created! Display Web checkout URL for user to complete payment
  puts new_order.rels[:web_checkout].href if new_order.has?(:web_checkout)
end

A user can now visit the order’s checkout URL in their browser and fullfil the order payment.

Order checkout

Alternatively, there’s also a :web_cart link relation where the user can load the order as a shopping cart in their browser, where they can further modify it by adding or removing products.

# New Order created! Display Web cart URL for user to modify cart further
puts new_order.rels[:web_cart].href if new_order.has?(:web_cart)

Order cart