spree_stripe 1.8.2 → 6.0.0.beta1
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 +21 -0
- data/README.md +41 -104
- data/Rakefile +7 -16
- data/app/jobs/spree_stripe/base_job.rb +1 -1
- data/app/jobs/spree_stripe/create_webhook_endpoint_job.rb +17 -2
- data/app/models/spree/payment_sessions/stripe.rb +45 -11
- data/app/models/spree_stripe/gateway/connect.rb +327 -0
- data/app/models/spree_stripe/gateway/payment_sessions.rb +225 -112
- data/app/models/spree_stripe/gateway/payment_setup_sessions.rb +15 -9
- data/app/models/spree_stripe/gateway/webhooks.rb +107 -0
- data/app/models/spree_stripe/gateway.rb +197 -244
- data/app/models/spree_stripe/payment_sources/ideal.rb +1 -1
- data/app/models/spree_stripe/payment_sources/przelewy24.rb +1 -1
- data/app/models/spree_stripe/payout_provider.rb +348 -0
- data/app/presenters/spree_stripe/customer_presenter.rb +7 -6
- data/app/presenters/spree_stripe/statement_descriptor_suffix_presenter.rb +11 -10
- data/app/services/spree_stripe/create_gateway_webhooks.rb +57 -33
- data/app/services/spree_stripe/create_payment.rb +46 -31
- data/app/services/spree_stripe/create_source.rb +22 -22
- data/app/services/spree_stripe/update_customer.rb +5 -4
- data/app/subscribers/spree_stripe/customer_updated_subscriber.rb +18 -0
- data/config/initializers/stripe.rb +1 -13
- data/config/locales/en.yml +4 -1
- data/lib/generators/spree_stripe/install/install_generator.rb +12 -12
- data/lib/spree_stripe/engine.rb +7 -38
- data/lib/spree_stripe/factories.rb +1 -3
- data/lib/spree_stripe/testing_support/factories/gateway_factory.rb +10 -9
- data/lib/spree_stripe/testing_support/factories/payment_session_factory.rb +50 -0
- data/lib/spree_stripe.rb +7 -8
- data/lib/tasks/migrate_webhook_keys.rake +128 -0
- metadata +29 -168
- data/app/assets/config/spree_stripe_manifest.js +0 -4
- data/app/assets/images/payment_icons/banktransfer.svg +0 -3
- data/app/assets/images/payment_icons/link.svg +0 -1
- data/app/assets/images/payment_icons/sepadebit.svg +0 -22
- data/app/controllers/spree_stripe/apple_pay_domain_verification_controller.rb +0 -11
- data/app/controllers/spree_stripe/confirm_payments_controller.rb +0 -52
- data/app/controllers/spree_stripe/store_controller_decorator.rb +0 -11
- data/app/controllers/stripe_event/webhook_controller_decorator.rb +0 -21
- data/app/helpers/spree_stripe/base_helper.rb +0 -42
- data/app/helpers/spree_stripe/checkout_helper_decorator.rb +0 -17
- data/app/javascript/spree_stripe/application.js +0 -18
- data/app/javascript/spree_stripe/controllers/stripe_button_controller.js +0 -431
- data/app/javascript/spree_stripe/controllers/stripe_controller.js +0 -234
- data/app/jobs/spree_stripe/attach_customer_to_credit_card_job.rb +0 -18
- data/app/jobs/spree_stripe/complete_order_from_session_job.rb +0 -12
- data/app/jobs/spree_stripe/create_tax_transaction_job.rb +0 -10
- data/app/jobs/spree_stripe/register_domain_job.rb +0 -20
- data/app/jobs/spree_stripe/update_customer_job.rb +0 -10
- data/app/models/spree_stripe/base.rb +0 -6
- data/app/models/spree_stripe/calculators/stripe_tax.rb +0 -96
- data/app/models/spree_stripe/credit_card_decorator.rb +0 -49
- data/app/models/spree_stripe/custom_domain_decorator.rb +0 -20
- data/app/models/spree_stripe/gateway/payment_intents.rb +0 -149
- data/app/models/spree_stripe/gateway_customer_decorator.rb +0 -9
- data/app/models/spree_stripe/order_decorator.rb +0 -9
- data/app/models/spree_stripe/payment_decorator.rb +0 -46
- data/app/models/spree_stripe/payment_method_decorator.rb +0 -18
- data/app/models/spree_stripe/payment_methods_webhook_key.rb +0 -8
- data/app/models/spree_stripe/payment_source_decorator.rb +0 -9
- data/app/models/spree_stripe/shipment_decorator.rb +0 -21
- data/app/models/spree_stripe/store_decorator.rb +0 -26
- data/app/models/spree_stripe/user_decorator.rb +0 -21
- data/app/models/spree_stripe/webhook_key.rb +0 -14
- data/app/presenters/spree_stripe/payment_intent_presenter.rb +0 -101
- data/app/presenters/spree_stripe/tax_presenter.rb +0 -63
- data/app/services/spree_stripe/complete_order.rb +0 -105
- data/app/services/spree_stripe/create_payment_session.rb +0 -18
- data/app/services/spree_stripe/register_domain.rb +0 -22
- data/app/services/spree_stripe/webhook_handlers/base.rb +0 -19
- data/app/services/spree_stripe/webhook_handlers/payment_intent_amount_capturable_updated.rb +0 -13
- data/app/services/spree_stripe/webhook_handlers/payment_intent_payment_failed.rb +0 -16
- data/app/services/spree_stripe/webhook_handlers/payment_intent_succeeded.rb +0 -11
- data/app/services/spree_stripe/webhook_handlers/setup_intent_succeeded.rb +0 -34
- data/app/subscribers/spree_stripe/order_completed_subscriber.rb +0 -14
- data/app/views/spree/admin/payment_methods/configuration_guides/_spree_stripe.html.erb +0 -0
- data/app/views/spree/admin/payment_methods/custom_form_fields/_spree_stripe.html.erb +0 -47
- data/app/views/spree/admin/payment_methods/descriptions/_spree_stripe.html.erb +0 -15
- data/app/views/spree/checkout/payment/_spree_stripe.html.erb +0 -66
- data/app/views/spree_stripe/_head.html.erb +0 -2
- data/app/views/spree_stripe/_quick_checkout.html.erb +0 -34
- data/config/i18n-tasks.yml +0 -173
- data/config/importmap.rb +0 -8
- data/config/initializers/spree.rb +0 -9
- data/config/routes.rb +0 -11
- data/db/migrate/20250310152812_setup_spree_stripe_models.rb +0 -24
- data/db/migrate/20260610120000_add_fingerprint_to_spree_credit_cards.rb +0 -52
- data/lib/spree_stripe/configuration.rb +0 -11
- data/lib/spree_stripe/testing_support/factories/after_pay_payment_source_factory.rb +0 -6
- data/lib/spree_stripe/testing_support/factories/alipay_payment_source_factory.rb +0 -6
- data/lib/spree_stripe/testing_support/factories/ideal_payment_source_factory.rb +0 -6
- data/lib/spree_stripe/testing_support/factories/klarna_payment_source_factory.rb +0 -6
- data/lib/spree_stripe/testing_support/factories/link_payment_source_factory.rb +0 -6
- data/lib/spree_stripe/testing_support/factories/przelewy24_payment_source_factory.rb +0 -6
- data/lib/spree_stripe/testing_support/factories/sepa_debit_payment_source_factory.rb +0 -6
- data/lib/spree_stripe/testing_support/factories/stripe_payment_session_factory.rb +0 -33
- data/lib/spree_stripe/testing_support/factories/webhook_key_factory.rb +0 -23
- data/lib/spree_stripe/version.rb +0 -7
- data/vendor/javascript/@stripe--stripe-js--dist--pure.esm.js.js +0 -4
|
@@ -1,431 +0,0 @@
|
|
|
1
|
-
import { Controller } from '@hotwired/stimulus'
|
|
2
|
-
import { loadStripe } from '@stripe/stripe-js/pure'
|
|
3
|
-
import showFlashMessage from 'spree/storefront/helpers/show_flash_message'
|
|
4
|
-
|
|
5
|
-
// Stripe zero-decimal currencies — amount is already in the smallest unit.
|
|
6
|
-
// https://docs.stripe.com/currencies#zero-decimal
|
|
7
|
-
const ZERO_DECIMAL_CURRENCIES = new Set([
|
|
8
|
-
'bif', 'clp', 'djf', 'gnf', 'jpy', 'kmf', 'krw', 'mga', 'pyg',
|
|
9
|
-
'rwf', 'ugx', 'vnd', 'vuv', 'xaf', 'xof', 'xpf'
|
|
10
|
-
])
|
|
11
|
-
|
|
12
|
-
// Apple Pay / Google Pay express checkout on the v3 Store API. The wallet sheet
|
|
13
|
-
// drives address + shipping selection against the server-provided cart API
|
|
14
|
-
// path, then a payment session is created and confirmed; completion is handled
|
|
15
|
-
// server-side by ConfirmPaymentsController via the `return_url` (same as the
|
|
16
|
-
// regular flow).
|
|
17
|
-
export default class extends Controller {
|
|
18
|
-
static values = {
|
|
19
|
-
apiKey: String,
|
|
20
|
-
spreeApiKey: String,
|
|
21
|
-
paymentMethodId: String,
|
|
22
|
-
cartApiPath: String,
|
|
23
|
-
cartToken: String,
|
|
24
|
-
confirmPaymentUrl: String,
|
|
25
|
-
currency: String,
|
|
26
|
-
amount: Number,
|
|
27
|
-
borderRadius: Number,
|
|
28
|
-
height: Number,
|
|
29
|
-
theme: String,
|
|
30
|
-
maxRows: Number,
|
|
31
|
-
maxColumns: Number,
|
|
32
|
-
buttonWidth: Number,
|
|
33
|
-
shippingRequired: { type: Boolean, default: true },
|
|
34
|
-
phoneRequired: { type: Boolean, default: false }
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
static targets = ['container']
|
|
38
|
-
|
|
39
|
-
connect() {
|
|
40
|
-
this.isGooglePay = false
|
|
41
|
-
this.shippingRateMap = new Map()
|
|
42
|
-
this.deliveryMethodMap = new Map()
|
|
43
|
-
this.initStripe()
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
initStripe() {
|
|
47
|
-
if (typeof Stripe === 'undefined') {
|
|
48
|
-
loadStripe(this.apiKeyValue).then((stripe) => {
|
|
49
|
-
this.stripe = stripe
|
|
50
|
-
this.prepareExpressCheckoutElement()
|
|
51
|
-
})
|
|
52
|
-
} else if (typeof this.stripe !== 'function') {
|
|
53
|
-
this.stripe = Stripe(this.apiKeyValue)
|
|
54
|
-
this.prepareExpressCheckoutElement()
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
prepareExpressCheckoutElement() {
|
|
59
|
-
this.elements = this.stripe.elements({
|
|
60
|
-
mode: 'payment',
|
|
61
|
-
currency: this.currencyValue,
|
|
62
|
-
amount: parseInt(this.amountValue),
|
|
63
|
-
appearance: {
|
|
64
|
-
theme: 'stripe',
|
|
65
|
-
variables: {
|
|
66
|
-
borderRadius: this.hasBorderRadiusValue ? `${this.borderRadiusValue}px` : undefined
|
|
67
|
-
}
|
|
68
|
-
},
|
|
69
|
-
paymentMethodCreation: 'manual'
|
|
70
|
-
})
|
|
71
|
-
|
|
72
|
-
const expressCheckout = this.elements.create('expressCheckout', {
|
|
73
|
-
buttonHeight: this.heightValue > 0 ? this.heightValue : undefined,
|
|
74
|
-
buttonTheme: {
|
|
75
|
-
applePay: this.themeValue.length ? this.themeValue : undefined,
|
|
76
|
-
googlePay: this.themeValue.length ? this.themeValue : undefined
|
|
77
|
-
},
|
|
78
|
-
layout: {
|
|
79
|
-
overflow: this.maxRowsValue > 0 ? 'auto' : 'never',
|
|
80
|
-
maxColumns: this.maxColumnsValue > 0 ? this.maxColumnsValue : undefined,
|
|
81
|
-
maxRows: this.maxRowsValue > 0 ? this.maxRowsValue : undefined
|
|
82
|
-
},
|
|
83
|
-
buttonType: {
|
|
84
|
-
applePay: 'check-out',
|
|
85
|
-
googlePay: 'checkout'
|
|
86
|
-
},
|
|
87
|
-
paymentMethodOrder: ['applePay', 'googlePay', 'link']
|
|
88
|
-
})
|
|
89
|
-
expressCheckout.mount('#payment-request-button')
|
|
90
|
-
|
|
91
|
-
expressCheckout.on('ready', this.handleReady.bind(this))
|
|
92
|
-
expressCheckout.on('click', this.handleClick.bind(this))
|
|
93
|
-
if (this.shippingRequiredValue) {
|
|
94
|
-
expressCheckout.on('shippingaddresschange', this.handleAddressChange.bind(this))
|
|
95
|
-
expressCheckout.on('shippingratechange', this.handleShippingOptionChange.bind(this))
|
|
96
|
-
}
|
|
97
|
-
expressCheckout.on('confirm', this.handleFinalizePayment.bind(this))
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
handleReady({ availablePaymentMethods }) {
|
|
101
|
-
if (!availablePaymentMethods) {
|
|
102
|
-
this.containerTarget.style.display = 'none'
|
|
103
|
-
return
|
|
104
|
-
}
|
|
105
|
-
const availableMethodsCount = Object.keys(availablePaymentMethods).filter(
|
|
106
|
-
(key) => availablePaymentMethods[key]
|
|
107
|
-
).length
|
|
108
|
-
if (this.buttonWidthValue > 0) {
|
|
109
|
-
this.containerTarget.style.setProperty('--desktop-max-width', this.buttonWidthValue * availableMethodsCount + 'px')
|
|
110
|
-
this.containerTarget.classList.add('desktop-max-width')
|
|
111
|
-
}
|
|
112
|
-
window.parent?.postMessage({ enabledPaymentMethodsCount: availableMethodsCount }, '*')
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
handleClick(event) {
|
|
116
|
-
this.isGooglePay = event.expressPaymentType === 'google_pay'
|
|
117
|
-
event.resolve({
|
|
118
|
-
emailRequired: true,
|
|
119
|
-
phoneNumberRequired: this.phoneRequiredValue,
|
|
120
|
-
shippingAddressRequired: this.shippingRequiredValue,
|
|
121
|
-
lineItems: [{ name: 'Subtotal', amount: parseInt(this.amountValue) }]
|
|
122
|
-
})
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
async handleAddressChange(event) {
|
|
126
|
-
try {
|
|
127
|
-
const { address } = event
|
|
128
|
-
// Stripe only shares city/zip/country/state at this stage; pad the rest
|
|
129
|
-
// with placeholders and quick_checkout to skip street-level validation.
|
|
130
|
-
const cart = await this.patchCart({
|
|
131
|
-
shipping_address: {
|
|
132
|
-
first_name: 'Express',
|
|
133
|
-
last_name: 'Checkout',
|
|
134
|
-
address1: 'TBD',
|
|
135
|
-
city: address.city,
|
|
136
|
-
postal_code: address.postal_code,
|
|
137
|
-
country_iso: address.country,
|
|
138
|
-
state_name: address.state || undefined,
|
|
139
|
-
quick_checkout: true
|
|
140
|
-
}
|
|
141
|
-
})
|
|
142
|
-
if (!cart) return event.reject()
|
|
143
|
-
|
|
144
|
-
const { shippingRates, selectionMap, methodMap } = this.buildShippingRates(cart.fulfillments || [])
|
|
145
|
-
this.shippingRateMap = selectionMap
|
|
146
|
-
this.deliveryMethodMap = methodMap
|
|
147
|
-
|
|
148
|
-
if (shippingRates.length === 0) return event.reject()
|
|
149
|
-
|
|
150
|
-
const lineItems = this.buildLineItems(cart)
|
|
151
|
-
lineItems.push({ name: 'Shipping', amount: shippingRates[0].amount })
|
|
152
|
-
|
|
153
|
-
// Amount must be updated before resolve — Stripe requires amount >= sum(lineItems).
|
|
154
|
-
this.elements.update({ amount: this.sumLineItems(lineItems) })
|
|
155
|
-
event.resolve({ shippingRates, lineItems })
|
|
156
|
-
} catch (_e) {
|
|
157
|
-
try { event.reject() } catch (_) { /* already resolved */ }
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
async handleShippingOptionChange(event) {
|
|
162
|
-
try {
|
|
163
|
-
const selections = this.shippingRateMap.get(event.shippingRate.id)
|
|
164
|
-
if (!selections || selections.length === 0) return event.reject()
|
|
165
|
-
|
|
166
|
-
let cart = null
|
|
167
|
-
for (const { fulfillmentId, rateId } of selections) {
|
|
168
|
-
cart = await this.patchFulfillment(fulfillmentId, rateId)
|
|
169
|
-
if (!cart) return event.reject()
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
const lineItems = this.buildLineItems(cart)
|
|
173
|
-
lineItems.push({ name: 'Shipping', amount: event.shippingRate.amount })
|
|
174
|
-
|
|
175
|
-
this.elements.update({ amount: this.sumLineItems(lineItems) })
|
|
176
|
-
event.resolve({ lineItems })
|
|
177
|
-
} catch (_e) {
|
|
178
|
-
try { event.reject() } catch (_) { /* already resolved */ }
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
async handleFinalizePayment(event) {
|
|
183
|
-
if (!this.stripe || !this.elements) {
|
|
184
|
-
event.paymentFailed({ reason: 'fail' })
|
|
185
|
-
return
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
const billing = event.billingDetails
|
|
189
|
-
const shipping = event.shippingAddress
|
|
190
|
-
const shipAddress = shipping?.address || billing?.address
|
|
191
|
-
const billAddress = billing?.address || shipping?.address
|
|
192
|
-
|
|
193
|
-
if (!shipAddress || !billAddress) {
|
|
194
|
-
event.paymentFailed({ reason: 'invalid_shipping_address' })
|
|
195
|
-
return
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
const phone = billing?.phone
|
|
199
|
-
const shipName = this.parseName(shipping?.name || billing?.name || '')
|
|
200
|
-
const billName = this.parseName(billing?.name || shipping?.name || '')
|
|
201
|
-
|
|
202
|
-
// Persist the final email + addresses before creating the payment session.
|
|
203
|
-
const cart = await this.patchCart({
|
|
204
|
-
email: billing?.email,
|
|
205
|
-
shipping_address: this.buildAddress(shipName, shipAddress, phone),
|
|
206
|
-
billing_address: this.buildAddress(billName, billAddress, phone)
|
|
207
|
-
})
|
|
208
|
-
if (!cart) {
|
|
209
|
-
event.paymentFailed({ reason: 'invalid_shipping_address' })
|
|
210
|
-
return
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
// The wallet can keep stale rates and an enabled Pay button after a rejected
|
|
214
|
-
// address change, so re-check the confirmed address is serviceable before
|
|
215
|
-
// charging — otherwise the payment confirms but the order can't complete.
|
|
216
|
-
if (this.shippingRequiredValue && !this.hasAvailableShipping(cart)) {
|
|
217
|
-
event.paymentFailed({ reason: 'address_unserviceable' })
|
|
218
|
-
return
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
// The address patch above rebuilds fulfillments with their default rate,
|
|
222
|
-
// discarding the wallet's selection — re-select it before charging, or the
|
|
223
|
-
// order completes with the wrong delivery method and total.
|
|
224
|
-
if (this.shippingRequiredValue && event.shippingRate) {
|
|
225
|
-
const reselected = await this.reselectDeliveryRates(cart, event.shippingRate.id)
|
|
226
|
-
if (!reselected) {
|
|
227
|
-
event.paymentFailed({ reason: 'fail' })
|
|
228
|
-
return
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
const { error: submitError } = await this.elements.submit()
|
|
233
|
-
if (submitError) {
|
|
234
|
-
event.paymentFailed({ reason: 'fail' })
|
|
235
|
-
this.handleError(submitError)
|
|
236
|
-
return
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
const { error: pmError, paymentMethod } = await this.stripe.createPaymentMethod({ elements: this.elements })
|
|
240
|
-
if (pmError || !paymentMethod) {
|
|
241
|
-
event.paymentFailed({ reason: 'invalid_payment_data' })
|
|
242
|
-
this.handleError(pmError)
|
|
243
|
-
return
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
const session = await this.createPaymentSession(paymentMethod.id)
|
|
247
|
-
const clientSecret = session?.external_data?.client_secret
|
|
248
|
-
if (!clientSecret) {
|
|
249
|
-
event.paymentFailed({ reason: 'fail' })
|
|
250
|
-
return
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
const returnUrl = `${this.confirmPaymentUrlValue}?session=${session.id}`
|
|
254
|
-
const { error: confirmError } = await this.stripe.confirmPayment({
|
|
255
|
-
clientSecret,
|
|
256
|
-
confirmParams: {
|
|
257
|
-
payment_method: paymentMethod.id,
|
|
258
|
-
return_url: returnUrl
|
|
259
|
-
},
|
|
260
|
-
redirect: 'if_required'
|
|
261
|
-
})
|
|
262
|
-
|
|
263
|
-
if (confirmError) {
|
|
264
|
-
event.paymentFailed({ reason: 'fail' })
|
|
265
|
-
this.handleError(confirmError)
|
|
266
|
-
return
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
// Payment confirmed — let the server finalize the session + cart.
|
|
270
|
-
window.location.href = returnUrl
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
// --- v3 Store API calls -------------------------------------------------
|
|
274
|
-
|
|
275
|
-
async patchCart(body) {
|
|
276
|
-
const response = await fetch(this.cartApiPathValue, {
|
|
277
|
-
method: 'PATCH',
|
|
278
|
-
headers: this.spreeApiHeaders,
|
|
279
|
-
body: JSON.stringify(body)
|
|
280
|
-
})
|
|
281
|
-
return response.ok ? response.json() : null
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
async patchFulfillment(fulfillmentId, deliveryRateId) {
|
|
285
|
-
const response = await fetch(`${this.cartApiPathValue}/fulfillments/${fulfillmentId}`, {
|
|
286
|
-
method: 'PATCH',
|
|
287
|
-
headers: this.spreeApiHeaders,
|
|
288
|
-
body: JSON.stringify({ selected_delivery_rate_id: deliveryRateId })
|
|
289
|
-
})
|
|
290
|
-
return response.ok ? response.json() : null
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
async createPaymentSession(stripePaymentMethodId) {
|
|
294
|
-
const response = await fetch(`${this.cartApiPathValue}/payment_sessions`, {
|
|
295
|
-
method: 'POST',
|
|
296
|
-
headers: this.spreeApiHeaders,
|
|
297
|
-
body: JSON.stringify({
|
|
298
|
-
payment_method_id: this.paymentMethodIdValue,
|
|
299
|
-
external_data: { stripe_payment_method_id: stripePaymentMethodId }
|
|
300
|
-
})
|
|
301
|
-
})
|
|
302
|
-
return response.ok ? response.json() : null
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
// --- helpers ------------------------------------------------------------
|
|
306
|
-
|
|
307
|
-
// Line items for the wallet sheet. Shipping is added separately by the
|
|
308
|
-
// address/rate handlers (Stripe owns shipping via shippingRates).
|
|
309
|
-
buildLineItems(cart) {
|
|
310
|
-
const items = [{ name: 'Subtotal', amount: this.toCents(cart.item_total) }]
|
|
311
|
-
|
|
312
|
-
const discount = this.toCents(cart.discount_total)
|
|
313
|
-
if (discount < 0) items.push({ name: 'Discount', amount: discount })
|
|
314
|
-
|
|
315
|
-
const tax = this.toCents(cart.additional_tax_total)
|
|
316
|
-
if (tax > 0) items.push({ name: 'Tax', amount: tax })
|
|
317
|
-
|
|
318
|
-
return items
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
// Builds deduped Stripe shipping rates (by delivery_method_id), a map from
|
|
322
|
-
// each Stripe rate id to the per-fulfillment delivery rate ids to select,
|
|
323
|
-
// and a map back to the delivery_method_id (the only id that survives a
|
|
324
|
-
// fulfillment rebuild — see reselectDeliveryRates).
|
|
325
|
-
buildShippingRates(fulfillments) {
|
|
326
|
-
const rateMap = new Map()
|
|
327
|
-
const selectionMap = new Map()
|
|
328
|
-
const methodMap = new Map()
|
|
329
|
-
|
|
330
|
-
for (const fulfillment of fulfillments) {
|
|
331
|
-
for (const rate of fulfillment.delivery_rates || []) {
|
|
332
|
-
const methodId = rate.delivery_method_id
|
|
333
|
-
|
|
334
|
-
if (!rateMap.has(methodId)) {
|
|
335
|
-
// Google Pay rejects duplicate rate ids across address changes, so
|
|
336
|
-
// give it a unique suffix.
|
|
337
|
-
const id = this.isGooglePay ? `${methodId}-${this.randomSuffix()}` : String(methodId)
|
|
338
|
-
rateMap.set(methodId, { id, displayName: rate.name, amount: this.toCents(rate.cost) })
|
|
339
|
-
selectionMap.set(id, [])
|
|
340
|
-
methodMap.set(id, methodId)
|
|
341
|
-
} else {
|
|
342
|
-
rateMap.get(methodId).amount += this.toCents(rate.cost)
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
const stripeId = rateMap.get(methodId).id
|
|
346
|
-
selectionMap.get(stripeId).push({ fulfillmentId: fulfillment.id, rateId: rate.id })
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
return { shippingRates: Array.from(rateMap.values()), selectionMap, methodMap }
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
// Re-selects the delivery method chosen in the wallet sheet on freshly
|
|
354
|
-
// rebuilt fulfillments. Matches by delivery_method_id: fulfillment and rate
|
|
355
|
-
// ids do not survive the rebuild that a shipping-address update triggers
|
|
356
|
-
// server-side (checkout reverts to the address step and shipments are
|
|
357
|
-
// recreated with their default rate).
|
|
358
|
-
async reselectDeliveryRates(cart, stripeRateId) {
|
|
359
|
-
const methodId = this.deliveryMethodMap.get(stripeRateId)
|
|
360
|
-
if (!methodId) return false
|
|
361
|
-
|
|
362
|
-
for (const fulfillment of cart.fulfillments || []) {
|
|
363
|
-
const rate = (fulfillment.delivery_rates || []).find((r) => r.delivery_method_id === methodId)
|
|
364
|
-
if (!rate || rate.selected) continue
|
|
365
|
-
|
|
366
|
-
const updated = await this.patchFulfillment(fulfillment.id, rate.id)
|
|
367
|
-
if (!updated) return false
|
|
368
|
-
}
|
|
369
|
-
return true
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
// True when every shippable fulfillment has at least one delivery rate — i.e.
|
|
373
|
-
// the confirmed address is serviceable. Digital fulfillments need no shipping.
|
|
374
|
-
hasAvailableShipping(cart) {
|
|
375
|
-
const shippable = (cart.fulfillments || []).filter((f) => f.fulfillment_type !== 'digital')
|
|
376
|
-
return shippable.length > 0 && shippable.every((f) => (f.delivery_rates || []).length > 0)
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
buildAddress(name, address, phone) {
|
|
380
|
-
return {
|
|
381
|
-
first_name: name.firstName,
|
|
382
|
-
last_name: name.lastName,
|
|
383
|
-
address1: address.line1,
|
|
384
|
-
address2: address.line2 || undefined,
|
|
385
|
-
city: address.city,
|
|
386
|
-
postal_code: address.postal_code,
|
|
387
|
-
country_iso: address.country,
|
|
388
|
-
state_name: address.state || undefined,
|
|
389
|
-
phone: phone || undefined,
|
|
390
|
-
quick_checkout: true
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
parseName(name) {
|
|
395
|
-
const parts = name.trim().split(/\s+/)
|
|
396
|
-
if (parts.length <= 1) return { firstName: parts[0] || '', lastName: '' }
|
|
397
|
-
return { firstName: parts.slice(0, -1).join(' '), lastName: parts[parts.length - 1] }
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
toCents(amount) {
|
|
401
|
-
const n = Number(amount)
|
|
402
|
-
if (!Number.isFinite(n)) return 0
|
|
403
|
-
return ZERO_DECIMAL_CURRENCIES.has(this.currencyValue.toLowerCase()) ? Math.round(n) : Math.round(n * 100)
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
sumLineItems(lineItems) {
|
|
407
|
-
return lineItems.reduce((sum, item) => sum + item.amount, 0)
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
randomSuffix() {
|
|
411
|
-
return Math.random().toString(36).slice(2, 6)
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
handleError(error) {
|
|
415
|
-
if (!error) return
|
|
416
|
-
|
|
417
|
-
if (error.type === 'card_error' || error.type === 'validation_error') {
|
|
418
|
-
showFlashMessage(error.message, 'error')
|
|
419
|
-
} else {
|
|
420
|
-
showFlashMessage('An unexpected error occurred. Please refresh the page and try again.', 'error')
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
get spreeApiHeaders() {
|
|
425
|
-
return {
|
|
426
|
-
'X-Spree-API-Key': this.spreeApiKeyValue,
|
|
427
|
-
'X-Spree-Token': this.cartTokenValue,
|
|
428
|
-
'Content-Type': 'application/json'
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
}
|
|
@@ -1,234 +0,0 @@
|
|
|
1
|
-
import { Controller } from '@hotwired/stimulus'
|
|
2
|
-
import showFlashMessage from 'spree/storefront/helpers/show_flash_message'
|
|
3
|
-
|
|
4
|
-
// Regular Stripe checkout on the v3 Store API — renders the payment form and
|
|
5
|
-
// confirms the payment. Completion is NOT handled here: both new and saved
|
|
6
|
-
// cards redirect to `return_url` (new cards via Stripe's redirect, saved cards
|
|
7
|
-
// manually after inline confirmation), where the server-side
|
|
8
|
-
// ConfirmPaymentsController finalizes the session + cart.
|
|
9
|
-
export default class extends Controller {
|
|
10
|
-
static values = {
|
|
11
|
-
apiKey: String,
|
|
12
|
-
clientSecret: String,
|
|
13
|
-
cartApiPath: String,
|
|
14
|
-
cartToken: String,
|
|
15
|
-
spreeApiKey: String,
|
|
16
|
-
orderEmail: String,
|
|
17
|
-
returnUrl: String,
|
|
18
|
-
colorPrimary: String,
|
|
19
|
-
colorText: String,
|
|
20
|
-
paymentElementAdditionalOptions: { type: Object, default: {} }
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
static targets = ['paymentElement', 'loading', 'defaultCard']
|
|
24
|
-
|
|
25
|
-
connect() {
|
|
26
|
-
this.submitTarget = document.querySelector('#checkout-payment-submit')
|
|
27
|
-
this.billingAddressCheckbox = document.querySelector('#order_use_shipping')
|
|
28
|
-
this.billingAddressForm = document.querySelector('form.edit_order')
|
|
29
|
-
this.stripe = Stripe(this.apiKeyValue)
|
|
30
|
-
|
|
31
|
-
if (this.hasDefaultCardTarget) {
|
|
32
|
-
this.initializeStripe({ target: { value: this.defaultCardTarget.value } })
|
|
33
|
-
} else {
|
|
34
|
-
this.initializeStripe({ target: { value: 'on' } })
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
if (this.submitTarget) {
|
|
38
|
-
this.submitTarget.addEventListener('click', this.submit.bind(this))
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// Mounts the Payment Element for a new card, or records the selected saved card.
|
|
43
|
-
initializeStripe(e) {
|
|
44
|
-
this.setLoading(true)
|
|
45
|
-
|
|
46
|
-
const value = e.target.value
|
|
47
|
-
this.stripePaymentMethodId = value && value !== 'on' ? value : null
|
|
48
|
-
|
|
49
|
-
if (this.stripePaymentMethodId === null && !this.elements) {
|
|
50
|
-
const appearance = {
|
|
51
|
-
theme: 'stripe',
|
|
52
|
-
variables: {
|
|
53
|
-
colorPrimary: this.colorPrimaryValue,
|
|
54
|
-
colorText: this.colorTextValue
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
this.elements = this.stripe.elements({ appearance, clientSecret: this.clientSecretValue })
|
|
58
|
-
const paymentElement = this.elements.create('payment', {
|
|
59
|
-
fields: {
|
|
60
|
-
billingDetails: {
|
|
61
|
-
name: 'never',
|
|
62
|
-
email: 'never',
|
|
63
|
-
address: { country: 'never', postalCode: 'never' }
|
|
64
|
-
}
|
|
65
|
-
},
|
|
66
|
-
...this.paymentElementAdditionalOptionsValue
|
|
67
|
-
})
|
|
68
|
-
paymentElement.mount(this.paymentElementTarget)
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
this.setLoading(false)
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
async submit(e) {
|
|
75
|
-
e.preventDefault()
|
|
76
|
-
this.setLoading(true)
|
|
77
|
-
|
|
78
|
-
const billing = await this.updateBillingAddress()
|
|
79
|
-
if (!billing) {
|
|
80
|
-
this.setLoading(false)
|
|
81
|
-
e.stopImmediatePropagation()
|
|
82
|
-
return
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (this.stripePaymentMethodId) {
|
|
86
|
-
// Saved card: confirm inline (Stripe binds the payment method to the
|
|
87
|
-
// intent), then hand off to the return controller — mirrors how Stripe
|
|
88
|
-
// redirects new cards back. The server reads the bound method from the
|
|
89
|
-
// confirmed intent, so no extra session call is needed.
|
|
90
|
-
const { error } = await this.stripe.confirmCardPayment(this.clientSecretValue, {
|
|
91
|
-
payment_method: this.stripePaymentMethodId
|
|
92
|
-
})
|
|
93
|
-
|
|
94
|
-
if (error) {
|
|
95
|
-
this.handleError(error)
|
|
96
|
-
this.setLoading(false)
|
|
97
|
-
return
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
window.location.href = this.returnUrlValue
|
|
101
|
-
} else {
|
|
102
|
-
// New card: confirm with the Payment Element; Stripe redirects to return_url.
|
|
103
|
-
const elements = this.elements
|
|
104
|
-
if (!elements || !elements.getElement('payment')) {
|
|
105
|
-
showFlashMessage('An unexpected error occurred. Please refresh the page and try again.', 'error')
|
|
106
|
-
this.setLoading(false)
|
|
107
|
-
return
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
const { error } = await this.stripe.confirmPayment({
|
|
111
|
-
elements,
|
|
112
|
-
confirmParams: {
|
|
113
|
-
return_url: this.returnUrlValue,
|
|
114
|
-
payment_method_data: { billing_details: this.billingDetails(billing) }
|
|
115
|
-
}
|
|
116
|
-
})
|
|
117
|
-
|
|
118
|
-
// Reached only on an immediate error; otherwise Stripe redirects to return_url.
|
|
119
|
-
if (error) this.handleError(error)
|
|
120
|
-
this.setLoading(false)
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// Persists the billing address to the cart via the v3 Store API.
|
|
125
|
-
// Returns the cart's billing_address on success, false on failure.
|
|
126
|
-
async updateBillingAddress() {
|
|
127
|
-
let body
|
|
128
|
-
if (this.billingAddressCheckbox?.checked) {
|
|
129
|
-
body = { use_shipping: true }
|
|
130
|
-
} else if (this.billingAddressForm?.checkValidity()) {
|
|
131
|
-
body = { billing_address: this.readBillingAddressForm() }
|
|
132
|
-
} else {
|
|
133
|
-
if (this.submitTarget) this.submitTarget.disabled = false
|
|
134
|
-
return false
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
const response = await fetch(this.cartApiPathValue, {
|
|
138
|
-
method: 'PATCH',
|
|
139
|
-
headers: this.spreeApiHeaders,
|
|
140
|
-
body: JSON.stringify(body)
|
|
141
|
-
})
|
|
142
|
-
const json = await response.json().catch(() => ({}))
|
|
143
|
-
|
|
144
|
-
if (response.ok) {
|
|
145
|
-
return json.billing_address || {}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
showFlashMessage(this.extractErrors(json) || 'Billing address is invalid', 'error')
|
|
149
|
-
if (this.submitTarget) this.submitTarget.disabled = false
|
|
150
|
-
return false
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
readBillingAddressForm() {
|
|
154
|
-
const form = this.billingAddressForm
|
|
155
|
-
const field = (name) => form.querySelector(`[name="order[bill_address_attributes][${name}]"]`)
|
|
156
|
-
const value = (name) => field(name)?.value || null
|
|
157
|
-
|
|
158
|
-
const countryOption = field('country_id')?.selectedOptions?.[0]
|
|
159
|
-
const stateOption = field('state_id')?.selectedOptions?.[0]
|
|
160
|
-
|
|
161
|
-
return {
|
|
162
|
-
first_name: value('firstname'),
|
|
163
|
-
last_name: value('lastname'),
|
|
164
|
-
address1: value('address1'),
|
|
165
|
-
address2: value('address2'),
|
|
166
|
-
city: value('city'),
|
|
167
|
-
postal_code: value('zipcode'),
|
|
168
|
-
phone: value('phone'),
|
|
169
|
-
country_iso: countryOption?.dataset?.iso || null,
|
|
170
|
-
state_name: stateOption?.text?.trim() || value('state_name')
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
billingDetails(address) {
|
|
175
|
-
return {
|
|
176
|
-
name: [address.first_name, address.last_name].filter(Boolean).join(' '),
|
|
177
|
-
email: this.orderEmailValue,
|
|
178
|
-
address: {
|
|
179
|
-
city: address.city,
|
|
180
|
-
country: address.country_iso,
|
|
181
|
-
line1: address.address1,
|
|
182
|
-
line2: address.address2,
|
|
183
|
-
postal_code: address.postal_code,
|
|
184
|
-
state: address.state_abbr || address.state_name
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
handleError(error) {
|
|
190
|
-
if (!error) return
|
|
191
|
-
|
|
192
|
-
// Per Stripe: only card_error / validation_error messages are meant to be
|
|
193
|
-
// shown to customers; other types are developer-facing, so fall back to a
|
|
194
|
-
// generic message.
|
|
195
|
-
if (error.type === 'card_error' || error.type === 'validation_error') {
|
|
196
|
-
showFlashMessage(error.message, 'error')
|
|
197
|
-
} else {
|
|
198
|
-
showFlashMessage('An unexpected error occurred. Please refresh the page and try again.', 'error')
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
if (this.submitTarget) this.submitTarget.disabled = false
|
|
202
|
-
window.scrollTo({ top: 0, behavior: 'smooth' })
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
setLoading(isLoading) {
|
|
206
|
-
if (isLoading) {
|
|
207
|
-
if (this.submitTarget) this.submitTarget.disabled = true
|
|
208
|
-
} else {
|
|
209
|
-
if (this.hasLoadingTarget) this.loadingTarget.classList.add('hidden')
|
|
210
|
-
if (this.submitTarget) {
|
|
211
|
-
this.submitTarget.disabled = false
|
|
212
|
-
this.submitTarget.classList.remove('hidden')
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
extractErrors(json) {
|
|
218
|
-
if (!json) return null
|
|
219
|
-
if (typeof json.error === 'string') return json.error
|
|
220
|
-
if (Array.isArray(json.error)) return json.error.join('. ')
|
|
221
|
-
if (json.errors) {
|
|
222
|
-
return Array.isArray(json.errors) ? json.errors.join('. ') : Object.values(json.errors).flat().join('. ')
|
|
223
|
-
}
|
|
224
|
-
return null
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
get spreeApiHeaders() {
|
|
228
|
-
return {
|
|
229
|
-
'X-Spree-API-Key': this.spreeApiKeyValue,
|
|
230
|
-
'X-Spree-Token': this.cartTokenValue,
|
|
231
|
-
'Content-Type': 'application/json'
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
module SpreeStripe
|
|
2
|
-
class AttachCustomerToCreditCardJob < BaseJob
|
|
3
|
-
def perform(order_id)
|
|
4
|
-
return if Spree.user_class.blank?
|
|
5
|
-
|
|
6
|
-
order = Spree::Order.find_by(id: order_id)
|
|
7
|
-
return if order.blank? || order.user_id.blank?
|
|
8
|
-
|
|
9
|
-
gateway = order.store.stripe_gateway
|
|
10
|
-
return if gateway.blank?
|
|
11
|
-
|
|
12
|
-
user = Spree.user_class.find_by(id: order.user_id)
|
|
13
|
-
return if user.blank?
|
|
14
|
-
|
|
15
|
-
gateway.attach_customer_to_credit_card(user)
|
|
16
|
-
end
|
|
17
|
-
end
|
|
18
|
-
end
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
module SpreeStripe
|
|
2
|
-
class CompleteOrderFromSessionJob < BaseJob
|
|
3
|
-
def perform(payment_session_id)
|
|
4
|
-
payment_session = Spree::PaymentSessions::Stripe.find(payment_session_id)
|
|
5
|
-
|
|
6
|
-
# PaymentSessions::Stripe duck-types as PaymentIntent
|
|
7
|
-
SpreeStripe::CompleteOrder.new(payment_intent: payment_session).call
|
|
8
|
-
|
|
9
|
-
payment_session.complete unless payment_session.completed?
|
|
10
|
-
end
|
|
11
|
-
end
|
|
12
|
-
end
|