Class: SolidusPaypalCommercePlatform::PaymentMethod

Inherits:
Spree::PaymentMethod
  • Object
show all
Includes:
ButtonOptionsHelper
Defined in:
app/models/solidus_paypal_commerce_platform/payment_method.rb

Instance Method Summary collapse

Methods included from ButtonOptionsHelper

#preferred_paypal_button_color_options, #preferred_paypal_button_layout_options, #preferred_paypal_button_shape_options, #preferred_paypal_button_size_options, #preferred_venmo_standalone_options

Instance Method Details

#button_styleObject



69
70
71
72
73
74
75
76
# File 'app/models/solidus_paypal_commerce_platform/payment_method.rb', line 69

def button_style
  {
    color: options[:paypal_button_color] || "gold",
    size: options[:paypal_button_size] || "responsive",
    shape: options[:paypal_button_shape] || "rect",
    layout: options[:paypal_button_layout] || "vertical"
  }
end

#cart_partial_nameObject



41
42
43
# File 'app/models/solidus_paypal_commerce_platform/payment_method.rb', line 41

def cart_partial_name
  "paypal_commerce_platform"
end

#client_idObject



53
54
55
# File 'app/models/solidus_paypal_commerce_platform/payment_method.rb', line 53

def client_id
  options[:client_id]
end

#client_secretObject



57
58
59
# File 'app/models/solidus_paypal_commerce_platform/payment_method.rb', line 57

def client_secret
  options[:client_secret]
end

#display_on_cartObject



37
38
39
# File 'app/models/solidus_paypal_commerce_platform/payment_method.rb', line 37

def display_on_cart
  options[:display_on_cart] || false
end

#display_on_product_pageObject



45
46
47
# File 'app/models/solidus_paypal_commerce_platform/payment_method.rb', line 45

def display_on_product_page
  options[:display_on_product_page] || false
end

#gateway_classObject



65
66
67
# File 'app/models/solidus_paypal_commerce_platform/payment_method.rb', line 65

def gateway_class
  Gateway
end

#javascript_sdk_url(order: nil, currency: nil) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/solidus_paypal_commerce_platform/payment_method.rb', line 78

def javascript_sdk_url(order: nil, currency: nil)
  # Ref: https://developer.paypal.com/sdk/js/configuration/

  # Both instance and class respond to checkout_steps.
  step_names = order ? order.checkout_steps : ::Spree::Order.checkout_steps.keys

  commit_immediately = step_names.include? "confirm"

  parameters = {
    'client-id': client_id,
    intent: auto_capture? ? "capture" : "authorize",
    commit: commit_immediately ? "false" : "true",
    components: options[:display_credit_messaging] ? "buttons,messages" : "buttons",
    currency: currency,
  }

  parameters['enable-funding'] = 'venmo' if venmo_standalone_enabled?

  unless Rails.env.production?
    parameters['buyer-country'] = options[:force_buyer_country].presence
  end

  "https://www.paypal.com/sdk/js?#{parameters.compact.to_query}".html_safe # rubocop:disable Rails/OutputSafety
end

#partial_nameObject



33
34
35
# File 'app/models/solidus_paypal_commerce_platform/payment_method.rb', line 33

def partial_name
  "paypal_commerce_platform"
end

#payment_source_classObject



61
62
63
# File 'app/models/solidus_paypal_commerce_platform/payment_method.rb', line 61

def payment_source_class
  PaymentSource
end

#product_page_partial_nameObject



49
50
51
# File 'app/models/solidus_paypal_commerce_platform/payment_method.rb', line 49

def product_page_partial_name
  "paypal_commerce_platform"
end

#render_only_venmo_standalone?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'app/models/solidus_paypal_commerce_platform/payment_method.rb', line 29

def render_only_venmo_standalone?
  options[:venmo_standalone] == 'render only standalone'
end

#try_void(payment) ⇒ Response|FalseClass

Will void the payment depending on its state or return false

If the payment has not yet been captured, we can void the transaction. Otherwise, we return false so Solidus creates a refund instead.

developer.paypal.com/docs/api/payments/v2/#authorizations_void

Parameters:

  • payment (Spree::Payment)

    the payment to void

Returns:

  • (Response|FalseClass)


113
114
115
116
117
118
# File 'app/models/solidus_paypal_commerce_platform/payment_method.rb', line 113

def try_void(payment)
  void_attempt = void(nil, { originator: payment })
  return void_attempt if void_attempt.success?

  false
end

#venmo_standalone_enabled?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'app/models/solidus_paypal_commerce_platform/payment_method.rb', line 25

def venmo_standalone_enabled?
  options[:venmo_standalone] != 'disabled'
end