Class: Pay::AwsMarketplace::Subscription

Inherits:
Subscription
  • Object
show all
Defined in:
lib/pay/aws_marketplace/subscription.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.sync(entitlement, customer: nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/pay/aws_marketplace/subscription.rb', line 25

def self.sync(entitlement, customer: nil)
  customer ||= Customer.find_by(processor_id: entitlement.customer_identifier)

  unless customer
    raise Pay::Error, "Customer with processor_id #{entitlement.customer_identifier} missing while syncing"
  end

  status = :active
  ends_at = nil

  # This class uses `ends_at` to track the time an already-scheduled cancellation request
  # should take effect.  Amazon uses `expiration_date` to tell us when the contracted
  # subscription period will end. As a result, we have to calculate the current status of the
  # sub ourselves and put that into `status`.
  if entitlement.expiration_date < Time.current
    status = :cancelled
    ends_at = entitlement.expiration_date
  end

  customer.subscriptions.find_or_initialize_by(
    processor_plan: entitlement.product_code
  ).update!(
    name: entitlement.dimension,
    quantity: entitlement.value.integer_value,
    ends_at: ends_at,
    status: status
  )
end

.sync_from_registration_token(token) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/pay/aws_marketplace/subscription.rb', line 4

def self.sync_from_registration_token(token)
  require "aws-sdk-marketplacemetering"
  aws_mm = Aws::MarketplaceMetering::Client.new(region: "us-east-1")
  customer_info = aws_mm.resolve_customer(registration_token: token)

  customer = Customer.create_with(
    aws_account_id: customer_info.,
    processor: "aws_marketplace"
  ).find_or_create_by!(processor_id: customer_info.customer_identifier)

  require "aws-sdk-marketplaceentitlementservice"
  aws_mes = Aws::MarketplaceEntitlementService::Client.new(region: "us-east-1")
  entitlement = aws_mes.get_entitlements(
    product_code: customer_info.product_code,
    filter: {CUSTOMER_IDENTIFIER: [customer_info.customer_identifier]},
    max_results: 1
  ).entitlements.first

  sync(entitlement, customer: customer)
end

Instance Method Details

#api_record(**options) ⇒ Object



58
59
60
# File 'lib/pay/aws_marketplace/subscription.rb', line 58

def api_record(**options)
  self
end

#cancel(**options) ⇒ Object

Raises:



62
63
64
# File 'lib/pay/aws_marketplace/subscription.rb', line 62

def cancel(**options)
  raise UpdateError
end

#cancel_now!(**options) ⇒ Object

Raises:



66
67
68
# File 'lib/pay/aws_marketplace/subscription.rb', line 66

def cancel_now!(**options)
  raise UpdateError
end

#change_quantity(quantity, **options) ⇒ Object

Raises:



90
91
92
# File 'lib/pay/aws_marketplace/subscription.rb', line 90

def change_quantity(quantity, **options)
  raise UpdateError
end

#pauseObject

Raises:



74
75
76
# File 'lib/pay/aws_marketplace/subscription.rb', line 74

def pause
  raise UpdateError
end

#paused?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/pay/aws_marketplace/subscription.rb', line 70

def paused?
  status == "paused"
end

#resumable?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/pay/aws_marketplace/subscription.rb', line 78

def resumable?
  false
end

#resumeObject

Raises:



82
83
84
# File 'lib/pay/aws_marketplace/subscription.rb', line 82

def resume
  raise UpdateError
end

#retry_failed_paymentObject

Raises:



94
95
96
# File 'lib/pay/aws_marketplace/subscription.rb', line 94

def retry_failed_payment
  raise ChargeError
end

#swap(plan, **options) ⇒ Object

Raises:



86
87
88
# File 'lib/pay/aws_marketplace/subscription.rb', line 86

def swap(plan, **options)
  raise UpdateError
end