Class: PurchaseKit::Purchase::Intent::Demo

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

Overview

Demo implementation of Intent for local development.

Stores intents in memory instead of making API calls. Designed for use with Xcode’s StoreKit testing.

Instance Attribute Summary collapse

Attributes inherited from PurchaseKit::Purchase::Intent

#id, #product, #success_path, #uuid

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, uuid:, product:, customer_id:, success_path:) ⇒ Demo

Returns a new instance of Demo.



14
15
16
17
# File 'lib/purchasekit/purchase/intent/demo.rb', line 14

def initialize(id:, uuid:, product:, customer_id:, success_path:)
  super(id: id, uuid: uuid, product: product, success_path: success_path)
  @customer_id = customer_id
end

Instance Attribute Details

#customer_idObject (readonly)

Returns the value of attribute customer_id.



12
13
14
# File 'lib/purchasekit/purchase/intent/demo.rb', line 12

def customer_id
  @customer_id
end

Class Method Details

.clear_store!Object

Clear the in-memory store (useful for tests)



52
53
54
# File 'lib/purchasekit/purchase/intent/demo.rb', line 52

def clear_store!
  @store = {}
end

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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/purchasekit/purchase/intent/demo.rb', line 31

def create(product_id:, customer_id:, success_path: nil, environment: nil)
  product = Product.find(product_id)
  uuid = SecureRandom.uuid
  id = "intent_#{SecureRandom.hex(8)}"

  intent = new(
    id: id,
    uuid: uuid,
    product: product,
    customer_id: customer_id,
    success_path: success_path
  )
  store[uuid] = intent
  intent
end

.find(uuid) ⇒ Object



25
26
27
28
29
# File 'lib/purchasekit/purchase/intent/demo.rb', line 25

def find(uuid)
  intent = store[uuid]
  raise PurchaseKit::NotFoundError, "Intent not found: #{uuid}" unless intent
  intent
end

.storeObject



47
48
49
# File 'lib/purchasekit/purchase/intent/demo.rb', line 47

def store
  @store ||= {}
end

Instance Method Details

#xcode_completion_urlObject

URL for Xcode StoreKit testing to simulate purchase completion



20
21
22
# File 'lib/purchasekit/purchase/intent/demo.rb', line 20

def xcode_completion_url
  "/purchasekit/purchase/completions/#{uuid}"
end