Class: Spree::PaypalPlatform::Gateway
- Inherits:
-
Gateway
- Object
- Gateway
- Spree::PaypalPlatform::Gateway
- Includes:
- PaymentSessions
- Defined in:
- app/models/spree/paypal_platform/gateway.rb,
app/models/spree/paypal_platform/gateway/payment_sessions.rb
Overview
PayPal Checkout payment method.
One method covers the PayPal wallet, Apple Pay, and Card Fields. Extra wallets are recorded as PaymentSources on the same gateway rather than as separate payment methods.
Defined Under Namespace
Modules: PaymentSessions Classes: GatewayResponse
Instance Method Summary collapse
-
#apple_pay_enabled? ⇒ TrueClass, FalseClass
Whether the storefront should offer Apple Pay for this method.
- #authorize(_amount_in_cents, _payment_source, _gateway_options = {}) ⇒ Object
-
#cancel(authorization, payment = nil) ⇒ GatewayResponse
Cancel a payment: refund if captured, otherwise void.
-
#capture(_amount_in_cents, paypal_id, gateway_options = {}) ⇒ GatewayResponse
Capture a previously created PayPal order.
- #client ⇒ PaypalServerSdk::Client
- #configuration_guide_partial_name ⇒ String
-
#create_profile(payment) ⇒ Spree::GatewayCustomer, NilClass
Persist a gateway-customer profile for a signed-in buyer.
-
#credit(amount_in_cents, _payment_source, paypal_payment_id, gateway_options = {}) ⇒ GatewayResponse
Refund a captured PayPal payment.
- #default_name ⇒ String
- #description_partial_name ⇒ String
- #method_type ⇒ String
- #payment_icon_name ⇒ String
- #payment_profiles_supported? ⇒ TrueClass
-
#payment_source_class ⇒ Class
Default source class for admin "previous cards" lookups.
- #provider_class ⇒ Class
-
#purchase(amount_in_cents, payment_source, gateway_options = {}) ⇒ GatewayResponse
Purchase is authorize + capture in one step.
- #source_partial_name ⇒ String
-
#void(authorization, _source, _gateway_options = {}) ⇒ GatewayResponse
Void a PayPal authorization.
- #webhook_url ⇒ String, NilClass
Methods included from PaymentSessions
#complete_payment_session, #create_payment_session, #parse_webhook_event, #payment_session_class, #session_required?, #update_payment_session
Instance Method Details
#apple_pay_enabled? ⇒ TrueClass, FalseClass
Whether the storefront should offer Apple Pay for this method.
Domain registration with PayPal is still required; this flag only advertises the capability.
105 106 107 |
# File 'app/models/spree/paypal_platform/gateway.rb', line 105 def apple_pay_enabled? preferred_enable_apple_pay end |
#authorize(_amount_in_cents, _payment_source, _gateway_options = {}) ⇒ Object
161 162 163 |
# File 'app/models/spree/paypal_platform/gateway.rb', line 161 def (_amount_in_cents, _payment_source, = {}) raise NotImplementedError, 'PayPal Checkout captures in one step; use #purchase' end |
#cancel(authorization, payment = nil) ⇒ GatewayResponse
Cancel a payment: refund if captured, otherwise void.
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'app/models/spree/paypal_platform/gateway.rb', line 259 def cancel(, payment = nil) protect_from_error do if payment&.completed? amount = payment.credit_allowed return success(, {}) 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' => , 'prefer' => 'return=representation' }) success(, response.data.as_json) end end end |
#capture(_amount_in_cents, paypal_id, gateway_options = {}) ⇒ GatewayResponse
Capture a previously created PayPal order.
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'app/models/spree/paypal_platform/gateway.rb', line 185 def capture(_amount_in_cents, paypal_id, = {}) protect_from_error do order = find_order([: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 ⇒ PaypalServerSdk::Client
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'app/models/spree/paypal_platform/gateway.rb', line 139 def client @client ||= PaypalServerSdk::Client.new( client_credentials_auth_credentials: PaypalServerSdk::ClientCredentialsAuthCredentials.new( o_auth_client_id: preferred_client_id, o_auth_client_secret: preferred_client_secret ), environment: preferred_test_mode ? PaypalServerSdk::Environment::SANDBOX : PaypalServerSdk::Environment::PRODUCTION, logging_configuration: PaypalServerSdk::LoggingConfiguration.new( log_level: Logger::WARN, request_logging_config: PaypalServerSdk::RequestLoggingConfiguration.new( log_body: false ), response_logging_config: PaypalServerSdk::ResponseLoggingConfiguration.new( log_headers: false ) ) ) end |
#configuration_guide_partial_name ⇒ String
86 87 88 |
# File 'app/models/spree/paypal_platform/gateway.rb', line 86 def configuration_guide_partial_name 'spree_paypal_platform' end |
#create_profile(payment) ⇒ Spree::GatewayCustomer, NilClass
Persist a gateway-customer profile for a signed-in buyer.
124 125 126 127 128 129 130 131 132 133 134 |
# File 'app/models/spree/paypal_platform/gateway.rb', line 124 def create_profile(payment) user = payment.order.user return if user.blank? return if payment.source.blank? return unless payment.source.is_a?(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 = {}) ⇒ GatewayResponse
Refund a captured PayPal payment.
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'app/models/spree/paypal_platform/gateway.rb', line 231 def credit(amount_in_cents, _payment_source, paypal_payment_id, = {}) refund_originator = [: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 ⇒ String
58 59 60 |
# File 'app/models/spree/paypal_platform/gateway.rb', line 58 def default_name 'PayPal' end |
#description_partial_name ⇒ String
79 80 81 |
# File 'app/models/spree/paypal_platform/gateway.rb', line 79 def description_partial_name 'spree_paypal_platform' end |
#method_type ⇒ String
65 66 67 |
# File 'app/models/spree/paypal_platform/gateway.rb', line 65 def method_type 'spree_paypal_platform' end |
#payment_icon_name ⇒ String
72 73 74 |
# File 'app/models/spree/paypal_platform/gateway.rb', line 72 def payment_icon_name 'paypal' end |
#payment_profiles_supported? ⇒ TrueClass
51 52 53 |
# File 'app/models/spree/paypal_platform/gateway.rb', line 51 def payment_profiles_supported? true end |
#payment_source_class ⇒ Class
Default source class for admin "previous cards" lookups. Actual captured payments may also be PaymentSources::ApplePay or PaymentSources::Card.
44 45 46 |
# File 'app/models/spree/paypal_platform/gateway.rb', line 44 def payment_source_class PaymentSources::Paypal end |
#provider_class ⇒ Class
33 34 35 |
# File 'app/models/spree/paypal_platform/gateway.rb', line 33 def provider_class self.class end |
#purchase(amount_in_cents, payment_source, gateway_options = {}) ⇒ GatewayResponse
Purchase is authorize + capture in one step.
173 174 175 |
# File 'app/models/spree/paypal_platform/gateway.rb', line 173 def purchase(amount_in_cents, payment_source, = {}) capture(amount_in_cents, payment_source.paypal_id, ) end |
#source_partial_name ⇒ String
93 94 95 |
# File 'app/models/spree/paypal_platform/gateway.rb', line 93 def source_partial_name 'paypal_platform' end |
#void(authorization, _source, _gateway_options = {}) ⇒ GatewayResponse
Void a PayPal authorization.
211 212 213 214 215 216 217 218 219 220 |
# File 'app/models/spree/paypal_platform/gateway.rb', line 211 def void(, _source, = {}) protect_from_error do response = client.payments.void_payment({ 'authorization_id' => , 'prefer' => 'return=representation' }) success(, response.data.as_json) end end |
#webhook_url ⇒ String, NilClass
112 113 114 115 116 |
# File 'app/models/spree/paypal_platform/gateway.rb', line 112 def webhook_url return nil unless store "#{store.url_or_custom_domain}/api/v3/webhooks/payments/#{prefixed_id}" end |