waffo-pancake 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "requests"
4
+ require_relative "connection"
5
+
6
+ module Waffo
7
+ module Pancake
8
+ # Anonymous storefront access, authenticated with only the store's public slug. It holds no
9
+ # secret, so it suits code that cannot keep an API key.
10
+ #
11
+ # storefront = Waffo::Pancake::StoreCheckout.new(store_slug: "my-store", environment: "test")
12
+ # storefront.create_checkout_session(product_id: "PROD_...", currency: "USD")
13
+ class StoreCheckout
14
+ include Requests
15
+
16
+ attr_reader :store_slug, :environment
17
+
18
+ def initialize(store_slug:, config: Pancake.configuration, environment: config.environment,
19
+ base_url: config.base_url, timeout: config.timeout, logger: config.logger,
20
+ instrumenter: config.instrumenter, transport: nil)
21
+ raise ConfigurationError, "Missing store_slug" if store_slug.to_s.strip.empty?
22
+
23
+ @store_slug = store_slug
24
+ @environment = Requests.environment!(environment)
25
+ @connection = Connection.new(base_url: base_url, timeout: timeout, logger: logger, instrumenter: instrumenter,
26
+ transport: transport)
27
+ end
28
+
29
+ def create_checkout_session(product_id:, currency:, **params)
30
+ action("/v1/actions/checkout/create-session", product_id: product_id, currency: currency, **params)
31
+ end
32
+
33
+ private
34
+ attr_reader :connection
35
+
36
+ def auth_headers(_path, _json)
37
+ { "X-Store-Slug" => @store_slug, "X-Environment" => @environment }
38
+ end
39
+ end
40
+ end
41
+ end
@@ -115,13 +115,13 @@ module Waffo
115
115
  "expiresAt" => (Time.now + 2700).utc.iso8601 }
116
116
  end
117
117
 
118
- def cancel_subscription(order_id)
119
- record(:cancel_subscription, order_id)
118
+ def cancel_subscription(order_id, **params)
119
+ record(:cancel_subscription, order_id, *optional(params))
120
120
  change_status(order_id, "canceling")
121
121
  end
122
122
 
123
- def reactivate_subscription(order_id)
124
- record(:reactivate_subscription, order_id)
123
+ def reactivate_subscription(order_id, **params)
124
+ record(:reactivate_subscription, order_id, *optional(params))
125
125
  change_status(order_id, "active")
126
126
  end
127
127
 
@@ -130,19 +130,41 @@ module Waffo
130
130
  {}
131
131
  end
132
132
 
133
- def action(path, params)
134
- record(:action, path, params)
133
+ def action(path, params = {}, **named)
134
+ record(:action, path, params.merge(named))
135
135
  {}
136
136
  end
137
137
 
138
+ # Recorded, then answers as this same fake, so a test reads every call from one list.
139
+ def customer(token, **options)
140
+ record(:customer, token, *optional(options))
141
+ self
142
+ end
143
+
138
144
  def called?(name) = calls.any? { |call| call.first == name }
139
145
 
146
+ # Every other API method records its call and answers empty, so a new endpoint cannot be
147
+ # missing from the fake. Lists answer [], counts 0, reads nil, writes {}.
148
+ answers = API::Queries.empty_answers.merge(webhook_deliveries_count: 0)
149
+ api_methods = [API::CustomerActions, API::StoreActions, API::ProductActions, API::PromotionActions,
150
+ API::Queries].flat_map(&:public_instance_methods)
151
+ client_only = %i[issue_session_token create_authenticated_plan_change webhook_deliveries_count
152
+ create_onetime_order create_subscription_order]
153
+ (api_methods + client_only).uniq.reject { |name| method_defined?(name) }.each do |name|
154
+ define_method(name) do |*arguments, **params|
155
+ record(name, *arguments, *optional(params))
156
+ answers.fetch(name, {})
157
+ end
158
+ end
159
+
140
160
  private
141
161
  def record(name, *arguments)
142
162
  calls << [name, *arguments]
143
163
  raise fail_with if fail_with
144
164
  end
145
165
 
166
+ def optional(params) = params.empty? ? [] : [params]
167
+
146
168
  def change_status(order_id, status)
147
169
  @orders[order_id.to_s]&.merge!("status" => status)
148
170
  { "orderId" => order_id, "status" => status }
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Waffo
4
4
  module Pancake
5
- VERSION = "0.3.0"
5
+ VERSION = "0.5.0"
6
6
  end
7
7
  end
@@ -27,11 +27,13 @@ module Waffo
27
27
 
28
28
  module_function
29
29
 
30
- # :payment (one charge), :refund, :subscription (a subscription's status), or nil.
30
+ # :payment (one charge), :refund, :subscription (a subscription's status), :store (a store's
31
+ # domain), or nil.
31
32
  def family(event_type)
32
33
  event_type = event_type.to_s
33
34
  return :payment if PAYMENT_EVENT_TYPES.include?(event_type)
34
35
  return :refund if REFUND_EVENT_TYPES.include?(event_type)
36
+ return :store if event_type.start_with?("store.")
35
37
 
36
38
  :subscription if event_type.start_with?("subscription.")
37
39
  end
@@ -68,6 +70,7 @@ module Waffo
68
70
  def payment? = family == :payment
69
71
  def refund? = family == :refund
70
72
  def subscription? = family == :subscription
73
+ def store? = family == :store
71
74
  end
72
75
 
73
76
  # The `data` of a delivery. Reading `amount`, `total`, `subtotal`, `taxAmount`, `taxRate` or
@@ -127,6 +130,41 @@ module Waffo
127
130
  def first_period? = period_number == 1
128
131
  def renewal? = period_number.is_a?(Integer) && period_number > 1
129
132
 
133
+ def store_id = self["storeId"]
134
+
135
+ # How the buyer paid, from the payment channel. Payment, refund and chargeback events.
136
+ # `payment_timestamp` is UTC with milliseconds; `payment_date` is its date part.
137
+ def payment_id = self["paymentId"]
138
+ def payment_status = self["paymentStatus"]
139
+ def payment_date = self["paymentDate"]
140
+ def payment_timestamp = self["paymentTimestamp"]
141
+ def payment_method = self["paymentMethod"]
142
+ # An open string, not an enum: "wechatpay", "alipay", "alipayhk", "visa", "mastercard", ...
143
+ def payment_method_brand = self["paymentMethodBrand"]
144
+ def payment_last4 = self["paymentLast4"]
145
+ def payment_failure_reason = self["paymentFailureReason"]
146
+
147
+ # Refund events only.
148
+ def refund_status = self["refundStatus"]
149
+ def refund_reason = self["refundReason"]
150
+ def refund_created_at = self["refundCreatedAt"]
151
+
152
+ # subscription.plan_changed, plan_change_scheduled and plan_change_failed only. Waffo's docs
153
+ # show it in examples only; its keys are direction, oldPlanName, oldAmount, newPlanName,
154
+ # newAmount, chargedAmount and effectiveDate.
155
+ def plan_change = self["planChange"]
156
+
157
+ # store.domain_changed: old_domain is nil when the store had no verified domain before.
158
+ def store_name = self["storeName"]
159
+ def old_domain = self["oldDomain"]
160
+ def new_domain = self["newDomain"]
161
+ def effective_at = self["effectiveAt"]
162
+
163
+ # store.domain_unbound (production webhooks only): unbind_method is "merchant" or "admin".
164
+ def domain = self["domain"]
165
+ def unbound_at = self["unboundAt"]
166
+ def unbind_method = self["method"]
167
+
130
168
  private
131
169
  def deprecated_read(key)
132
170
  replacements = DEPRECATED_FIELDS[key.to_s]
@@ -135,14 +173,16 @@ module Waffo
135
173
  Deprecation.warn(deprecation_message(key.to_s, replacements), caller_locations(2))
136
174
  end
137
175
 
176
+ # A family with its own replacement names it; any other family (or none) lists them all.
138
177
  def deprecation_message(key, replacements)
139
178
  family = Webhook.family(event_type)
140
- instead = if family
179
+ specific = family && replacements.key?(family)
180
+ instead = if specific
141
181
  reader(replacements.fetch(family))
142
182
  else
143
183
  replacements.map { |name, field| "#{reader(field)} on #{name} events" }.join(", ")
144
184
  end
145
- "Waffo webhook data[#{key.inspect}] is deprecated#{" on #{event_type}" if family}; read #{instead} " \
185
+ "Waffo webhook data[#{key.inspect}] is deprecated#{" on #{event_type}" if specific}; read #{instead} " \
146
186
  "instead. Waffo removes it in its next major API version, not before #{DEPRECATED_FIELDS_REMOVAL}."
147
187
  end
148
188
 
@@ -57,6 +57,7 @@ module Waffo
57
57
  subscription.plan_changed subscription.plan_change_scheduled subscription.plan_change_failed
58
58
  subscription.canceling subscription.uncanceled subscription.canceled subscription.past_due
59
59
  refund.succeeded refund.failed
60
+ store.domain_changed store.domain_unbound
60
61
  ].freeze
61
62
 
62
63
  module_function
data/lib/waffo/pancake.rb CHANGED
@@ -4,9 +4,11 @@ require_relative "pancake/version"
4
4
  require_relative "pancake/errors"
5
5
  require_relative "pancake/keys"
6
6
  require_relative "pancake/client"
7
+ require_relative "pancake/store_checkout"
7
8
  require_relative "pancake/deprecation"
8
9
  require_relative "pancake/webhook"
9
10
  require_relative "pancake/webhook/event"
11
+ require_relative "pancake/invoice"
10
12
  require_relative "pancake/configuration"
11
13
  require_relative "pancake/railtie" if defined?(Rails::Railtie)
12
14
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: waffo-pancake
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Good First Issue
@@ -41,13 +41,23 @@ files:
41
41
  - lib/generators/waffo_pancake/install/templates/webhook_job.rb.tt
42
42
  - lib/generators/waffo_pancake/install/templates/webhooks_controller.rb.tt
43
43
  - lib/waffo/pancake.rb
44
+ - lib/waffo/pancake/api/customer_actions.rb
45
+ - lib/waffo/pancake/api/product_actions.rb
46
+ - lib/waffo/pancake/api/promotion_actions.rb
47
+ - lib/waffo/pancake/api/queries.rb
48
+ - lib/waffo/pancake/api/store_actions.rb
44
49
  - lib/waffo/pancake/client.rb
45
50
  - lib/waffo/pancake/configuration.rb
51
+ - lib/waffo/pancake/connection.rb
46
52
  - lib/waffo/pancake/customer_portal_controller.rb
53
+ - lib/waffo/pancake/customer_session.rb
47
54
  - lib/waffo/pancake/deprecation.rb
48
55
  - lib/waffo/pancake/errors.rb
56
+ - lib/waffo/pancake/invoice.rb
49
57
  - lib/waffo/pancake/keys.rb
50
58
  - lib/waffo/pancake/railtie.rb
59
+ - lib/waffo/pancake/requests.rb
60
+ - lib/waffo/pancake/store_checkout.rb
51
61
  - lib/waffo/pancake/test_helpers.rb
52
62
  - lib/waffo/pancake/version.rb
53
63
  - lib/waffo/pancake/webhook.rb