spree-paypal_merchant 6.0.5 → 6.1.0
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/CHANGELOG.md +37 -0
- data/README.md +39 -1
- data/app/models/spree/payment_sessions/paypal_merchant.rb +14 -3
- data/app/models/spree/paypal_merchant/gateway/payment_sessions.rb +155 -19
- data/app/models/spree/paypal_merchant/gateway.rb +12 -1
- data/app/presenters/spree/paypal_merchant/order_presenter.rb +128 -20
- data/app/services/spree/paypal_merchant/apply_express_details.rb +223 -0
- data/lib/spree/paypal_merchant/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ccb57ced4d77a5cb45ee2182c866a99b7f107a147e5ad05b05f6cfc6bf482f55
|
|
4
|
+
data.tar.gz: 639c780e14b5a76aed6da1758a3c3c2af93f4e4e274580494590876983513c28
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 79b9068b949b395b034eb92fbc8114bf9779a9a7bdfeec657d7a2b5d22e56e60dfc5a2a5dfae83515b0949b0d8422a5a612307b65ae056da7dcc19f20f195464
|
|
7
|
+
data.tar.gz: 774433fed669448379bfb363fcf6416d45f0869c671312b23698acf9e41a9daff217fca7200e1d2fc43097b55ff4c54c2261e29aa6fbc1ef387b7908ff843586
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,42 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 6.1.0
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- Express checkout. `create_payment_session` with
|
|
8
|
+
`external_data: { express: true }` creates a PayPal order that collects the
|
|
9
|
+
shipping address (`GET_FROM_FILE`), with shipping options built from the
|
|
10
|
+
cart's delivery rates (the option id is the delivery method's prefixed id).
|
|
11
|
+
Gift card and store credit count in the breakdown's discount; unpriced rates
|
|
12
|
+
are left out; if the breakdown can't balance, the order carries the amount
|
|
13
|
+
alone with no options. PayPal takes at most 10 options, so a cart with more
|
|
14
|
+
rates offers the selected rate and the first 9 others, cheapest first.
|
|
15
|
+
- `complete_payment_session` on an uncaptured express order writes PayPal's
|
|
16
|
+
shipping name and address (as ship and bill address), the payer's email (if
|
|
17
|
+
the cart has none) and phone onto the cart, and re-selects the delivery the
|
|
18
|
+
buyer chose in PayPal (or, when PayPal offered no choice, the delivery the
|
|
19
|
+
cart had, if the address is still offered it), atomically under the cart's
|
|
20
|
+
row lock and skipped when already applied. The amount check and capture
|
|
21
|
+
follow. A refusal fails the session with nothing captured.
|
|
22
|
+
|
|
23
|
+
If a store requires a phone number on addresses and the buyer's PayPal account
|
|
24
|
+
has none, express completion fails after approval. Such stores should not
|
|
25
|
+
require a phone number, or should keep express checkout off.
|
|
26
|
+
- The `client_id` preference is now public, so storefronts receive it with the
|
|
27
|
+
payment method.
|
|
28
|
+
- A new `enable_express_checkout` preference (default on). It is exposed with
|
|
29
|
+
the public preferences so a storefront can hide express checkout; the gem does
|
|
30
|
+
not enforce it.
|
|
31
|
+
|
|
32
|
+
### Changed
|
|
33
|
+
|
|
34
|
+
- `update_payment_session` accepts an update on a pending express session: it
|
|
35
|
+
PATCHes the PayPal order from the cart (amount replaced, shipping options
|
|
36
|
+
added). It refuses a session that isn't pending, a cart with no delivery, and
|
|
37
|
+
an amount the cart doesn't owe. Non-express sessions still refuse amount
|
|
38
|
+
changes.
|
|
39
|
+
|
|
3
40
|
## 6.0.5
|
|
4
41
|
|
|
5
42
|
### Fixed
|
data/README.md
CHANGED
|
@@ -67,11 +67,12 @@ the webhook address and events, and testing in the sandbox. The card's
|
|
|
67
67
|
|
|
68
68
|
| Preference | Purpose |
|
|
69
69
|
|---|---|
|
|
70
|
-
| `client_id` / `client_secret` | REST app credentials from the PayPal Developer Dashboard |
|
|
70
|
+
| `client_id` / `client_secret` | REST app credentials from the PayPal Developer Dashboard. `client_id` is public and sent to storefronts with the payment method; `client_secret` never is |
|
|
71
71
|
| `webhook_secret` | PayPal webhook ID (used as the verify-webhook `webhook_id`) |
|
|
72
72
|
| `test_mode` | Sandbox vs live |
|
|
73
73
|
| `enable_apple_pay` | Offer Apple Pay on the storefront (default on) |
|
|
74
74
|
| `enable_card_fields` | Offer Card Fields on the storefront (default on) |
|
|
75
|
+
| `enable_express_checkout` | Offer PayPal express checkout (PayPal's button on the cart) on the storefront (default on). A storefront switch only: the gem exposes it but does not enforce it |
|
|
75
76
|
|
|
76
77
|
Webhook URL is the standard Spree v3 payments webhook. PayPal only accepts an
|
|
77
78
|
absolute `https://` URL:
|
|
@@ -172,6 +173,43 @@ bin/rails 'spree:paypal_merchant:sync_refunds[R123456789]'
|
|
|
172
173
|
`Spree::PaypalMerchant::PaymentSources::ApplePay`.
|
|
173
174
|
4. Safari + HTTPS only.
|
|
174
175
|
|
|
176
|
+
## Express checkout
|
|
177
|
+
|
|
178
|
+
Express checkout lets a buyer pay from the cart: PayPal collects the shipping
|
|
179
|
+
address and delivery choice, and the gem copies them onto the cart.
|
|
180
|
+
|
|
181
|
+
- **Create.** `create_payment_session` with `external_data: { express: true }`
|
|
182
|
+
creates a PayPal order with `shipping_preference: GET_FROM_FILE`, so PayPal
|
|
183
|
+
asks for the address. Shipping options are built from the cart's delivery
|
|
184
|
+
rates; each option's id is the Spree delivery method's prefixed id. Gift card
|
|
185
|
+
and store credit amounts count as a discount in PayPal's breakdown. Rates
|
|
186
|
+
without a price are left out. PayPal takes at most 10 options, so a cart with
|
|
187
|
+
more rates offers the selected rate and the first 9 others, cheapest first.
|
|
188
|
+
If the breakdown can't balance, the order is created with the amount alone
|
|
189
|
+
and no shipping options.
|
|
190
|
+
- **Update.** `update_payment_session` on a pending express session PATCHes the
|
|
191
|
+
PayPal order from the cart: the amount is replaced and the shipping options
|
|
192
|
+
are added. It refuses a session that isn't pending, a cart with no delivery,
|
|
193
|
+
and an amount the cart doesn't owe. Sessions that aren't express still refuse
|
|
194
|
+
amount changes.
|
|
195
|
+
- **Complete.** `complete_payment_session` on an express order PayPal hasn't
|
|
196
|
+
captured writes PayPal's shipping name and address (as both ship and bill
|
|
197
|
+
address), the payer's email (only if the cart has none) and phone onto the
|
|
198
|
+
cart, and selects the delivery the buyer chose in PayPal. When PayPal offered
|
|
199
|
+
no choice, the delivery the cart had is selected again, if the address is
|
|
200
|
+
still offered it; otherwise Spree's default stays and the amount check
|
|
201
|
+
decides. This happens atomically under the cart's row lock and is skipped if
|
|
202
|
+
already applied. The existing amount check and the capture follow. If any
|
|
203
|
+
step refuses, the session fails and nothing is captured.
|
|
204
|
+
- **Switch.** `enable_express_checkout` (default on) is exposed with the other
|
|
205
|
+
public preferences so a storefront can hide the button. The gem does not
|
|
206
|
+
enforce it.
|
|
207
|
+
|
|
208
|
+
If your store requires a phone number on addresses and the buyer's PayPal
|
|
209
|
+
account has none, express completion fails after the buyer has approved the
|
|
210
|
+
payment. Such stores should not require a phone number, or should keep express
|
|
211
|
+
checkout off.
|
|
212
|
+
|
|
175
213
|
## Upgrading from 5.2
|
|
176
214
|
|
|
177
215
|
Bump the gem to `~> 6.0` as part of your Spree 6 upgrade.
|
|
@@ -61,6 +61,17 @@ module Spree
|
|
|
61
61
|
SETTLED_CAPTURE_STATUSES.include?(paypal_capture_status)
|
|
62
62
|
end
|
|
63
63
|
|
|
64
|
+
##
|
|
65
|
+
# Whether the session is for express checkout: the buyer picks the
|
|
66
|
+
# address and the delivery in PayPal's popup. Set when the session is
|
|
67
|
+
# created, and only by the boolean +true+.
|
|
68
|
+
#
|
|
69
|
+
# @return [TrueClass, FalseClass]
|
|
70
|
+
#
|
|
71
|
+
def express?
|
|
72
|
+
external_data&.dig('express') == true
|
|
73
|
+
end
|
|
74
|
+
|
|
64
75
|
##
|
|
65
76
|
# @return [Hash, NilClass]
|
|
66
77
|
#
|
|
@@ -94,9 +105,9 @@ module Spree
|
|
|
94
105
|
|
|
95
106
|
##
|
|
96
107
|
# Stores a PayPal order read from PayPal on the session. The order's own
|
|
97
|
-
# fields replace the stored ones; the keys the
|
|
98
|
-
# (+client_token+, +enable_apple_pay+, +enable_card_fields
|
|
99
|
-
# the session can still be open when it is stored.
|
|
108
|
+
# fields replace the stored ones; the keys the gem put there for the
|
|
109
|
+
# storefront (+client_token+, +enable_apple_pay+, +enable_card_fields+,
|
|
110
|
+
# +express+) stay, because the session can still be open when it is stored.
|
|
100
111
|
#
|
|
101
112
|
# @param paypal_order [Hash] a PayPal order as JSON
|
|
102
113
|
# @return [void]
|
|
@@ -59,6 +59,19 @@ module Spree
|
|
|
59
59
|
#
|
|
60
60
|
REFUND_EVENT = 'PAYMENT.CAPTURE.REFUNDED'
|
|
61
61
|
|
|
62
|
+
##
|
|
63
|
+
# Why a session's amount is refused: it is not what is due.
|
|
64
|
+
#
|
|
65
|
+
AMOUNT_CHANGE_MESSAGE = 'The amount of a PayPal payment session cannot change; create a new payment session for the new amount'
|
|
66
|
+
|
|
67
|
+
##
|
|
68
|
+
# Where an express order's update writes, in PayPal's JSON Patch
|
|
69
|
+
# paths. The gem's orders have one purchase unit, which PayPal names
|
|
70
|
+
# +default+.
|
|
71
|
+
#
|
|
72
|
+
EXPRESS_AMOUNT_PATH = "/purchase_units/@reference_id=='default'/amount"
|
|
73
|
+
EXPRESS_OPTIONS_PATH = "/purchase_units/@reference_id=='default'/shipping/options"
|
|
74
|
+
|
|
62
75
|
##
|
|
63
76
|
# @return [TrueClass, FalseClass]
|
|
64
77
|
#
|
|
@@ -82,16 +95,25 @@ module Spree
|
|
|
82
95
|
# Also requests a Card Fields client token. Failure is non-fatal —
|
|
83
96
|
# the wallet / Apple Pay buttons still work without it.
|
|
84
97
|
#
|
|
98
|
+
# An express session (+external_data+ +express+ is +true+) creates an
|
|
99
|
+
# order the buyer completes in PayPal's popup: PayPal collects the
|
|
100
|
+
# address, and the cart's delivery rates are its shipping options. The
|
|
101
|
+
# session keeps +express: true+ ({Spree::PaymentSessions::PaypalMerchant#express?}).
|
|
102
|
+
# Nothing else the client sends is stored.
|
|
103
|
+
#
|
|
85
104
|
# @param order [Spree::Cart, Spree::Order] the session's owner; a cart during checkout
|
|
86
105
|
# @param amount [Numeric, NilClass] defaults to the owner's total less store credits
|
|
106
|
+
# @param external_data [Hash, ActionController::Parameters] read for +express+ only,
|
|
107
|
+
# under a string or symbol key
|
|
87
108
|
# @return [Spree::PaymentSessions::PaypalMerchant]
|
|
88
109
|
# @raise [Spree::Core::GatewayError] when nothing is due, this method is
|
|
89
110
|
# set to capture later, the owner has already paid with PayPal, or
|
|
90
111
|
# PayPal fails
|
|
91
112
|
#
|
|
92
|
-
def create_payment_session(order:, amount: nil, **_unused)
|
|
113
|
+
def create_payment_session(order:, amount: nil, external_data: {}, **_unused)
|
|
93
114
|
owner = order
|
|
94
115
|
total = amount.presence || owner.total_minus_store_credits
|
|
116
|
+
express = express_requested?(external_data)
|
|
95
117
|
|
|
96
118
|
raise Spree::Core::GatewayError, 'PayPal cannot take a payment of zero' unless total.to_d.positive?
|
|
97
119
|
|
|
@@ -100,7 +122,7 @@ module Spree
|
|
|
100
122
|
protect_from_error do
|
|
101
123
|
retire_open_sessions(owner)
|
|
102
124
|
|
|
103
|
-
order_presenter = OrderPresenter.new(owner, amount: total)
|
|
125
|
+
order_presenter = OrderPresenter.new(owner, amount: total, express: express)
|
|
104
126
|
paypal_response = client.orders.create_order(order_presenter.to_json)
|
|
105
127
|
|
|
106
128
|
session_data = paypal_response.data.as_json
|
|
@@ -112,6 +134,7 @@ module Spree
|
|
|
112
134
|
session_data['client_id'] = preferred_client_id
|
|
113
135
|
session_data['enable_apple_pay'] = apple_pay_enabled?
|
|
114
136
|
session_data['enable_card_fields'] = card_fields_enabled?
|
|
137
|
+
session_data['express'] = true if express
|
|
115
138
|
|
|
116
139
|
if card_fields_enabled?
|
|
117
140
|
client_token = generate_client_token
|
|
@@ -132,30 +155,45 @@ module Spree
|
|
|
132
155
|
end
|
|
133
156
|
|
|
134
157
|
##
|
|
135
|
-
#
|
|
136
|
-
# exists.
|
|
137
|
-
#
|
|
138
|
-
#
|
|
139
|
-
#
|
|
140
|
-
#
|
|
158
|
+
# Brings an express session's PayPal order in step with its cart; any
|
|
159
|
+
# other session is fixed once its PayPal order exists.
|
|
160
|
+
#
|
|
161
|
+
# An express session ({Spree::PaymentSessions::PaypalMerchant#express?})
|
|
162
|
+
# follows the cart while the buyer changes the address or the delivery
|
|
163
|
+
# in PayPal's popup. The update is built from the cart, never from
|
|
164
|
+
# the client: PayPal's order gets the amount due, with the breakdown
|
|
165
|
+
# {OrderPresenter#purchase_amount} sends, and the cart's delivery rates
|
|
166
|
+
# as its shipping options ({OrderPresenter#shipping_options}), when it
|
|
167
|
+
# can offer them. The session's amount becomes the amount due. A
|
|
168
|
+
# session that is no longer pending, or a cart with no delivery rate
|
|
169
|
+
# for its address, is refused before PayPal is asked.
|
|
170
|
+
#
|
|
171
|
+
# Any other session changes nothing. Its amount is the PayPal order's,
|
|
172
|
+
# and the payment records it, so a different amount is refused: the
|
|
173
|
+
# storefront creates a new session for the new amount.
|
|
174
|
+
#
|
|
175
|
+
# An +amount+, when sent, must be what is due (an express session's
|
|
176
|
+
# cart, or any other session's own amount): it is checked, never
|
|
177
|
+
# applied.
|
|
141
178
|
#
|
|
142
179
|
# +external_data+ is ignored. The gem writes every key the storefront
|
|
143
180
|
# reads (+id+, +status+, +client_id+, +client_token+,
|
|
144
|
-
# +enable_apple_pay+, +enable_card_fields+), and settlement reads
|
|
181
|
+
# +enable_apple_pay+, +enable_card_fields+, +express+), and settlement reads
|
|
145
182
|
# PayPal's own order (+purchase_units+, +payment_source+), so no key
|
|
146
183
|
# is the client's to write.
|
|
147
184
|
#
|
|
148
185
|
# @param payment_session [Spree::PaymentSessions::PaypalMerchant]
|
|
149
186
|
# @param amount [Numeric, String, NilClass]
|
|
150
187
|
# @return [NilClass]
|
|
151
|
-
# @raise [Spree::Core::GatewayError] when the amount
|
|
152
|
-
# session
|
|
188
|
+
# @raise [Spree::Core::GatewayError] when the amount is not what is
|
|
189
|
+
# due, an express session is no longer pending or its cart has no
|
|
190
|
+
# delivery for its address, or PayPal fails
|
|
153
191
|
#
|
|
154
192
|
def update_payment_session(payment_session:, amount: nil, **_external_data)
|
|
193
|
+
return update_express_order(payment_session, amount) if payment_session.express?
|
|
155
194
|
return if amount.blank? || amount.to_d == payment_session.amount.to_d
|
|
156
195
|
|
|
157
|
-
raise Spree::Core::GatewayError,
|
|
158
|
-
'The amount of a PayPal payment session cannot change; create a new payment session for the new amount'
|
|
196
|
+
raise Spree::Core::GatewayError, AMOUNT_CHANGE_MESSAGE
|
|
159
197
|
end
|
|
160
198
|
|
|
161
199
|
##
|
|
@@ -171,9 +209,22 @@ module Spree
|
|
|
171
209
|
#
|
|
172
210
|
# Does not complete the cart — that is handled by Carts::Complete.
|
|
173
211
|
#
|
|
174
|
-
#
|
|
175
|
-
#
|
|
176
|
-
#
|
|
212
|
+
# An express session ({Spree::PaymentSessions::PaypalMerchant#express?})
|
|
213
|
+
# whose PayPal order has no capture yet first gets what the buyer gave
|
|
214
|
+
# PayPal written onto its cart ({ApplyExpressDetails}): the address,
|
|
215
|
+
# the email when the cart has none, and the delivery chosen in
|
|
216
|
+
# PayPal's popup. The amount check then compares PayPal's amount with
|
|
217
|
+
# what that cart owes. When the details cannot be written (Spree
|
|
218
|
+
# refuses the address, or the chosen delivery is not offered for it),
|
|
219
|
+
# the cart is left as it was, the session fails and nothing is
|
|
220
|
+
# captured. A second confirm of the same PayPal order finds the
|
|
221
|
+
# details written and changes nothing. A PayPal order that already
|
|
222
|
+
# has a capture is settled as it is, with nothing written.
|
|
223
|
+
#
|
|
224
|
+
# Only a refusal fails the session: PayPal's, a changed amount, or
|
|
225
|
+
# express details that cannot be written. When the outcome is unknown
|
|
226
|
+
# (no answer, a server error, an answer that cannot be read, a rate
|
|
227
|
+
# limit), PayPal may have captured anyway, so the session stays
|
|
177
228
|
# pending: a retry reads the order first and settles a capture that
|
|
178
229
|
# went through.
|
|
179
230
|
#
|
|
@@ -181,14 +232,16 @@ module Spree
|
|
|
181
232
|
# @param params [Hash] not used
|
|
182
233
|
# @return [Spree::PaymentSessions::PaypalMerchant]
|
|
183
234
|
# @raise [Spree::Core::GatewayError] when the session can no longer be
|
|
184
|
-
# used,
|
|
185
|
-
# is unknown
|
|
235
|
+
# used, an express session's details cannot be written, the amount
|
|
236
|
+
# due has changed, PayPal refuses, or the outcome is unknown
|
|
186
237
|
#
|
|
187
238
|
def complete_payment_session(payment_session:, **_unused)
|
|
188
239
|
ensure_session_completable!(payment_session)
|
|
189
240
|
|
|
190
241
|
response = fetch_paypal_order(payment_session.external_id)
|
|
191
|
-
|
|
242
|
+
paypal_order = response.data.as_json
|
|
243
|
+
if payment_session_class.paypal_capture_in(paypal_order).blank?
|
|
244
|
+
apply_express_details!(payment_session, paypal_order) if payment_session.express?
|
|
192
245
|
ensure_amount_matches!(payment_session, response)
|
|
193
246
|
response = capture_paypal_order(payment_session.external_id)
|
|
194
247
|
end
|
|
@@ -387,6 +440,70 @@ module Spree
|
|
|
387
440
|
"PayPal captures at checkout; capture method #{capture_method} is not supported"
|
|
388
441
|
end
|
|
389
442
|
|
|
443
|
+
##
|
|
444
|
+
# Whether the storefront asked for an express session: +express+ is
|
|
445
|
+
# the boolean +true+. The Store API passes JSON, so the key is a
|
|
446
|
+
# string; a Ruby caller may use a symbol.
|
|
447
|
+
#
|
|
448
|
+
# @param external_data [Hash, ActionController::Parameters, NilClass]
|
|
449
|
+
# @return [TrueClass, FalseClass]
|
|
450
|
+
#
|
|
451
|
+
def express_requested?(external_data)
|
|
452
|
+
return false if external_data.blank?
|
|
453
|
+
|
|
454
|
+
(external_data['express'] || external_data[:express]) == true
|
|
455
|
+
end
|
|
456
|
+
|
|
457
|
+
##
|
|
458
|
+
# PATCHes an express session's PayPal order to match its cart, then
|
|
459
|
+
# sets the session's amount to what is due ({#update_payment_session}).
|
|
460
|
+
#
|
|
461
|
+
# Shipping options are sent only when the presenter offers some (it
|
|
462
|
+
# offers none when the breakdown cannot carry them); otherwise the
|
|
463
|
+
# amount is sent alone, and it includes the cart's own delivery.
|
|
464
|
+
#
|
|
465
|
+
# The amount is a +replace+: every order has one. The options are an
|
|
466
|
+
# +add+, because an order created before the cart had an address has
|
|
467
|
+
# no options yet, and JSON Patch's +replace+ needs the member to
|
|
468
|
+
# exist. An +add+ sets an object member whether or not it exists, and
|
|
469
|
+
# PayPal accepts both operations on +shipping.options+.
|
|
470
|
+
#
|
|
471
|
+
# @param payment_session [Spree::PaymentSessions::PaypalMerchant] an express session
|
|
472
|
+
# @param amount [Numeric, String, NilClass] checked against what is due
|
|
473
|
+
# @return [NilClass]
|
|
474
|
+
# @raise [Spree::Core::GatewayError]
|
|
475
|
+
#
|
|
476
|
+
def update_express_order(payment_session, amount)
|
|
477
|
+
raise Spree::Core::GatewayError, 'This PayPal payment session can no longer change' unless payment_session.pending?
|
|
478
|
+
|
|
479
|
+
owner = payment_session.owner
|
|
480
|
+
raise Spree::Core::GatewayError, 'PayPal cannot be offered a delivery for this address' unless owner.fulfillments.joins(:delivery_rates).exists?
|
|
481
|
+
|
|
482
|
+
due = owner.total_minus_store_credits
|
|
483
|
+
raise Spree::Core::GatewayError, AMOUNT_CHANGE_MESSAGE if amount.present? && amount.to_d != due.to_d
|
|
484
|
+
|
|
485
|
+
order_presenter = OrderPresenter.new(owner, amount: due, express: true)
|
|
486
|
+
operations = [paypal_patch(PaypalServerSdk::PatchOp::REPLACE, EXPRESS_AMOUNT_PATH, order_presenter.purchase_amount)]
|
|
487
|
+
if order_presenter.shipping_options.any?
|
|
488
|
+
operations << paypal_patch(PaypalServerSdk::PatchOp::ADD, EXPRESS_OPTIONS_PATH, order_presenter.shipping_options)
|
|
489
|
+
end
|
|
490
|
+
|
|
491
|
+
protect_from_error { client.orders.patch_order('id' => payment_session.external_id, 'body' => operations) }
|
|
492
|
+
payment_session.update!(amount: due)
|
|
493
|
+
|
|
494
|
+
nil
|
|
495
|
+
end
|
|
496
|
+
|
|
497
|
+
##
|
|
498
|
+
# @param operation [String] a {PaypalServerSdk::PatchOp}
|
|
499
|
+
# @param path [String] a JSON Patch path in a PayPal order
|
|
500
|
+
# @param value [Object] what PayPal writes there
|
|
501
|
+
# @return [PaypalServerSdk::Patch]
|
|
502
|
+
#
|
|
503
|
+
def paypal_patch(operation, path, value)
|
|
504
|
+
PaypalServerSdk::Patch.new(op: operation, path: path, value: value)
|
|
505
|
+
end
|
|
506
|
+
|
|
390
507
|
##
|
|
391
508
|
# Cancels the owner's earlier PayPal sessions before a new one opens.
|
|
392
509
|
# A completed session means the owner already has its payment, so
|
|
@@ -460,6 +577,25 @@ module Spree
|
|
|
460
577
|
payment_session.complete unless payment_session.completed?
|
|
461
578
|
end
|
|
462
579
|
|
|
580
|
+
##
|
|
581
|
+
# Writes the details the buyer gave PayPal onto an express session's
|
|
582
|
+
# cart ({ApplyExpressDetails}), then reads the cart again for the
|
|
583
|
+
# amount check. Anything that stops the write fails the session before
|
|
584
|
+
# PayPal is asked to capture.
|
|
585
|
+
#
|
|
586
|
+
# @param payment_session [Spree::PaymentSessions::PaypalMerchant] an express session
|
|
587
|
+
# @param paypal_order [Hash] its approved PayPal order, as JSON
|
|
588
|
+
# @raise [Spree::Core::GatewayError] when Spree refuses the address, or
|
|
589
|
+
# the delivery chosen in PayPal is not offered for it
|
|
590
|
+
#
|
|
591
|
+
def apply_express_details!(payment_session, paypal_order)
|
|
592
|
+
ApplyExpressDetails.new(owner: payment_session.owner, paypal_order: paypal_order).call
|
|
593
|
+
payment_session.owner.reload
|
|
594
|
+
rescue StandardError
|
|
595
|
+
payment_session.fail if payment_session.can_fail?
|
|
596
|
+
raise
|
|
597
|
+
end
|
|
598
|
+
|
|
463
599
|
##
|
|
464
600
|
# Refuses to capture a PayPal order whose amount no longer matches what
|
|
465
601
|
# is due — e.g. a gift card was applied, or the cart changed, after the
|
|
@@ -110,6 +110,7 @@ module Spree
|
|
|
110
110
|
preference :test_mode, :boolean, default: true
|
|
111
111
|
preference :enable_apple_pay, :boolean, default: true
|
|
112
112
|
preference :enable_card_fields, :boolean, default: true
|
|
113
|
+
preference :enable_express_checkout, :boolean, default: true
|
|
113
114
|
|
|
114
115
|
validates :preferred_client_id, :preferred_client_secret, presence: true
|
|
115
116
|
|
|
@@ -223,6 +224,16 @@ module Spree
|
|
|
223
224
|
preferred_enable_card_fields
|
|
224
225
|
end
|
|
225
226
|
|
|
227
|
+
##
|
|
228
|
+
# Whether the storefront should offer express checkout (PayPal and Apple
|
|
229
|
+
# Pay buttons ahead of the checkout form) for this method.
|
|
230
|
+
#
|
|
231
|
+
# @return [TrueClass, FalseClass]
|
|
232
|
+
#
|
|
233
|
+
def express_checkout_enabled?
|
|
234
|
+
preferred_enable_express_checkout
|
|
235
|
+
end
|
|
236
|
+
|
|
226
237
|
##
|
|
227
238
|
# PayPal captures at checkout, whatever the store's default. Spree reads
|
|
228
239
|
# this to decide whether a pending payment is captured on dispatch or
|
|
@@ -476,7 +487,7 @@ module Spree
|
|
|
476
487
|
protected
|
|
477
488
|
|
|
478
489
|
def public_preference_keys
|
|
479
|
-
%i[enable_apple_pay enable_card_fields]
|
|
490
|
+
%i[enable_apple_pay enable_card_fields client_id enable_express_checkout]
|
|
480
491
|
end
|
|
481
492
|
|
|
482
493
|
private
|
|
@@ -14,12 +14,19 @@ module Spree
|
|
|
14
14
|
# `additional_tax_total` — sending full `tax_total` double-counts
|
|
15
15
|
# included VAT and trips PayPal `AMOUNT_MISMATCH`.
|
|
16
16
|
#
|
|
17
|
+
# An express order is the exception: the buyer picks the address and the
|
|
18
|
+
# delivery in PayPal's popup. It asks PayPal to collect the address
|
|
19
|
+
# (`GET_FROM_FILE`), sends none of its own, and offers the cart's delivery
|
|
20
|
+
# rates as PayPal shipping options ({#shipping_options}).
|
|
21
|
+
#
|
|
17
22
|
# Every amount is written the way PayPal reads it: exactly the currency's
|
|
18
23
|
# decimal places (`Spree::Money::Rounding.format`), so a yen amount has
|
|
19
24
|
# none and a dinar amount has three.
|
|
20
25
|
#
|
|
21
26
|
class OrderPresenter
|
|
22
27
|
PAYPAL_ITEM_NAME_MAX_LENGTH = 127
|
|
28
|
+
PAYPAL_SHIPPING_LABEL_MAX_LENGTH = 127
|
|
29
|
+
PAYPAL_SHIPPING_OPTIONS_MAX = 10
|
|
23
30
|
BREAKDOWN_FIELDS = %i[item_total shipping handling tax_total shipping_discount discount].freeze
|
|
24
31
|
|
|
25
32
|
##
|
|
@@ -28,10 +35,13 @@ module Spree
|
|
|
28
35
|
# @param amount [Numeric, NilClass] what PayPal should charge; defaults to
|
|
29
36
|
# the owner's total. Less than the total when a gift card or store credit
|
|
30
37
|
# pays part of it.
|
|
38
|
+
# @param express [Boolean] an express order: the buyer picks the address
|
|
39
|
+
# and the delivery in PayPal rather than in the storefront
|
|
31
40
|
#
|
|
32
|
-
def initialize(owner, amount: nil)
|
|
41
|
+
def initialize(owner, amount: nil, express: false)
|
|
33
42
|
@owner = owner
|
|
34
43
|
@amount = amount
|
|
44
|
+
@express = express
|
|
35
45
|
end
|
|
36
46
|
|
|
37
47
|
attr_reader :owner
|
|
@@ -49,20 +59,59 @@ module Spree
|
|
|
49
59
|
}
|
|
50
60
|
end
|
|
51
61
|
|
|
62
|
+
##
|
|
63
|
+
# What PayPal charges, with the breakdown it checks the charge against
|
|
64
|
+
# when one can be sent (see {#purchase_unit}). An update of an express
|
|
65
|
+
# order's amount sends the same.
|
|
66
|
+
#
|
|
67
|
+
# @return [PaypalServerSdk::AmountWithBreakdown]
|
|
68
|
+
#
|
|
69
|
+
def purchase_amount
|
|
70
|
+
value = format_amount(charge_amount)
|
|
71
|
+
return PaypalServerSdk::AmountWithBreakdown.new(currency_code: currency_code, value: value) if amount_alone?
|
|
72
|
+
|
|
73
|
+
PaypalServerSdk::AmountWithBreakdown.new(currency_code: currency_code, value: value, breakdown: breakdown)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
##
|
|
77
|
+
# The delivery choices an express order offers in PayPal's popup: the
|
|
78
|
+
# priced rates of the cart's one fulfillment, the selected rate
|
|
79
|
+
# selected. Each is keyed on its delivery method's prefixed id, which
|
|
80
|
+
# outlives the rate: Spree recreates rates whenever it rebuilds the
|
|
81
|
+
# cart's delivery. A rate with no price yet is left out. PayPal takes
|
|
82
|
+
# at most 10 options and refuses an order with more, so a cart with
|
|
83
|
+
# more rates offers the selected one and the first 9 others, in rate
|
|
84
|
+
# order (cheapest first).
|
|
85
|
+
#
|
|
86
|
+
# PayPal checks the selected option against the breakdown's shipping and
|
|
87
|
+
# refuses the order when they differ, so there are none unless the
|
|
88
|
+
# breakdown is sent and the selected rate is what it charges for
|
|
89
|
+
# delivery. There are none either when the cart has no rate selected, its
|
|
90
|
+
# selected rate has no price yet, or it is split across fulfillments
|
|
91
|
+
# (PayPal's options are one list for the order). With none, PayPal offers
|
|
92
|
+
# no choice, and the cart's own delivery is what is charged.
|
|
93
|
+
#
|
|
94
|
+
# @return [Array<PaypalServerSdk::ShippingOption>] always empty unless express
|
|
95
|
+
#
|
|
96
|
+
def shipping_options
|
|
97
|
+
@shipping_options ||= express? ? offered_shipping_options : []
|
|
98
|
+
end
|
|
99
|
+
|
|
52
100
|
private
|
|
53
101
|
|
|
102
|
+
def express?
|
|
103
|
+
@express == true
|
|
104
|
+
end
|
|
105
|
+
|
|
54
106
|
def purchase_unit
|
|
55
107
|
# PayPal accepts an amount on its own. That is all we send when the
|
|
56
108
|
# breakdown and items can't describe the charge: a gift card or store
|
|
57
109
|
# credit pays part of the order (they always sum to the full total), or
|
|
58
110
|
# the breakdown doesn't add up to the amount (PayPal would refuse it).
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
else
|
|
64
|
-
{ amount: amount_with_breakdown, items: items }
|
|
65
|
-
end
|
|
111
|
+
# An express order counts the gift card or store credit as a discount
|
|
112
|
+
# instead ({#credit_discount}), so only the second applies to it.
|
|
113
|
+
args = { amount: purchase_amount }
|
|
114
|
+
args[:items] = items unless amount_alone?
|
|
66
115
|
args[:shipping] = shipping_details if shipping_details
|
|
67
116
|
|
|
68
117
|
PaypalServerSdk::PurchaseUnitRequest.new(**args)
|
|
@@ -81,15 +130,7 @@ module Spree
|
|
|
81
130
|
end
|
|
82
131
|
|
|
83
132
|
def amount_alone?
|
|
84
|
-
partial_charge? || !breakdown_balances?
|
|
85
|
-
end
|
|
86
|
-
|
|
87
|
-
def amount_with_breakdown
|
|
88
|
-
PaypalServerSdk::AmountWithBreakdown.new(
|
|
89
|
-
currency_code: currency_code,
|
|
90
|
-
value: format_amount(owner.total),
|
|
91
|
-
breakdown: breakdown
|
|
92
|
-
)
|
|
133
|
+
(partial_charge? && !express?) || !breakdown_balances?
|
|
93
134
|
end
|
|
94
135
|
|
|
95
136
|
# NOTE: the breakdown only sends additional (non-included) tax — sending
|
|
@@ -102,7 +143,7 @@ module Spree
|
|
|
102
143
|
handling: handling,
|
|
103
144
|
tax_total: paypal_money(owner.additional_tax_total),
|
|
104
145
|
shipping_discount: shipping_discount,
|
|
105
|
-
discount: paypal_money(line_item_discount_rows_total)
|
|
146
|
+
discount: paypal_money(line_item_discount_rows_total + credit_discount)
|
|
106
147
|
)
|
|
107
148
|
end
|
|
108
149
|
|
|
@@ -111,12 +152,26 @@ module Spree
|
|
|
111
152
|
# refuses the order when they differ; it also refuses a negative figure.
|
|
112
153
|
# Spree's own totals add up, so this only fails when a total is out of
|
|
113
154
|
# step with the rest.
|
|
155
|
+
#
|
|
156
|
+
# PayPal also refuses a discount bigger than the item total, which is why
|
|
157
|
+
# a free-shipping discount goes in `shipping_discount`. An express
|
|
158
|
+
# order's discount carries the gift card or store credit, which can be
|
|
159
|
+
# bigger than the goods, so there it counts as not balancing.
|
|
114
160
|
def breakdown_balances?
|
|
115
161
|
return false if BREAKDOWN_FIELDS.any? { |field| breakdown_value(field).negative? }
|
|
162
|
+
return false if express? && breakdown_value(:discount) > breakdown_value(:item_total)
|
|
116
163
|
|
|
117
164
|
charges = %i[item_total shipping handling tax_total].sum { |field| breakdown_value(field) }
|
|
118
165
|
discounts = %i[shipping_discount discount].sum { |field| breakdown_value(field) }
|
|
119
|
-
charges - discounts == format_amount(
|
|
166
|
+
charges - discounts == format_amount(charge_amount).to_d
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
# What a gift card or store credit pays, which an express order counts as
|
|
170
|
+
# a discount so the breakdown adds up to the part PayPal charges: an
|
|
171
|
+
# express order needs its breakdown, which PayPal checks the selected
|
|
172
|
+
# shipping option against. Any other order sends the amount alone instead.
|
|
173
|
+
def credit_discount
|
|
174
|
+
express? ? owner.total.to_d - charge_amount.to_d : 0
|
|
120
175
|
end
|
|
121
176
|
|
|
122
177
|
def breakdown_value(field)
|
|
@@ -195,6 +250,8 @@ module Spree
|
|
|
195
250
|
end
|
|
196
251
|
|
|
197
252
|
def shipping_details
|
|
253
|
+
return express_shipping_details if express?
|
|
254
|
+
|
|
198
255
|
address = owner.ship_address
|
|
199
256
|
return nil if address.blank?
|
|
200
257
|
|
|
@@ -211,6 +268,55 @@ module Spree
|
|
|
211
268
|
)
|
|
212
269
|
end
|
|
213
270
|
|
|
271
|
+
# An express order sends no address (PayPal collects it), only the
|
|
272
|
+
# delivery choices, when there are any.
|
|
273
|
+
def express_shipping_details
|
|
274
|
+
PaypalServerSdk::ShippingDetails.new(options: shipping_options) if shipping_options.any?
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
def offered_shipping_options
|
|
278
|
+
return [] unless breakdown_balances?
|
|
279
|
+
|
|
280
|
+
rates = delivery_rates
|
|
281
|
+
selected_rate = rates.find(&:selected?)
|
|
282
|
+
return [] unless selected_rate && priced?(selected_rate) && format_amount(selected_rate.cost).to_d == breakdown_value(:shipping)
|
|
283
|
+
|
|
284
|
+
offered_rates(rates.select { |rate| priced?(rate) }, selected_rate).map do |rate|
|
|
285
|
+
shipping_option(rate, selected: rate.equal?(selected_rate))
|
|
286
|
+
end
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
# The selected rate and the first others, as many as PayPal takes, in
|
|
290
|
+
# the order given.
|
|
291
|
+
def offered_rates(rates, selected_rate)
|
|
292
|
+
others = rates.reject { |rate| rate.equal?(selected_rate) }.first(PAYPAL_SHIPPING_OPTIONS_MAX - 1)
|
|
293
|
+
rates.select { |rate| rate.equal?(selected_rate) || others.any? { |other| other.equal?(rate) } }
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
# A rate with no price yet (freight quoted after review, or no cost at
|
|
297
|
+
# all) is left out: PayPal would offer it as free.
|
|
298
|
+
def priced?(rate)
|
|
299
|
+
!rate.cost.nil? && !rate.unpriced?
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
# The rates of the owner's fulfillment; none unless it has exactly one.
|
|
303
|
+
def delivery_rates
|
|
304
|
+
fulfillments = owner.fulfillments.to_a
|
|
305
|
+
return [] unless fulfillments.one?
|
|
306
|
+
|
|
307
|
+
fulfillments.first.delivery_rates.includes(:delivery_method).to_a
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
def shipping_option(rate, selected:)
|
|
311
|
+
PaypalServerSdk::ShippingOption.new(
|
|
312
|
+
id: rate.delivery_method.prefixed_id,
|
|
313
|
+
label: rate.name.to_s[0...PAYPAL_SHIPPING_LABEL_MAX_LENGTH],
|
|
314
|
+
type: PaypalServerSdk::ShippingType::SHIPPING,
|
|
315
|
+
amount: paypal_money(rate.cost),
|
|
316
|
+
selected: selected
|
|
317
|
+
)
|
|
318
|
+
end
|
|
319
|
+
|
|
214
320
|
def region_code(address)
|
|
215
321
|
address.state_code.presence || address.state_name.presence
|
|
216
322
|
end
|
|
@@ -228,7 +334,9 @@ module Spree
|
|
|
228
334
|
end
|
|
229
335
|
|
|
230
336
|
def shipping_preference
|
|
231
|
-
if
|
|
337
|
+
if express?
|
|
338
|
+
PaypalServerSdk::PaypalWalletContextShippingPreference::GET_FROM_FILE
|
|
339
|
+
elsif owner.ship_address.present?
|
|
232
340
|
PaypalServerSdk::PaypalWalletContextShippingPreference::SET_PROVIDED_ADDRESS
|
|
233
341
|
else
|
|
234
342
|
PaypalServerSdk::PaypalWalletContextShippingPreference::NO_SHIPPING
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Spree
|
|
4
|
+
module PaypalMerchant
|
|
5
|
+
##
|
|
6
|
+
# Writes onto an express session's cart what the buyer gave PayPal, before
|
|
7
|
+
# the PayPal order is captured: the name and address PayPal ships to, as
|
|
8
|
+
# the cart's full shipping and billing address; the buyer's email, when
|
|
9
|
+
# the cart has none; and the delivery the buyer chose in PayPal's popup.
|
|
10
|
+
#
|
|
11
|
+
# Everything is read from PayPal's own order, which the gateway fetched,
|
|
12
|
+
# never from the client. The address is written the way the Store API
|
|
13
|
+
# writes one ({Spree.carts_update_service}), so the cart is priced, taxed
|
|
14
|
+
# and quoted delivery again for it. A new destination puts the delivery
|
|
15
|
+
# back to Spree's default, so the buyer's choice is selected again
|
|
16
|
+
# afterwards, by delivery method: an option's id is its delivery method's
|
|
17
|
+
# prefixed id ({OrderPresenter#shipping_options}), while delivery rates
|
|
18
|
+
# are recreated by every quote. A PayPal order with no options offered no
|
|
19
|
+
# choice, and its amount was priced with the cart's own delivery, so that
|
|
20
|
+
# delivery is selected again, where the new address is still offered it.
|
|
21
|
+
# Where it is not, Spree's default stays, and the gateway's amount check
|
|
22
|
+
# decides.
|
|
23
|
+
#
|
|
24
|
+
# The address and the delivery are written together, holding the cart's
|
|
25
|
+
# row lock, or not at all: a refusal rolls the address back, and no other
|
|
26
|
+
# request ever sees PayPal's address with Spree's default delivery. Two
|
|
27
|
+
# confirms of one PayPal order (a double submit, or a retry) take turns,
|
|
28
|
+
# and the second finds the details written and changes nothing.
|
|
29
|
+
#
|
|
30
|
+
# The amount is not checked here: the gateway compares PayPal's amount
|
|
31
|
+
# with what the cart now owes before it captures.
|
|
32
|
+
#
|
|
33
|
+
class ApplyExpressDetails
|
|
34
|
+
##
|
|
35
|
+
# Why the delivery the buyer chose cannot be selected again.
|
|
36
|
+
#
|
|
37
|
+
DELIVERY_UNAVAILABLE_MESSAGE = 'The delivery chosen in PayPal is not available for this address'
|
|
38
|
+
|
|
39
|
+
##
|
|
40
|
+
# @param owner [Spree::Cart] the express session's cart
|
|
41
|
+
# @param paypal_order [Hash] the approved PayPal order, as JSON
|
|
42
|
+
#
|
|
43
|
+
def initialize(owner:, paypal_order:)
|
|
44
|
+
@owner = owner
|
|
45
|
+
@paypal_order = paypal_order
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
##
|
|
49
|
+
# Does nothing when the cart already holds the details.
|
|
50
|
+
#
|
|
51
|
+
# @return [void]
|
|
52
|
+
# @raise [Spree::Core::GatewayError] when Spree refuses the address, or
|
|
53
|
+
# the delivery chosen in PayPal is not offered for it; the cart is
|
|
54
|
+
# left as it was
|
|
55
|
+
#
|
|
56
|
+
def call
|
|
57
|
+
owner.with_lock do
|
|
58
|
+
next if already_written?
|
|
59
|
+
|
|
60
|
+
kept_delivery_method = selected_delivery_method
|
|
61
|
+
write_address
|
|
62
|
+
shipping_options.empty? ? keep_delivery(kept_delivery_method) : select_chosen_delivery
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
private
|
|
67
|
+
|
|
68
|
+
attr_reader :owner, :paypal_order
|
|
69
|
+
|
|
70
|
+
# Whether the cart already holds this PayPal order's details, as a
|
|
71
|
+
# second confirm or a retry finds them: the email, the address (shared
|
|
72
|
+
# by the bill address), and the delivery chosen in PayPal.
|
|
73
|
+
def already_written?
|
|
74
|
+
email.nil? && address_written? && chosen_delivery_selected?
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def address_written?
|
|
78
|
+
address = owner.ship_address
|
|
79
|
+
return false if address.nil? || address.quick_checkout? || owner.bill_address_id != address.id
|
|
80
|
+
|
|
81
|
+
address_key(address.attributes.symbolize_keys) == address_key(shipping_address)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# An address's fields as Spree stores them: the postcode without spaces
|
|
85
|
+
# or dashes, and the region as its code wherever the country has one.
|
|
86
|
+
# The phone counts only when PayPal sends one, as only then is it
|
|
87
|
+
# written.
|
|
88
|
+
def address_key(fields)
|
|
89
|
+
country_code = fields[:country_code].to_s.upcase
|
|
90
|
+
region = fields[:state_code].presence ||
|
|
91
|
+
Spree::IsoData.subdivision_code(country_code, fields[:state_name]) ||
|
|
92
|
+
fields[:state_name].presence
|
|
93
|
+
|
|
94
|
+
[
|
|
95
|
+
*fields.values_at(:first_name, :last_name, :address1, :address2, :city).map(&:presence),
|
|
96
|
+
Spree::Address.normalize_postal_code(fields[:postal_code]),
|
|
97
|
+
country_code,
|
|
98
|
+
region,
|
|
99
|
+
(fields[:phone].presence if paypal_phone.present?)
|
|
100
|
+
]
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def chosen_delivery_selected?
|
|
104
|
+
return true if shipping_options.empty?
|
|
105
|
+
|
|
106
|
+
delivery_method = sole_fulfillment&.selected_delivery_rate&.delivery_method
|
|
107
|
+
delivery_method.present? && delivery_method.prefixed_id == chosen_option_id
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def write_address
|
|
111
|
+
result = Spree.carts_update_service.call(
|
|
112
|
+
cart: owner,
|
|
113
|
+
params: { email: email, shipping_address: shipping_address, use_shipping: true }
|
|
114
|
+
)
|
|
115
|
+
raise Spree::Core::GatewayError, result.error.to_s if result.failure?
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# PayPal's only when the cart has none: one the buyer gave the store wins.
|
|
119
|
+
def email
|
|
120
|
+
payer['email_address'] if owner.email.blank?
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# A full address (+quick_checkout+ off), so Spree checks every field it
|
|
124
|
+
# requires. A part of the name PayPal's shipping name lacks comes from
|
|
125
|
+
# the payer's name.
|
|
126
|
+
def shipping_address
|
|
127
|
+
address = shipping['address'] || {}
|
|
128
|
+
first_name, last_name = shipping.dig('name', 'full_name').to_s.split(' ', 2)
|
|
129
|
+
|
|
130
|
+
attributes = {
|
|
131
|
+
first_name: first_name.presence || payer.dig('name', 'given_name'),
|
|
132
|
+
last_name: last_name.presence || payer.dig('name', 'surname'),
|
|
133
|
+
address1: address['address_line_1'],
|
|
134
|
+
address2: address['address_line_2'],
|
|
135
|
+
city: address['admin_area_2'],
|
|
136
|
+
postal_code: address['postal_code'],
|
|
137
|
+
country_code: address['country_code'],
|
|
138
|
+
state_code: region_code(address['country_code'], address['admin_area_1']),
|
|
139
|
+
state_name: address['admin_area_1'],
|
|
140
|
+
quick_checkout: false
|
|
141
|
+
}
|
|
142
|
+
attributes[:phone] = paypal_phone if paypal_phone.present?
|
|
143
|
+
attributes
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def paypal_phone
|
|
147
|
+
payer.dig('phone', 'phone_number', 'national_number')
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# PayPal sends a region as a code or a name ("California"). Where the
|
|
151
|
+
# country requires one, it becomes the subdivision's code. The name is
|
|
152
|
+
# sent as well, for a country with no subdivisions; Spree drops it once
|
|
153
|
+
# the address has a code. The same split as Spree's own wallet
|
|
154
|
+
# addresses (SpreeStripe's +patch_wallet_address+).
|
|
155
|
+
#
|
|
156
|
+
# @return [String, NilClass]
|
|
157
|
+
def region_code(country_code, region)
|
|
158
|
+
country = Spree::Country.by_iso(country_code) if country_code.present?
|
|
159
|
+
Spree::IsoData.subdivision_code(country.iso, region) if country&.states_required?
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
# The cart's delivery method, read before the address is written.
|
|
163
|
+
def selected_delivery_method
|
|
164
|
+
sole_fulfillment&.selected_delivery_rate&.delivery_method
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def select_chosen_delivery
|
|
168
|
+
raise Spree::Core::GatewayError, DELIVERY_UNAVAILABLE_MESSAGE unless select_delivery(chosen_option_id)
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
# PayPal offered no choice: the delivery the cart had before, if the
|
|
172
|
+
# new address is still offered it.
|
|
173
|
+
def keep_delivery(delivery_method)
|
|
174
|
+
select_delivery(delivery_method.prefixed_id) if delivery_method
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
# Selects the rate of the delivery method an option names, on the cart
|
|
178
|
+
# as the address left it.
|
|
179
|
+
#
|
|
180
|
+
# @return [Spree::DeliveryRate, NilClass] the rate, or nil when the
|
|
181
|
+
# method is not offered
|
|
182
|
+
def select_delivery(option_id)
|
|
183
|
+
owner.reload
|
|
184
|
+
fulfillment = sole_fulfillment
|
|
185
|
+
rate = fulfillment && rate_for_option(fulfillment, option_id)
|
|
186
|
+
return if rate.nil?
|
|
187
|
+
|
|
188
|
+
fulfillment.selected_delivery_rate_id = rate.id
|
|
189
|
+
owner.recalculate_totals!
|
|
190
|
+
rate
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
# PayPal's options are one list for the order, so the gem offers them
|
|
194
|
+
# only for a cart with one fulfillment.
|
|
195
|
+
def sole_fulfillment
|
|
196
|
+
fulfillments = owner.fulfillments.to_a
|
|
197
|
+
fulfillments.first if fulfillments.one?
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
def rate_for_option(fulfillment, option_id)
|
|
201
|
+
return if option_id.blank?
|
|
202
|
+
|
|
203
|
+
fulfillment.delivery_rates.includes(:delivery_method).find { |rate| rate.delivery_method&.prefixed_id == option_id }
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
def chosen_option_id
|
|
207
|
+
shipping_options.find { |option| option['selected'] }&.dig('id')
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
def shipping_options
|
|
211
|
+
Array(shipping['options'])
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
def shipping
|
|
215
|
+
paypal_order.dig('purchase_units', 0, 'shipping') || {}
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
def payer
|
|
219
|
+
paypal_order['payer'] || {}
|
|
220
|
+
end
|
|
221
|
+
end
|
|
222
|
+
end
|
|
223
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: spree-paypal_merchant
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 6.0
|
|
4
|
+
version: 6.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Aypex
|
|
@@ -145,6 +145,7 @@ files:
|
|
|
145
145
|
- app/models/spree/paypal_merchant/store_decorator.rb
|
|
146
146
|
- app/presenters/spree/paypal_merchant/order_presenter.rb
|
|
147
147
|
- app/serializers/spree/api/v3/payment_method_serializer_decorator.rb
|
|
148
|
+
- app/services/spree/paypal_merchant/apply_express_details.rb
|
|
148
149
|
- app/services/spree/paypal_merchant/create_source.rb
|
|
149
150
|
- app/services/spree/paypal_merchant/sync_refunds.rb
|
|
150
151
|
- config/initializers/spree.rb
|