Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added Customer, BankAccount, Address and Session classes

  • Loading branch information...
commit 167a9c6a3a0555abe0e60cff3b3e212ae6920104 1 parent a575834
Jan Schwenzien authored May 19, 2012
10  Gemfile
... ...
@@ -1,14 +1,12 @@
1 1
 source "http://rubygems.org"
2  
-# Add dependencies required to use your gem here.
3  
-# Example:
4  
-#   gem "activesupport", ">= 2.3.5"
5 2
 
6  
-# Add dependencies to develop your gem here.
7  
-# Include everything needed to run rake, tests, features, etc.
  3
+gem "rails", ">= 3.0.0"
  4
+gem 'micropayment'
  5
+
8 6
 group :development do
9 7
   gem "shoulda", ">= 0"
10 8
   gem "rdoc", "~> 3.12"
11  
-  gem "bundler", "~> 1.0.0"
  9
+  gem "bundler", "~> 1.1.0"
12 10
   gem "jeweler", "~> 1.8.3"
13 11
   gem "rcov", ">= 0"
14 12
 end
108  Gemfile.lock
... ...
@@ -0,0 +1,108 @@
  1
+GEM
  2
+  remote: http://rubygems.org/
  3
+  specs:
  4
+    actionmailer (3.2.3)
  5
+      actionpack (= 3.2.3)
  6
+      mail (~> 2.4.4)
  7
+    actionpack (3.2.3)
  8
+      activemodel (= 3.2.3)
  9
+      activesupport (= 3.2.3)
  10
+      builder (~> 3.0.0)
  11
+      erubis (~> 2.7.0)
  12
+      journey (~> 1.0.1)
  13
+      rack (~> 1.4.0)
  14
+      rack-cache (~> 1.2)
  15
+      rack-test (~> 0.6.1)
  16
+      sprockets (~> 2.1.2)
  17
+    activemodel (3.2.3)
  18
+      activesupport (= 3.2.3)
  19
+      builder (~> 3.0.0)
  20
+    activerecord (3.2.3)
  21
+      activemodel (= 3.2.3)
  22
+      activesupport (= 3.2.3)
  23
+      arel (~> 3.0.2)
  24
+      tzinfo (~> 0.3.29)
  25
+    activeresource (3.2.3)
  26
+      activemodel (= 3.2.3)
  27
+      activesupport (= 3.2.3)
  28
+    activesupport (3.2.3)
  29
+      i18n (~> 0.6)
  30
+      multi_json (~> 1.0)
  31
+    addressable (2.2.8)
  32
+    arel (3.0.2)
  33
+    builder (3.0.0)
  34
+    erubis (2.7.0)
  35
+    git (1.2.5)
  36
+    hike (1.2.1)
  37
+    i18n (0.6.0)
  38
+    jeweler (1.8.3)
  39
+      bundler (~> 1.0)
  40
+      git (>= 1.2.5)
  41
+      rake
  42
+      rdoc
  43
+    journey (1.0.3)
  44
+    json (1.7.3)
  45
+    mail (2.4.4)
  46
+      i18n (>= 0.4.0)
  47
+      mime-types (~> 1.16)
  48
+      treetop (~> 1.4.8)
  49
+    micropayment (0.1.1)
  50
+      addressable
  51
+      yajl-ruby
  52
+    mime-types (1.18)
  53
+    multi_json (1.3.5)
  54
+    polyglot (0.3.3)
  55
+    rack (1.4.1)
  56
+    rack-cache (1.2)
  57
+      rack (>= 0.4)
  58
+    rack-ssl (1.3.2)
  59
+      rack
  60
+    rack-test (0.6.1)
  61
+      rack (>= 1.0)
  62
+    rails (3.2.3)
  63
+      actionmailer (= 3.2.3)
  64
+      actionpack (= 3.2.3)
  65
+      activerecord (= 3.2.3)
  66
+      activeresource (= 3.2.3)
  67
+      activesupport (= 3.2.3)
  68
+      bundler (~> 1.0)
  69
+      railties (= 3.2.3)
  70
+    railties (3.2.3)
  71
+      actionpack (= 3.2.3)
  72
+      activesupport (= 3.2.3)
  73
+      rack-ssl (~> 1.3.2)
  74
+      rake (>= 0.8.7)
  75
+      rdoc (~> 3.4)
  76
+      thor (~> 0.14.6)
  77
+    rake (0.9.2.2)
  78
+    rcov (1.0.0)
  79
+    rdoc (3.12)
  80
+      json (~> 1.4)
  81
+    shoulda (3.0.1)
  82
+      shoulda-context (~> 1.0.0)
  83
+      shoulda-matchers (~> 1.0.0)
  84
+    shoulda-context (1.0.0)
  85
+    shoulda-matchers (1.0.0)
  86
+    sprockets (2.1.3)
  87
+      hike (~> 1.2)
  88
+      rack (~> 1.0)
  89
+      tilt (~> 1.1, != 1.3.0)
  90
+    thor (0.14.6)
  91
+    tilt (1.3.3)
  92
+    treetop (1.4.10)
  93
+      polyglot
  94
+      polyglot (>= 0.3.1)
  95
+    tzinfo (0.3.33)
  96
+    yajl-ruby (1.1.0)
  97
+
  98
+PLATFORMS
  99
+  ruby
  100
+
  101
+DEPENDENCIES
  102
+  bundler (~> 1.1.0)
  103
+  jeweler (~> 1.8.3)
  104
+  micropayment
  105
+  rails (>= 3.0.0)
  106
+  rcov
  107
+  rdoc (~> 3.12)
  108
+  shoulda
2  LICENSE.txt
... ...
@@ -1,4 +1,4 @@
1  
-Copyright (c) 2012 Jan Schwenzien
  1
+Copyright (c) 2012 General Scripting UG (haftungsbeschränkt)
2 2
 
3 3
 Permission is hereby granted, free of charge, to any person obtaining
4 4
 a copy of this software and associated documentation files (the
49  README.markdown
Source Rendered
... ...
@@ -0,0 +1,49 @@
  1
+# micropayment-rails
  2
+
  3
+Use the micropayment API in your Rails project.
  4
+
  5
+## Installation
  6
+
  7
+In your Gemfile:
  8
+```
  9
+gem 'micropayment-rails'
  10
+```
  11
+
  12
+## Usage
  13
+
  14
+```
  15
+# create a blank customer
  16
+customer = Micropayment::Customer.create
  17
+
  18
+# create a customer with a customer id
  19
+customer = Micropayment::Customer.create :customerId => 'my_customer_id'
  20
+
  21
+# create a customer with a bank_account
  22
+customer = Micropayment::Customer.create :bank_account => { :bankCode => '10010010', :accountNumber => '1234567', :accountHolder => 'Jeff Winger' }
  23
+
  24
+# access a customers bank account
  25
+customer.bank_account
  26
+
  27
+# set/update a customers bank account
  28
+customer.bank_account = { :bankCode => '10010010', :accountNumber => '1234567', :accountHolder => 'Jeff Winger' }
  29
+
  30
+# initiate a session
  31
+# (defaults for all hash params can be set on micropayment.de)
  32
+Micropayment::Session.create 'project_key', customer, :amount => 1000, :currency => 'EUR'
  33
+
  34
+```
  35
+
  36
+## Contributing to micropayment-rails
  37
+ 
  38
+* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
  39
+* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
  40
+* Fork the project.
  41
+* Start a feature/bugfix branch.
  42
+* Commit and push until you are happy with your contribution.
  43
+* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
  44
+* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
  45
+
  46
+## Copyright
  47
+
  48
+Copyright (c) 2012 General Scripting UG (haftungsbeschränkt). See LICENSE.txt for further details.
  49
+
19  README.rdoc
Source Rendered
... ...
@@ -1,19 +0,0 @@
1  
-= micropayment-rails
2  
-
3  
-Description goes here.
4  
-
5  
-== Contributing to micropayment-rails
6  
- 
7  
-* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8  
-* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9  
-* Fork the project.
10  
-* Start a feature/bugfix branch.
11  
-* Commit and push until you are happy with your contribution.
12  
-* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13  
-* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14  
-
15  
-== Copyright
16  
-
17  
-Copyright (c) 2012 Jan Schwenzien. See LICENSE.txt for
18  
-further details.
19  
-
1  VERSION
... ...
@@ -0,0 +1 @@
  1
+0.1.0
8  lib/micropayment-rails.rb
... ...
@@ -0,0 +1,8 @@
  1
+require 'ostruct'
  2
+require 'micropayment'
  3
+require 'micropayment-rails/setup'
  4
+require 'micropayment-rails/classes/base'
  5
+require 'micropayment-rails/classes/address'
  6
+require 'micropayment-rails/classes/bank_account'
  7
+require 'micropayment-rails/classes/customer'
  8
+require 'micropayment-rails/classes/session'
12  lib/micropayment-rails/classes/address.rb
... ...
@@ -0,0 +1,12 @@
  1
+module Micropayment
  2
+  class Address < Micropayment::Base
  3
+
  4
+    VALID_ATTRIBUTES  = [:customerId, :firstName, :surName, :street, :zip, :city, :country]
  5
+    IDENTIFIER        = :customerId
  6
+    FIND_METHOD       = :addressGet
  7
+    CREATE_METHOD     = :addressSet
  8
+
  9
+
  10
+
  11
+  end
  12
+end
10  lib/micropayment-rails/classes/bank_account.rb
... ...
@@ -0,0 +1,10 @@
  1
+module Micropayment
  2
+  class BankAccount < Micropayment::Base
  3
+
  4
+    VALID_ATTRIBUTES  = [:customerId, :bankName, :accountHolder, :accountNumber, :barStatus, :bankCode, :country]
  5
+    IDENTIFIER        = :customerId
  6
+    FIND_METHOD       = :bankaccountGet
  7
+    CREATE_METHOD     = :bankaccountSet
  8
+
  9
+  end
  10
+end
44  lib/micropayment-rails/classes/base.rb
... ...
@@ -0,0 +1,44 @@
  1
+module Micropayment
  2
+  class Base < OpenStruct
  3
+
  4
+    def self.valid_attributes(attrs)
  5
+      attrs.reject {|k,v| !self::VALID_ATTRIBUTES.include?(k.to_sym) }
  6
+    end
  7
+
  8
+
  9
+
  10
+    def id
  11
+      send self::IDENTIFIER
  12
+    end
  13
+
  14
+
  15
+
  16
+    def self.find(id)
  17
+      result = Micropayment::Debit.send self::FIND_METHOD, self::IDENTIFIER => id
  18
+      case result["error"]
  19
+      when "0"
  20
+        self.new( valid_attributes(result) )
  21
+      else
  22
+        raise "#{result["error"]}: #{result["errorMessage"]}"
  23
+      end
  24
+    end
  25
+
  26
+    def self.create(id, params={})
  27
+      params.symbolized_keys!
  28
+      params.merge!(self::IDENTIFIER => id) if id
  29
+      result = Micropayment::Debit.send self::CREATE_METHOD, params
  30
+      if result["error"] == "0"
  31
+        self.new( valid_attributes( params.merge(result.symbolized_keys) ) )
  32
+      else
  33
+        raise "#{result["error"]}: #{result["errorMessage"]}"
  34
+      end
  35
+    end
  36
+
  37
+    def self.find_or_create_by_id(id, params={})
  38
+      params.symbolized_keys!
  39
+      obj = (find(id) rescue nil)
  40
+      obj ? obj : create( id, params )
  41
+    end
  42
+
  43
+  end
  44
+end
60  lib/micropayment-rails/classes/customer.rb
... ...
@@ -0,0 +1,60 @@
  1
+module Micropayment
  2
+  class Customer < Micropayment::Base
  3
+
  4
+    IDENTIFIER        = :customerId
  5
+
  6
+
  7
+    def address=(params={})
  8
+      @address = Micropayment::Address.create( id, params )
  9
+    end
  10
+
  11
+    def address
  12
+      @address ||= (Micropayment::Address.find( id ) rescue nil)
  13
+    end
  14
+
  15
+    def bank_account=(params={})
  16
+      @bank_account = Micropayment::BankAccount.create( id, params )
  17
+    end
  18
+
  19
+    def bank_account
  20
+      @bank_account ||= (Micropayment::BankAccount.find( id ) rescue nil)
  21
+    end
  22
+
  23
+
  24
+
  25
+    def self.find(customerId)
  26
+      result = Micropayment::Debit.customerGet( :customerId => customerId )
  27
+      case result["error"]
  28
+      when "0"
  29
+        self.new( :customerId => customerId, :freeParams => result["freeParams"] )
  30
+      else
  31
+        raise "#{result["error"]}: #{result["errorMessage"]}"
  32
+      end
  33
+    end
  34
+
  35
+    def self.create(params={})
  36
+      params.symbolized_keys!
  37
+      bank_account_params = params.delete(:bank_account)
  38
+      address_params      = params.delete(:address)
  39
+      create_params = {}.tap do |hsh|
  40
+        hsh[:customerId]  = params.delete(:customerId)  if params.has_key?(:customerId)
  41
+        hsh[:freeParams]  = params
  42
+      end
  43
+      result = Micropayment::Debit.customerCreate( create_params )
  44
+      if result["error"] == "0"
  45
+        customer = self.new( create_params )
  46
+        customer.bank_account = bank_account_params   if bank_account_params
  47
+        customer.address      = address_params        if address_params
  48
+        customer
  49
+      else
  50
+        raise "#{result["error"]}: #{result["errorMessage"]}"
  51
+      end
  52
+    end
  53
+
  54
+    def self.session_list
  55
+      # TODO wrap in an array
  56
+      Micropayment::Debit.sessionList( :customerId => id )
  57
+    end
  58
+
  59
+  end
  60
+end
36  lib/micropayment-rails/classes/session.rb
... ...
@@ -0,0 +1,36 @@
  1
+module Micropayment
  2
+  class Session < Micropayment::Base
  3
+
  4
+    VALID_ATTRIBUTES  = [:sessionId, :customerId, :status, :expire, :statusDetail, :project, :projectCampaign, :account, :webmasterCampaign, :amount, :openAmount, :currency, :title, :payText, :ip, :freeParams]
  5
+    IDENTIFIER        = :sessionId
  6
+    FIND_METHOD       = :sessionGet
  7
+    CREATE_METHOD     = :sessionCreate
  8
+
  9
+
  10
+    def create(project, customer, params={})
  11
+      params.symbolize_keys!
  12
+      params.merge!( :customerId => customer.id, :project => project )
  13
+      result = Micropayment::Debit.sessionCreate( params )
  14
+      case result["error"]
  15
+      when "0"
  16
+        self.new( valid_attributes(result) )
  17
+      else
  18
+        raise "#{result["error"]}: #{result["errorMessage"]}"
  19
+      end
  20
+    end
  21
+
  22
+    def approve
  23
+      result = Micropayment::Debit.sessionApprove( :sessionId => id )
  24
+      case result["error"]
  25
+      when "0"
  26
+        self.status = result["status"]
  27
+        self.expire = result["expire"]
  28
+        status
  29
+      else
  30
+        raise "#{result["error"]}: #{result["errorMessage"]}"
  31
+      end
  32
+    end
  33
+
  34
+
  35
+  end
  36
+end
9  lib/micropayment-rails/setup.rb
... ...
@@ -0,0 +1,9 @@
  1
+module Micropayment
  2
+
  3
+  def self.setup
  4
+    yield Micropayment::Config
  5
+    Micropayment::Config.complete = true
  6
+    Micropayment::Config
  7
+  end
  8
+
  9
+end

0 notes on commit 167a9c6

Please sign in to comment.
Something went wrong with that request. Please try again.