spree_paypal_checkout 0.7.0 → 6.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE +16 -4
- data/README.md +34 -52
- data/Rakefile +7 -17
- data/app/jobs/spree_paypal_checkout/base_job.rb +5 -0
- data/app/jobs/spree_paypal_checkout/register_webhook_job.rb +20 -0
- data/app/maintenance_tasks/spree_paypal_checkout/maintenance_tasks/migrate_payments.rb +100 -0
- data/app/models/spree/payment_sessions/paypal_checkout.rb +39 -57
- data/app/models/spree_paypal_checkout/amounts.rb +55 -0
- data/app/models/spree_paypal_checkout/gateway/payment_sessions.rb +232 -149
- data/app/models/spree_paypal_checkout/gateway/webhooks.rb +112 -0
- data/app/models/spree_paypal_checkout/gateway.rb +418 -128
- data/app/models/spree_paypal_checkout/notifications_client.rb +82 -0
- data/app/models/spree_paypal_checkout/payment_sources/paypal.rb +43 -3
- data/app/presenters/spree_paypal_checkout/order_presenter.rb +121 -49
- data/app/services/spree_paypal_checkout/register_webhook.rb +30 -0
- data/config/locales/en.yml +23 -4
- data/lib/generators/spree_paypal_checkout/install/install_generator.rb +12 -12
- data/lib/spree_paypal_checkout/engine.rb +6 -37
- data/lib/spree_paypal_checkout/factories.rb +1 -48
- data/lib/spree_paypal_checkout/testing_support/factories/gateway_factory.rb +18 -0
- data/lib/spree_paypal_checkout/testing_support/factories/payment_session_factory.rb +16 -0
- data/lib/spree_paypal_checkout.rb +11 -3
- data/lib/tasks/register_paypal_webhooks.rake +37 -0
- metadata +33 -101
- data/app/assets/config/spree_paypal_checkout_manifest.js +0 -2
- data/app/controllers/spree_paypal_checkout/store_controller_decorator.rb +0 -9
- data/app/helpers/spree_paypal_checkout/base_helper.rb +0 -11
- data/app/javascript/spree_paypal_checkout/application.js +0 -16
- data/app/javascript/spree_paypal_checkout/controllers/checkout_paypal_controller.js +0 -117
- data/app/models/spree_paypal_checkout/base.rb +0 -6
- data/app/models/spree_paypal_checkout/order.rb +0 -71
- data/app/models/spree_paypal_checkout/order_decorator.rb +0 -11
- data/app/models/spree_paypal_checkout/payment_method_decorator.rb +0 -15
- data/app/models/spree_paypal_checkout/store_decorator.rb +0 -9
- data/app/services/spree_paypal_checkout/capture_order.rb +0 -104
- data/app/services/spree_paypal_checkout/create_payment.rb +0 -34
- data/app/services/spree_paypal_checkout/create_source.rb +0 -52
- data/app/views/spree/admin/payment_methods/configuration_guides/_spree_paypal_checkout.html.erb +0 -6
- data/app/views/spree/admin/payment_methods/descriptions/_spree_paypal_checkout.html.erb +0 -10
- data/app/views/spree/admin/payments/source_forms/_spree_paypal_checkout.html.erb +0 -7
- data/app/views/spree/checkout/payment/_spree_paypal_checkout.html.erb +0 -13
- data/app/views/spree/payment_sources/_paypal_checkout.html.erb +0 -16
- data/app/views/spree_paypal_checkout/_head.html.erb +0 -11
- data/config/importmap.rb +0 -6
- data/config/initializers/spree.rb +0 -7
- data/config/routes.rb +0 -16
- data/db/migrate/20250528095719_create_spree_paypal_checkout_orders.rb +0 -20
- data/lib/spree_api_v2/spree/api/v2/storefront/paypal_order_serializer.rb +0 -11
- data/lib/spree_api_v2/spree/api/v2/storefront/paypal_orders_controller.rb +0 -55
- data/lib/spree_paypal_checkout/configuration.rb +0 -5
- data/lib/spree_paypal_checkout/version.rb +0 -7
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
require 'net/http'
|
|
2
|
+
|
|
3
|
+
module SpreePaypalCheckout
|
|
4
|
+
# The PayPal calls the SDK does not cover: webhook signature verification and
|
|
5
|
+
# webhook registration under /v1/notifications. None of them moves money; a
|
|
6
|
+
# failed verification is redelivered by PayPal and a failed registration is
|
|
7
|
+
# retried by its job, so short timeouts and no retries are enough.
|
|
8
|
+
class NotificationsClient
|
|
9
|
+
OPEN_TIMEOUT = 5
|
|
10
|
+
READ_TIMEOUT = 10
|
|
11
|
+
# Raised by Net::HTTP when PayPal cannot be reached or does not answer.
|
|
12
|
+
NETWORK_ERRORS = [
|
|
13
|
+
Net::OpenTimeout, Net::ReadTimeout, Net::WriteTimeout, SocketError, EOFError, OpenSSL::SSL::SSLError,
|
|
14
|
+
Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::EHOSTUNREACH, Errno::ETIMEDOUT
|
|
15
|
+
].freeze
|
|
16
|
+
|
|
17
|
+
# PayPal answered with an error status.
|
|
18
|
+
class Error < StandardError; end
|
|
19
|
+
|
|
20
|
+
# @param gateway [SpreePaypalCheckout::Gateway]
|
|
21
|
+
def initialize(gateway)
|
|
22
|
+
@gateway = gateway
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# The event is sent as PayPal delivered it, byte for byte: re-encoding it
|
|
26
|
+
# can change how numbers and characters are written.
|
|
27
|
+
#
|
|
28
|
+
# @param raw_body [String] the delivery's body, already known to be JSON
|
|
29
|
+
# @return [String] PayPal's verdict, SUCCESS or FAILURE
|
|
30
|
+
def verify_webhook_signature(raw_body:, headers:, webhook_id:)
|
|
31
|
+
fields = headers.merge(webhook_id: webhook_id).to_json
|
|
32
|
+
body = "#{fields.delete_suffix('}')},\"webhook_event\":#{raw_body}}"
|
|
33
|
+
|
|
34
|
+
request(Net::HTTP::Post, '/v1/notifications/verify-webhook-signature', body)['verification_status']
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# @return [Array<Hash>] every webhook of the REST app the credentials belong to
|
|
38
|
+
def list_webhooks
|
|
39
|
+
Array(request(Net::HTTP::Get, '/v1/notifications/webhooks')['webhooks'])
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# @return [Hash] the created webhook
|
|
43
|
+
def create_webhook(url:, event_types:)
|
|
44
|
+
body = { url: url, event_types: event_types.map { |name| { name: name } } }.to_json
|
|
45
|
+
request(Net::HTTP::Post, '/v1/notifications/webhooks', body)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# @return [Hash] the updated webhook
|
|
49
|
+
def replace_webhook_events(webhook_id, event_types)
|
|
50
|
+
body = [{ op: 'replace', path: '/event_types', value: event_types.map { |name| { name: name } } }].to_json
|
|
51
|
+
request(Net::HTTP::Patch, "/v1/notifications/webhooks/#{webhook_id}", body)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
private
|
|
55
|
+
|
|
56
|
+
attr_reader :gateway
|
|
57
|
+
|
|
58
|
+
def request(verb, path, body = nil)
|
|
59
|
+
uri = URI.join(gateway.client.config.get_base_uri, path)
|
|
60
|
+
http_request = verb.new(uri)
|
|
61
|
+
http_request['Authorization'] = "Bearer #{access_token}"
|
|
62
|
+
http_request['Content-Type'] = 'application/json'
|
|
63
|
+
http_request['Accept'] = 'application/json'
|
|
64
|
+
http_request.body = body if body
|
|
65
|
+
|
|
66
|
+
response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https',
|
|
67
|
+
open_timeout: OPEN_TIMEOUT, read_timeout: READ_TIMEOUT) do |http|
|
|
68
|
+
http.request(http_request)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
parsed = response.body.present? ? JSON.parse(response.body) : {}
|
|
72
|
+
return parsed if response.is_a?(Net::HTTPSuccess)
|
|
73
|
+
|
|
74
|
+
raise Error, "PayPal answered #{response.code}: #{[parsed['name'], parsed['message']].compact.join(' — ')}"
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# From the SDK's own OAuth client, so token handling lives in one place.
|
|
78
|
+
def access_token
|
|
79
|
+
@access_token ||= gateway.request_access_token.access_token
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
@@ -1,10 +1,50 @@
|
|
|
1
1
|
module SpreePaypalCheckout
|
|
2
2
|
module PaymentSources
|
|
3
|
+
# What the buyer paid with in PayPal's window: a PayPal or Venmo account, or
|
|
4
|
+
# a card typed in by a buyer without one. A card is kept here rather than as
|
|
5
|
+
# a Spree::CreditCard: PayPal issues no reusable card id without vaulting,
|
|
6
|
+
# so it could never be charged again and must not appear among saved cards.
|
|
3
7
|
class Paypal < ::Spree::PaymentSource
|
|
4
|
-
|
|
8
|
+
WALLETS = %w[paypal venmo].freeze
|
|
5
9
|
|
|
6
|
-
|
|
7
|
-
|
|
10
|
+
store_accessor :metadata, :wallet, :email, :name, :account_id, :account_status, :brand, :last_digits
|
|
11
|
+
|
|
12
|
+
# @param payment_source [Hash, nil] the PayPal order's `payment_source`
|
|
13
|
+
# @param payment_method [SpreePaypalCheckout::Gateway]
|
|
14
|
+
# @param customer [Spree::Customer, nil]
|
|
15
|
+
# @param paypal_order_id [String] keys the source
|
|
16
|
+
# @return [Paypal, nil] nil for a kind the gem does not model
|
|
17
|
+
def self.for_payment_source(payment_source, payment_method:, customer:, paypal_order_id:)
|
|
18
|
+
kind, details = Hash(payment_source).find { |key, value| (WALLETS + ['card']).include?(key) && value.is_a?(Hash) }
|
|
19
|
+
return if kind.nil?
|
|
20
|
+
|
|
21
|
+
metadata = if kind == 'card'
|
|
22
|
+
{ 'name' => details['name'], 'brand' => details['brand'], 'last_digits' => details['last_digits'] }
|
|
23
|
+
else
|
|
24
|
+
{
|
|
25
|
+
'email' => details['email_address'],
|
|
26
|
+
'name' => paypal_name(details['name']),
|
|
27
|
+
'account_id' => details['account_id'],
|
|
28
|
+
'account_status' => details['account_status'],
|
|
29
|
+
'user_name' => details['user_name']
|
|
30
|
+
}
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# One source per PayPal order: nothing is saved at PayPal to reuse,
|
|
34
|
+
# and a source shared by account would show one customer's payment
|
|
35
|
+
# under another who paid with the same PayPal account.
|
|
36
|
+
source = find_or_initialize_by(payment_method: payment_method, gateway_payment_profile_id: "#{kind}-#{paypal_order_id}")
|
|
37
|
+
source.customer ||= customer
|
|
38
|
+
source.metadata = { 'wallet' => kind }.merge(metadata.compact)
|
|
39
|
+
source.save!
|
|
40
|
+
source
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def self.paypal_name(name)
|
|
44
|
+
return name if name.is_a?(String)
|
|
45
|
+
return if name.blank?
|
|
46
|
+
|
|
47
|
+
[name['given_name'], name['surname']].compact.join(' ').presence || name['full_name']
|
|
8
48
|
end
|
|
9
49
|
|
|
10
50
|
def self.display_name
|
|
@@ -1,61 +1,133 @@
|
|
|
1
|
-
require 'paypal_server_sdk'
|
|
2
|
-
|
|
3
1
|
module SpreePaypalCheckout
|
|
2
|
+
# The PayPal order for a cart, built with the SDK's request models. One
|
|
3
|
+
# purchase unit carries the amount, the shipping address and, when they
|
|
4
|
+
# reconcile exactly to that amount, the items and their breakdown: PayPal
|
|
5
|
+
# refuses an order whose breakdown does not add up, and 6.0 carts carry fees,
|
|
6
|
+
# line discounts, included tax and store credit a breakdown may not express.
|
|
4
7
|
class OrderPresenter
|
|
5
8
|
include PaypalServerSdk
|
|
6
9
|
|
|
7
|
-
|
|
10
|
+
ITEM_NAME_MAX_LENGTH = 127
|
|
11
|
+
ITEM_SKU_MAX_LENGTH = 127
|
|
8
12
|
|
|
9
|
-
|
|
13
|
+
# @param order [Spree::Cart, Spree::Order]
|
|
14
|
+
# @param amount [BigDecimal] what the session charges
|
|
15
|
+
# @param intent [String] CheckoutPaymentIntent::CAPTURE or ::AUTHORIZE
|
|
16
|
+
def initialize(order:, amount:, intent:)
|
|
10
17
|
@order = order
|
|
18
|
+
@amount = amount
|
|
19
|
+
@intent = intent
|
|
11
20
|
end
|
|
12
21
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
amount: AmountWithBreakdown.new(
|
|
22
|
-
currency_code: order.currency.upcase,
|
|
23
|
-
value: order.total.to_s,
|
|
24
|
-
breakdown: AmountBreakdown.new(
|
|
25
|
-
item_total: Money.new(
|
|
26
|
-
currency_code: order.currency.upcase,
|
|
27
|
-
value: order.item_total.to_s
|
|
28
|
-
),
|
|
29
|
-
shipping: Money.new(
|
|
30
|
-
currency_code: order.currency.upcase,
|
|
31
|
-
value: order.ship_total.to_s
|
|
32
|
-
),
|
|
33
|
-
tax_total: Money.new(
|
|
34
|
-
currency_code: order.currency.upcase,
|
|
35
|
-
value: order.tax_total.to_s
|
|
36
|
-
),
|
|
37
|
-
discount: Money.new(
|
|
38
|
-
currency_code: order.currency.upcase,
|
|
39
|
-
value: (order.promo_total || 0).abs.to_s
|
|
40
|
-
)
|
|
41
|
-
)
|
|
42
|
-
),
|
|
43
|
-
items: order.line_items.map do |line_item|
|
|
44
|
-
Item.new(
|
|
45
|
-
name: line_item.name.to_s[0...PAYPAL_ITEM_NAME_MAX_LENGTH],
|
|
46
|
-
unit_amount: Money.new(
|
|
47
|
-
currency_code: order.currency.upcase,
|
|
48
|
-
value: line_item.price.to_s
|
|
49
|
-
),
|
|
50
|
-
quantity: line_item.quantity.to_s,
|
|
51
|
-
sku: line_item.sku,
|
|
52
|
-
category: line_item.variant.digital? ? ItemCategory::DIGITAL_GOODS : ItemCategory::PHYSICAL_GOODS
|
|
53
|
-
)
|
|
54
|
-
end
|
|
55
|
-
)
|
|
56
|
-
]
|
|
22
|
+
# @return [PaypalServerSdk::OrderRequest]
|
|
23
|
+
def order_request
|
|
24
|
+
OrderRequest.new(
|
|
25
|
+
intent: intent,
|
|
26
|
+
purchase_units: [purchase_unit],
|
|
27
|
+
application_context: OrderApplicationContext.new(
|
|
28
|
+
brand_name: order.store.name.to_s.first(127),
|
|
29
|
+
shipping_preference: shipping_details ? OrderApplicationContextShippingPreference::SET_PROVIDED_ADDRESS : OrderApplicationContextShippingPreference::NO_SHIPPING
|
|
57
30
|
)
|
|
58
|
-
|
|
31
|
+
)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# @return [PaypalServerSdk::PurchaseUnitRequest]
|
|
35
|
+
def purchase_unit
|
|
36
|
+
items = paypal_items
|
|
37
|
+
breakdown = items && amount_breakdown
|
|
38
|
+
|
|
39
|
+
# Every SDK model has its own "not sent" sentinel, so absent fields are
|
|
40
|
+
# left out of the arguments instead.
|
|
41
|
+
PurchaseUnitRequest.new(**{
|
|
42
|
+
amount: AmountWithBreakdown.new(**{
|
|
43
|
+
currency_code: currency,
|
|
44
|
+
value: Amounts.to_paypal(amount, currency),
|
|
45
|
+
breakdown: breakdown
|
|
46
|
+
}.compact),
|
|
47
|
+
items: (items if breakdown),
|
|
48
|
+
custom_id: order.prefixed_id,
|
|
49
|
+
shipping: shipping_details
|
|
50
|
+
}.compact)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
private
|
|
54
|
+
|
|
55
|
+
attr_reader :order, :amount, :intent
|
|
56
|
+
|
|
57
|
+
def currency
|
|
58
|
+
order.currency.upcase
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# nil when the parts do not add up to the amount charged, or any of them
|
|
62
|
+
# cannot be written in PayPal's precision.
|
|
63
|
+
def amount_breakdown
|
|
64
|
+
item_total = line_items.sum { |line_item| line_item.price * line_item.quantity }
|
|
65
|
+
shipping = order.delivery_total
|
|
66
|
+
tax_total = order.additional_tax_total
|
|
67
|
+
handling = order.fee_total
|
|
68
|
+
discount = -order.discount_total + order.total_applied_store_credit
|
|
69
|
+
|
|
70
|
+
parts = [item_total, shipping, tax_total, handling, discount]
|
|
71
|
+
return if parts.any?(&:negative?)
|
|
72
|
+
return if item_total != order.item_total
|
|
73
|
+
return if item_total + shipping + tax_total + handling - discount != amount
|
|
74
|
+
|
|
75
|
+
AmountBreakdown.new(
|
|
76
|
+
item_total: money(item_total),
|
|
77
|
+
shipping: money(shipping),
|
|
78
|
+
tax_total: money(tax_total),
|
|
79
|
+
handling: money(handling),
|
|
80
|
+
discount: money(discount)
|
|
81
|
+
)
|
|
82
|
+
rescue Amounts::UnsupportedAmount
|
|
83
|
+
nil
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# nil when a unit price cannot be written in PayPal's precision, such as a
|
|
87
|
+
# price quoted below the minor unit.
|
|
88
|
+
def paypal_items
|
|
89
|
+
line_items.map do |line_item|
|
|
90
|
+
ItemRequest.new(**{
|
|
91
|
+
name: line_item.name.to_s.first(ITEM_NAME_MAX_LENGTH),
|
|
92
|
+
unit_amount: money(line_item.price),
|
|
93
|
+
quantity: line_item.quantity.to_s,
|
|
94
|
+
# PayPal refuses a longer SKU, and a cut one could name another product.
|
|
95
|
+
sku: (line_item.sku.presence if line_item.sku.to_s.length <= ITEM_SKU_MAX_LENGTH),
|
|
96
|
+
category: line_item.digital? ? ItemCategory::DIGITAL_GOODS : ItemCategory::PHYSICAL_GOODS
|
|
97
|
+
}.compact)
|
|
98
|
+
end
|
|
99
|
+
rescue Amounts::UnsupportedAmount
|
|
100
|
+
nil
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# Only an address the buyer cannot change in PayPal's window, and none at
|
|
104
|
+
# all when nothing ships, so PayPal never collects one Spree did not price.
|
|
105
|
+
def shipping_details
|
|
106
|
+
return @shipping_details if defined?(@shipping_details)
|
|
107
|
+
|
|
108
|
+
address = order.ship_address
|
|
109
|
+
@shipping_details = if address.present? && address.address1.present? && order.shipping_address_required?
|
|
110
|
+
ShippingDetails.new(
|
|
111
|
+
name: ShippingName.new(full_name: address.full_name.to_s.first(300)),
|
|
112
|
+
address: Address.new(**{
|
|
113
|
+
address_line_1: address.address1,
|
|
114
|
+
address_line_2: address.address2.presence,
|
|
115
|
+
admin_area_2: address.city.presence,
|
|
116
|
+
admin_area_1: address.state_code.presence,
|
|
117
|
+
postal_code: address.postal_code.presence,
|
|
118
|
+
country_code: address.country_code
|
|
119
|
+
}.compact)
|
|
120
|
+
)
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def line_items
|
|
125
|
+
# Delivery profiles decide whether a line is digital.
|
|
126
|
+
@line_items ||= order.line_items.includes(variant: [:delivery_profile, { product: :delivery_profile }]).to_a
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def money(value)
|
|
130
|
+
Amounts.to_paypal_money(value, currency)
|
|
59
131
|
end
|
|
60
132
|
end
|
|
61
133
|
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
module SpreePaypalCheckout
|
|
2
|
+
# Finds or creates the PayPal webhook for a payment method's URL and stores
|
|
3
|
+
# its id on the method. A webhook found for the URL has its events replaced
|
|
4
|
+
# with the gem's: stores upgraded from 5.x created one by hand, subscribed to
|
|
5
|
+
# other events. One already subscribed to them is kept as it is, since
|
|
6
|
+
# PayPal refuses a change that changes nothing.
|
|
7
|
+
class RegisterWebhook
|
|
8
|
+
# @param payment_method [SpreePaypalCheckout::Gateway]
|
|
9
|
+
# @return [Hash, nil] the webhook, or nil when the method has no URL
|
|
10
|
+
def call(payment_method:)
|
|
11
|
+
url = payment_method.webhook_url
|
|
12
|
+
return if url.blank?
|
|
13
|
+
|
|
14
|
+
events = SpreePaypalCheckout::Gateway::Webhooks::SUPPORTED_EVENTS
|
|
15
|
+
client = payment_method.notifications_client
|
|
16
|
+
existing = client.list_webhooks.find { |webhook| webhook['url'] == url }
|
|
17
|
+
|
|
18
|
+
webhook = if existing.nil?
|
|
19
|
+
client.create_webhook(url: url, event_types: events)
|
|
20
|
+
elsif Array(existing['event_types']).map { |event| event['name'] }.sort == events.sort
|
|
21
|
+
existing
|
|
22
|
+
else
|
|
23
|
+
client.replace_webhook_events(existing['id'], events)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
payment_method.update!(preferred_webhook_id: webhook['id']) if payment_method.preferred_webhook_id != webhook['id']
|
|
27
|
+
webhook
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
data/config/locales/en.yml
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
# See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
|
|
3
|
-
|
|
1
|
+
---
|
|
4
2
|
en:
|
|
5
|
-
|
|
3
|
+
spree:
|
|
4
|
+
paypal_checkout:
|
|
5
|
+
errors:
|
|
6
|
+
authorization_not_open: 'PayPal can no longer capture this payment: its authorization is %{status}.'
|
|
7
|
+
capture_not_completed: PayPal did not complete the capture (status %{status}).
|
|
8
|
+
capture_pending: 'PayPal is holding this capture: %{reason}. It completes when PayPal releases it.'
|
|
9
|
+
captured_other_amount: PayPal already captured %{captured} on this order, not the %{requested} asked for.
|
|
10
|
+
credentials_rejected: PayPal %{environment} rejected this client id and secret — check that Test mode matches the credentials.
|
|
11
|
+
no_answer: 'PayPal did not answer: %{message}'
|
|
12
|
+
nothing_to_capture: This PayPal order has no authorization to capture (order status %{status}).
|
|
13
|
+
nothing_to_refund: This PayPal order has no capture to refund.
|
|
14
|
+
outcome_unknown: 'PayPal could not say whether it took the payment: %{message}'
|
|
15
|
+
paypal_unavailable: PayPal could not be reached to check the client id and secret. Try again later.
|
|
16
|
+
refund_not_completed: PayPal did not complete the refund (status %{status}).
|
|
17
|
+
void_captured: PayPal has already captured this payment; refund it instead.
|
|
18
|
+
void_pending_capture: PayPal is still holding a capture on this payment; refund it once it completes.
|
|
19
|
+
payment_session_errors:
|
|
20
|
+
declined: PayPal declined the payment (status %{status}).
|
|
21
|
+
not_open: This PayPal payment session can no longer be completed.
|
|
22
|
+
payment_captured: PayPal has already taken the money for this session.
|
|
23
|
+
payment_in_progress: A PayPal payment for this checkout is already in progress.
|
|
24
|
+
zero_amount: Payment session amount must be greater than zero
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
+
require 'rails/generators'
|
|
2
|
+
require 'spree/core'
|
|
3
|
+
|
|
1
4
|
module SpreePaypalCheckout
|
|
2
5
|
module Generators
|
|
6
|
+
# Kept as a no-op so `rake test_app` and any existing
|
|
7
|
+
# `rails g spree_paypal_checkout:install` invocations keep working. The
|
|
8
|
+
# gateway registers itself from the engine and stores its configuration in
|
|
9
|
+
# payment method preferences, so it owns no tables and has nothing to copy
|
|
10
|
+
# into the host app.
|
|
3
11
|
class InstallGenerator < Rails::Generators::Base
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
def add_migrations
|
|
7
|
-
run 'bundle exec rake railties:install:migrations FROM=spree_paypal_checkout'
|
|
8
|
-
end
|
|
12
|
+
desc 'No-op. spree_paypal_checkout registers its gateway automatically and ships no migrations.'
|
|
9
13
|
|
|
10
|
-
def
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
run 'bin/rails db:migrate'
|
|
14
|
-
else
|
|
15
|
-
puts 'Skipping rails db:migrate, don\'t forget to run it!'
|
|
16
|
-
end
|
|
14
|
+
def notify_nothing_to_install
|
|
15
|
+
say 'spree_paypal_checkout registers its gateway automatically — nothing to install. ' \
|
|
16
|
+
'Add a PayPal payment method in the admin to enter your client id and secret.'
|
|
17
17
|
end
|
|
18
18
|
end
|
|
19
19
|
end
|
|
@@ -1,45 +1,14 @@
|
|
|
1
|
+
require 'rails/engine'
|
|
2
|
+
|
|
1
3
|
module SpreePaypalCheckout
|
|
2
4
|
class Engine < Rails::Engine
|
|
3
|
-
require 'spree/core'
|
|
4
5
|
isolate_namespace Spree
|
|
5
6
|
engine_name 'spree_paypal_checkout'
|
|
6
7
|
|
|
7
|
-
#
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
config.
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
# use rspec for tests
|
|
14
|
-
config.generators do |g|
|
|
15
|
-
g.test_framework :rspec
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
initializer 'spree_paypal_checkout.environment', before: :load_config_initializers do |_app|
|
|
19
|
-
SpreePaypalCheckout::Config = SpreePaypalCheckout::Configuration.new
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
initializer 'spree_paypal_checkout.assets' do |app|
|
|
23
|
-
if app.config.respond_to?(:assets)
|
|
24
|
-
app.config.assets.paths << root.join('app/javascript')
|
|
25
|
-
app.config.assets.precompile += %w[spree_paypal_checkout_manifest]
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
initializer 'spree_paypal_checkout.importmap', before: 'importmap' do |app|
|
|
30
|
-
if app.config.respond_to?(:importmap)
|
|
31
|
-
app.config.importmap.paths << root.join('config/importmap.rb')
|
|
32
|
-
# https://github.com/rails/importmap-rails?tab=readme-ov-file#sweeping-the-cache-in-development-and-test
|
|
33
|
-
app.config.importmap.cache_sweepers << root.join('app/javascript')
|
|
34
|
-
end
|
|
8
|
+
# Core assigns the payment method registry in its own after_initialize, so
|
|
9
|
+
# appending has to happen in a later one — engine callbacks run in load order.
|
|
10
|
+
config.after_initialize do
|
|
11
|
+
Rails.application.config.spree.payment_methods << SpreePaypalCheckout::Gateway
|
|
35
12
|
end
|
|
36
|
-
|
|
37
|
-
def self.activate
|
|
38
|
-
Dir.glob(File.join(File.dirname(__FILE__), '../../app/**/*_decorator*.rb')) do |c|
|
|
39
|
-
Rails.configuration.cache_classes ? require(c) : load(c)
|
|
40
|
-
end
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
config.to_prepare(&method(:activate).to_proc)
|
|
44
13
|
end
|
|
45
14
|
end
|
|
@@ -1,48 +1 @@
|
|
|
1
|
-
|
|
2
|
-
# Define your Spree extensions Factories within this file to enable applications, and other extensions to use and override them.
|
|
3
|
-
#
|
|
4
|
-
# Example adding this to your spec_helper will load these Factories for use:
|
|
5
|
-
# require 'spree_paypal_checkout/factories'
|
|
6
|
-
#
|
|
7
|
-
|
|
8
|
-
factory :paypal_checkout_gateway, class: 'SpreePaypalCheckout::Gateway' do
|
|
9
|
-
name { 'PayPal Checkout' }
|
|
10
|
-
preferences do
|
|
11
|
-
{
|
|
12
|
-
client_id: ENV.fetch('PAYPAL_CLIENT_ID', 'client_id_test'),
|
|
13
|
-
client_secret: ENV.fetch('PAYPAL_CLIENT_SECRET', 'client_secret_test'),
|
|
14
|
-
test_mode: true
|
|
15
|
-
}
|
|
16
|
-
end
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
factory :paypal_checkout_payment_source, class: 'SpreePaypalCheckout::PaymentSource' do
|
|
20
|
-
gateway_customer_profile_id { 'PAY-CUSTOMER-ID' }
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
factory :paypal_checkout_payment_session, class: 'Spree::PaymentSessions::PaypalCheckout' do
|
|
24
|
-
association :order, factory: :order
|
|
25
|
-
association :payment_method, factory: :paypal_checkout_gateway
|
|
26
|
-
amount { order.total }
|
|
27
|
-
currency { order.currency }
|
|
28
|
-
status { 'pending' }
|
|
29
|
-
external_id { "PAYPAL-ORDER-#{SecureRandom.hex(8).upcase}" }
|
|
30
|
-
external_data { JSON.parse(File.read(SpreePaypalCheckout::Engine.root.join('spec', 'fixtures', 'paypal_order.json'))) }
|
|
31
|
-
|
|
32
|
-
factory :completed_paypal_checkout_payment_session do
|
|
33
|
-
status { 'completed' }
|
|
34
|
-
external_data { JSON.parse(File.read(SpreePaypalCheckout::Engine.root.join('spec', 'fixtures', 'captured_paypal_order.json'))) }
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
factory :paypal_checkout_order, class: 'SpreePaypalCheckout::Order' do
|
|
39
|
-
paypal_id { 'PAY-ORDER-ID' }
|
|
40
|
-
order { create(:order) }
|
|
41
|
-
amount { order.total }
|
|
42
|
-
data { JSON.parse(File.read(SpreePaypalCheckout::Engine.root.join('spec', 'fixtures', 'paypal_order.json'))) }
|
|
43
|
-
|
|
44
|
-
factory :captured_paypal_checkout_order do
|
|
45
|
-
data { JSON.parse(File.read(SpreePaypalCheckout::Engine.root.join('spec', 'fixtures', 'captured_paypal_order.json'))) }
|
|
46
|
-
end
|
|
47
|
-
end
|
|
48
|
-
end
|
|
1
|
+
Dir["#{File.dirname(__FILE__)}/testing_support/factories/**/*.rb"].sort.each { |f| load File.expand_path(f) }
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
FactoryBot.define do
|
|
2
|
+
factory :paypal_checkout_gateway, parent: :payment_method, class: 'SpreePaypalCheckout::Gateway' do
|
|
3
|
+
sequence(:name) { |n| "PayPal #{n}" }
|
|
4
|
+
type { 'SpreePaypalCheckout::Gateway' }
|
|
5
|
+
|
|
6
|
+
preferences do
|
|
7
|
+
{
|
|
8
|
+
client_id: ENV.fetch('PAYPAL_CLIENT_ID', 'paypal_client_id_test'),
|
|
9
|
+
client_secret: ENV.fetch('PAYPAL_CLIENT_SECRET', 'paypal_client_secret_test'),
|
|
10
|
+
test_mode: true
|
|
11
|
+
}
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
trait :with_webhook do
|
|
15
|
+
after(:build) { |gateway| gateway.preferred_webhook_id = 'WH-TEST-1234567890' }
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
FactoryBot.define do
|
|
2
|
+
factory :paypal_checkout_payment_session, class: 'Spree::PaymentSessions::PaypalCheckout' do
|
|
3
|
+
transient do
|
|
4
|
+
owner { create(:order_with_line_items) }
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
cart { owner if owner.is_a?(Spree::Cart) }
|
|
8
|
+
order { owner unless owner.is_a?(Spree::Cart) }
|
|
9
|
+
payment_method { create(:paypal_checkout_gateway, store: owner.store) }
|
|
10
|
+
amount { owner.total }
|
|
11
|
+
currency { owner.currency }
|
|
12
|
+
status { 'pending' }
|
|
13
|
+
external_id { SecureRandom.alphanumeric(17).upcase }
|
|
14
|
+
external_data { { 'client_id' => 'paypal_client_id_test', 'intent' => 'CAPTURE' } }
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -1,6 +1,14 @@
|
|
|
1
|
+
require 'paypal_server_sdk'
|
|
1
2
|
require 'spree_core'
|
|
2
3
|
require 'spree_paypal_checkout/engine'
|
|
3
|
-
require 'spree_paypal_checkout/version'
|
|
4
|
-
require 'spree_paypal_checkout/configuration'
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
module SpreePaypalCheckout
|
|
6
|
+
# Background queue for the gem's jobs. Defaults to Spree's default queue.
|
|
7
|
+
def self.queue
|
|
8
|
+
@queue ||= Spree.queues.default
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
class << self
|
|
12
|
+
attr_writer :queue
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
namespace :spree do
|
|
2
|
+
namespace :upgrade do
|
|
3
|
+
desc <<~DESC
|
|
4
|
+
Registers the PayPal webhook of every PayPal payment method and stores
|
|
5
|
+
its id on the method, replacing the events of a webhook created by hand
|
|
6
|
+
under the spree_paypal_checkout 0.x guide.
|
|
7
|
+
|
|
8
|
+
Run once after upgrading to Spree 6.0. Without it, PayPal methods
|
|
9
|
+
created before the upgrade have no webhook id and reject every
|
|
10
|
+
delivery.
|
|
11
|
+
|
|
12
|
+
Safe to run again.
|
|
13
|
+
DESC
|
|
14
|
+
task register_paypal_webhooks: :environment do
|
|
15
|
+
registered = 0
|
|
16
|
+
failed = []
|
|
17
|
+
|
|
18
|
+
SpreePaypalCheckout::Gateway.find_each do |payment_method|
|
|
19
|
+
# Locked as RegisterWebhookJob locks it, since a save can run one meanwhile.
|
|
20
|
+
webhook = payment_method.with_lock { SpreePaypalCheckout::RegisterWebhook.new.call(payment_method: payment_method) }
|
|
21
|
+
if webhook
|
|
22
|
+
registered += 1
|
|
23
|
+
else
|
|
24
|
+
failed << { id: payment_method.id, error: 'it has no webhook URL; its store has no URL' }
|
|
25
|
+
end
|
|
26
|
+
rescue StandardError => e
|
|
27
|
+
failed << { id: payment_method.id, error: e.message }
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
puts "Registered the webhook of #{registered} PayPal payment method(s)."
|
|
31
|
+
next if failed.empty?
|
|
32
|
+
|
|
33
|
+
failed.each { |failure| puts " payment method #{failure[:id]}: #{failure[:error]}" }
|
|
34
|
+
abort 'Some PayPal webhooks could not be registered. Re-run after resolving the errors above.'
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|