Class: ShopifyGraphql::CreateRecurringSubscription

Inherits:
Object
  • Object
show all
Includes:
Mutation
Defined in:
app/graphql/shopify_graphql/create_recurring_subscription.rb

Constant Summary collapse

MUTATION =
<<~GRAPHQL
  #{AppSubscriptionFields::FRAGMENT}

  mutation appSubscriptionCreate(
    $name: String!,
    $lineItems: [AppSubscriptionLineItemInput!]!,
    $returnUrl: URL!,
    $trialDays: Int,
    $test: Boolean
  ) {
    appSubscriptionCreate(
      name: $name,
      lineItems: $lineItems,
      returnUrl: $returnUrl,
      trialDays: $trialDays,
      test: $test
    ) {
      appSubscription {
        ... AppSubscriptionFields
      }
      confirmationUrl
      userErrors {
        field
        message
      }
    }
  }
GRAPHQL

Instance Method Summary collapse

Methods included from Mutation

#client

Instance Method Details

#call(name:, price:, return_url:, trial_days: nil, test: nil, interval: :monthly) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/graphql/shopify_graphql/create_recurring_subscription.rb', line 34

def call(name:, price:, return_url:, trial_days: nil, test: nil, interval: :monthly)
  payload = {name: name, returnUrl: return_url}
  plan_interval = interval == :monthly ? "EVERY_30_DAYS" : "ANNUAL"
  payload[:lineItems] = [{
    plan: {
      appRecurringPricingDetails: {
        price: {amount: price, currencyCode: "USD"},
        interval: plan_interval
      }
    }
  }]
  payload[:trialDays] = trial_days if trial_days
  payload[:test] = test if test

  response = execute(MUTATION, **payload)
  response.data = response.data.appSubscriptionCreate
  handle_user_errors(response.data)
  response.data = parse_data(response.data)
  response
end