Browse Source

Add helper methods to Checkout resource

Jamie Dwyer 6 years ago
parent
commit
16922a9191

+ 7 - 0
README.md

@@ -10,6 +10,13 @@ The Shopify API gem allows Ruby developers to programmatically access the admin
 
 The API is implemented as JSON over HTTP using all four verbs (GET/POST/PUT/DELETE). Each resource, like Order, Product, or Collection, has its own URL and is manipulated in isolation. In other words, we’ve tried to make the API follow the REST principles as much as possible.
 
+## ⚠️ Breaking change notice for version 5.0.0 ⚠️
+The [Abandoned Checkout API](https://help.shopify.com/en/api/reference/orders/abandoned_checkouts) is now accessed through the `ShopifyAPI::AbandonedCheckout` resource. If you were previously accessing the Abandoned Checkout API through the `ShopifyAPI::Checkout` resource, you will need to update your code after upgrading from version 4.x.x or earlier.
+
+Going forward, the `ShopifyAPI::Checkout` resource is used to access the [Checkout API](https://help.shopify.com/en/api/reference/sales_channels/checkout), which can be used to create new checkouts.
+
+For more details, [please see this issue](https://github.com/Shopify/shopify_api/issues/471).
+
 ## Usage
 
 ### Requirements

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

@@ -8,5 +8,20 @@ module ShopifyAPI
     def complete
       post(:complete)
     end
+
+    def ready?
+      return false unless persisted?
+
+      reload
+      [200, 201].include?(ShopifyAPI::Base.connection.response.code.to_i)
+    end
+
+    def payments
+      Payment.find(:all, params: { checkout_id: id })
+    end
+
+    def shipping_rates
+      ShippingRate.find(:all, params: { checkout_id: id })
+    end
   end
 end

+ 33 - 0
test/checkouts_test.rb

@@ -35,4 +35,37 @@ class CheckoutsTest < Test::Unit::TestCase
 
     checkout.complete
   end
+
+  test ":ready? returns true when status is 201" do
+    fake "checkouts/#{@expected_checkout_id}", method: :get, status: 201, body: load_fixture('checkout')
+    checkout = ShopifyAPI::Checkout.find(@expected_checkout_id)
+
+    assert_predicate checkout, :ready?
+  end
+
+  test ":ready? returns false when status is 202" do
+    fake "checkouts/#{@expected_checkout_id}", method: :get, status: 202, body: load_fixture('checkout')
+    checkout = ShopifyAPI::Checkout.find(@expected_checkout_id)
+
+    refute_predicate checkout, :ready?
+  end
+
+  test ":payments returns payments for a checkout" do
+    fake "checkouts/#{@expected_checkout_id}", method: :get, status: 200, body: load_fixture('checkout')
+    checkout = ShopifyAPI::Checkout.find(@expected_checkout_id)
+
+    fake "checkouts/#{@expected_checkout_id}/payments", method: :get, status: 202, body: load_fixture('payments')
+    assert_equal 10.00, checkout.payments.first.amount
+  end
+
+  test ":shipping_rates returns shipping rates for a checkout" do
+    fake "checkouts/#{@expected_checkout_id}", method: :get, status: 200, body: load_fixture('checkout')
+    checkout = ShopifyAPI::Checkout.find(@expected_checkout_id)
+
+    fake("checkouts/#{@expected_checkout_id}/shipping_rates",
+      method: :get,
+      status: 202,
+      body: load_fixture('shipping_rates'))
+    assert_equal "canada_post-INT.TP.BOGUS-4.00", checkout.shipping_rates.first.id
+  end
 end

+ 9 - 0
test/fixtures/payments.json

@@ -0,0 +1,9 @@
+[
+  {
+    "payment" : {
+      "amount": 10.00,
+      "session_id": "east-44a400ef20b36e38f10b882cb7260796",
+      "unique_token": "my-client-generated-idempotency-token"
+    }
+  }
+]

+ 12 - 0
test/fixtures/shipping_rates.json

@@ -0,0 +1,12 @@
+[
+  {
+    "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"
+  }
+]