unlock_gateway 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 980ea43877bcb670bca3fd458f8ac2353db2295b
4
- data.tar.gz: 70b880d149d425f0626c01c3cc09c619a2d8150a
3
+ metadata.gz: 1cb7317f716d9074645b160bbbb03c2ad4b35a6e
4
+ data.tar.gz: a4352f39986625f4148ddc8696bf007f8bae8956
5
5
  SHA512:
6
- metadata.gz: 762907885a6b2577d1a92995270b7d8b9c569d3aeb15a14c71f9f7929ce7e5cac3f42245af2c6221026373966b57e58d8a328dfc8636861a71369a462f142604
7
- data.tar.gz: 3f0f83ec2b34e87abb55e129e21fe3d461b77e6ef1ae75091e0b473bd3db9f981437e6208d50579f6dbfb2a0d165f95a2e3f59bc9dbd22a3fa237ca7ed3ccf4e
6
+ metadata.gz: cac357585717cc0a38b2122b5cee94222c9e1995ea1aa55e2b42a23becffc9f48bb6b6f2de7af92baab08fdfe75509080f313047f9dd3ee4e0a60562ee5efac2
7
+ data.tar.gz: bf90f5df27a2fa31a799bee3dbc98a248c0411f4fc059a4557678c05b4db24f1363d4f89843d23e750d4751579389a4ba36331029226c370af0a87912e9820a8
@@ -1,6 +1,24 @@
1
1
  require 'unlock_gateway/setting'
2
+ require "unlock_gateway/controller"
2
3
  require "unlock_gateway/models/gateway"
3
4
  require "unlock_gateway/models/contribution"
4
5
 
5
6
  module UnlockGateway
7
+
8
+ # Registers the gateway with unlock's Gateway model
9
+ def self.register(module_name)
10
+ begin
11
+ Gateway.register module_name
12
+ rescue
13
+ end
14
+ end
15
+
16
+ end
17
+
18
+ class ActionController::Base
19
+ # Extends and includes UnlockGateway::Controller class and instance methods, preparing a controller to be an unlock gateway controller
20
+ def self.is_unlock_gateway
21
+ extend UnlockGateway::Controller::ClassMethods
22
+ include UnlockGateway::Controller
23
+ end
6
24
  end
@@ -0,0 +1,114 @@
1
+ module UnlockGateway
2
+
3
+ # This module will be extended (ClassMethods) and included by is_unlock_gateway on controllers
4
+ module Controller
5
+
6
+ module ClassMethods
7
+
8
+ # This will be executed first, to set the controller up. Then the instance methods will be included
9
+ def self.extended(base)
10
+ base.class_eval do
11
+
12
+ inherit_resources
13
+
14
+ actions :create, :edit
15
+ respond_to :html
16
+
17
+ after_action :verify_authorized
18
+ after_action :verify_policy_scoped, only: %i[]
19
+ before_action :authenticate_user!, only: %i[edit]
20
+
21
+ end
22
+ end
23
+
24
+ end
25
+
26
+ # A second step or final checkout should use this action
27
+ def edit
28
+ edit! { authorize resource }
29
+ end
30
+
31
+ # This action will be used when the user requests to activate/reactivate a contribution
32
+ def activate
33
+ respond_to do |format|
34
+ format.html { transition_state(:active) }
35
+ end
36
+ end
37
+
38
+ # This action will be used when the user requests to suspend a contribution
39
+ def suspend
40
+ respond_to do |format|
41
+ format.html { transition_state(:suspended) }
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ # Creates the contribution, sets session[:gateway_id], returns true if successful and renders new action if not
48
+ def create_contribution
49
+
50
+ @initiative = Initiative.find(contribution_params[:initiative_id])
51
+ @gateways = @initiative.gateways.without_state(:draft).order(:ordering)
52
+ @contribution = @initiative.contributions.new(contribution_params)
53
+ @contribution.gateway_state = @contribution.gateway.state
54
+ current_user.update module_name: @contribution.gateway.module_name
55
+ authorize @contribution
56
+
57
+ if @contribution.save
58
+ true
59
+ else
60
+ render '/initiatives/contributions/new'
61
+ false
62
+ end
63
+
64
+ end
65
+
66
+ # This method authorizes the resource, checks if the contribution can be transitioned to the desired state, calls Contribution#update_state_on_gateway!, transition the contribution's state, and return the proper JSON for Unlock's AJAX calls
67
+ def transition_state(state)
68
+ authorize resource
69
+ @initiative = resource.initiative
70
+ @user = resource.user
71
+ state = state.to_sym
72
+ transition = resource.transition_by_state(state)
73
+ initial_state = resource.state_name
74
+ if resource.send("can_#{transition}?")
75
+ begin
76
+ if resource.state_on_gateway != state
77
+ if resource.update_state_on_gateway!(state)
78
+ resource.send("#{transition}!")
79
+ else
80
+ flash[:failure] = "Não foi possível alterar o status de seu apoio."
81
+ end
82
+ else
83
+ resource.send("#{transition}!")
84
+ end
85
+ rescue
86
+ flash[:failure] = "Ooops, ocorreu um erro ao alterar o status de seu apoio."
87
+ end
88
+ else
89
+ flash[:failure] = "Não foi permitido alterar o status deste apoio."
90
+ end
91
+ if flash[:failure].present?
92
+ render 'initiatives/contributions/show'
93
+ else
94
+ if initial_state == :pending
95
+ flash[:success] = "Apoio realizado com sucesso!"
96
+ else
97
+ flash[:success] = "Status do apoio alterado com sucesso!"
98
+ end
99
+ redirect_to initiative_contribution_path(resource.initiative.id, resource)
100
+ end
101
+ end
102
+
103
+ # Strong parameters for Contribution. This duplication is due to a inherited_resources problem that requires both
104
+ def permitted_params
105
+ params.permit(contribution: policy(@contribution || Contribution.new).permitted_attributes)
106
+ end
107
+
108
+ # Strong parameters for Contribution
109
+ def contribution_params
110
+ params.require(:contribution).permit(*policy(@contribution || Contribution.new).permitted_attributes)
111
+ end
112
+
113
+ end
114
+ end
@@ -4,8 +4,36 @@ module UnlockGateway
4
4
  # Your module UnlockMyGatewayName::Models::Contribution, that should implement this interface, will be included in Unlock's Contribution model. All methods will run in the context of an instance of Contribution model.
5
5
  module Contribution
6
6
 
7
- # This method should implement a way to check the state of the subscription with the gateway and update the contribution's state according to it's real state within the gateway.
7
+ # This method should return the unique identifier of the contribution on the gateway
8
+ def gateway_identifier
9
+ end
10
+
11
+ # This method should return the actual state of a contribution on the gateway, with name according to Contribution's state machine, as a Symbol.
12
+ def state_on_gateway
13
+ end
14
+
15
+ # This method should change the state of a contribution on the gateway, according to the new desired state.
16
+ def update_state_on_gateway!(state)
17
+ end
18
+
19
+ # Updates the contribution's state according to it's actual state within the gateway, based on each gateway's implementation of state_on_gateway.
8
20
  def update_state_from_gateway!
21
+ return unless gateway_state = self.state_on_gateway
22
+ if self.state_name != gateway_state
23
+ transition = self.transition_by_state(gateway_state)
24
+ self.send("#{transition}!") if self.send("can_#{transition}?")
25
+ end
26
+ end
27
+
28
+ # Helper method to find transition name by the desired new state name
29
+ def transition_by_state(state)
30
+ state = state.try(:to_sym)
31
+ case state
32
+ when :active
33
+ :activate
34
+ when :suspended
35
+ :suspend
36
+ end
9
37
  end
10
38
 
11
39
  end
@@ -17,6 +17,10 @@ module UnlockGateway
17
17
  def image
18
18
  end
19
19
 
20
+ # This method should return the gateways institutional website URL, to which Unlock will refer users.
21
+ def url
22
+ end
23
+
20
24
  # This method should return the path for the routes generated by the gateway, such as '/my_gateway'.
21
25
  def path
22
26
  end
@@ -1,3 +1,3 @@
1
1
  module UnlockGateway
2
- VERSION = "0.0.5"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unlock_gateway
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Weinmann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-08 00:00:00.000000000 Z
11
+ date: 2014-11-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -24,7 +24,7 @@ dependencies:
24
24
  - - ~>
25
25
  - !ruby/object:Gem::Version
26
26
  version: 4.1.6
27
- description: Abstract gateway for Unlock's payment gateway integrations
27
+ description: Base gateway for Unlock's payment gateway integrations
28
28
  email:
29
29
  - danielweinmann@gmail.com
30
30
  executables: []
@@ -35,6 +35,7 @@ files:
35
35
  - Rakefile
36
36
  - lib/tasks/unlock_gateway_tasks.rake
37
37
  - lib/unlock_gateway.rb
38
+ - lib/unlock_gateway/controller.rb
38
39
  - lib/unlock_gateway/models/contribution.rb
39
40
  - lib/unlock_gateway/models/gateway.rb
40
41
  - lib/unlock_gateway/setting.rb
@@ -62,5 +63,5 @@ rubyforge_project:
62
63
  rubygems_version: 2.4.2
63
64
  signing_key:
64
65
  specification_version: 4
65
- summary: Abstract gateway for Unlock's payment gateway integrations
66
+ summary: Base gateway for Unlock's payment gateway integrations
66
67
  test_files: []