Browse Source

Added complete endpoint to draft order

Tanner Rutgers 8 years ago
parent
commit
af9cc3d7b3

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

@@ -6,5 +6,9 @@ module ShopifyAPI
       resource = post(:send_invoice, {}, draft_order_invoice.encode)
       ShopifyAPI::DraftOrderInvoice.new(ShopifyAPI::DraftOrder.format.decode(resource.body))
     end
+
+    def complete(options = {})
+      load_attributes_from_response(put(:complete, options, nil))
+    end
   end
 end

+ 25 - 0
test/draft_order_test.rb

@@ -111,4 +111,29 @@ class DraftOrderTest < Test::Unit::TestCase
     assert_equal 2, metafields.length
     assert metafields.all? { |m| m.is_a?(ShopifyAPI::Metafield) }
   end
+
+  def test_complete_draft_order_with_no_params
+    completed_fixture = load_fixture('draft_order_completed')
+    completed_draft = ActiveSupport::JSON.decode(completed_fixture)['draft_order']
+    fake 'draft_orders/517119332/complete', method: :put, status: 200, body: completed_fixture
+
+    @draft_order.complete
+
+    assert_equal completed_draft['status'], @draft_order.status
+    assert_equal completed_draft['order_id'], @draft_order.order_id
+    refute_nil @draft_order.completed_at
+  end
+
+  def test_complete_draft_order_with_params
+    completed_fixture = load_fixture('draft_order_completed')
+    completed_draft = ActiveSupport::JSON.decode(completed_fixture)['draft_order']
+    complete_params = { payment_pending: true }
+    fake 'draft_orders/517119332/complete.json?payment_pending=true', extension: false, method: :put, status: 200, body: completed_fixture
+
+    @draft_order.complete(complete_params)
+
+    assert_equal completed_draft['status'], @draft_order.status
+    assert_equal completed_draft['order_id'], @draft_order.order_id
+    refute_nil @draft_order.completed_at
+  end
 end

+ 159 - 0
test/fixtures/draft_order_completed.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": "2017-02-02T14:15:16-17:00",
+    "name": "#D1",
+    "status": "completed",
+    "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": 450789469,
+    "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
+      }
+    }
+  }
+}