Class: PurchaseKit::Purchase::Intent::Remote

Inherits:
PurchaseKit::Purchase::Intent show all
Defined in:
lib/purchasekit/purchase/intent/remote.rb

Overview

Production implementation of Intent that creates via PurchaseKit API.

Instance Attribute Summary

Attributes inherited from PurchaseKit::Purchase::Intent

#id, #product, #success_path, #uuid

Class Method Summary collapse

Methods inherited from PurchaseKit::Purchase::Intent

#initialize, #xcode_completion_url

Constructor Details

This class inherits a constructor from PurchaseKit::Purchase::Intent

Class Method Details

.create(product_id:, customer_id:, success_path: nil, environment: nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/purchasekit/purchase/intent/remote.rb', line 8

def create(product_id:, customer_id:, success_path: nil, environment: nil)
  response = ApiClient.new.post("/purchase/intents", {
    product_id: product_id,
    customer_id: customer_id,
    success_path: success_path,
    environment: environment
  })

  case response.code
  when 201
    product_data = response["product"]
    product = Product.new(
      id: product_data["id"],
      apple_product_id: product_data["apple_product_id"],
      google_product_id: product_data["google_product_id"],
      google_base_plan_id: product_data["google_base_plan_id"]
    )
    new(id: response["id"], uuid: response["uuid"], product: product, success_path: success_path)
  when 402
    raise PurchaseKit::SubscriptionRequiredError, response["error"] || "Subscription required for production purchases"
  when 404
    raise PurchaseKit::NotFoundError, "App or product not found"
  else
    raise PurchaseKit::Error, "API error: #{response.code} #{response.message}"
  end
end