Skip to content

Instantly share code, notes, and snippets.

@MisinformedDNA
Last active August 29, 2015 14:04
Show Gist options
  • Save MisinformedDNA/21c8fc3f8bf4b6014aef to your computer and use it in GitHub Desktop.
Save MisinformedDNA/21c8fc3f8bf4b6014aef to your computer and use it in GitHub Desktop.
Returns
require_relative '../base'
module Examples
module Orders
class Importing
def self.run(client)
# Add address information the order
address = {
firstname: 'Test',
lastname: 'User',
company: 'company',
address1: 'Unit 1',
address2: '1 Test Lane',
country_id: 49,
state_id: 1,
city: 'Bethesda',
zipcode: '20814',
phone: '(555) 555-5555'
}
response = client.post('/api/orders', {
order: {
user_id: 1,
completed_at: Date.today.to_s,
line_items: {
"0" => {
variant_id: 1,
quantity: 1
}
},
bill_address_attributes: address,
ship_address_attributes: address,
shipments: [
{
# The tracking number for this shipment (if there is one)
# tracking: 'H12345',
stock_location: 'Digital Store', # Actual stock location name as per the database
shipping_method: 'Digital', # Actual shipping method as per the database
cost: 0,
inventory_units: [
variant_id: 1
],
shipped_at: '2014-08-04T23:01:17.620Z'
}
],
payments: [{
amount: 15.82,
payment_method: 'Braintree'
}
],
# Apply a negative adjustment for the cost of the line item:
adjustments_attributes: [
{
label: 'Cyber Monday 2013',
amount: -3.95
}
]
#
# These adjustments are not assignable to specific items within the order through this API.
# That's even though we support this feature in Spree 2.2.
}
})
if response.status == 201
order = JSON.parse(response.body)
client.succeeded 'Order has been successfully imported.'
puts order
else
client.failed 'Order failed to import.'
puts response.body
end
client.post("/api/orders/#{order['number']}/return_authorizations",
{
return_authorization: {
state: "returned",
amount: order["total"],
order_id: order["id"]
}
})
if response.status == 201
order = JSON.parse(response.body)
client.succeeded 'Return has been successfully imported.'
else
client.failed 'Return failed to import.'
end
end
end
end
end
Examples.run(Examples::Orders::Importing)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment