payment_recipes 0.0.9 → 1.0.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: dfae9ce52297d8a794125ce5948cf2b989993d0a
4
- data.tar.gz: d839c8a5412422a3a90149cbf981924f6d05b27e
3
+ metadata.gz: 6b7d9a4d62f0859042531a4e4e810c35f12c5c0f
4
+ data.tar.gz: 88704cdd2e845b0a999305403f47aa165095ae8c
5
5
  SHA512:
6
- metadata.gz: 30df4c816c12db1862120cfa03c440bf9712e348c6d91416e6d393e3f5e44a1a391535fc257de62ba7535360891626b0a5957b7154f645233aa1107e4a5a8595
7
- data.tar.gz: 832612a488131917e861d959ceec6a16c897d299cdbabbcd45ceeca97585d4ff71b69c87e60d956c59c65f91c545453e1e0d0f93b4a7b7dd28fdac8fce632a50
6
+ metadata.gz: a47e76ee300adf8d56d0fedacd4cfa76a20d7041ba402ab3cc0a609c63fc0403439179a01c5336fb4636706d1f0296b04dbb106149344af7511900df1a15691b
7
+ data.tar.gz: 32e5432bdd57b93f8c8a44671f7ce6408747a3dce8c489f31f4fc4b62a9fe28e26f4dd14ddfc48099c50cbf457df42de4f185ab390055d889ddbdb3d7782237e
@@ -0,0 +1,25 @@
1
+ module PaymentRecipes
2
+ module PayPal
3
+ module Action
4
+ class CaptureAuthorization < PaymentRecipes::Utils::Action
5
+ variable :authorization, ::PaymentRecipes::PayPal::Authorization
6
+
7
+ def perform
8
+ if @authorization.can_be_captured?
9
+ currency = @authorization.currency
10
+ total = @authorization.total.to_s
11
+
12
+ @authorization.raw_authorization.capture({:amount => { :currency => currency, :total => total } })
13
+
14
+ @authorization.reload!
15
+ @authorization.reload_payment!
16
+ else
17
+ raise Exception, "Authorization can't be captured"
18
+ end
19
+
20
+ @authorization
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,66 @@
1
+ module PaymentRecipes
2
+ module PayPal
3
+ module Action
4
+ class CreatePayment < PaymentRecipes::Utils::Action
5
+ variable :intent, ::Symbol
6
+ variable :attributes, ::Hash
7
+ variable :amount, ::Money
8
+ variable :description, ::String
9
+
10
+ include ::PaymentRecipes::Utils::Converters
11
+
12
+ def perform
13
+ unless [:sale, :authorize].include?(@intent)
14
+ raise Exception, "Allowed values for intent: :sale, :authorize"
15
+ end
16
+
17
+ @paypal_payment = ::PayPal::SDK::REST::Payment.new(payment_attributes)
18
+ @paypal_payment.create
19
+
20
+ @payment = ::PaymentRecipes::PayPal::Payment.find(@paypal_payment.id)
21
+
22
+ @payment
23
+ end
24
+
25
+ def payment
26
+ @payment
27
+ end
28
+
29
+ def paypal_payment
30
+ @paypal_payment
31
+ end
32
+
33
+ def redirect_url
34
+ paypal_payment.links.select { |link| link.rel == 'approval_url' }.first.href rescue nil
35
+ end
36
+
37
+ def sale
38
+ return nil unless @payment
39
+
40
+ @payment.sale
41
+ end
42
+
43
+ def authorization
44
+ return nil unless @payment
45
+
46
+ @payment.authorization
47
+ end
48
+
49
+ def payment_attributes
50
+ {
51
+ "intent" => convert_to_string(@intent),
52
+ "transactions" => [
53
+ {
54
+ "amount" => {
55
+ "total" => @amount.to_s,
56
+ "currency" => @amount.currency.iso_code
57
+ },
58
+ "description" => @description
59
+ }
60
+ ]
61
+ }.merge(attributes)
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,17 @@
1
+ module PaymentRecipes
2
+ module PayPal
3
+ module Action
4
+ class ExecutePayment < PaymentRecipes::Utils::Action
5
+ variable :payment, PaymentRecipes::PayPal::Payment
6
+ variable :payer_id, String
7
+
8
+ def perform
9
+ @payment.raw_payment.execute( :payer_id => payer_id )
10
+ @payment.reload!
11
+
12
+ @payment
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,22 @@
1
+ module PaymentRecipes
2
+ module PayPal
3
+ module Action
4
+ class RefundCapture < PaymentRecipes::Utils::Action
5
+ variable :capture, PaymentRecipes::PayPal::Capture
6
+
7
+ def perform
8
+ if @capture.can_be_refunded?
9
+ @capture.raw_capture.refund({})
10
+
11
+ @capture.reload!
12
+ @capture.reload_payment!
13
+ else
14
+ raise Exception, "Capture can't be refunded"
15
+ end
16
+
17
+ @capture
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ module PaymentRecipes
2
+ module PayPal
3
+ module Action
4
+ class RefundSale < PaymentRecipes::Utils::Action
5
+ variable :sale, PaymentRecipes::PayPal::Sale
6
+
7
+ def perform
8
+ if @sale.can_be_refunded?
9
+ @sale.raw_sale.refund({})
10
+
11
+ @sale.reload!
12
+ @sale.reload_payment!
13
+ else
14
+ raise Exception, "Sale can't be refunded"
15
+ end
16
+
17
+ @sale
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ module PaymentRecipes
2
+ module PayPal
3
+ module Action
4
+ class VoidAuthorization < PaymentRecipes::Utils::Action
5
+ variable :authorization, PaymentRecipes::PayPal::Authorization
6
+
7
+ def perform
8
+ if @authorization.can_be_captured?
9
+ @authorization.raw_authorization.void()
10
+
11
+ @authorization.reload!
12
+ @authorization.reload_payment!
13
+ else
14
+ raise Exception, "Authorization can't be voided"
15
+ end
16
+
17
+ @authorization
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,170 @@
1
+ module PaymentRecipes
2
+ module PayPal
3
+ class Authorization
4
+ attr_reader :id
5
+ attr_reader :currency
6
+ attr_reader :total
7
+ attr_reader :payment
8
+ attr_reader :payment_id
9
+ attr_reader :subtotal
10
+ attr_reader :state
11
+ attr_reader :create_time
12
+ attr_reader :update_time
13
+ attr_reader :valid_until
14
+
15
+ attr_reader :payment_mode
16
+ attr_reader :protection_eligibility
17
+ attr_reader :protection_eligibility_type
18
+
19
+ attr_reader :expanded
20
+ attr_reader :raw_authorization
21
+
22
+ include PaymentRecipes::Utils::Converters
23
+ include PaymentRecipes::Utils::Equality
24
+
25
+ def initialize(paypal_authorization, payment: nil, expanded: false)
26
+ unless paypal_authorization.is_a?(::PayPal::SDK::REST::DataTypes::Authorization)
27
+ raise Exception, "#{ self.class.name } must be initialized with a PayPal Authorization"
28
+ end
29
+
30
+ if payment
31
+ unless payment.is_a?(::PaymentRecipes::PayPal::Payment)
32
+ raise Exception, "Parameter payment must be a PaymentRecipes::PayPal::Payment"
33
+ end
34
+
35
+ @payment = payment
36
+ @payment_id = payment.id
37
+ end
38
+
39
+ extract_and_store(paypal_authorization)
40
+ @expanded = expanded
41
+ end
42
+
43
+ def reload!
44
+ paypal_authorization = self.class.find_raw(@id)
45
+ extract_and_store(paypal_authorization)
46
+ @expanded = true
47
+
48
+ self
49
+ end
50
+
51
+ def reload_payment!
52
+ @payment = ::PaymentRecipes::PayPal::Payment.find(@payment_id)
53
+
54
+ @payment
55
+ end
56
+
57
+ def payment
58
+ reload_payment! unless @payment
59
+
60
+ @payment
61
+ end
62
+
63
+ def payment_mode
64
+ reload! unless @payment_mode
65
+
66
+ @payment_mode
67
+ end
68
+
69
+ def protection_eligibility
70
+ reload! unless @protection_eligibility
71
+
72
+ @protection_eligibility
73
+ end
74
+
75
+ def protection_eligibility_type
76
+ reload! unless @protection_eligibility_type
77
+
78
+ @protection_eligibility_type
79
+ end
80
+
81
+ def captures
82
+ payment.captures
83
+ end
84
+
85
+ def capture
86
+ captures.first
87
+ end
88
+
89
+ def inspect
90
+ to_str
91
+ end
92
+
93
+ def pending?
94
+ @state == :pending
95
+ end
96
+
97
+ def authorized?
98
+ @state == :authorized
99
+ end
100
+
101
+ def partially_captured?
102
+ @state == :partially_captured
103
+ end
104
+
105
+ def captured?
106
+ @state == :captured
107
+ end
108
+
109
+ def expired?
110
+ @state == :expired
111
+ end
112
+
113
+ def voided?
114
+ @state == :voided
115
+ end
116
+
117
+ def can_be_captured?
118
+ authorized? || partially_captured?
119
+ end
120
+
121
+ def to_s
122
+ to_str
123
+ end
124
+
125
+ def to_str
126
+ "<#{ self.class.name } total=#{ @total.format } state=#{ @state } id=#{ @id }>"
127
+ end
128
+
129
+ class << self
130
+ def find(id)
131
+ paypal_authorization = find_raw(id)
132
+
133
+ if paypal_authorization
134
+ new(paypal_authorization, payment: nil, expanded: true)
135
+ else
136
+ nil
137
+ end
138
+ end
139
+
140
+ def find_raw(id)
141
+ begin
142
+ ::PayPal::SDK::REST::Authorization.find(id)
143
+
144
+ rescue ::PayPal::SDK::Core::Exceptions::ResourceNotFound
145
+ nil
146
+ end
147
+ end
148
+ end
149
+
150
+ private
151
+
152
+ def extract_and_store(paypal_authorization)
153
+ @id = convert_to_string(paypal_authorization.id)
154
+ @currency = convert_to_string(paypal_authorization.amount.currency)
155
+ @total = convert_to_money(amount: paypal_authorization.amount.total, currency: @currency)
156
+ @subtotal = convert_to_money(amount: paypal_authorization.amount.details.subtotal, currency: @currency)
157
+ @create_time = convert_to_time(paypal_authorization.create_time)
158
+ @update_time = convert_to_time(paypal_authorization.update_time)
159
+ @valid_until = convert_to_time(paypal_authorization.valid_until)
160
+ @payment_id = convert_to_string(paypal_authorization.parent_payment)
161
+ @state = convert_to_symbol(paypal_authorization.state)
162
+ @raw_authorization = paypal_authorization
163
+
164
+ @payment_mode = convert_to_string(paypal_authorization.payment_mode)
165
+ @protection_eligibility = convert_to_string(paypal_authorization.protection_eligibility)
166
+ @protection_eligibility_type = convert_to_string(paypal_authorization.protection_eligibility_type)
167
+ end
168
+ end
169
+ end
170
+ end
@@ -0,0 +1,138 @@
1
+ module PaymentRecipes
2
+ module PayPal
3
+ class Capture
4
+ attr_reader :currency
5
+ attr_reader :total
6
+ attr_reader :create_time
7
+ attr_reader :id
8
+ attr_reader :payment
9
+ attr_reader :payment_id
10
+ attr_reader :state
11
+ attr_reader :update_time
12
+ attr_reader :transaction_fee
13
+ attr_reader :raw_capture
14
+
15
+ include ::PaymentRecipes::Utils::Converters
16
+ include ::PaymentRecipes::Utils::Equality
17
+
18
+ def initialize(paypal_capture, payment: nil)
19
+ unless paypal_capture.is_a?(::PayPal::SDK::REST::DataTypes::Capture)
20
+ raise Exception, "#{ self.class.name } must be initialized with a PayPal Capture"
21
+ end
22
+
23
+ if payment
24
+ unless payment.is_a?(::PaymentRecipes::PayPal::Payment)
25
+ raise Exception, "Parameter payment must be a PaymentRecipes::PayPal::Payment"
26
+ end
27
+
28
+ @payment = payment
29
+ @payment_id = payment.id
30
+ end
31
+
32
+ extract_and_store(paypal_capture)
33
+ end
34
+
35
+ def reload!
36
+ paypal_capture = self.class.find_raw(@id)
37
+ extract_and_store(paypal_capture)
38
+
39
+ self
40
+ end
41
+
42
+ def reload_payment!
43
+ @payment = ::PaymentRecipes::PayPal::Payment.find(@payment_id)
44
+
45
+ @payment
46
+ end
47
+
48
+ def payment
49
+ reload_payment! unless @payment
50
+
51
+ @payment
52
+ end
53
+
54
+ def authorizations
55
+ payment.authorizations
56
+ end
57
+
58
+ def authorization
59
+ payment.authorization
60
+ end
61
+
62
+ def refunds
63
+ payment.refunds.select do |refund|
64
+ refund.capture_id == @id
65
+ end
66
+ end
67
+
68
+ def refund
69
+ refunds.first
70
+ end
71
+
72
+ def can_be_refunded?
73
+ completed?
74
+ end
75
+
76
+ def pending?
77
+ @state == :pending
78
+ end
79
+
80
+ def completed?
81
+ @state == :completed
82
+ end
83
+
84
+ def refunded?
85
+ @state = :refunded
86
+ end
87
+
88
+ def partially_refunded?
89
+ @state = :partially_refunded
90
+ end
91
+
92
+ def inspect
93
+ to_str
94
+ end
95
+
96
+ def to_s
97
+ to_str
98
+ end
99
+
100
+ def to_str
101
+ "<#{ self.class.name } total=#{ @total.format } state=#{ @state } id=#{ @id }>"
102
+ end
103
+
104
+ class << self
105
+ def find(id)
106
+ paypal_capture = find_raw(id)
107
+
108
+ if paypal_capture
109
+ new(paypal_capture, payment: nil)
110
+ else
111
+ nil
112
+ end
113
+ end
114
+
115
+ def find_raw(id)
116
+ begin
117
+ ::PayPal::SDK::REST::Capture.find(id)
118
+
119
+ rescue ::PayPal::SDK::Core::Exceptions::ResourceNotFound
120
+ nil
121
+ end
122
+ end
123
+ end
124
+
125
+ def extract_and_store(paypal_capture)
126
+ @currency = convert_to_string(paypal_capture.amount.currency)
127
+ @total = convert_to_money(amount: paypal_capture.amount.total, currency: @currency)
128
+ @create_time = convert_to_time(paypal_capture.create_time)
129
+ @update_time = convert_to_time(paypal_capture.update_time)
130
+ @id = convert_to_string(paypal_capture.id)
131
+ @payment_id = convert_to_string(paypal_capture.parent_payment)
132
+ @state = convert_to_symbol(paypal_capture.state)
133
+ @transaction_fee = convert_to_money(amount: paypal_capture.transaction_fee.value, currency: paypal_capture.transaction_fee.currency)
134
+ @raw_capture = paypal_capture
135
+ end
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,36 @@
1
+ module PaymentRecipes
2
+ module PayPal
3
+ class Payer
4
+ attr_reader :payment_method
5
+ attr_reader :funding_instruments
6
+ attr_reader :raw_payer
7
+
8
+ include ::PaymentRecipes::Utils::Converters
9
+
10
+ def initialize(paypal_payer)
11
+ unless paypal_payer.is_a?(::PayPal::SDK::REST::DataTypes::Payer)
12
+ raise Exception, "#{ self.class.name } must be initialized with a PayPal Payer"
13
+ end
14
+
15
+ @payment_method = convert_to_symbol(paypal_payer.payment_method)
16
+ @raw_payer = paypal_payer
17
+ end
18
+
19
+ def funding_instrument
20
+ @funding_instruments.first
21
+ end
22
+
23
+ def inspect
24
+ to_str
25
+ end
26
+
27
+ def to_s
28
+ to_str
29
+ end
30
+
31
+ def to_str
32
+ "<#{ self.class.name } payment_method=#{ @payment_method }>"
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,124 @@
1
+ module PaymentRecipes
2
+ module PayPal
3
+ class Payment
4
+ attr_reader :id
5
+ attr_reader :intent
6
+ attr_reader :create_time
7
+ attr_reader :update_time
8
+ attr_reader :state
9
+ attr_reader :transactions
10
+ attr_reader :sales
11
+ attr_reader :authorizations
12
+ attr_reader :captures
13
+ attr_reader :refunds
14
+ attr_reader :payer
15
+ attr_reader :raw_payment
16
+
17
+ include ::PaymentRecipes::Utils::Converters
18
+ include ::PaymentRecipes::Utils::Equality
19
+
20
+ def initialize(paypal_payment, expand: false)
21
+ unless paypal_payment.is_a?(::PayPal::SDK::REST::DataTypes::Payment)
22
+ raise Exception, "#{ self.class.name } must be initialized with a PayPal Payment"
23
+ end
24
+
25
+ extract_and_store(paypal_payment, expand: expand)
26
+ end
27
+
28
+ def reload!
29
+ paypal_payment = self.class.find_raw(@id)
30
+ extract_and_store(paypal_payment)
31
+
32
+ self
33
+ end
34
+
35
+ def transaction
36
+ @transactions.first
37
+ end
38
+
39
+ def sale
40
+ @sales.first
41
+ end
42
+
43
+ def authorization
44
+ @authorizations.first
45
+ end
46
+
47
+ def capture
48
+ @captures.first
49
+ end
50
+
51
+ def refund
52
+ @refunds.first
53
+ end
54
+
55
+ def inspect
56
+ to_str
57
+ end
58
+
59
+ def to_s
60
+ to_str
61
+ end
62
+
63
+ def to_str
64
+ "<#{ self.class.name } intent=#{ @intent } state=#{ @state } id=#{ @id }>"
65
+ end
66
+
67
+ class << self
68
+ def find(id, expand: false)
69
+ paypal_payment = find_raw(id)
70
+
71
+ if paypal_payment
72
+ new(paypal_payment, expand: expand)
73
+ else
74
+ nil
75
+ end
76
+ end
77
+
78
+ def find_raw(id)
79
+ begin
80
+ ::PayPal::SDK::REST::Payment.find(id)
81
+
82
+ rescue ::PayPal::SDK::Core::Exceptions::ResourceNotFound
83
+ nil
84
+ end
85
+ end
86
+
87
+ def history(count:, page: 1, expand: false)
88
+ paypal_payment_history = ::PayPal::SDK::REST::Payment.all(count: count, start_index: count * (page - 1))
89
+
90
+ paypal_payment_history.payments.map do |paypal_payment|
91
+ new(paypal_payment, expand: expand)
92
+ end
93
+ end
94
+ end
95
+
96
+ private
97
+
98
+ def extract_and_store(paypal_payment, expand: false)
99
+ @id = convert_to_string(paypal_payment.id)
100
+ @intent = convert_to_symbol(paypal_payment.intent)
101
+ @create_time = convert_to_time(paypal_payment.create_time)
102
+ @update_time = convert_to_time(paypal_payment.update_time)
103
+ @state = convert_to_symbol(paypal_payment.state)
104
+ @raw_payment = paypal_payment
105
+ @transactions = []
106
+ @sales = []
107
+ @authorizations = []
108
+ @captures = []
109
+ @refunds = []
110
+ @payer = ::PaymentRecipes::PayPal::Payer.new(paypal_payment.payer)
111
+
112
+ paypal_payment.transactions.each do |paypal_transaction|
113
+ transaction = ::PaymentRecipes::PayPal::Transaction.new(paypal_transaction, payment: self, expand: expand)
114
+ @sales += transaction.sales
115
+ @authorizations += transaction.authorizations
116
+ @captures += transaction.captures
117
+ @refunds += transaction.refunds
118
+
119
+ @transactions << transaction
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,158 @@
1
+ module PaymentRecipes
2
+ module PayPal
3
+ class Refund
4
+ attr_reader :id
5
+ attr_reader :currency
6
+ attr_reader :total
7
+ attr_reader :payment
8
+ attr_reader :payment_id
9
+ attr_reader :state
10
+
11
+ attr_reader :create_time
12
+ attr_reader :update_time
13
+
14
+ attr_reader :sale_id
15
+ attr_reader :sale
16
+
17
+ attr_reader :capture_id
18
+ attr_reader :capture
19
+
20
+ attr_reader :raw_refund
21
+
22
+ include ::PaymentRecipes::Utils::Converters
23
+ include ::PaymentRecipes::Utils::Equality
24
+
25
+ def initialize(paypal_refund, payment: nil)
26
+ unless paypal_refund.is_a?(::PayPal::SDK::REST::DataTypes::Refund)
27
+ raise Exception, "#{ self.class.name } must be initialized with a PayPal Refund"
28
+ end
29
+
30
+ if payment
31
+ unless payment.is_a?(::PaymentRecipes::PayPal::Payment)
32
+ raise Exception, "Parameter payment must be a PaymentRecipes::PayPal::Payment"
33
+ end
34
+
35
+ @payment = payment
36
+ @payment_id = payment.id
37
+ end
38
+
39
+ extract_and_store(paypal_refund)
40
+ end
41
+
42
+ def reload!
43
+ paypal_refund = self.class.find_raw(@id)
44
+ extract_and_store(paypal_refund)
45
+
46
+ self
47
+ end
48
+
49
+ def reload_payment!
50
+ @payment = ::PaymentRecipes::PayPal::Payment.find(@payment_id)
51
+
52
+ @payment
53
+ end
54
+
55
+ def reload_sale!
56
+ return unless @sale_id
57
+
58
+ @sale = ::PaymentRecipes::PayPal::Sale.find(@sale_id)
59
+
60
+ @sale
61
+ end
62
+
63
+ def reload_capture!
64
+ return unless @capture_id
65
+
66
+ @capture = ::PaymentRecipes::PayPal::Capture.find(@capture_id)
67
+
68
+ @capture
69
+ end
70
+
71
+ def payment
72
+ reload_payment! unless @payment
73
+
74
+ @payment
75
+ end
76
+
77
+ def capture
78
+ reload_capture! unless @capture
79
+
80
+ @capture
81
+ end
82
+
83
+ def sale
84
+ reload_sale! unless @sale
85
+
86
+ @sale
87
+ end
88
+
89
+ def authorizations
90
+ payment.authorizations
91
+ end
92
+
93
+ def authorization
94
+ payment.authorization
95
+ end
96
+
97
+ def pending?
98
+ @state == :pending
99
+ end
100
+
101
+ def completed?
102
+ @state == :completed
103
+ end
104
+
105
+ def failed?
106
+ @state == :failed
107
+ end
108
+
109
+ def inspect
110
+ to_str
111
+ end
112
+
113
+ def to_s
114
+ to_str
115
+ end
116
+
117
+ def to_str
118
+ "<#{ self.class.name } total=#{ @total.format } state=#{ @state } id=#{ @id }>"
119
+ end
120
+
121
+ class << self
122
+ def find(id)
123
+ paypal_refund = find_raw(id)
124
+
125
+ if paypal_refund
126
+ new(paypal_refund, payment: nil)
127
+ else
128
+ nil
129
+ end
130
+ end
131
+
132
+ def find_raw(id)
133
+ begin
134
+ ::PayPal::SDK::REST::Refund.find(id)
135
+
136
+ rescue ::PayPal::SDK::Core::Exceptions::ResourceNotFound
137
+ nil
138
+ end
139
+ end
140
+ end
141
+
142
+ def extract_and_store(paypal_refund)
143
+ @id = convert_to_string(paypal_refund.id)
144
+ @currency = convert_to_string(paypal_refund.amount.currency)
145
+ @total = convert_to_money(amount: paypal_refund.amount.total, currency: paypal_refund.amount.currency)
146
+ @payment_id = convert_to_string(paypal_refund.parent_payment)
147
+ @state = convert_to_symbol(paypal_refund.state)
148
+
149
+ @create_time = convert_to_time(paypal_refund.create_time)
150
+ @update_time = convert_to_time(paypal_refund.update_time)
151
+
152
+ @sale_id = convert_to_string(paypal_refund.sale_id)
153
+ @capture_id = convert_to_string(paypal_refund.capture_id)
154
+ @raw_refund = paypal_refund
155
+ end
156
+ end
157
+ end
158
+ end
@@ -0,0 +1,170 @@
1
+ module PaymentRecipes
2
+ module PayPal
3
+ class Sale
4
+ attr_reader :currency
5
+ attr_reader :total
6
+ attr_reader :create_time
7
+ attr_reader :id
8
+ attr_reader :payment
9
+ attr_reader :payment_id
10
+ attr_reader :state
11
+ attr_reader :update_time
12
+
13
+ attr_reader :transaction_fee
14
+ attr_reader :payment_mode
15
+ attr_reader :protection_eligibility
16
+ attr_reader :protection_eligibility_type
17
+
18
+ attr_reader :expanded
19
+ attr_reader :raw_sale
20
+
21
+ include ::PaymentRecipes::Utils::Converters
22
+ include ::PaymentRecipes::Utils::Equality
23
+
24
+ def initialize(paypal_sale, payment: nil, expanded: false)
25
+ unless paypal_sale.is_a?(::PayPal::SDK::REST::DataTypes::Sale)
26
+ raise Exception, "#{ self.class.name } must be initialized with a PayPal Sale"
27
+ end
28
+
29
+ if payment
30
+ unless payment.is_a?(::PaymentRecipes::PayPal::Payment)
31
+ raise Exception, "Parameter payment must be a PaymentRecipes::PayPal::Payment"
32
+ end
33
+
34
+ @payment = payment
35
+ @payment_id = payment.id
36
+ end
37
+
38
+ extract_and_store(paypal_sale)
39
+ @expanded = expanded
40
+ end
41
+
42
+ def reload!
43
+ paypal_sale = self.class.find_raw(@id)
44
+ extract_and_store(paypal_sale)
45
+ @expanded = true
46
+
47
+ self
48
+ end
49
+
50
+ def reload_payment!
51
+ @payment = ::PaymentRecipes::PayPal::Payment.find(@payment_id)
52
+
53
+ @payment
54
+ end
55
+
56
+ def payment
57
+ reload_payment! unless @payment
58
+
59
+ @payment
60
+ end
61
+
62
+ def transaction_fee
63
+ reload! unless @transaction_fee
64
+
65
+ @transaction_fee
66
+ end
67
+
68
+ def payment_mode
69
+ reload! unless @payment_mode
70
+
71
+ @payment_mode
72
+ end
73
+
74
+ def protection_eligibility
75
+ reload! unless @protection_eligibility
76
+
77
+ @protection_eligibility
78
+ end
79
+
80
+ def protection_eligibility_type
81
+ reload! unless @protection_eligibility_type
82
+
83
+ @protection_eligibility_type
84
+ end
85
+
86
+ def refunds
87
+ payment.refunds.select do |refund|
88
+ refund.sale_id == @id
89
+ end
90
+ end
91
+
92
+ def refund
93
+ refunds.first
94
+ end
95
+
96
+ def inspect
97
+ to_str
98
+ end
99
+
100
+ def to_s
101
+ to_str
102
+ end
103
+
104
+ def to_str
105
+ "<#{ self.class.name } total=#{ @total.format } state=#{ @state } id=#{ @id }>"
106
+ end
107
+
108
+ def can_be_refunded?
109
+ completed?
110
+ end
111
+
112
+ def completed?
113
+ @state == :completed
114
+ end
115
+
116
+ def partially_refunded?
117
+ @state == :partially_refunded
118
+ end
119
+
120
+ def pending?
121
+ @state == :pending
122
+ end
123
+
124
+ def refunded?
125
+ @state == :refunded
126
+ end
127
+
128
+ def denied?
129
+ @state == :denied
130
+ end
131
+
132
+ class << self
133
+ def find(id)
134
+ paypal_sale = find_raw(id)
135
+
136
+ if paypal_sale
137
+ new(paypal_sale, payment: nil, expanded: true)
138
+ else
139
+ nil
140
+ end
141
+ end
142
+
143
+ def find_raw(id)
144
+ begin
145
+ ::PayPal::SDK::REST::Sale.find(id)
146
+
147
+ rescue ::PayPal::SDK::Core::Exceptions::ResourceNotFound
148
+ nil
149
+ end
150
+ end
151
+ end
152
+
153
+ def extract_and_store(paypal_sale)
154
+ @currency = convert_to_string(paypal_sale.amount.currency)
155
+ @total = convert_to_money(amount: paypal_sale.amount.total, currency: @currency)
156
+ @create_time = convert_to_time(paypal_sale.create_time)
157
+ @update_time = convert_to_time(paypal_sale.update_time)
158
+ @id = convert_to_string(paypal_sale.id)
159
+ @payment_id = convert_to_string(paypal_sale.parent_payment)
160
+ @state = convert_to_symbol(paypal_sale.state)
161
+ @raw_sale = paypal_sale
162
+
163
+ @transaction_fee = convert_to_money(amount: paypal_sale.transaction_fee.value, currency: paypal_sale.transaction_fee.currency)
164
+ @payment_mode = convert_to_string(paypal_sale.payment_mode)
165
+ @protection_eligibility = convert_to_string(paypal_sale.protection_eligibility)
166
+ @protection_eligibility_type = convert_to_string(paypal_sale.protection_eligibility_type)
167
+ end
168
+ end
169
+ end
170
+ end
@@ -0,0 +1,14 @@
1
+ module PaymentRecipes
2
+ module PayPal
3
+ module Settings
4
+ def self.configure(mode:, client_id:, client_secret:)
5
+ ::PayPal::SDK.configure(
6
+ :mode => mode.to_s, # "sandbox" or "live"
7
+ :client_id => client_id,
8
+ :client_secret => client_secret,
9
+ :ssl_options => { }
10
+ )
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,116 @@
1
+ module PaymentRecipes
2
+ module PayPal
3
+ class Transaction
4
+ attr_reader :currency
5
+ attr_reader :total
6
+ attr_reader :payment
7
+ attr_reader :payment_id
8
+ attr_reader :description
9
+
10
+ attr_reader :subtotal
11
+ attr_reader :tax
12
+ attr_reader :fee
13
+ attr_reader :shipping
14
+ attr_reader :handling_fee
15
+ attr_reader :insurance
16
+ attr_reader :shipping_discount
17
+ attr_reader :insurance
18
+ attr_reader :gift_wrap
19
+
20
+ attr_reader :sales
21
+ attr_reader :authorizations
22
+ attr_reader :captures
23
+ attr_reader :refunds
24
+
25
+ attr_reader :raw_transaction
26
+
27
+ include ::PaymentRecipes::Utils::Converters
28
+
29
+ def initialize(paypal_transaction, payment:, expand: false)
30
+ unless paypal_transaction.is_a?(::PayPal::SDK::REST::DataTypes::Transaction)
31
+ raise Exception, "#{ self.class.name } must be initialized with a PayPal Transaction"
32
+ end
33
+
34
+ unless payment.is_a?(::PaymentRecipes::PayPal::Payment)
35
+ raise Exception, "Parameter payment must be a PaymentRecipes::PayPal::Payment"
36
+ end
37
+
38
+ @currency = convert_to_string(paypal_transaction.amount.currency)
39
+ @total = convert_to_money(amount: paypal_transaction.amount.total, currency: @currency)
40
+ @description = convert_to_string(paypal_transaction.description)
41
+ @payment = payment
42
+ @payment_id = payment.id
43
+
44
+ @subtotal = convert_to_money(amount: paypal_transaction.amount.details.subtotal, currency: @currency)
45
+ @tax = convert_to_money(amount: paypal_transaction.amount.details.tax, currency: @currency)
46
+ @fee = convert_to_money(amount: paypal_transaction.amount.details.fee, currency: @currency)
47
+ @shipping = convert_to_money(amount: paypal_transaction.amount.details.shipping, currency: @currency)
48
+ @handling_fee = convert_to_money(amount: paypal_transaction.amount.details.handling_fee, currency: @currency)
49
+ @insurance = convert_to_money(amount: paypal_transaction.amount.details.insurance, currency: @currency)
50
+ @shipping_discount = convert_to_money(amount: paypal_transaction.amount.details.shipping_discount, currency: @currency)
51
+ @insurance = convert_to_money(amount: paypal_transaction.amount.details.insurance, currency: @currency)
52
+ @gift_wrap = convert_to_money(amount: paypal_transaction.amount.details.gift_wrap, currency: @currency)
53
+ @raw_transaction = paypal_transaction
54
+
55
+ @sales = []
56
+ @authorizations = []
57
+ @captures = []
58
+ @refunds = []
59
+
60
+ paypal_transaction.related_resources.each do |paypal_related_resource|
61
+ if paypal_related_resource.sale.id
62
+ sale = ::PaymentRecipes::PayPal::Sale.new(paypal_related_resource.sale, payment: payment)
63
+ sale.reload! if expand
64
+
65
+ @sales << sale
66
+ end
67
+
68
+ if paypal_related_resource.authorization.id
69
+ authorization = ::PaymentRecipes::PayPal::Authorization.new(paypal_related_resource.authorization, payment: payment)
70
+ authorization.reload! if expand
71
+
72
+ @authorizations << authorization
73
+ end
74
+
75
+ if paypal_related_resource.capture.id
76
+ capture = ::PaymentRecipes::PayPal::Capture.new(paypal_related_resource.capture, payment: payment)
77
+ @captures << capture
78
+ end
79
+
80
+ if paypal_related_resource.refund.id
81
+ refund = ::PaymentRecipes::PayPal::Refund.new(paypal_related_resource.refund, payment: payment)
82
+ @refunds << refund
83
+ end
84
+ end
85
+ end
86
+
87
+ def sale
88
+ @sales.first
89
+ end
90
+
91
+ def authorization
92
+ @authorizations.first
93
+ end
94
+
95
+ def capture
96
+ @captures.first
97
+ end
98
+
99
+ def refund
100
+ @refunds.first
101
+ end
102
+
103
+ def inspect
104
+ to_str
105
+ end
106
+
107
+ def to_s
108
+ to_str
109
+ end
110
+
111
+ def to_str
112
+ "<#{ self.class.name } total=#{ @total.format } sales=#{ @sales.size } authorizations=#{ @authorizations.size } captures=#{ @captures.size }>"
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,39 @@
1
+ module PaymentRecipes
2
+ module Utils
3
+ class Action
4
+ class << self
5
+ def variable(key, klass)
6
+ @rules ||= {}
7
+ @rules[key] = klass
8
+
9
+ define_method key do
10
+ instance_variable_get("@#{ key }".to_sym)
11
+ end
12
+ end
13
+
14
+ def rules
15
+ @rules
16
+ end
17
+
18
+ def prepare(parameters)
19
+ @rules.each do |key, klass|
20
+ unless parameters[key].is_a?(klass)
21
+ raise Exception, "#{ key } should be a #{ klass }"
22
+ end
23
+ end
24
+
25
+ new(parameters)
26
+ end
27
+ end
28
+
29
+ def initialize(parameters)
30
+ raise Exception, "Action params should be a Hash" unless parameters.is_a?(Hash)
31
+
32
+ @params = parameters
33
+ @params.each do |param, value|
34
+ instance_variable_set("@#{ param }".to_sym, value)
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,45 @@
1
+ module PaymentRecipes
2
+ module Utils
3
+ module Converters
4
+ def convert_to_time(target)
5
+ nil_check(target) do
6
+ if defined?(Time.zone)
7
+ Time.zone.parse(target)
8
+ else
9
+ Time.parse(target)
10
+ end
11
+ end
12
+ end
13
+
14
+ def convert_to_string(target)
15
+ nil_check(target) do
16
+ target.to_s
17
+ end
18
+ end
19
+
20
+ def convert_to_money(amount:, currency:)
21
+ nil_check(amount, currency) do
22
+ raise Exception, "Money amount must be a String" unless amount.is_a?(String)
23
+
24
+ ::Money.new(::BigDecimal.new(amount) * 100, currency)
25
+ end
26
+ end
27
+
28
+ def convert_to_symbol(target)
29
+ nil_check(target) do
30
+ target.to_sym
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def nil_check(*target)
37
+ if target.any? {|x| x.nil?}
38
+ nil
39
+ else
40
+ yield if block_given?
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,9 @@
1
+ module PaymentRecipes
2
+ module Utils
3
+ module Equality
4
+ def ==(other)
5
+ @id = other.id && self.class.name == other.class.name
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module PaymentRecipes
2
+ VERSION = '1.0.0'
3
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: payment_recipes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Arvin Lat
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-08 00:00:00.000000000 Z
11
+ date: 2017-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: money
@@ -74,6 +74,24 @@ extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
76
  - lib/payment_recipes.rb
77
+ - lib/payment_recipes/paypal/action/capture_authorization.rb
78
+ - lib/payment_recipes/paypal/action/create_payment.rb
79
+ - lib/payment_recipes/paypal/action/execute_payment.rb
80
+ - lib/payment_recipes/paypal/action/refund_capture.rb
81
+ - lib/payment_recipes/paypal/action/refund_sale.rb
82
+ - lib/payment_recipes/paypal/action/void_authorization.rb
83
+ - lib/payment_recipes/paypal/authorization.rb
84
+ - lib/payment_recipes/paypal/capture.rb
85
+ - lib/payment_recipes/paypal/payer.rb
86
+ - lib/payment_recipes/paypal/payment.rb
87
+ - lib/payment_recipes/paypal/refund.rb
88
+ - lib/payment_recipes/paypal/sale.rb
89
+ - lib/payment_recipes/paypal/settings.rb
90
+ - lib/payment_recipes/paypal/transaction.rb
91
+ - lib/payment_recipes/utils/action.rb
92
+ - lib/payment_recipes/utils/converters.rb
93
+ - lib/payment_recipes/utils/equality.rb
94
+ - lib/payment_recipes/version.rb
77
95
  homepage: ''
78
96
  licenses:
79
97
  - MIT