Class: SpreePaypalCheckout::Gateway
- Inherits:
-
Spree::Gateway
- Object
- Spree::Gateway
- SpreePaypalCheckout::Gateway
show all
- Includes:
- PaypalServerSdk, PaymentSessions
- Defined in:
- app/models/spree_paypal_checkout/gateway.rb,
app/models/spree_paypal_checkout/gateway/payment_sessions.rb
Defined Under Namespace
Modules: PaymentSessions
Classes: GatewayResponse
Instance Method Summary
collapse
-
#authorize(amount_in_cents, payment_source, gateway_options = {}) ⇒ Object
-
#cancel(authorization, payment = nil) ⇒ Object
-
#capture(amount_in_cents, paypal_id, gateway_options = {}) ⇒ Object
Capture a previously authorized payment.
-
#client ⇒ Object
-
#configuration_guide_partial_name ⇒ Object
-
#create_profile(payment) ⇒ Object
-
#credit(amount_in_cents, _payment_source, paypal_payment_id, gateway_options = {}) ⇒ Object
-
#default_name ⇒ Object
-
#description_partial_name ⇒ Object
-
#method_type ⇒ Object
-
#payment_icon_name ⇒ Object
-
#payment_profiles_supported? ⇒ Boolean
-
#payment_source_class ⇒ Object
-
#provider_class ⇒ Object
-
#purchase(amount_in_cents, payment_source, gateway_options = {}) ⇒ Object
Purchase is the same as authorize + capture in one step.
-
#source_partial_name ⇒ Object
-
#void(authorization, _source, gateway_options = {}) ⇒ Object
-
#webhook_url ⇒ Object
#complete_payment_session, #create_payment_session, #parse_webhook_event, #payment_session_class, #session_required?, #update_payment_session
Instance Method Details
#authorize(amount_in_cents, payment_source, gateway_options = {}) ⇒ Object
99
100
101
|
# File 'app/models/spree_paypal_checkout/gateway.rb', line 99
def authorize(amount_in_cents, payment_source, gateway_options = {})
raise 'Not implemented'
end
|
#cancel(authorization, payment = nil) ⇒ Object
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
# File 'app/models/spree_paypal_checkout/gateway.rb', line 162
def cancel(authorization, payment = nil)
protect_from_error do
if payment&.completed?
amount = payment.credit_allowed
return success(authorization, {}) if amount.zero?
refund = payment.refunds.create!(
amount: amount,
reason: Spree::RefundReason.order_canceled_reason,
refunder_id: payment.order.canceler_id
)
success(payment.response_code, refund.response.params)
else
response = client.payments.void_payment({
'authorization_id' => authorization,
'prefer' => 'return=representation'
})
success(authorization, response.data.as_json)
end
end
end
|
#capture(amount_in_cents, paypal_id, gateway_options = {}) ⇒ Object
Capture a previously authorized payment
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'app/models/spree_paypal_checkout/gateway.rb', line 112
def capture(amount_in_cents, paypal_id, gateway_options = {})
protect_from_error do
order = find_order(gateway_options[:order_id])
return failure('Order not found') unless order
response = client.orders.capture_order({
'id' => paypal_id,
'prefer' => 'return=representation'
})
if response.data.status == 'COMPLETED'
success(response.data.id, response.data.as_json)
else
failure('Failed to capture PayPal payment', response.data)
end
end
end
|
#client ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'app/models/spree_paypal_checkout/gateway.rb', line 80
def client
@client ||= Client.new(
client_credentials_auth_credentials: ClientCredentialsAuthCredentials.new(
o_auth_client_id: preferred_client_id,
o_auth_client_secret: preferred_client_secret
),
environment: preferred_test_mode ? Environment::SANDBOX : Environment::PRODUCTION,
logging_configuration: LoggingConfiguration.new(
log_level: Logger::INFO,
request_logging_config: RequestLoggingConfiguration.new(
log_body: true
),
response_logging_config: ResponseLoggingConfiguration.new(
log_headers: true
)
)
)
end
|
#configuration_guide_partial_name ⇒ Object
51
52
53
|
# File 'app/models/spree_paypal_checkout/gateway.rb', line 51
def configuration_guide_partial_name
'spree_paypal_checkout'
end
|
#create_profile(payment) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'app/models/spree_paypal_checkout/gateway.rb', line 66
def create_profile(payment)
user = payment.order.user
return if user.blank?
return if payment.source.blank?
return unless payment.source.is_a?(SpreePaypalCheckout::PaymentSources::Paypal)
paypal_account_id = payment.source.account_id
return if paypal_account_id.blank?
payment.payment_method.gateway_customers.find_or_create_by(user: user, profile_id: paypal_account_id)
end
|
#credit(amount_in_cents, _payment_source, paypal_payment_id, gateway_options = {}) ⇒ Object
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
# File 'app/models/spree_paypal_checkout/gateway.rb', line 141
def credit(amount_in_cents, _payment_source, paypal_payment_id, gateway_options = {})
refund_originator = gateway_options[:originator]
order = refund_originator.respond_to?(:order) ? refund_originator.order : refund_originator
return failure('Order not found') unless order
protect_from_error do
payload = {
capture_id: paypal_payment_id,
amount: {
value: (amount_in_cents / 100.0).to_s,
currency_code: order.currency.upcase
}
}.deep_stringify_keys
response = client.payments.refund_captured_payment(payload)
success(response.data.id, response.data.as_json)
end
end
|
#default_name ⇒ Object
35
36
37
|
# File 'app/models/spree_paypal_checkout/gateway.rb', line 35
def default_name
'PayPal'
end
|
#description_partial_name ⇒ Object
47
48
49
|
# File 'app/models/spree_paypal_checkout/gateway.rb', line 47
def description_partial_name
'spree_paypal_checkout'
end
|
#method_type ⇒ Object
39
40
41
|
# File 'app/models/spree_paypal_checkout/gateway.rb', line 39
def method_type
'spree_paypal_checkout'
end
|
#payment_icon_name ⇒ Object
43
44
45
|
# File 'app/models/spree_paypal_checkout/gateway.rb', line 43
def payment_icon_name
'paypal'
end
|
#payment_profiles_supported? ⇒ Boolean
31
32
33
|
# File 'app/models/spree_paypal_checkout/gateway.rb', line 31
def payment_profiles_supported?
true
end
|
#payment_source_class ⇒ Object
#provider_class ⇒ Object
23
24
25
|
# File 'app/models/spree_paypal_checkout/gateway.rb', line 23
def provider_class
self.class
end
|
#purchase(amount_in_cents, payment_source, gateway_options = {}) ⇒ Object
Purchase is the same as authorize + capture in one step
104
105
106
|
# File 'app/models/spree_paypal_checkout/gateway.rb', line 104
def purchase(amount_in_cents, payment_source, gateway_options = {})
capture(amount_in_cents, payment_source.paypal_id, gateway_options)
end
|
#source_partial_name ⇒ Object
55
56
57
|
# File 'app/models/spree_paypal_checkout/gateway.rb', line 55
def source_partial_name
'paypal_checkout'
end
|
#void(authorization, _source, gateway_options = {}) ⇒ Object
130
131
132
133
134
135
136
137
138
139
|
# File 'app/models/spree_paypal_checkout/gateway.rb', line 130
def void(authorization, _source, gateway_options = {})
protect_from_error do
response = client.payments.void_payment({
'authorization_id' => authorization,
'prefer' => 'return=representation'
})
success(authorization, response.data.as_json)
end
end
|
#webhook_url ⇒ Object
59
60
61
62
63
64
|
# File 'app/models/spree_paypal_checkout/gateway.rb', line 59
def webhook_url
store = stores.first
return nil unless store
"#{store.formatted_url}/api/v3/webhooks/payments/#{prefixed_id}"
end
|