Browse Source

Add support for draft orders

Dominique Simoneau-Ritchie 8 years ago
parent
commit
a42ccd8820

+ 10 - 0
lib/shopify_api/resources/draft_order.rb

@@ -0,0 +1,10 @@
+module ShopifyAPI
+  class DraftOrder < Base
+    include Metafields
+
+    def send_invoice(draft_order_invoice = ShopifyAPI::DraftOrderInvoice.new)
+      response = post(:send_invoice, {}, draft_order_invoice.encode)
+      load_attributes_from_response(response)
+    end
+  end
+end

+ 4 - 0
lib/shopify_api/resources/draft_order_invoice.rb

@@ -0,0 +1,4 @@
+module ShopifyAPI
+  class DraftOrderInvoice < Base
+  end
+end

+ 92 - 0
test/draft_order_test.rb

@@ -0,0 +1,92 @@
+require 'test_helper'
+
+class DraftOrderTest < Test::Unit::TestCase
+  def setup
+    super
+
+    fake 'draft_orders/517119332', body: load_fixture('draft_order')
+
+    @draft_order = ShopifyAPI::DraftOrder.find(517119332)
+  end
+
+  def test_get_draft_order
+    fake 'draft_orders/517119332', method: :get, status: 200, body: load_fixture('draft_order')
+
+    draft_order = ShopifyAPI::DraftOrder.find(517119332)
+
+    assert_equal 517119332, draft_order.id
+  end
+
+  def test_get_all_draft_orders
+    fake 'draft_orders', method: :get, status: 200, body: load_fixture('draft_orders')
+
+    draft_orders = ShopifyAPI::DraftOrder.all
+
+    assert_equal 1, draft_orders.length
+    assert_equal 517119332, draft_orders.first.id
+  end
+
+  def test_create_draft_order
+    fake 'draft_orders', method: :post, status: 201, body: load_fixture('draft_order')
+
+    draft_order = ShopifyAPI::DraftOrder.create(line_items: [{ quantity: 1, variant_id: 39072856 }])
+
+    assert_equal '{"draft_order":{"line_items":[{"quantity":1,"variant_id":39072856}]}}', FakeWeb.last_request.body
+    assert_equal 39072856, draft_order.line_items.first.variant_id
+  end
+
+  def test_update_draft_order
+    draft_order_response = ActiveSupport::JSON.decode(load_fixture('draft_order'))
+    draft_order_response['draft_order']['note'] = 'Test new note'
+    @draft_order.note = 'Test new note'
+    fake 'draft_orders/517119332', method: :put, status: 200, body: ActiveSupport::JSON.encode(draft_order_response)
+
+    @draft_order.save
+
+    assert_equal draft_order_response['draft_order']['note'], @draft_order.note
+  end
+
+  def test_send_invoice_with_no_params
+    fake 'draft_orders/517119332/send_invoice', method: :post, body: load_fixture('draft_order_invoice_no_params')
+
+    @draft_order.send_invoice
+
+    assert_equal '{"draft_order_invoice":{}}', FakeWeb.last_request.body
+  end
+
+  def test_send_invoice_with_params
+    draft_order_invoice_fixture = load_fixture('draft_order_invoice')
+    draft_order_invoice = ActiveSupport::JSON.decode(draft_order_invoice_fixture)
+    fake 'draft_orders/517119332/send_invoice', method: :post, body: draft_order_invoice_fixture
+
+    @draft_order.send_invoice(ShopifyAPI::DraftOrderInvoice.new(draft_order_invoice['draft_order_invoice']))
+
+    assert_equal draft_order_invoice, ActiveSupport::JSON.decode(FakeWeb.last_request.body)
+  end
+
+  def test_delete_draft_order
+    fake 'draft_orders/517119332', method: :delete, body: 'destroyed'
+    assert @draft_order.destroy
+  end
+
+  def test_add_metafields_to_draft_order
+    fake 'draft_orders/517119332/metafields', method: :post, status: 201, body: load_fixture('metafield')
+
+    field = @draft_order.add_metafield(ShopifyAPI::Metafield.new(namespace: 'contact', key: 'email', value: '123@example.com', value_type: 'string'))
+
+    assert_equal ActiveSupport::JSON.decode('{"metafield":{"namespace":"contact","key":"email","value":"123@example.com","value_type":"string"}}'), ActiveSupport::JSON.decode(FakeWeb.last_request.body)
+    assert !field.new_record?
+    assert_equal 'contact', field.namespace
+    assert_equal 'email', field.key
+    assert_equal '123@example.com', field.value
+  end
+
+  def test_get_metafields_for_draft_order
+    fake 'draft_orders/517119332/metafields', body: load_fixture('metafields')
+
+    metafields = @draft_order.metafields
+
+    assert_equal 2, metafields.length
+    assert metafields.all? { |m| m.is_a?(ShopifyAPI::Metafield) }
+  end
+end

+ 159 - 0
test/fixtures/draft_order.json

@@ -0,0 +1,159 @@
+{
+  "draft_order": {
+    "id": 517119332,
+    "note": "This is a note",
+    "email": "montana_hilpert@example.com",
+    "taxes_included": false,
+    "currency": "CAD",
+    "subtotal_price": "1007.41",
+    "total_tax": "0.00",
+    "total_price": "1027.41",
+    "invoice_sent_at": null,
+    "created_at": "2017-02-02T13:14:38-05:00",
+    "updated_at": "2017-02-02T13:14:38-05:00",
+    "tax_exempt": false,
+    "completed_at": null,
+    "name": "#D1",
+    "status": "open",
+    "line_items": [
+      {
+        "variant_id": 39072856,
+        "product_id": 632910392,
+        "title": "IPod Nano - 8gb",
+        "variant_title": "green",
+        "sku": "IPOD2008GREEN",
+        "vendor": null,
+        "price": "199.00",
+        "grams": 200,
+        "quantity": 1,
+        "requires_shipping": true,
+        "taxable": true,
+        "gift_card": false,
+        "fulfillment_service": "manual",
+        "tax_lines": [],
+        "applied_discount": null,
+        "name": "IPod Nano - 8gb - green",
+        "properties": [
+          {
+            "name": "Custom Engraving",
+            "value": "Happy Birthday"
+          }
+        ],
+        "custom": false
+      },
+      {
+        "variant_id": null,
+        "product_id": null,
+        "title": "Custom Item",
+        "variant_title": null,
+        "sku": null,
+        "vendor": null,
+        "price": "494.14",
+        "grams": 0,
+        "quantity": 2,
+        "requires_shipping": false,
+        "taxable": false,
+        "gift_card": false,
+        "fulfillment_service": "manual",
+        "tax_lines": [],
+        "applied_discount": {
+          "description": "A percentage discount for a custom line item",
+          "value": "3.58",
+          "title": "Custom",
+          "amount": "35.38",
+          "value_type": "percentage"
+        },
+        "name": "Custom item",
+        "properties": [],
+        "custom": true
+      }
+    ],
+    "shipping_address": {
+      "first_name": "Jan",
+      "address1": "512 Ernestina Forks",
+      "phone": "(639) 372 1289",
+      "city": "Lakefurt",
+      "zip": "24093",
+      "province": "Virginia",
+      "country": "United States",
+      "last_name": "Fisher",
+      "address2": "Apt. 702",
+      "company": "Steuber and Sons",
+      "latitude": 45.416311,
+      "longitude": -75.68683,
+      "name": "Jan Fisher",
+      "country_code": "US",
+      "province_code": "VA"
+    },
+    "billing_address": {
+      "first_name": "Jan",
+      "address1": "512 Ernestina Forks",
+      "phone": "(639) 372 1289",
+      "city": "Lakefurt",
+      "zip": "24093",
+      "province": "Virginia",
+      "country": "United States",
+      "last_name": "Fisher",
+      "address2": "Apt. 702",
+      "company": "Steuber and Sons",
+      "latitude": 45.416311,
+      "longitude": -75.68683,
+      "name": "Jan Fisher",
+      "country_code": "US",
+      "province_code": "VA"
+    },
+    "invoice_url": "https://checkout.myshopify.io/1/invoices/8e72bdccd0ac51067b947ac68c6f3804",
+    "applied_discount": {
+      "description": "A discount on the entire order",
+      "value": "1.48",
+      "title": "Custom",
+      "amount": "1.48",
+      "value_type": "fixed_amount"
+    },
+    "order_id": null,
+    "shipping_line": {
+      "title": "Custom shipping",
+      "price": "20.00",
+      "custom": true,
+      "handle": null
+    },
+    "tax_lines": [],
+    "tags": "",
+    "customer": {
+      "accepts_marketing": false,
+      "created_at": "2014-03-07T16:14:08-05:00",
+      "email": "bob.norman@hostmail.com",
+      "first_name": "Bob",
+      "id": 207119551,
+      "last_name": "Norman",
+      "last_order_id": null,
+      "multipass_identifier": null,
+      "note": null,
+      "orders_count": 0,
+      "state": "disabled",
+      "total_spent": "0.00",
+      "updated_at": "2014-03-07T16:14:08-05:00",
+      "verified_email": true,
+      "tags": "",
+      "last_order_name": null,
+      "default_address": {
+        "address1": "Chestnut Street 92",
+        "address2": "",
+        "city": "Louisville",
+        "company": null,
+        "country": "United States",
+        "first_name": null,
+        "id": 207119551,
+        "last_name": null,
+        "phone": "555-625-1199",
+        "province": "Kentucky",
+        "zip": "40202",
+        "name": null,
+        "province_code": "KY",
+        "country_code": "US",
+        "country_name": "United States",
+        "default": true
+      }
+    }
+  }
+}

+ 9 - 0
test/fixtures/draft_order_invoice.json

@@ -0,0 +1,9 @@
+{
+  "draft_order_invoice": {
+    "to": "paul.norman@example.com",
+    "from": "steve@apple.com",
+    "subject": "Here is your new order invoice!",
+    "custom_message": "This is a test custom message.",
+    "bcc": [ ]
+  }
+}

+ 4 - 0
test/fixtures/draft_order_invoice_no_params.json

@@ -0,0 +1,4 @@
+{
+  "draft_order_invoice": {
+  }
+}

+ 161 - 0
test/fixtures/draft_orders.json

@@ -0,0 +1,161 @@
+{
+  "draft_orders": [
+    {
+      "id": 517119332,
+      "note": "This is a note",
+      "email": "montana_hilpert@example.com",
+      "taxes_included": false,
+      "currency": "CAD",
+      "subtotal_price": "1007.41",
+      "total_tax": "0.00",
+      "total_price": "1027.41",
+      "invoice_sent_at": null,
+      "created_at": "2017-02-02T13:14:38-05:00",
+      "updated_at": "2017-02-02T13:14:38-05:00",
+      "tax_exempt": false,
+      "completed_at": null,
+      "name": "#D1",
+      "status": "open",
+      "line_items": [
+        {
+          "variant_id": 39072856,
+          "product_id": 632910392,
+          "title": "IPod Nano - 8gb",
+          "variant_title": "green",
+          "sku": "IPOD2008GREEN",
+          "vendor": null,
+          "price": "199.00",
+          "grams": 200,
+          "quantity": 1,
+          "requires_shipping": true,
+          "taxable": true,
+          "gift_card": false,
+          "fulfillment_service": "manual",
+          "tax_lines": [],
+          "applied_discount": null,
+          "name": "IPod Nano - 8gb - green",
+          "properties": [
+            {
+              "name": "Custom Engraving",
+              "value": "Happy Birthday"
+            }
+          ],
+          "custom": false
+        },
+        {
+          "variant_id": null,
+          "product_id": null,
+          "title": "Custom Item",
+          "variant_title": null,
+          "sku": null,
+          "vendor": null,
+          "price": "494.14",
+          "grams": 0,
+          "quantity": 2,
+          "requires_shipping": false,
+          "taxable": false,
+          "gift_card": false,
+          "fulfillment_service": "manual",
+          "tax_lines": [],
+          "applied_discount": {
+            "description": "A percentage discount for a custom line item",
+            "value": "3.58",
+            "title": "Custom",
+            "amount": "35.38",
+            "value_type": "percentage"
+          },
+          "name": "Custom item",
+          "properties": [],
+          "custom": true
+        }
+      ],
+      "shipping_address": {
+        "first_name": "Jan",
+        "address1": "512 Ernestina Forks",
+        "phone": "(639) 372 1289",
+        "city": "Lakefurt",
+        "zip": "24093",
+        "province": "Virginia",
+        "country": "United States",
+        "last_name": "Fisher",
+        "address2": "Apt. 702",
+        "company": "Steuber and Sons",
+        "latitude": 45.416311,
+        "longitude": -75.68683,
+        "name": "Jan Fisher",
+        "country_code": "US",
+        "province_code": "VA"
+      },
+      "billing_address": {
+        "first_name": "Jan",
+        "address1": "512 Ernestina Forks",
+        "phone": "(639) 372 1289",
+        "city": "Lakefurt",
+        "zip": "24093",
+        "province": "Virginia",
+        "country": "United States",
+        "last_name": "Fisher",
+        "address2": "Apt. 702",
+        "company": "Steuber and Sons",
+        "latitude": 45.416311,
+        "longitude": -75.68683,
+        "name": "Jan Fisher",
+        "country_code": "US",
+        "province_code": "VA"
+      },
+      "invoice_url": "https://checkout.myshopify.io/1/invoices/8e72bdccd0ac51067b947ac68c6f3804",
+      "applied_discount": {
+        "description": "A discount on the entire order",
+        "value": "1.48",
+        "title": "Custom",
+        "amount": "1.48",
+        "value_type": "fixed_amount"
+      },
+      "order_id": null,
+      "shipping_line": {
+        "title": "Custom shipping",
+        "price": "20.00",
+        "custom": true,
+        "handle": null
+      },
+      "tax_lines": [],
+      "tags": "",
+      "customer": {
+        "accepts_marketing": false,
+        "created_at": "2014-03-07T16:14:08-05:00",
+        "email": "bob.norman@hostmail.com",
+        "first_name": "Bob",
+        "id": 207119551,
+        "last_name": "Norman",
+        "last_order_id": null,
+        "multipass_identifier": null,
+        "note": null,
+        "orders_count": 0,
+        "state": "disabled",
+        "total_spent": "0.00",
+        "updated_at": "2014-03-07T16:14:08-05:00",
+        "verified_email": true,
+        "tags": "",
+        "last_order_name": null,
+        "default_address": {
+          "address1": "Chestnut Street 92",
+          "address2": "",
+          "city": "Louisville",
+          "company": null,
+          "country": "United States",
+          "first_name": null,
+          "id": 207119551,
+          "last_name": null,
+          "phone": "555-625-1199",
+          "province": "Kentucky",
+          "zip": "40202",
+          "name": null,
+          "province_code": "KY",
+          "country_code": "US",
+          "country_name": "United States",
+          "default": true
+        }
+      }
+    }
+  ]
+}