Class: SpreePaypalCheckout::Gateway
- Inherits:
-
Spree::Gateway
- Object
- Spree::Gateway
- SpreePaypalCheckout::Gateway
- Includes:
- PaypalServerSdk
- Defined in:
- app/models/spree_paypal_checkout/gateway.rb
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
Instance Method Details
#authorize(amount_in_cents, payment_source, gateway_options = {}) ⇒ Object
86 87 88 |
# File 'app/models/spree_paypal_checkout/gateway.rb', line 86 def (amount_in_cents, payment_source, = {}) raise 'Not implemented' end |
#cancel(authorization, payment = nil) ⇒ Object
142 143 144 |
# File 'app/models/spree_paypal_checkout/gateway.rb', line 142 def cancel(, payment = nil) raise 'Not implemented' end |
#capture(amount_in_cents, paypal_id, gateway_options = {}) ⇒ Object
Capture a previously authorized payment
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'app/models/spree_paypal_checkout/gateway.rb', line 99 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 ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'app/models/spree_paypal_checkout/gateway.rb', line 67 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
45 46 47 |
# File 'app/models/spree_paypal_checkout/gateway.rb', line 45 def configuration_guide_partial_name 'spree_paypal_checkout' end |
#create_profile(payment) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'app/models/spree_paypal_checkout/gateway.rb', line 53 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
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'app/models/spree_paypal_checkout/gateway.rb', line 121 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 ⇒ Object
29 30 31 |
# File 'app/models/spree_paypal_checkout/gateway.rb', line 29 def default_name 'PayPal' end |
#description_partial_name ⇒ Object
41 42 43 |
# File 'app/models/spree_paypal_checkout/gateway.rb', line 41 def description_partial_name 'spree_paypal_checkout' end |
#method_type ⇒ Object
33 34 35 |
# File 'app/models/spree_paypal_checkout/gateway.rb', line 33 def method_type 'spree_paypal_checkout' end |
#payment_icon_name ⇒ Object
37 38 39 |
# File 'app/models/spree_paypal_checkout/gateway.rb', line 37 def payment_icon_name 'paypal' end |
#payment_profiles_supported? ⇒ Boolean
25 26 27 |
# File 'app/models/spree_paypal_checkout/gateway.rb', line 25 def payment_profiles_supported? true end |
#payment_source_class ⇒ Object
21 22 23 |
# File 'app/models/spree_paypal_checkout/gateway.rb', line 21 def payment_source_class SpreePaypalCheckout::PaymentSources::Paypal end |
#provider_class ⇒ Object
17 18 19 |
# File 'app/models/spree_paypal_checkout/gateway.rb', line 17 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
91 92 93 |
# File 'app/models/spree_paypal_checkout/gateway.rb', line 91 def purchase(amount_in_cents, payment_source, = {}) capture(amount_in_cents, payment_source.paypal_id, ) end |
#source_partial_name ⇒ Object
49 50 51 |
# File 'app/models/spree_paypal_checkout/gateway.rb', line 49 def source_partial_name 'paypal_checkout' end |
#void(authorization, source, gateway_options = {}) ⇒ Object
117 118 119 |
# File 'app/models/spree_paypal_checkout/gateway.rb', line 117 def void(, source, = {}) raise 'Not implemented' end |