spook_and_pay 1.0.0 → 1.1.1

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
  SHA1:
3
- metadata.gz: fb962e2e5cb48aa5da0423021039c2e37f0d54cf
4
- data.tar.gz: e247fc0540cd4656aab4f18edc31d9c4c853d349
3
+ metadata.gz: eea62b53ebc042a14887a5437eefcb88a0faceda
4
+ data.tar.gz: aac44773d4119dd8452f30b22a5fbfc245eccf84
5
5
  SHA512:
6
- metadata.gz: 25de0492cbc31f34355c366cf2b55f4196ec0fa4914beea0b78f5a5d85d203026ebf6680cfc991b241b01f44760c8facdc2f3d838c7db05d18ed0af18e151b0e
7
- data.tar.gz: 9a6240d5ad254c9fb48db134967f7d47e716f2b14f9784d3ad512946ff9ad78c408478d08c16d7b66be2fc9915e272978c21306ea9192a58f321b87f865f744e
6
+ metadata.gz: fa7557c8bfd51c9078afa0832fbbf710678a2b97b88d7f8cbdd33a2dd7ae56b487ae200acdf4ca09f024cd361c94f42c11528393aef9608c12d6bc918d22f54f
7
+ data.tar.gz: fe08f81b8f355f1889a5453d614c646cf6e4aca6ffec54cd93793c05a9c63a00c6943401fd7b82eb36e987ca09921fe9921ebf048dea2bd3e55de37086726182
@@ -8,7 +8,7 @@ module SpookAndPay
8
8
  # accessed externally, but is put here for debugging etc.
9
9
  attr_reader :gateway
10
10
 
11
- # Constructs an instance of the Braintree gateway which it then acts as
11
+ # Constructs an instance of the Braintree gateway which it then acts as
12
12
  # a proxy to.
13
13
  #
14
14
  # @param [:development, :test, :production] environment
@@ -55,7 +55,7 @@ module SpookAndPay
55
55
  end
56
56
  end
57
57
 
58
- # Generates the hash and query string that needs to be embedded inside
58
+ # Generates the hash and query string that needs to be embedded inside
59
59
  # of a form in order to interact with Braintree's transparent redirect.
60
60
  #
61
61
  # @param Hash data
@@ -65,7 +65,7 @@ module SpookAndPay
65
65
  gateway.transparent_redirect.transaction_data(data)
66
66
  end
67
67
 
68
- # Used to confirm the submission of purchase or authorize transactions
68
+ # Used to confirm the submission of purchase or authorize transactions
69
69
  # via transparent redirect.
70
70
  #
71
71
  # @param String query_string
@@ -82,6 +82,19 @@ module SpookAndPay
82
82
  gateway.transaction.submit_for_settlement(id)
83
83
  end
84
84
 
85
+ # Makes a purchase using a credit token.
86
+ #
87
+ # @param String id
88
+ # @param Number amount
89
+ # @return [Braintree::SuccessfulResult, Braintree::ErrorResult]
90
+ def credit_card_purchase(id, amount)
91
+ gateway.transaction.sale(
92
+ :payment_method_token => id,
93
+ :amount => amount,
94
+ :options => {:submit_for_settlement => true}
95
+ )
96
+ end
97
+
85
98
  # Refunds the funds in a settled transaction.
86
99
  #
87
100
  # @param String id
@@ -90,6 +103,15 @@ module SpookAndPay
90
103
  gateway.transaction.refund(id)
91
104
  end
92
105
 
106
+ # Partially refunds the funds in a settled transaction.
107
+ #
108
+ # @param String id
109
+ # @param Float amount
110
+ # @return [Braintree::SuccessfulResult, Braintree::ErrorResult]
111
+ def partially_refund(id, amount)
112
+ gateway.transaction.refund(id, amount)
113
+ end
114
+
93
115
  # Voids a transaction.
94
116
  #
95
117
  # @param String id
@@ -17,8 +17,8 @@ module SpookAndPay
17
17
  # @option config String :private_key
18
18
  def initialize(env, config)
19
19
  @adapter = SpookAndPay::Adapters::Braintree.new(
20
- env,
21
- config[:merchant_id],
20
+ env,
21
+ config[:merchant_id],
22
22
  config[:public_key],
23
23
  config[:private_key]
24
24
  )
@@ -26,11 +26,11 @@ module SpookAndPay
26
26
  super(env, config)
27
27
  end
28
28
 
29
- # Braintree specific version of this method. Can be used to either
30
- # authorize a payment — and capture it later — or submit a payment for
29
+ # Braintree specific version of this method. Can be used to either
30
+ # authorize a payment — and capture it later — or submit a payment for
31
31
  # settlement immediately. This is done via the type param.
32
32
  #
33
- # Because Braintree accepts payment details and processes payment in a
33
+ # Because Braintree accepts payment details and processes payment in a
34
34
  # single step, this method must also be provided with an amount.
35
35
  #
36
36
  # @param String redirect_url
@@ -70,9 +70,9 @@ module SpookAndPay
70
70
  case result
71
71
  when ::Braintree::SuccessfulResult
72
72
  SpookAndPay::Result.new(
73
- true,
73
+ true,
74
74
  result,
75
- :credit_card => extract_credit_card(result.transaction.credit_card_details, true, false),
75
+ :credit_card => extract_credit_card(result.transaction.credit_card_details, true, false),
76
76
  :transaction => extract_transaction(result.transaction)
77
77
  )
78
78
  when ::Braintree::ErrorResult
@@ -97,6 +97,11 @@ module SpookAndPay
97
97
  extract_credit_card(result.credit_card_details, true, false)
98
98
  end
99
99
 
100
+ def purchase_via_credit_card(id, amount)
101
+ result = adapter.credit_card_purchase(credit_card_id(id), amount)
102
+ generate_result(result)
103
+ end
104
+
100
105
  def transaction(id)
101
106
  result = adapter.transaction(id)
102
107
  extract_transaction(result) if result
@@ -112,6 +117,11 @@ module SpookAndPay
112
117
  generate_result(result)
113
118
  end
114
119
 
120
+ def partially_refund_transaction(id, amount)
121
+ result = adapter.partially_refund(transaction_id(id), amount)
122
+ generate_result(result)
123
+ end
124
+
115
125
  def void_transaction(id)
116
126
  result = adapter.void(transaction_id(id))
117
127
  generate_result(result)
@@ -119,12 +129,12 @@ module SpookAndPay
119
129
 
120
130
  private
121
131
 
122
- # Maps the error codes returned by Braintree to a triple of target, type
132
+ # Maps the error codes returned by Braintree to a triple of target, type
123
133
  # and field used by the SubmissionError class.
124
134
  #
125
135
  # The key is the error code from Braintree. The first entry in the triple
126
- # is the specific portion of the transaction that has the error. The
127
- # second is the type of error and the third is the field — if any — it
136
+ # is the specific portion of the transaction that has the error. The
137
+ # second is the type of error and the third is the field — if any — it
128
138
  # applies to.
129
139
  ERROR_CODE_MAPPING = {
130
140
  "81715" => [:credit_card, :invalid, :number],
@@ -147,7 +157,7 @@ module SpookAndPay
147
157
  def extract_errors(result)
148
158
  result.errors.map do |e|
149
159
  mapping = ERROR_CODE_MAPPING[e.code]
150
- if mapping
160
+ if mapping
151
161
  SubmissionError.new(*mapping, e)
152
162
  else
153
163
  SubmissionError.new(:unknown, :unknown, :unknown, e)
@@ -155,7 +165,7 @@ module SpookAndPay
155
165
  end
156
166
  end
157
167
 
158
- # A generic method for generating results on actions. It doesn't capture
168
+ # A generic method for generating results on actions. It doesn't capture
159
169
  # anything action specific i.e. it might need to be replaced later.
160
170
  #
161
171
  # @param [Braintree::SuccessfulResult, Braintree:ErrorResult] result
@@ -164,22 +174,22 @@ module SpookAndPay
164
174
  case result
165
175
  when ::Braintree::SuccessfulResult
166
176
  SpookAndPay::Result.new(
167
- true,
168
- result,
169
- :credit_card => extract_credit_card(result.transaction.credit_card_details, true, false),
177
+ true,
178
+ result,
179
+ :credit_card => extract_credit_card(result.transaction.credit_card_details, true, false),
170
180
  :transaction => extract_transaction(result.transaction)
171
181
  )
172
182
  when ::Braintree::ErrorResult
173
183
  SpookAndPay::Result.new(
174
- false,
175
- result,
184
+ false,
185
+ result,
176
186
  :errors => extract_errors(result)
177
187
  )
178
188
  end
179
189
  end
180
190
 
181
191
  # Extracts credit card details from a payload extracted from a result.
182
- # It could be either a Hash, Braintree::CreditCard or
192
+ # It could be either a Hash, Braintree::CreditCard or
183
193
  # Braintree::Transaction::CreditCardDetails. BOO!
184
194
  #
185
195
  # @param [Hash, Braintree::CreditCard, Braintree::Transaction::CreditCardDetails] card
@@ -190,14 +200,16 @@ module SpookAndPay
190
200
  # @todo figure out validity and expiry ourselves
191
201
  def extract_credit_card(card, valid, expired)
192
202
  opts = case card
193
- when Hash
203
+ when Hash
194
204
  {
195
205
  :token => card[:token],
196
206
  :card_type => card[:card_type],
197
207
  :number => card[:last_4],
198
208
  :name => card[:cardholder_name],
199
209
  :expiration_month => card[:expiration_month],
200
- :expiration_year => card[:expiration_year]
210
+ :expiration_year => card[:expiration_year],
211
+ :expired => card_expired?(card[:expiration_month].to_i, card[:expiration_year].to_i),
212
+ :valid => true # We have to assume it's valid, since BT won't say
201
213
  }
202
214
  else
203
215
  {
@@ -206,14 +218,26 @@ module SpookAndPay
206
218
  :number => card.last_4,
207
219
  :name => card.cardholder_name,
208
220
  :expiration_month => card.expiration_month,
209
- :expiration_year => card.expiration_year
221
+ :expiration_year => card.expiration_year,
222
+ :expired => card_expired?(card.expiration_month.to_i, card.expiration_year.to_i),
223
+ :valid => true # We have to assume it's valid, since BT won't say
210
224
  }
211
225
  end
212
226
 
213
227
  SpookAndPay::CreditCard.new(self, opts.delete(:token), opts)
214
228
  end
215
229
 
216
- # Extracts transaction details from whatever payload is passed in. This
230
+ # Checks to see if a credit card has expired.
231
+ #
232
+ # @param Number month
233
+ # @param Number year
234
+ # @return [true, false]
235
+ def card_expired?(month, year)
236
+ now = Time.now
237
+ year < now.year or (year == now.year and month < now.month)
238
+ end
239
+
240
+ # Extracts transaction details from whatever payload is passed in. This
217
241
  # might be Hash or a Braintree:Transaction.
218
242
  #
219
243
  # @param [Hash, Braintree::Transaction] result
@@ -233,7 +257,7 @@ module SpookAndPay
233
257
  :type => result[:type].to_sym,
234
258
  :created_at => result[:created_at],
235
259
  :amount => result[:amount]
236
- )
260
+ )
237
261
  else
238
262
  SpookAndPay::Transaction.new(
239
263
  self,
@@ -243,7 +267,7 @@ module SpookAndPay
243
267
  :type => result.type.to_sym,
244
268
  :created_at => result.created_at,
245
269
  :amount => result.amount
246
- )
270
+ )
247
271
  end
248
272
  end
249
273
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spook_and_pay
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luke Sutton
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-11-03 00:00:00.000000000 Z
12
+ date: 2016-09-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: braintree