Browse Source

Add support for complete endpoint

Emmanuel Milou 6 years ago
parent
commit
2e4af1e3df

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

@@ -4,5 +4,9 @@ module ShopifyAPI
   class Checkout < Base
     self.primary_key = :token
     headers['X-Shopify-Checkout-Version'] = '2016-09-06'
+
+    def complete
+      post(:complete)
+    end
   end
 end

+ 2 - 0
lib/shopify_api/resources/shipping_rate.rb

@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
 module ShopifyAPI
   class ShippingRate < Base
     self.prefix = '/admin/checkouts/:checkout_id/'

+ 4 - 2
test/abandoned_checkouts_test.rb

@@ -1,8 +1,10 @@
+# frozen_string_literal: true
+
 require 'test_helper'
 
 class AbandonedCheckoutsTest < Test::Unit::TestCase
-  test "get all should get all orders" do
-    fake 'checkouts', :method => :get, :status => 200, :body => load_fixture('checkouts')
+  test ":get lists all abandoned checkouts indexed by id" do
+    fake 'checkouts', method: :get, status: 200, body: load_fixture('checkouts')
     abandoned_checkout = ShopifyAPI::AbandonedCheckout.all
     assert_equal 450789469, abandoned_checkout.first.id
   end

+ 11 - 1
test/checkouts_test.rb

@@ -2,8 +2,18 @@ require 'test_helper'
 
 class CheckoutsTest < Test::Unit::TestCase
   test "get all checkouts indexed by token" do
-    fake 'checkouts', :method => :get, :status => 200, :body => load_fixture('checkouts')
+    fake 'checkouts', method: :get, status: 200, body: load_fixture('checkouts')
     checkout = ShopifyAPI::Checkout.all
     assert_equal '2a1ace52255252df566af0faaedfbfa7', checkout.first.id
   end
+
+  test ":complete completes a checkout" do
+    checkout_id = JSON.parse(load_fixture('checkout'))['checkout']['id']
+
+    fake "checkouts/#{checkout_id}", method: :get, status: 200, body: load_fixture('checkout')
+    checkout = ShopifyAPI::Checkout.find(checkout_id)
+
+    fake "checkouts/#{checkout.id}/complete", method: :post, status: 200, body: load_fixture('checkouts')
+    checkout.complete
+  end
 end

+ 196 - 0
test/fixtures/checkout.json

@@ -0,0 +1,196 @@
+{
+  "checkout": {
+    "buyer_accepts_marketing": false,
+    "cart_token": "68778783ad298f1c80c3bafcddeea02f",
+    "closed_at": null,
+    "completed_at": null,
+    "created_at": "2012-10-12T07:05:27-04:00",
+    "currency": "USD",
+    "email": "bob.norman@hostmail.com",
+    "gateway": null,
+    "id": 450789469,
+    "landing_site": null,
+    "note": null,
+    "referring_site": null,
+    "shipping_lines": [
+      {
+        "title": "Free Shipping",
+        "price": "0.00",
+        "code": "Free Shipping",
+        "source": "shopify"
+      }
+    ],
+    "shipping_rates": [
+      {
+        "id": "canada_post-INT.TP.BOGUS-4.00",
+        "price": "4.00",
+        "title": "Small Packet International Air Bogus"
+      },
+      {
+        "id": "canada_post-INT.TP.BOGUS-8.00",
+        "price": "8.00",
+        "title": "Medium Packet International Air Bogus"
+      }
+    ],
+    "source": null,
+    "source_identifier": null,
+    "source_name": "web",
+    "source_url": null,
+    "subtotal_price": "398.00",
+    "taxes_included": false,
+    "token": "2a1ace52255252df566af0faaedfbfa7",
+    "total_discounts": "0.00",
+    "total_line_items_price": "398.00",
+    "total_price": "409.94",
+    "total_tax": "11.94",
+    "total_weight": 400,
+    "updated_at": "2012-10-12T07:05:27-04:00",
+    "line_items": [
+      {
+        "applied_discounts": [
+
+        ],
+        "compare_at_price": null,
+        "fulfillment_service": "manual",
+        "gift_card": false,
+        "grams": 200,
+        "id": 49148385,
+        "line_price": "199.00",
+        "price": "199.00",
+        "product_id": 632910392,
+        "properties": null,
+        "quantity": 1,
+        "requires_shipping": true,
+        "sku": "IPOD2008RED",
+        "tax_lines": [
+
+        ],
+        "taxable": true,
+        "title": "IPod Nano - 8GB",
+        "variant_id": 49148385,
+        "variant_title": "Red",
+        "vendor": "Apple"
+      },
+      {
+        "applied_discounts": [
+
+        ],
+        "compare_at_price": null,
+        "fulfillment_service": "manual",
+        "gift_card": false,
+        "grams": 200,
+        "id": 808950810,
+        "line_price": "199.00",
+        "price": "199.00",
+        "product_id": 632910392,
+        "properties": null,
+        "quantity": 1,
+        "requires_shipping": true,
+        "sku": "IPOD2008PINK",
+        "tax_lines": [
+
+        ],
+        "taxable": true,
+        "title": "IPod Nano - 8GB",
+        "variant_id": 808950810,
+        "variant_title": "Pink",
+        "vendor": "Apple"
+      }
+    ],
+    "name": "#450789469",
+    "note_attributes": [
+      {
+        "name": "custom engraving",
+        "value": "Happy Birthday"
+      },
+      {
+        "name": "colour",
+        "value": "green"
+      }
+    ],
+    "discount_codes": [
+      {
+        "code": "TENOFF",
+        "amount": "10.00"
+      }
+    ],
+    "abandoned_checkout_url": "https://checkout.local/orders/690933842/2a1ace52255252df566af0faaedfbfa7?recovered=1",
+    "tax_lines": [
+      {
+        "price": "11.94",
+        "rate": 0.06,
+        "title": "State Tax"
+      }
+    ],
+    "billing_address": {
+      "address1": "Chestnut Street 92",
+      "address2": "",
+      "city": "Louisville",
+      "company": null,
+      "country": "United States",
+      "first_name": "Bob",
+      "last_name": "Norman",
+      "latitude": "45.41634",
+      "longitude": "-75.6868",
+      "phone": "555-625-1199",
+      "province": "Kentucky",
+      "zip": "40202",
+      "name": "Bob Norman",
+      "country_code": "US",
+      "province_code": "KY"
+    },
+    "shipping_address": {
+      "address1": "Chestnut Street 92",
+      "address2": "",
+      "city": "Louisville",
+      "company": null,
+      "country": "United States",
+      "first_name": "Bob",
+      "last_name": "Norman",
+      "latitude": "45.41634",
+      "longitude": "-75.6868",
+      "phone": "555-625-1199",
+      "province": "Kentucky",
+      "zip": "40202",
+      "name": "Bob Norman",
+      "country_code": "US",
+      "province_code": "KY"
+    },
+    "customer": {
+      "accepts_marketing": false,
+      "created_at": "2014-03-07T16:12:37-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:12:37-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
+      }
+    }
+  }
+}

+ 12 - 0
test/fixtures/checkouts.json

@@ -21,6 +21,18 @@
           "source": "shopify"
         }
       ],
+      "shipping_rates": [
+        {
+          "id": "canada_post-INT.TP.BOGUS-4.00",
+          "price": "4.00",
+          "title": "Small Packet International Air Bogus"
+        },
+        {
+          "id": "canada_post-INT.TP.BOGUS-8.00",
+          "price": "8.00",
+          "title": "Medium Packet International Air Bogus"
+        }
+      ],
       "source": null,
       "source_identifier": null,
       "source_name": "web",

+ 17 - 0
test/shipping_rate_test.rb

@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+require 'test_helper'
+
+class ShippingRateTest < Test::Unit::TestCase
+  test ":get lists all shipping rates for a given checkout" do
+    fake 'checkouts', method: :get, status: 200, body: load_fixture('checkouts')
+    checkouts = ShopifyAPI::Checkout.all
+
+    fake "checkouts/#{checkouts.first.id}/shipping_rates",
+      method: :get, status: 200, body: load_fixture('checkouts')
+    shipping_rates = ShopifyAPI::ShippingRate.find(:all, params: { checkout_id: checkouts.first.id })
+
+    assert_equal 2, shipping_rates.first.shipping_rates.length
+    assert_equal 'canada_post-INT.TP.BOGUS-4.00', shipping_rates.first.shipping_rates.first.id
+  end
+end