module DidwwUps
DidwwUps::Base
class base class for API resources
DidwwUps::Collection
class with ability to handle :total_count, :offset_value, :limit_value
DidwwUps::Connection
class with overridden authorization_header method
DidwwUps::CreditCard
class to handle Credit Cards
Routes:
GET /api/rest/v1/credit_cards/:id/modules(.:format) GET /api/rest/v1/credit_cards/:credit_card_id/properties(.:format) GET /api/rest/v1/credit_cards/:credit_card_id/billing_address(.:format) GET /api/rest/v1/credit_cards(.:format) GET /api/rest/v1/credit_cards/:id(.:format) DELETE /api/rest/v1/credit_cards/:id(.:format)
Filter Examples
credit_cards = DidwwUps::CreditCard.all(params: { page:1, per_page:10 , q:{token_ref_is_present: true, cc_expired_gte: '2014-06-01'}}) # => GET "/api/rest/v1/credit_cards?page=1&per_page=10&q%5Bcc_number_contains%5D=37"
Possible filter keys for q hash
* +created_at_gte+ * +created_at_lte+ * +cc_expired_gte+ * +cc_expired_lte+ * +store_id_eq+ * +cc_number_contains+ * +token_ref_contains+ * +token_ref_is_present+ (true) * +token_ref_is_blank+ (true) * +customer_id_eq+ * +customer_id_starts_with+
Methods for pagination
# credit_cards.total_count # credit_cards.limit_value # credit_cards.offset_value
Find by token example
DidwwUps::CreditCard.find("ToTXNnMAUika3o0sS0sqMw") # => GET "/api/rest/v1/credit_cards/ToTXNnMAUika3o0sS0sqMw"
Remove example
DidwwUps::CreditCard.new({token_ref:"ToTXNnMAUika3o0sS0sqMw"}, true).destroy # => DELETE "/api/rest/v1/credit_cards/ToTXNnMAUika3o0sS0sqMw"
DidwwUps::Base
class base class for API resources
Routes: /api/rest/v1/logs/:type/tokens(.:format) Example
DidwwUps::ModulesPayment.all(:params=> {:module_name=>'Braintree'})
#=> “[{”amount“:”2054.0“,”created_at“:”2014-03-13T14:04:06Z“,”id“:134,”store_id“:14,”success“:false,”module_name“:”Braintree“,”error“:”Reversal amount does not match authorization amount“},{”amount“:”42.0“,”created_at“:”2014-03-13T14:01:08Z“,”id“:133,”store_id“:14,”success“:true,”module_name“:”Braintree“,”error“:null},{”amount“:”16.0“,”created_at“:”2014-03-13T13:56:29Z“,”id“:132,”store_id“:14,”success“:true,”module_name“:”Braintree“,”error“:null},{”amount“:”91.0“,”created_at“:”2014-03-13T13:27:56Z“,”id“:131,”store_id“:14,”success“:true,”module_name“:”Braintree“,”error“:null},{”amount“:”10.0“,”created_at“:”2014-03-13T13:24:07Z“,”id“:130,”store_id“:14,”success“:true,”module_name“:”Braintree“,”error“:null},{”amount“:”10.0“,”created_at“:”2014-03-13T13:21:38Z“,”id“:129,”store_id“:14,”success“:true,”module_name“:”Braintree“,”error“:null},{”amount“:”10.0“,”created_at“:”2014-03-13T13:15:26Z“,”id“:128,”store_id“:14,”success“:true,”module_name“:”Braintree“,”error“:null},{”amount“:”10.0“,”created_at“:”2014-03-13T13:13:34Z“,”id“:127,”store_id“:14,”success“:true,”module_name“:”Braintree“,”error“:null},{”amount“:”10.0“,”created_at“:”2014-03-13T13:09:32Z“,”id“:126,”store_id“:14,”success“:true,”module_name“:”Braintree“,”error“:null},{”amount“:”50.0“,”created_at“:”2014-03-13T12:57:57Z“,”id“:125,”store_id“:14,”success“:true,”module_name“:”Braintree“,”error“:null}]”
Routes: /api/rest/v1/logs/:type/tokens(.:format) Example
DidwwUps::ModulesRefund.all(params: {module_name: "Braintree", :order => "created_at_desc"})
#=>“[{”amount“:”34.91“,”created_at“:”2013-12-17T14:09:14Z“,”id“:31,”store_id“:14,”success“:true,”module_name“:”Braintree“,”error“:null},{”amount“:”12.0“,”created_at“:”2013-12-13T15:07:28Z“,”id“:29,”store_id“:14,”success“:true,”module_name“:”Braintree“,”error“:null},{”amount“:”10.0“,”created_at“:”2013-12-07T09:07:28Z“,”id“:28,”store_id“:14,”success“:true,”module_name“:”Braintree“,”error“:null},{”amount“:”23.0“,”created_at“:”2013-12-05T17:05:03Z“,”id“:27,”store_id“:14,”success“:true,”module_name“:”Braintree“,”error“:null},{”amount“:”50.0“,”created_at“:”2013-12-05T12:40:11Z“,”id“:26,”store_id“:14,”success“:true,”module_name“:”Braintree“,”error“:null},{”amount“:”31.21“,”created_at“:”2013-12-05T10:09:17Z“,”id“:25,”store_id“:14,”success“:true,”module_name“:”Braintree“,”error“:null},{”amount“:”12.0“,”created_at“:”2013-12-05T09:53:53Z“,”id“:24,”store_id“:14,”success“:true,”module_name“:”Braintree“,”error“:null},{”amount“:”12.0“,”created_at“:”2013-12-05T09:27:31Z“,”id“:23,”store_id“:14,”success“:true,”module_name“:”Braintree“,”error“:null},{”amount“:”34.0“,”created_at“:”2013-11-18T13:55:28Z“,”id“:15,”store_id“:14,”success“:true,”module_name“:”Braintree“,”error“:null},{”amount“:”15.0“,”created_at“:”2013-11-11T09:05:22Z“,”id“:14,”store_id“:14,”success“:true,”module_name“:”Braintree“,”error“:null}]”
Routes: /api/rest/v1/logs/:type/tokens(.:format) Example
DidwwUps::ModulesToken.all(params: {module_name: "Braintree", :order => "created_at_desc"})
DidwwUps::Payment
class to handle Payments
Routes:
GET /api/rest/v1/payments(.:format) POST /api/rest/v1/payments(.:format)
Filter Examples
payments = DidwwUps::Payment.all(params: { page:1, per_page:10 , q:{success_eq: true}}) # => GET "/api/rest/v1/payments?page=1&per_page=10&q%5Bsuccess_eq%5D=true" Methods for pagination # payments.total_count # payments.limit_value # payments.offset_value
Possible filter keys for params
* +created_at_gte+ * +created_at_lte+ * +amount_eq+ * +cc_number_contains+ * +order_id_eq+ * +success_eq+ * +store_id_eq+ * +customer_id_eq+
Create Payment
Example failed by merchant
payment = DidwwUps::Payment.create({token_ref: "LQ8IQ4Av6hM3tgX_WLJp9w", order_id: 7, amount: 20.01, operator: 'Admin', description: 'Payment for order # 7'}) # => POST "/api/rest/v1/payments" "{\"order_id\":7,\"amount\":20.1,\"operator\":\"Admin\",\"description\":\"Payment for order # 7\",\"token_ref\":\"LQ8IQ4Av6hM3tgX_WLJp9w\"}" # => "{\"id\":1967,\"token_ref\":\"LQ8IQ4Av6hM3tgX_WLJp9w\",\"cc_type\":\"MASTERCARD\",\"cc_number\":\"540400******0001\",\"success\":false,\"error\":\"Declined\",\"amount\":20.5,\"order_id\":7,\"description\":\"Payment for order # 7\",\"operator\":\"Admin\",\"type\":\"WorldnetTps\"}" payment.valid? # => true payment.success? # => false
Example failed by UPS
payment = DidwwUps::Payment.create({token_ref: "LQ8IQ4Av6hM3tgX_WLJp9w", order_id: 6, amount: 20.60, operator: 'Admin', description: 'Payment for order # 6'}) # => "{\"errors\":{\"base\":[\"Order was already processed\"]}}" payment.success? # => false payment.valid? # => false errors.full_messages # => ["Order was already processed"]
Example successfull
payment = DidwwUps::Payment.create({token_ref: "LQ8IQ4Av6hM3tgX_WLJp9w",order_id: 8, amount: 20.50, operator: 'Admin', description: 'Payment for order # 8'}) # => "{\"id\":1968,\"token_ref\":\"LQ8IQ4Av6hM3tgX_WLJp9w\",\"cc_type\":\"MASTERCARD\",\"cc_number\":\"540400******0001\",\"success\":true,\"error\":null,\"amount\":20.5,\"order_id\":8,\"description\":\"Payment for order # 8\",\"operator\":\"Admin\",\"type\":\"WorldnetTps\"}" payment.success? # => true payment.valid? # => true
DidwwUps::Refund
class to handle Refunds
Routes:
GET /api/rest/v1/refunds(.:format) POST /api/rest/v1/refunds(.:format)
Filter Examples
refunds = DidwwUps::Refund.all(params: { page:1, per_page:10 , q:{success_eq: true}}) # => GET "/api/rest/v1/refunds?page=1&per_page=10&q%5Bsuccess_eq%5D=true"
Methods for pagination
# refunds.total_count # refunds.limit_value # refunds.offset_value
Possible filter keys for params
* +:created_at_gte+ * +:created_at_lte+ * +:store_id_eq+ * +:amount_eq+ * +:description_contains+ * +:operator_contains+ * +:order_id_eq+ * +:success_eq+
Create Refund
Example failed by merchant
refund = DidwwUps::Refund.create(order_id: "1294526", amount:12.00, operator: "Bob", description: "Stolen Card") # => "{\"id\":466,\"success\":false,\"Credit Card is expired\":null,\"amount\":12.0,\"order_id\":\"1294526\",\"description\":\"Stolen Card\",\"operator\":\"Bob\"}" refund.valid? # => true refund.success? # => false
Example failed by UPS
refund = DidwwUps::Refund.create(order_id: "1294526", amount:1000.00, operator: "Bob", description: "Stolen Card") # => "{\"errors\":{\"amount\":[\"must be less or equal to 14.0\"]}}" refund.success? # => false refund.valid? # => false errors.full_messages # => ["Amount must be less or equal to 14.0"]
Example successfull
refund = DidwwUps::Refund.create(order_id: "1294526", amount:12.00, operator: "Bob", description: "Stolen Card") # => "{\"id\":466,\"success\":true,\"error\":null,\"amount\":12.0,\"order_id\":\"1294526\",\"description\":\"Stolen Card\",\"operator\":\"Bob\"}" refund.success? # => true refund.valid? # => true
Params
-
:amount
- amount of refund, if not present payment amount is used -
:description
- Description of refund, if not present payment description is used -
:operator
- responsible for refund, if not present payment operator is used -
:order_id
- order_id of payment
DidwwUps::Store
class with Store
used as API client
Constants
- PRODUCTION_HOST
- SANDBOX_HOST
- VERSION
Public Class Methods
# File lib/didww_ups.rb, line 18 def self.configure(env) @configuration_env = env all_configs = load_configuration if all_configs.any? self.config = all_configs[env].symbolize_keys if config[:ups_host].present? self.service_host = config[:ups_host] else self.environment = config[:ups_env] end DidwwUps::Base.site = 'https://' + self.service_host store = DidwwUps::Store.new(api_public_key: config[:store_public_key], api_secret_key: config[:store_secret_key]) DidwwUps.global_store = store DidwwUps.store = store end end
# File lib/didww_ups.rb, line 49 def self.current_store Thread.current['didww_ups.active_store'] || DidwwUps.global_store end
# File lib/didww_ups.rb, line 38 def self.environment=(env) env = env.to_sym if [:live, :production].include? env self.service_host = PRODUCTION_HOST elsif [:test, :development, :sandbox].include? env self.service_host = SANDBOX_HOST else raise ArgumentError.new("environment= #{env} accepts any of :live, :production, :test, :sandbox") end end
# File lib/didww_ups.rb, line 82 def self.load_configuration config_file = ENV['DIDWW_UPS_CONFIG_FILE'] || 'config/didww_ups.yml' if File.exist?(config_file) YAML.load_file(config_file) else raise StandardError.new("File #{config_file} doesn't exist") if ENV['DIDWW_UPS_CONFIG_FILE'].present? logger.warn "Can't find #{config_file}" {} end end
# File lib/didww_ups.rb, line 53 def self.profile current_store.class.profile end
# File lib/didww_ups.rb, line 58 def self.store=(store) Thread.current['didww_ups.active_store'] = store end
# File lib/didww_ups.rb, line 62 def self.with_configuration(new_env) old_env = @configuration_env begin self.configure(new_env) yield if block_given? ensure self.configure(old_env) end end
# File lib/didww_ups.rb, line 72 def self.with_store(new_store) old_store = current_store begin self.store = new_store yield if block_given? ensure self.store=old_store end end