laundry 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -10,7 +10,15 @@ The basic idea is to have a somewhat ActiveRecord-y syntax to making payments, u
10
10
 
11
11
  Add this line to your application's Gemfile:
12
12
 
13
- gem 'laundry'
13
+ ```ruby
14
+ gem 'laundry'
15
+ ```
16
+
17
+ There is a [bug in Savon's XML parser](https://github.com/rubiii/nori/issues/19) that can affect you if you are intending to serialize any of Laundry's objects to YAML. For the time being, adding the following line to your Gemfile can resolve this:
18
+
19
+ ```ruby
20
+ gem 'nori', git: 'git@github.com:supapuerco/nori.git' # Fixes some YAML serialization issues.
21
+ ```
14
22
 
15
23
  And then execute:
16
24
 
@@ -17,7 +17,7 @@ module Laundry
17
17
  r = client_driver.get_payment_method({'ClientID' => self.client.id, 'PaymentMethodID' => payment_method_id}) do
18
18
  http.headers["SOAPAction"] = 'https://ws.paymentsgateway.net/v1/IClientService/getPaymentMethod'
19
19
  end
20
- Account.new(r, self.merchant)
20
+ Account.from_response(r, self.merchant)
21
21
  end
22
22
 
23
23
  # Returns the payment method id
@@ -16,7 +16,7 @@ module Laundry
16
16
  r = get_client({'ClientID' => id}) do
17
17
  http.headers["SOAPAction"] = 'https://ws.paymentsgateway.net/v1/IClientService/getClient'
18
18
  end
19
- Client.new(r, self.merchant)
19
+ Client.from_response(r, self.merchant)
20
20
  end
21
21
 
22
22
  # Creates a client and returns the newly created client id.
@@ -16,7 +16,7 @@ module Laundry
16
16
  r = get_transaction({'ClientID' => client_id, 'TransactionID' => transaction_id}) do
17
17
  http.headers["SOAPAction"] = "https://ws.paymentsgateway.net/v1/ITransactionService/getTransaction"
18
18
  end
19
- Transaction.new(r, self.merchant)
19
+ Transaction.from_response(r, self.merchant)
20
20
  end
21
21
 
22
22
  end
@@ -2,11 +2,11 @@ module Laundry
2
2
  module PaymentsGateway
3
3
 
4
4
  class Account < ResponseModel
5
-
6
- def record
7
- response[:get_payment_method_response][:get_payment_method_result][:payment_method]
5
+
6
+ def initialize_with_response(response)
7
+ self.record = response[:get_payment_method_response][:get_payment_method_result][:payment_method]
8
8
  end
9
-
9
+
10
10
  # EFT TRANSACTION_CODES
11
11
  EFT_SALE = 20
12
12
  EFT_AUTH_ONLY = 21
@@ -33,6 +33,7 @@ module Laundry
33
33
  end
34
34
 
35
35
  def perform_transaction(dollars, type, options = {})
36
+ require_merchant!
36
37
  options = {
37
38
  'pg_merchant_id' => self.merchant.id,
38
39
  'pg_password' => self.merchant.transaction_password,
@@ -42,7 +43,7 @@ module Laundry
42
43
  'pg_transaction_type' => type
43
44
  }.merge(options)
44
45
  r = self.merchant.socket_driver.exec(options)
45
- TransactionResponse.new(r, self.merchant)
46
+ TransactionResponse.from_response(r, self.merchant)
46
47
  end
47
48
 
48
49
  def id
@@ -3,8 +3,8 @@ module Laundry
3
3
 
4
4
  class Client < ResponseModel
5
5
 
6
- def record
7
- response[:get_client_response][:get_client_result][:client_record]
6
+ def initialize_with_response(response)
7
+ self.record = response[:get_client_response][:get_client_result][:client_record]
8
8
  end
9
9
 
10
10
  def id
@@ -12,6 +12,7 @@ module Laundry
12
12
  end
13
13
 
14
14
  def accounts_driver
15
+ require_merchant!
15
16
  AccountDriver.new(self, self.merchant)
16
17
  end
17
18
  alias_method :accounts, :accounts_driver
@@ -1,33 +1,62 @@
1
1
  module Laundry
2
2
  module PaymentsGateway
3
3
 
4
+ class MerchantNotSetError < StandardError; end
5
+
4
6
  class ResponseModel
5
7
 
6
- attr_accessor :response
8
+ attr_accessor :record
7
9
  attr_accessor :merchant
8
- def initialize(response, merchant)
9
- self.response = response
10
- self.merchant = merchant
11
- end
12
10
 
13
- # Override please.
14
- def record
15
- response.try(:to_hash) || {}
11
+ def self.from_response(response, merchant)
12
+ model = self.new
13
+ model.merchant = merchant
14
+ model.initialize_with_response response
15
+ model
16
16
  end
17
17
 
18
+ def initialize_with_response(response)
19
+ self.record = response.try(:to_hash) || {}
20
+ end
21
+
18
22
  def to_hash
19
- record
23
+ record.try(:to_hash)
20
24
  end
21
25
 
22
26
  def blank?
23
27
  record == {} || record.nil? || !record
24
28
  end
25
29
 
30
+ def require_merchant!
31
+ unless merchant && merchant.class == Laundry::PaymentsGateway::Merchant
32
+ raise MerchantNotSetError, "Tried to call a method that requires a merchant to be set on the model. Try calling merchant= first."
33
+ end
34
+ end
35
+
26
36
  def method_missing(id, *args)
27
37
  return record[id.to_sym] if record.is_a?(Hash) && record.has_key?(id.to_sym)
28
38
  super
29
39
  end
30
-
40
+
41
+ ## Support cleaner serialization to ActiveRecord
42
+ ## Apparently supported in Rails >= 3.1
43
+
44
+ # We don't want to serialize the merchant because it has upsetting
45
+ # amounts of passwords in it.
46
+ def dumpable
47
+ d = self.dup
48
+ d.merchant = nil
49
+ d
50
+ end
51
+
52
+ def self.dump(model)
53
+ model ? YAML::dump(model.dumpable) : nil
54
+ end
55
+
56
+ def self.load(model_text)
57
+ model_text ? YAML::load(model_text) : nil
58
+ end
59
+
31
60
  end
32
61
 
33
62
  end
@@ -2,9 +2,9 @@ module Laundry
2
2
  module PaymentsGateway
3
3
 
4
4
  class Transaction < ResponseModel
5
-
6
- def record
7
- response[:get_transaction_response][:get_transaction_result]
5
+
6
+ def initialize_with_response(response)
7
+ self.record = response[:get_transaction_response][:get_transaction_result]
8
8
  end
9
9
 
10
10
  def status
@@ -2,16 +2,9 @@ module Laundry
2
2
  module PaymentsGateway
3
3
 
4
4
  class TransactionResponse < ResponseModel
5
-
6
- attr_accessor :response_record
7
-
8
- def initialize(response, merchant)
9
- super
10
- parse
11
- end
12
-
13
- def record
14
- self.response_record
5
+
6
+ def initialize_with_response(response)
7
+ self.record = parse(response)
15
8
  end
16
9
 
17
10
  def success?
@@ -19,21 +12,22 @@ module Laundry
19
12
  end
20
13
 
21
14
  def full_transaction
15
+ require_merchant!
22
16
  self.merchant.transactions.find pg_payment_method_id, pg_trace_number
23
17
  end
24
18
 
25
19
  private
26
20
 
27
- def parse
21
+ def parse(response)
28
22
  data = {}
29
- res = self.response[:execute_socket_query_response][:execute_socket_query_result].split("\n")
23
+ res = response[:execute_socket_query_response][:execute_socket_query_result].split("\n")
30
24
  res.each do |key_value_pair|
31
25
  kv = key_value_pair.split('=')
32
26
  if kv.size == 2
33
27
  data[ kv[0].to_sym ] = kv[1]
34
28
  end
35
29
  end
36
- self.response_record = data
30
+ data
37
31
  end
38
32
 
39
33
  end
@@ -1,3 +1,3 @@
1
1
  module Laundry
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: laundry
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-09 00:00:00.000000000 Z
12
+ date: 2012-08-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: savon
@@ -70,7 +70,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
70
70
  version: '0'
71
71
  segments:
72
72
  - 0
73
- hash: -1941831342828825987
73
+ hash: 759543362406420744
74
74
  required_rubygems_version: !ruby/object:Gem::Requirement
75
75
  none: false
76
76
  requirements:
@@ -79,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
79
79
  version: '0'
80
80
  segments:
81
81
  - 0
82
- hash: -1941831342828825987
82
+ hash: 759543362406420744
83
83
  requirements: []
84
84
  rubyforge_project:
85
85
  rubygems_version: 1.8.24