effective_orders 6.12.1 → 6.12.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a5a55ea6c14b02fce58218a825c9f936516c0d46a1246b7190a503b97070d394
4
- data.tar.gz: 97c6253dfe17a99e7113755e935432b7953c89714ef1a77dff48f6a014204a9e
3
+ metadata.gz: 4f5fbabf9c4012193962576f7ef67838e17e382740ad3860ab9c9f527ce86787
4
+ data.tar.gz: 71f6af17259b5b62ee66b2fd993bb085dc8411ad8040c21fa0a3bc639381c655
5
5
  SHA512:
6
- metadata.gz: 1d4f1885182a55a73e39665ab477f31bb1fc6c801fb04f13f0311fd870ad87edb84f27d8bf5ac0abb456f61fdd236961df650da27e157a7b532629e67e7c567a
7
- data.tar.gz: 4c74eaa371c3e9db830ce252cd611068ab727640a836f6e6dac1c7abc05439b7a9beeb0f80bf2374367afbc6a8b37d8e8c1188057f19c2e062b1d0c73436c214
6
+ metadata.gz: 60eff8ba4e046b46575a23c98daf14b0ba07cf548041826590ac49c3bf193cde6632437baeae38bfe011f37a3cae0e7d3d5d444aeeb0340a0990d1c93a88ce2b
7
+ data.tar.gz: 9b91126b76b01d50fefc86b31d1a163dfe8164c523f69a2c3a523a827c632faaf0b0b5e13e0174b01450078dd32754a76157b5c4338b39b16b47a10ca1d25c59
@@ -19,7 +19,7 @@ module Admin
19
19
  scope :all
20
20
  scope :purchased
21
21
 
22
- scope :deferred if EffectiveOrders.deferred_providers.present?
22
+ scope :deferred if EffectiveOrders.deferred? || EffectiveOrders.delayed?
23
23
  scope :voided
24
24
 
25
25
  scope :pending_refunds if EffectiveOrders.refund && !EffectiveOrders.buyer_purchases_refund?
@@ -6,7 +6,7 @@ class EffectiveOrdersDatatable < Effective::Datatable
6
6
  scope :all
7
7
  scope :purchased
8
8
 
9
- scope :deferred if EffectiveOrders.deferred_providers.present?
9
+ scope :deferred if EffectiveOrders.deferred? || EffectiveOrders.delayed?
10
10
  scope :refunds if EffectiveOrders.refund
11
11
  scope :not_purchased
12
12
  end
@@ -23,18 +23,52 @@ module Effective
23
23
  self.currency = currency || EffectiveOrders.deluxe.fetch(:currency)
24
24
  end
25
25
 
26
- def payment
27
- raise('expected purchase response to be present') unless purchase_response.kind_of?(Hash)
28
- purchase_response
26
+ def health_check
27
+ get('/')
28
+ end
29
+
30
+ def healthy?
31
+ response = health_check()
32
+
33
+ return false unless response.kind_of?(Hash)
34
+ return false unless response['appName'].present?
35
+ return false unless response['environment'].present?
36
+
37
+ true
38
+ end
39
+
40
+ # Decode the base64 encoded JSON object into a Hash
41
+ def decode_payment_intent_payload(payload)
42
+ raise('expected a string') unless payload.kind_of?(String)
43
+
44
+ payment_intent = (JSON.parse(Base64.decode64(payload)) rescue nil)
45
+
46
+ raise('expected payment_intent to be a Hash') unless payment_intent.kind_of?(Hash)
47
+ raise('expected a token payment') unless payment_intent['type'] == 'Token'
48
+
49
+ payment_intent
50
+ end
51
+
52
+ # Takes a payment_intent and returns the card info we can store
53
+ def card_info(payment_intent)
54
+ token = extract_token(payment_intent)
55
+
56
+ # Return the authorization params merged with the card info
57
+ last4 = token['maskedPan'].to_s.last(4)
58
+ card = token['cardType'].to_s.downcase
59
+ date = token['expDate']
60
+ cvv = token['cvv']
61
+
62
+ active_card = "**** **** **** #{last4} #{card} #{date}" if last4.present?
63
+
64
+ { 'active_card' => active_card, 'card' => card, 'expDate' => date, 'cvv' => cvv }.compact
29
65
  end
30
66
 
67
+ # After we store a payment intent we can call purchase! immediately or wait till later.
31
68
  # This calls Authorize Payment and Complete Payment
32
- # Returns true if all good.
33
- # Returns false if there was an error.
34
- # Always sets the @purchase_response which is api.payment
69
+ # Returns true when purchased. Returns false when declined.
70
+ # The response is stored in api.payment() after this is run
35
71
  def purchase!(order, payment_intent)
36
- raise('expected a deluxe payment provider') unless ['deluxe', 'deluxe_delayed'].include?(order.payment_provider)
37
-
38
72
  payment_intent = decode_payment_intent_payload(payment_intent) if payment_intent.kind_of?(String)
39
73
  raise('expected payment_intent to be a Hash') unless payment_intent.kind_of?(Hash)
40
74
  raise('expected a token payment') unless payment_intent['type'] == 'Token'
@@ -60,21 +94,56 @@ module Effective
60
94
  true
61
95
  end
62
96
 
63
- # Health Check
64
- def health_check
65
- get('/')
97
+ def payment
98
+ raise('expected purchase response to be present') unless purchase_response.kind_of?(Hash)
99
+ purchase_response
66
100
  end
67
101
 
68
- def healthy?
69
- response = health_check()
102
+ def purchase_delayed_orders!(orders)
103
+ now = Time.zone.now
70
104
 
71
- return false unless response.kind_of?(Hash)
72
- return false unless response['timestamp'].to_s.start_with?(Time.zone.now.strftime('%Y-%m-%d'))
73
- return false unless response['environment'].present?
105
+ Array(orders).each do |order|
106
+ puts "Trying order #{order.id}"
107
+
108
+ begin
109
+ raise('expected a delayed order') unless order.delayed?
110
+ raise('expected a deferred order') unless order.deferred?
111
+ raise('expected delayed payment intent') unless order.delayed_payment_intent.present?
112
+
113
+ order.update_columns(delayed_payment_purchase_ran_at: now, delayed_payment_purchase_result: nil)
114
+
115
+ purchased = purchase!(order, order.delayed_payment_intent)
116
+ provider = order.payment_provider
117
+ payment = self.payment()
118
+ card = payment["card"]
119
+
120
+ if purchased
121
+ order.assign_attributes(delayed_payment_purchase_result: "success")
122
+ order.purchase!(payment: payment, provider: provider, card: card, email: true, skip_buyer_validations: true)
123
+
124
+ puts "Successfully purchased order #{order.id}"
125
+ else
126
+ order.assign_attributes(delayed_payment_purchase_result: "failed with message: #{Array(payment['responseMessage']).to_sentence.presence || 'none'}.")
127
+ order.decline!(payment: payment, provider: provider, card: card)
128
+
129
+ puts "Failed to purchase order #{order.id} #{order.delayed_payment_purchase_result}"
130
+ end
131
+
132
+ rescue => e
133
+ order.update_columns(delayed_payment_purchase_ran_at: now, delayed_payment_purchase_result: "error: #{e.message}")
134
+
135
+ EffectiveLogger.error(e.message, associated: order) if defined?(EffectiveLogger)
136
+ ExceptionNotifier.notify_exception(e, data: { order_id: order.id }) if defined?(ExceptionNotifier)
137
+
138
+ puts "Error purchasing #{order.id}: #{e.message}"
139
+ end
140
+ end
74
141
 
75
142
  true
76
143
  end
77
144
 
145
+ protected
146
+
78
147
  # Authorize Payment
79
148
  def authorize_payment(order, payment_intent)
80
149
  response = post('/payments/authorize', params: authorize_payment_params(order, payment_intent))
@@ -119,19 +188,6 @@ module Effective
119
188
  response.reverse_merge(authorization)
120
189
  end
121
190
 
122
- def complete_payment_params(order, payment_intent)
123
- raise('expected an Effective::Order') unless order.kind_of?(Effective::Order)
124
-
125
- payment_id = extract_payment_id(payment_intent)
126
- amount = { amount: order.total_to_f, currency: currency }
127
-
128
- # Params passed into Complete Payment
129
- {
130
- paymentId: payment_id,
131
- amount: amount
132
- }
133
- end
134
-
135
191
  def authorize_payment_params(order, payment_intent)
136
192
  raise('expected an Effective::Order') unless order.kind_of?(Effective::Order)
137
193
 
@@ -185,6 +241,19 @@ module Effective
185
241
  }.compact
186
242
  end
187
243
 
244
+ def complete_payment_params(order, payment_intent)
245
+ raise('expected an Effective::Order') unless order.kind_of?(Effective::Order)
246
+
247
+ payment_id = extract_payment_id(payment_intent)
248
+ amount = { amount: order.total_to_f, currency: currency }
249
+
250
+ # Params passed into Complete Payment
251
+ {
252
+ paymentId: payment_id,
253
+ amount: amount
254
+ }
255
+ end
256
+
188
257
  def get(endpoint, params: nil)
189
258
  query = ('?' + params.compact.map { |k, v| "$#{k}=#{v}" }.join('&')) if params.present?
190
259
 
@@ -225,33 +294,6 @@ module Effective
225
294
  JSON.parse(result.body)
226
295
  end
227
296
 
228
- # Takes a payment_intent and returns the card info we can store
229
- def card_info(payment_intent)
230
- token = extract_token(payment_intent)
231
-
232
- # Return the authorization params merged with the card info
233
- last4 = token['maskedPan'].to_s.last(4)
234
- card = token['cardType'].to_s.downcase
235
- date = token['expDate']
236
- cvv = token['cvv']
237
-
238
- active_card = "**** **** **** #{last4} #{card} #{date}" if last4.present?
239
-
240
- { 'active_card' => active_card, 'card' => card, 'expDate' => date, 'cvv' => cvv }.compact
241
- end
242
-
243
- # Decode the base64 encoded JSON object into a Hash
244
- def decode_payment_intent_payload(payload)
245
- raise('expected a string') unless payload.kind_of?(String)
246
-
247
- payment_intent = (JSON.parse(Base64.decode64(payload)) rescue nil)
248
-
249
- raise('expected payment_intent to be a Hash') unless payment_intent.kind_of?(Hash)
250
- raise('expected a token payment') unless payment_intent['type'] == 'Token'
251
-
252
- payment_intent
253
- end
254
-
255
297
  private
256
298
 
257
299
  def headers
@@ -241,7 +241,9 @@ module Effective
241
241
  validates :payment_provider, presence: true
242
242
 
243
243
  validate do
244
- errors.add(:payment_provider, "unknown deferred payment provider") unless EffectiveOrders.deferred_providers.include?(payment_provider)
244
+ unless EffectiveOrders.deferred_providers.include?(payment_provider) || EffectiveOrders.delayed_providers.include?(payment_provider)
245
+ errors.add(:payment_provider, "unknown deferred payment provider")
246
+ end
245
247
  end
246
248
  end
247
249
 
@@ -673,7 +675,9 @@ module Effective
673
675
 
674
676
  payment: payment_to_h(payment.presence || 'none'),
675
677
  payment_provider: (provider.presence || 'none'),
676
- payment_card: (card.presence || 'none')
678
+ payment_card: (card.presence || 'none'),
679
+
680
+ delayed_payment_intent: nil # Do not store the delayed payment intent any longer
677
681
  )
678
682
 
679
683
  if current_user&.email.present?
@@ -1,3 +1,3 @@
1
1
  module EffectiveOrders
2
- VERSION = '6.12.1'.freeze
2
+ VERSION = '6.12.3'.freeze
3
3
  end
@@ -185,8 +185,9 @@ module EffectiveOrders
185
185
  ].compact
186
186
  end
187
187
 
188
+ # Should not include delayed providers
188
189
  def self.deferred_providers
189
- [('cheque' if cheque?), ('deluxe_delayed' if deluxe_delayed?), ('etransfer' if etransfer?), ('phone' if phone?)].compact
190
+ [('cheque' if cheque?), ('etransfer' if etransfer?), ('phone' if phone?)].compact
190
191
  end
191
192
 
192
193
  def self.delayed_providers
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_orders
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.12.1
4
+ version: 6.12.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-05-30 00:00:00.000000000 Z
11
+ date: 2024-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails