Class: SpreePaypalCheckout::Gateway

Inherits:
Spree::Gateway
  • Object
show all
Includes:
PaypalServerSdk
Defined in:
app/models/spree_paypal_checkout/gateway.rb

Instance Method Summary collapse

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 authorize(amount_in_cents, payment_source, gateway_options = {})
  raise 'Not implemented'
end

#cancel(authorization, payment = nil) ⇒ Object



142
143
144
# File 'app/models/spree_paypal_checkout/gateway.rb', line 142

def cancel(authorization, payment = nil)
  raise 'Not implemented'
end

#capture(amount_in_cents, paypal_id, gateway_options = {}) ⇒ Object

Capture a previously authorized payment

Parameters:

  • amount_in_cents (Integer)

    the amount in cents to capture

  • paypal_id (String)

    the PayPal Order ID

  • gateway_options (Hash) (defaults to: {})

    this is an instance of Spree::Payment::GatewayOptions.to_hash



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, 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

#clientObject



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_nameObject



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)

   = payment.source.

  return if .blank?

  payment.payment_method.gateway_customers.find_or_create_by(user: user, profile_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, 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_nameObject



29
30
31
# File 'app/models/spree_paypal_checkout/gateway.rb', line 29

def default_name
  'PayPal'
end

#description_partial_nameObject



41
42
43
# File 'app/models/spree_paypal_checkout/gateway.rb', line 41

def description_partial_name
  'spree_paypal_checkout'
end

#method_typeObject



33
34
35
# File 'app/models/spree_paypal_checkout/gateway.rb', line 33

def method_type
  'spree_paypal_checkout'
end

#payment_icon_nameObject



37
38
39
# File 'app/models/spree_paypal_checkout/gateway.rb', line 37

def payment_icon_name
  'paypal'
end

#payment_profiles_supported?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'app/models/spree_paypal_checkout/gateway.rb', line 25

def payment_profiles_supported?
  true
end

#payment_source_classObject



21
22
23
# File 'app/models/spree_paypal_checkout/gateway.rb', line 21

def payment_source_class
  SpreePaypalCheckout::PaymentSources::Paypal
end

#provider_classObject



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, gateway_options = {})
  capture(amount_in_cents, payment_source.paypal_id, gateway_options)
end

#source_partial_nameObject



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(authorization, source, gateway_options = {})
  raise 'Not implemented'
end