Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fix: PayPal refunds, long SKUs and webhook registration from review

Łukasz Adamczyk2cdfba5fa6a4
diff --git a/spree/providers/paypal/README.md b/spree/providers/paypal/README.md
index 5366e1348e..56029cfe8d 100644
--- a/spree/providers/paypal/README.md
+++ b/spree/providers/paypal/README.md
@@ -23,7 +23,9 @@ run.
Create a PayPal payment method in the admin dashboard and enter the client id
and secret of your PayPal REST app. Leave Test mode on for sandbox credentials
-and turn it off for live ones. The webhook is registered with PayPal on save.
+and turn it off for live ones. Saving queues a job that registers the webhook
+with PayPal; until it has run, PayPal's notifications are refused, so if none
+arrive, check the job queue for a failed `SpreePaypalCheckout::RegisterWebhookJob`.
Set `PAYPAL_LOG_LEVEL` (for example `debug`) to change how much the PayPal
client logs. Request and response bodies are never logged.
diff --git a/spree/providers/paypal/app/models/spree_paypal_checkout/gateway.rb b/spree/providers/paypal/app/models/spree_paypal_checkout/gateway.rb
index 6d3f8a4100..23d238222c 100644
--- a/spree/providers/paypal/app/models/spree_paypal_checkout/gateway.rb
+++ b/spree/providers/paypal/app/models/spree_paypal_checkout/gateway.rb
@@ -191,6 +191,12 @@ module SpreePaypalCheckout
result = protect_from_error(money_moving: true) do
refund_capture(capture['id'], money, request_id: request_id)
end
+ # PayPal can answer a refund it did not make with a status, not an error.
+ # A PENDING refund is recorded, as Stripe's are: core has no pending refund.
+ if result['status'].in?(%w[FAILED CANCELLED])
+ raise PaypalError.new(I18n.t('spree.paypal_checkout.errors.refund_not_completed', status: result['status']),
+ issue: result['status'])
+ end
success(result['id'], result)
end
diff --git a/spree/providers/paypal/app/presenters/spree_paypal_checkout/order_presenter.rb b/spree/providers/paypal/app/presenters/spree_paypal_checkout/order_presenter.rb
index e808f1a6cb..c44e9c0e43 100644
--- a/spree/providers/paypal/app/presenters/spree_paypal_checkout/order_presenter.rb
+++ b/spree/providers/paypal/app/presenters/spree_paypal_checkout/order_presenter.rb
@@ -8,6 +8,7 @@ module SpreePaypalCheckout
include PaypalServerSdk
ITEM_NAME_MAX_LENGTH = 127
+ ITEM_SKU_MAX_LENGTH = 127
# @param order [Spree::Cart, Spree::Order]
# @param amount [BigDecimal] what the session charges
@@ -90,7 +91,8 @@ module SpreePaypalCheckout
name: line_item.name.to_s.first(ITEM_NAME_MAX_LENGTH),
unit_amount: money(line_item.price),
quantity: line_item.quantity.to_s,
- sku: line_item.sku.presence,
+ # PayPal refuses a longer SKU, and a cut one could name another product.
+ sku: (line_item.sku.presence if line_item.sku.to_s.length <= ITEM_SKU_MAX_LENGTH),
category: line_item.digital? ? ItemCategory::DIGITAL_GOODS : ItemCategory::PHYSICAL_GOODS
}.compact)
end
diff --git a/spree/providers/paypal/config/locales/en.yml b/spree/providers/paypal/config/locales/en.yml
index 9c8b583e9e..1acb9bfd08 100644
--- a/spree/providers/paypal/config/locales/en.yml
+++ b/spree/providers/paypal/config/locales/en.yml
@@ -13,6 +13,7 @@ en:
nothing_to_refund: "This PayPal order has no capture to refund."
outcome_unknown: "PayPal could not say whether it took the payment: %{message}"
paypal_unavailable: "PayPal could not be reached to check the client id and secret. Try again later."
+ refund_not_completed: "PayPal did not complete the refund (status %{status})."
void_captured: "PayPal has already captured this payment; refund it instead."
void_pending_capture: "PayPal is still holding a capture on this payment; refund it once it completes."
payment_session_errors:
diff --git a/spree/providers/paypal/lib/tasks/register_paypal_webhooks.rake b/spree/providers/paypal/lib/tasks/register_paypal_webhooks.rake
index 4630c5d463..c45f7cec0f 100644
--- a/spree/providers/paypal/lib/tasks/register_paypal_webhooks.rake
+++ b/spree/providers/paypal/lib/tasks/register_paypal_webhooks.rake
@@ -16,7 +16,8 @@ namespace :spree do
failed = []
SpreePaypalCheckout::Gateway.find_each do |payment_method|
- webhook = SpreePaypalCheckout::RegisterWebhook.new.call(payment_method: payment_method)
+ # Locked as RegisterWebhookJob locks it, since a save can run one meanwhile.
+ webhook = payment_method.with_lock { SpreePaypalCheckout::RegisterWebhook.new.call(payment_method: payment_method) }
if webhook
registered += 1
else
diff --git a/spree/providers/paypal/spec/models/spree_paypal_checkout/gateway_spec.rb b/spree/providers/paypal/spec/models/spree_paypal_checkout/gateway_spec.rb
index 0c90fa8cb0..9cfc531807 100644
--- a/spree/providers/paypal/spec/models/spree_paypal_checkout/gateway_spec.rb
+++ b/spree/providers/paypal/spec/models/spree_paypal_checkout/gateway_spec.rb
@@ -407,6 +407,14 @@ RSpec.describe SpreePaypalCheckout::Gateway do
to raise_error(Spree::Core::GatewayError, 'This PayPal order has no capture to refund.')
end
+ it 'raises when PayPal answers that it did not make the refund' do
+ allow(gateway).to receive(:paypal_order).and_return(paypal_order_data(captures: [paypal_capture]))
+ allow(gateway).to receive(:refund_capture).and_return({ 'id' => 'REFUND1234567890A', 'status' => 'FAILED' })
+
+ expect { gateway.credit(money, order_id, originator: refund) }.
+ to raise_error(Spree::Core::GatewayError, 'PayPal did not complete the refund (status FAILED).')
+ end
+
it 'refunds a capture against PayPal', vcr: { cassette_name: 'refund_capture' } do
# Recording needs a captured sandbox order; PAYPAL_CAPTURED_ORDER_ID names it.
paypal_order_id = ENV.fetch('PAYPAL_CAPTURED_ORDER_ID', order_id)
diff --git a/spree/providers/paypal/spec/presenters/spree_paypal_checkout/order_presenter_spec.rb b/spree/providers/paypal/spec/presenters/spree_paypal_checkout/order_presenter_spec.rb
index 3d68eae280..e2cdd22882 100644
--- a/spree/providers/paypal/spec/presenters/spree_paypal_checkout/order_presenter_spec.rb
+++ b/spree/providers/paypal/spec/presenters/spree_paypal_checkout/order_presenter_spec.rb
@@ -156,6 +156,17 @@ RSpec.describe SpreePaypalCheckout::OrderPresenter do
end
end
+ context 'with a SKU longer than PayPal takes' do
+ before { cart.line_items.first.variant.update_columns(sku: 'S' * 128) }
+
+ it 'leaves that SKU out' do
+ skus = purchase_unit['items'].map { |item| item['sku'] }
+ expect(skus).not_to include('S' * 128)
+ expect(purchase_unit['items'].size).to eq(cart.line_items.size)
+ expect(purchase_unit['items'].count { |item| !item.key?('sku') }).to eq(1)
+ end
+ end
+
context 'with a JPY amount with a fraction' do
let(:amount) { BigDecimal('100.5') }