Class: AppManager::GraphqlHelper
- Inherits:
-
Object
- Object
- AppManager::GraphqlHelper
- Defined in:
- lib/app_manager/graphql_helper.rb
Instance Method Summary collapse
- #api_call(query, is_json = false) ⇒ Object
- #current_rc_charge_gql(shopify_charge_id) ⇒ Object
-
#initialize(shopify_domain, shopify_token) ⇒ GraphqlHelper
constructor
A new instance of GraphqlHelper.
- #recurring_charge_api_call(plan, return_url, shop, discount_local_storage) ⇒ Object
- #recurring_charge_cancel_api_call(charge_id, shop) ⇒ Object
- #recurring_charge_current_api_call(shop) ⇒ Object
- #rest_client ⇒ Object
- #run_graph_api(query, variables = '', is_retrieve_all = false, after = '', is_json = true) ⇒ Object
- #usage_charge_api_call(charge_id, amount, description = '') ⇒ Object
Constructor Details
#initialize(shopify_domain, shopify_token) ⇒ GraphqlHelper
Returns a new instance of GraphqlHelper.
4 5 6 7 8 9 |
# File 'lib/app_manager/graphql_helper.rb', line 4 def initialize(shopify_domain,shopify_token) @api_key = AppManager.configuration.shopify_api_key || nil @api_version = AppManager.configuration.shopify_api_version || nil @shopify_domain = shopify_domain @shopify_token = shopify_token end |
Instance Method Details
#api_call(query, is_json = false) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/app_manager/graphql_helper.rb', line 15 def api_call(query,is_json=false) require 'uri' require 'net/http' if !@api_key.nil? && !@api_version.nil? && !@shopify_domain.nil? && !@shopify_token.nil? url = URI("https://#{@api_key}:#{@shopify_token}@#{@shopify_domain}/admin/api/#{@api_version}/graphql.json") http = Net::HTTP.new(url.host, url.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER request = Net::HTTP::Post.new(url) request["X-Shopify-Access-Token"] = @shopify_token request.body = query request["Accept"] = 'application/json' if is_json.present? request["Content-Type"] = 'application/json' else request["Content-Type"] = 'application/graphql' end response = http.request(request) response = ActiveSupport::JSON.decode(response.read_body) rescue nil return response else Rollbar.error("=== params missing from any of these api_key=#{@api_key}, api_version=#{@api_version}, shopify_domain=#{@shopify_domain}, shopify_token ===#{@shopify_token}") return [] end end |
#current_rc_charge_gql(shopify_charge_id) ⇒ Object
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/app_manager/graphql_helper.rb', line 251 def current_rc_charge_gql(shopify_charge_id) query = 'query { node(id: "gid://shopify/AppSubscription/'+shopify_charge_id+'") { ...on AppSubscription { createdAt currentPeriodEnd id name status test lineItems { plan { pricingDetails { ...on AppRecurringPricing { interval price { amount currencyCode } discount { priceAfterDiscount { amount } } } ...on AppUsagePricing { terms cappedAmount { amount currencyCode } balanceUsed { amount currencyCode } } } } } } } }' data = run_graph_api(query,'') return data end |
#recurring_charge_api_call(plan, return_url, shop, discount_local_storage) ⇒ Object
57 58 59 60 61 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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/app_manager/graphql_helper.rb', line 57 def recurring_charge_api_call(plan,return_url,shop,discount_local_storage) plan_test = nil shop_plan_field = AppManager.configuration.field_names['shopify_plan'] rescue nil if !plan['affiliate'].nil? && plan['affiliate'].any? && !shop_plan_field.nil? && plan['affiliate'].map{|e| e['value']}.include?(shop[shop_plan_field]) plan_test = true end trial_days = plan['trial_days'] || 0 if shop && shop.shopify_domain && trial_days trial_activated_at = shop[AppManager.configuration.field_names['trial_activated_at']] rescue nil plan_id_field = shop[AppManager.configuration.plan_id_or_name_field] rescue nil remaining_obj = AppManager::Client.new remaining = remaining_obj.get_remaining_days(shop.shopify_domain,trial_activated_at,plan_id_field) # trial_days = !remaining.nil? ? remaining : trial_days if !remaining.nil? if !plan_id_field.nil? plan_obj = AppManager::Client.new current_plan = plan_obj.get_plan(plan_id_field) rescue nil used_days = (current_plan['trial_days'].to_i - remaining.to_i) if current_plan rescue 0 if used_days > 0 days = trial_days - used_days trial_days = days > 0 ? days : 0 end else trial_days = remaining end end end promotional_discount = [] if !discount_local_storage.nil? && shop plan_obj = AppManager::Client.new promotional_discount = plan_obj.get_promotional_discount(shop.shopify_domain, discount_local_storage) promotional_discount = [] if promotional_discount.class.to_s == "Hash" && promotional_discount.has_key?('status') && promotional_discount['status'] == 404 end if !plan['discount'].nil? && plan['discount'] != 0 discount_type = plan['discount_type'] || "percentage" discount_val = discount_type == "percentage" ? (plan['discount'].to_f/ 100) : "#{plan['discount']}" if !@api_version.nil? && @api_version.to_s.include?('2024') plan_discount = if plan['discount'] && plan['cycle_count'] && plan['cycle_count'].to_i != 0 { "discount" => { "durationLimitInIntervals" => (plan['cycle_count'].to_i), "value" => {"#{discount_type}" => discount_val} } } elsif plan['discount'] { "discount" => { "value" => {"#{discount_type}" => discount_val} } } else {} end else plan_discount = plan['discount'] ? { "discount" => { "durationLimitInIntervals" => (plan['cycle_count'].to_i), "value" => {"#{discount_type}" => discount_val} } } : {} end else if promotional_discount.any? if promotional_discount['plan_relation'].any? && !promotional_discount['plan_relation'].include?(plan['id']) && plan['is_global'] plan_discount = {} else discount_type = promotional_discount['type'] || 'percentage' discount_value = { discount_type => discount_type == 'percentage' ? promotional_discount['value'].to_f / 100 : [promotional_discount['value'].to_f, plan['price']].min } discount = { 'value' => discount_value } discount['durationLimitInIntervals'] = promotional_discount['duration_intervals'].to_i if promotional_discount['duration_intervals'].to_i.positive? plan_discount = { "discount" => discount} end end end discount_exists = !plan['discount'].nil? && plan['discount'] != 0 promotional_discount_applies = promotional_discount.present? && promotional_discount.any? promotional_discount_id = discount_exists && promotional_discount_applies ? 0 : promotional_discount_applies ? promotional_discount['id'] : 0 return_url += "&discount=#{promotional_discount_id}" price_details = { "price": { "amount": plan['price'], "currencyCode": 'USD' }, "interval": plan['interval']['value'] } price_details.merge! plan_discount if plan_discount && plan_discount.any? plan_var = { "appRecurringPricingDetails": price_details, } line_items_data = [{ "plan": plan_var }] if plan['interval']['value'] == 'EVERY_30_DAYS' && plan['is_external_charge'].present? && plan['is_external_charge'] == true && plan['external_charge_limit'] != '' plan_var_usage = { "plan": { "appUsagePricingDetails": { "cappedAmount": { "amount": plan['external_charge_limit'], "currencyCode": "USD" }, "terms": plan['terms'] } } } line_items_data.push(plan_var_usage) end query = 'mutation( $name: String!, $returnUrl: URL!, $trialDays: Int, $test: Boolean, $lineItems: [AppSubscriptionLineItemInput!]! ) { appSubscriptionCreate( name: $name, returnUrl: $returnUrl, trialDays: $trialDays, test: $test, lineItems: $lineItems ) { userErrors { field message } confirmationUrl appSubscription { id } } }' variables = { "name": plan['name'], "returnUrl": return_url, "trialDays": trial_days, "test": plan_test, "lineItems": line_items_data } data = run_graph_api(query,variables) return data end |
#recurring_charge_cancel_api_call(charge_id, shop) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/app_manager/graphql_helper.rb', line 212 def recurring_charge_cancel_api_call(charge_id,shop) query = 'mutation( $id: ID! ){ appSubscriptionCancel( id: $id ) { userErrors { field message } appSubscription { id status } } } ' variables = { "id": "gid://shopify/AppSubscription/#{charge_id}" } data = run_graph_api(query,variables) return data end |
#recurring_charge_current_api_call(shop) ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/app_manager/graphql_helper.rb', line 237 def recurring_charge_current_api_call(shop) query = 'query { currentAppInstallation { activeSubscriptions { id name test } } }' data = run_graph_api(query,{}) return data end |
#rest_client ⇒ Object
11 12 13 |
# File 'lib/app_manager/graphql_helper.rb', line 11 def rest_client end |
#run_graph_api(query, variables = '', is_retrieve_all = false, after = '', is_json = true) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/app_manager/graphql_helper.rb', line 41 def run_graph_api(query,variables = '',is_retrieve_all = false,after = '',is_json = true) query = query.dup.force_encoding("UTF-8") if variables.present? query = {"query": "#{query}","variables": variables } else query = {"query": "#{query}","variables": {} } end if is_json response = api_call(query.to_json,true) else response = api_call(query,false) end return response end |
#usage_charge_api_call(charge_id, amount, description = '') ⇒ Object
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/app_manager/graphql_helper.rb', line 301 def usage_charge_api_call(charge_id,amount,description = '') query = 'mutation appUsageRecordCreate($description: String!, $price: MoneyInput!, $subscriptionLineItemId: ID!) { appUsageRecordCreate(description: $description, price: $price, subscriptionLineItemId: $subscriptionLineItemId) { userErrors { field message } appUsageRecord { id } } }' variables = { "subscriptionLineItemId": "gid://shopify/AppSubscriptionLineItem/#{charge_id}?v=1&index=1", "price": { "amount": amount, "currencyCode": "USD" }, "description": "#{description}" } data = run_graph_api(query,variables) return data end |