Module: ShopifySalesChannel::Integrations::Shopify
- Defined in:
- lib/shopify_sales_channel/integrations/shopify.rb
Instance Method Summary collapse
- #add_tag_to_order(store, order_no, data) ⇒ Object
- #check_products(store, orders) ⇒ Object
- #count_products(store) ⇒ Object
- #get_access_token(code, shop_url) ⇒ Object
- #get_order(store, order_no) ⇒ Object
- #get_store_details(access_token, shop_name) ⇒ Object
- #separate_name(name) ⇒ Object
- #set_webhooks(store) ⇒ Object
- #sync_orders(store, orders, marketplace) ⇒ Object
Instance Method Details
#add_tag_to_order(store, order_no, data) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/shopify_sales_channel/integrations/shopify.rb', line 158 def add_tag_to_order(store, order_no, data) url = URI("https://#{store.url}/admin/orders/#{order_no}.json") http = Net::HTTP.new(url.host, url.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE request = Net::HTTP::Put.new(url) request["content-type"] = 'application/json' request["x-shopify-access-token"] = store.access_token request.body = {"order": { "tags": data.to_s }}.to_json response = http.request(request) puts response.read_body end |
#check_products(store, orders) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/shopify_sales_channel/integrations/shopify.rb', line 115 def check_products(store, orders) url = URI("https://#{store.url}/admin/products.json?limit=150") http = Net::HTTP.new(url.host, url.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE request = Net::HTTP::Get.new(url) request["x-shopify-access-token"] = store.access_token request["content-type"] = 'application/json' response = http.request(request) products = JSON.parse(response.read_body)["products"] flag = true variants = [] order_sku_qty = orders.map{|x| [x["partCode"], x["ordQty"]] if x["partCode"].present?} | orders.map{|x| [x["sellerPrdCd"], x["ordQty"]] if x["sellerPrdCd"].present?} order_sku_hash = order_sku_qty.compact.to_h products.each do |product| product["variants"].each do |variant| order_skus = order_sku_hash.keys if (order_skus.include?(variant["sku"])) variant["order_quantity"] = order_sku_hash[variant["sku"]] variant["pre_order"] = true if product["tags"].split(", ").include?("pre-order") variants << variant end end end #variants = variants.select{|variant| (variant["sku"] == orders["partCode"] || variant["sku"] == orders["sellerPrdCd"])} store.variants = variants flag = false if variants.blank? return flag end |
#count_products(store) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/shopify_sales_channel/integrations/shopify.rb', line 100 def count_products(store) url = URI("https://#{store.url}/admin/products/count.json") http = Net::HTTP.new(url.host, url.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE request = Net::HTTP::Get.new(url) request["x-shopify-access-token"] = store.access_token request["content-type"] = 'application/json' response = http.request(request) count = JSON.parse(response.read_body)["count"] return count end |
#get_access_token(code, shop_url) ⇒ Object
2 3 4 5 6 7 8 |
# File 'lib/shopify_sales_channel/integrations/shopify.rb', line 2 def get_access_token(code, shop_url) token_params = {'code' => code, 'client_id' => SHOPIFY_CLIENT_ID, 'client_secret' => SHOPIFY_CLIENT_SECRET} response = Net::HTTP.post_form(URI.parse("https://#{shop_url}/admin/oauth/access_token"), token_params) response = JSON.parse(response.body) store_details = get_store_details(response["access_token"], shop_url) response = response.merge!(store_details["shop"]) end |
#get_order(store, order_no) ⇒ Object
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/shopify_sales_channel/integrations/shopify.rb', line 147 def get_order(store, order_no) url = URI("https://#{store.url}/admin/orders/#{order_no}.json") http = Net::HTTP.new(url.host, url.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE request = Net::HTTP::Get.new(url) request["x-shopify-access-token"] = store.access_token response = http.request(request) JSON.parse(response.read_body) end |
#get_store_details(access_token, shop_name) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/shopify_sales_channel/integrations/shopify.rb', line 10 def get_store_details(access_token, shop_name) url = URI("https://#{shop_name}/admin/shop.json") http = Net::HTTP.new(url.host, url.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE request = Net::HTTP::Get.new(url) request["x-shopify-access-token"] = access_token response = http.request(request) response = JSON.parse(response.body) return response end |
#separate_name(name) ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/shopify_sales_channel/integrations/shopify.rb', line 92 def separate_name(name) name_array = name.split(" ") first_name = name_array[0] last_name = name_array[1..-1].join(" ") last_name = "." unless last_name.present? separate_name = [first_name, last_name] end |
#set_webhooks(store) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/shopify_sales_channel/integrations/shopify.rb', line 22 def set_webhooks(store) url = URI("https://#{store.url}/admin/webhooks.json") http = Net::HTTP.new(url.host, url.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE request = Net::HTTP::Post.new(url) request["x-shopify-access-token"] = store.access_token request["content-type"] = 'application/json' SHOPIFY_WEBHOOKS.each do |webhook| request.body = {"webhook" => {"topic" => webhook, "address" => "#{HOST}/#{webhook}", "format" => "json"}}.to_json response = http.request(request) response = JSON.parse(response.body) unless response["errors"] #do something end end end |
#sync_orders(store, orders, marketplace) ⇒ Object
response.each do |customer|
name = {"name"=> "#{customer["first_name"]} #{customer["last_name"]}"}
customers << (customer.merge!(name))
end
return customers
end
end
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/shopify_sales_channel/integrations/shopify.rb', line 62 def sync_orders(store, orders, marketplace) order = orders.first if check_products(store, orders) url = URI("https://#{store.url}/admin/orders.json") http = Net::HTTP.new(url.host, url.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE request = Net::HTTP::Post.new(url) request["x-shopify-access-token"] = store.access_token request["content-type"] = 'application/json' cust_name = separate_name(order["ordNm"]) rec_name = separate_name(order["rcvrNm"]) line_items = [] store.variants.map{|v| line_items << {"variant_id" => v["id"], "quantity" => v["order_quantity"]}} base_addr_arr = order["rcvrBaseAddr"].split(",") state = base_addr_arr.pop.squish || "." city_and_postal_code = base_addr_arr.join(" ").split(" ") city = city_and_postal_code[1..-1].try(:first) || "." postcode = order["rcvrMailNo"] || city_and_postal_code[0] request.body = {"order" => {"line_items": line_items, "customer": {"first_name": cust_name[0], "last_name": cust_name[1], "email": order["ordEmail"]}, "shipping_address": {"first_name": rec_name[0], "last_name": rec_name[1], "address1": order["rcvrDtlsAddr"], "city": city, "province": state, "country": marketplace.country, "phone": order["rcvrTlphn"], "zip": postcode}}}.to_json response = http.request(request) response = JSON.parse(response.body) response else store.errors.add(:base, "products not present for order #{order['ordNo']}") return false end end |