@@ -124,7 +124,8 @@ def add_idempotency(options)
124
124
end
125
125
126
126
def add_address ( post , creditcard , options )
127
- return unless address = options [ :billing_address ]
127
+ address = options [ :billing_address ]
128
+ return unless valid_address? ( address )
128
129
129
130
post [ :address ] = { }
130
131
# name and line_1 are required at the gateway
@@ -138,6 +139,15 @@ def add_address(post, creditcard, options)
138
139
post [ :address ] [ :phone_number ] = address [ :phone ] . gsub ( /\D / , '' ) if address [ :phone ]
139
140
end
140
141
142
+ def valid_address? ( address )
143
+ return false unless address
144
+
145
+ required_fields = %i[ name address1 city state country zip ]
146
+ missing_fields = required_fields . select { |field | address [ field ] . nil? || address [ field ] . to_s . strip . empty? }
147
+
148
+ missing_fields . any? ? false : true
149
+ end
150
+
141
151
def add_invoice ( post , money , options )
142
152
post [ :amount ] = money . zero? ? 0 : amount ( money ) . to_f . to_s
143
153
post [ :currency ] = ( options [ :currency ] || currency ( money ) )
@@ -265,7 +275,6 @@ def add_customer_data(post, payment, options, action = '')
265
275
phone_number = options . dig ( :billing_address , :phone ) || options . dig ( :billing_address , :phone_number )
266
276
post [ :phone_number ] = phone_number . gsub ( /\D / , '' ) unless phone_number . nil?
267
277
post [ :receipt_email ] = options [ :email ] if payment . is_a? ( String ) && options [ :customer_id ] . present? && !send_customer_object? ( options )
268
-
269
278
return if payment . is_a? ( String )
270
279
return add_customer_id ( post , options ) if options [ :customer_id ]
271
280
@@ -283,12 +292,15 @@ def customer_fields(payment, options)
283
292
customer_data = { }
284
293
customer_data [ :name ] = "#{ payment . first_name } #{ payment . last_name } " unless payment . is_a? ( String )
285
294
customer_data [ :email ] = options [ :email ] unless payment . is_a? ( String ) && options [ :customer_id ] . blank?
295
+ customer_data [ :phone_number ] = options . dig ( :billing_address , :phone ) || options . dig ( :billing_address , :phone_number )
296
+ customer_data [ :phone_number ] = customer_data [ :phone_number ] . gsub ( /\D / , '' ) if customer_data [ :phone_number ]
286
297
customer_data [ :addresses ] = [ customer_address ] if customer_address
287
298
customer_data
288
299
end
289
300
290
301
def address ( options )
291
- return unless address = options [ :billing_address ]
302
+ address = options [ :billing_address ]
303
+ return unless valid_address? ( address )
292
304
293
305
formatted_address = { }
294
306
0 commit comments