@storecraft/payments-stripe 1.2.5 → 1.3.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.
- package/adapter.html.js +65 -24
- package/adapter.js +19 -22
- package/package.json +1 -1
package/adapter.html.js
CHANGED
@@ -13,7 +13,7 @@
|
|
13
13
|
* Or use the following dummy details:
|
14
14
|
* https://docs.stripe.com/testing?testing-method=card-numbers
|
15
15
|
* - Card number: 4242424242424242
|
16
|
-
* - Expiry date:
|
16
|
+
* - Expiry date:
|
17
17
|
* - CVC code: 897
|
18
18
|
*
|
19
19
|
* @param {Config} config
|
@@ -37,7 +37,33 @@ export default function html_buy_ui(config, order_data) {
|
|
37
37
|
<script defer>
|
38
38
|
// This is your test publishable API key.
|
39
39
|
const stripe = new Stripe("${config.publishable_key}");
|
40
|
+
const dispatchEvent = (event, data) => {
|
41
|
+
window?.parent?.postMessage(
|
42
|
+
{
|
43
|
+
who: "storecraft",
|
44
|
+
event,
|
45
|
+
data
|
46
|
+
},
|
47
|
+
"*"
|
48
|
+
);
|
49
|
+
}
|
50
|
+
// Override console.log to send messages to the parent window
|
51
|
+
console.log = function(...args) {
|
52
|
+
// Log to console
|
53
|
+
dispatchEvent(
|
54
|
+
"storecraft/checkout-log",
|
55
|
+
args
|
56
|
+
);
|
57
|
+
};
|
40
58
|
|
59
|
+
console.error = function(...args) {
|
60
|
+
// Log to console
|
61
|
+
dispatchEvent(
|
62
|
+
"storecraft/checkout-error",
|
63
|
+
args
|
64
|
+
);
|
65
|
+
};
|
66
|
+
|
41
67
|
let elements;
|
42
68
|
|
43
69
|
window.onload = function() {
|
@@ -73,20 +99,34 @@ export default function html_buy_ui(config, order_data) {
|
|
73
99
|
const { error } = await stripe.confirmPayment({
|
74
100
|
elements,
|
75
101
|
confirmParams: {
|
76
|
-
//
|
77
|
-
|
102
|
+
// return to this same page, which can also interpret the
|
103
|
+
// payment intent status from the URL parameters
|
104
|
+
return_url: window.location.href,
|
78
105
|
},
|
106
|
+
redirect: 'if_required'
|
79
107
|
});
|
80
108
|
|
81
109
|
// This point will only be reached if there is an immediate error when
|
82
110
|
// confirming the payment. Otherwise, your customer will be redirected to
|
83
|
-
// your \`return_url
|
111
|
+
// your \`return_url\` unless 'if_required' ise set. For some payment methods like iDEAL, your customer will
|
84
112
|
// be redirected to an intermediate site first to authorize the payment, then
|
85
113
|
// redirected to the \`return_url\`.
|
86
|
-
if
|
87
|
-
|
114
|
+
if(error) {
|
115
|
+
dispatchEvent("storecraft/checkout-error", {
|
116
|
+
order_id: "${order_data.id}"
|
117
|
+
});
|
118
|
+
|
119
|
+
if (error.type === "card_error" || error.type === "validation_error") {
|
120
|
+
showMessage(error.message);
|
121
|
+
} else if(error) {
|
122
|
+
showMessage("An unexpected error occurred.");
|
123
|
+
}
|
88
124
|
} else {
|
89
|
-
|
125
|
+
// if 'if_required' is set and no redirect is needed,
|
126
|
+
// Payment succeeded, you can show a success message to your customer
|
127
|
+
dispatchEvent("storecraft/checkout-complete", {
|
128
|
+
order_id: "${order_data.id}"
|
129
|
+
});
|
90
130
|
}
|
91
131
|
|
92
132
|
setLoading(false);
|
@@ -107,6 +147,10 @@ export default function html_buy_ui(config, order_data) {
|
|
107
147
|
switch (paymentIntent.status) {
|
108
148
|
case "succeeded":
|
109
149
|
showMessage("Payment succeeded!");
|
150
|
+
dispatchEvent("storecraft/checkout-complete", {
|
151
|
+
order_id: "${order_data.id}"
|
152
|
+
});
|
153
|
+
|
110
154
|
break;
|
111
155
|
case "processing":
|
112
156
|
showMessage("Your payment is processing.");
|
@@ -116,6 +160,9 @@ export default function html_buy_ui(config, order_data) {
|
|
116
160
|
break;
|
117
161
|
default:
|
118
162
|
showMessage("Something went wrong.");
|
163
|
+
dispatchEvent("storecraft/checkout-error", {
|
164
|
+
order_id: "${order_data.id}"
|
165
|
+
});
|
119
166
|
break;
|
120
167
|
}
|
121
168
|
}
|
@@ -128,6 +175,8 @@ export default function html_buy_ui(config, order_data) {
|
|
128
175
|
messageContainer.classList.remove("hidden");
|
129
176
|
messageContainer.textContent = messageText;
|
130
177
|
|
178
|
+
console.log(messageText);
|
179
|
+
|
131
180
|
setTimeout(function () {
|
132
181
|
messageContainer.classList.add("hidden");
|
133
182
|
messageContainer.textContent = "";
|
@@ -158,23 +207,21 @@ export default function html_buy_ui(config, order_data) {
|
|
158
207
|
|
159
208
|
body {
|
160
209
|
font-family: -apple-system, BlinkMacSystemFont, sans-serif;
|
161
|
-
font-size:
|
210
|
+
font-size: 10px;
|
162
211
|
-webkit-font-smoothing: antialiased;
|
212
|
+
height: content;
|
213
|
+
background-color: white;
|
163
214
|
display: flex;
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
width: 100vw;
|
215
|
+
flex-direction: column;
|
216
|
+
justify-content: start;
|
217
|
+
align-items: center;
|
168
218
|
}
|
169
219
|
|
170
220
|
form {
|
171
|
-
width:
|
172
|
-
|
221
|
+
width: 100%;
|
222
|
+
max-width: 500px;
|
223
|
+
mmmin-width: 500px;
|
173
224
|
align-self: center;
|
174
|
-
box-shadow: 0px 0px 0px 0.5px rgba(50, 50, 93, 0.1),
|
175
|
-
0px 2px 5px 0px rgba(50, 50, 93, 0.1), 0px 1px 1.5px 0px rgba(0, 0, 0, 0.07);
|
176
|
-
border-radius: 7px;
|
177
|
-
padding: 40px;
|
178
225
|
}
|
179
226
|
|
180
227
|
.hidden {
|
@@ -287,12 +334,6 @@ export default function html_buy_ui(config, order_data) {
|
|
287
334
|
}
|
288
335
|
}
|
289
336
|
|
290
|
-
@media only screen and (max-width: 600px) {
|
291
|
-
form {
|
292
|
-
width: 80vw;
|
293
|
-
min-width: initial;
|
294
|
-
}
|
295
|
-
}
|
296
337
|
|
297
338
|
</style>
|
298
339
|
</head>
|
package/adapter.js
CHANGED
@@ -20,15 +20,17 @@ import { Stripe as StripeCls } from 'stripe'
|
|
20
20
|
*/
|
21
21
|
|
22
22
|
/**
|
23
|
-
* @description in a {@link StripeCls.PaymentIntent},
|
24
|
-
* storage where we store the
|
23
|
+
* @description in a {@link StripeCls.PaymentIntent},
|
24
|
+
* is a `metadata` key-value storage where we store the
|
25
|
+
* `order_id` of the `storecraft` order.
|
25
26
|
*/
|
26
27
|
export const metadata_storecraft_order_id = 'storecraft_order_id'
|
27
28
|
|
28
29
|
/**
|
29
30
|
* @implements {Impl}
|
30
31
|
*
|
31
|
-
* @description **Stripe** gateway
|
32
|
+
* @description **Stripe** gateway
|
33
|
+
* (https://docs.stripe.com/payments/place-a-hold-on-a-payment-method)
|
32
34
|
*/
|
33
35
|
export class Stripe {
|
34
36
|
|
@@ -55,6 +57,7 @@ export class Stripe {
|
|
55
57
|
automatic_payment_methods: {
|
56
58
|
enabled: true,
|
57
59
|
},
|
60
|
+
capture_method: 'manual',
|
58
61
|
payment_method_options: {
|
59
62
|
card: {
|
60
63
|
capture_method: 'manual',
|
@@ -98,7 +101,7 @@ export class Stripe {
|
|
98
101
|
get info() {
|
99
102
|
return {
|
100
103
|
name: 'Stripe',
|
101
|
-
description: `
|
104
|
+
description: `Pay with Credit Card and other payment methods`,
|
102
105
|
url: 'https://docs.stripe.com/payments/place-a-hold-on-a-payment-method',
|
103
106
|
logo_url: 'https://images.ctfassets.net/fzn2n1nzq965/HTTOloNPhisV9P4hlMPNA/cacf1bb88b9fc492dfad34378d844280/Stripe_icon_-_square.svg?q=80&w=256'
|
104
107
|
}
|
@@ -129,7 +132,6 @@ export class Stripe {
|
|
129
132
|
}
|
130
133
|
|
131
134
|
/**
|
132
|
-
*
|
133
135
|
* @type {Impl["invokeAction"]}
|
134
136
|
*/
|
135
137
|
invokeAction(action_handle) {
|
@@ -148,7 +150,6 @@ export class Stripe {
|
|
148
150
|
|
149
151
|
/**
|
150
152
|
* @description (Optional) buy link ui
|
151
|
-
*
|
152
153
|
* @type {Impl["onBuyLinkHtml"]}
|
153
154
|
*/
|
154
155
|
async onBuyLinkHtml(order) {
|
@@ -160,7 +161,6 @@ export class Stripe {
|
|
160
161
|
|
161
162
|
/**
|
162
163
|
* @description on checkout create `hook`
|
163
|
-
*
|
164
164
|
* @type {Impl["onCheckoutCreate"]}
|
165
165
|
*/
|
166
166
|
async onCheckoutCreate(order) {
|
@@ -184,7 +184,6 @@ export class Stripe {
|
|
184
184
|
* They advocate async flows where confirmation happens async from
|
185
185
|
* client side into their servers, and then you are notified via a
|
186
186
|
* webhook.
|
187
|
-
*
|
188
187
|
* @type {Impl["onCheckoutComplete"]}
|
189
188
|
*/
|
190
189
|
async onCheckoutComplete(create_result) {
|
@@ -228,7 +227,6 @@ export class Stripe {
|
|
228
227
|
|
229
228
|
/**
|
230
229
|
* @description Fetch the order and analyze it's status
|
231
|
-
*
|
232
230
|
* @type {Impl["status"]}
|
233
231
|
*/
|
234
232
|
async status(create_result) {
|
@@ -257,7 +255,10 @@ export class Stripe {
|
|
257
255
|
);
|
258
256
|
}
|
259
257
|
if(lc?.refunded) {
|
260
|
-
const date = lc?.refunds?.data?.[0]?.created ?
|
258
|
+
const date = lc?.refunds?.data?.[0]?.created ?
|
259
|
+
(new Date(lc?.refunds?.data?.[0]?.created).toUTCString()) :
|
260
|
+
'unknown time';
|
261
|
+
|
261
262
|
stat.messages.push(
|
262
263
|
`**${fmt(lc.amount_refunded)}** was \`REFUNDED\` at \`${date}\``,
|
263
264
|
);
|
@@ -268,7 +269,8 @@ export class Stripe {
|
|
268
269
|
stat.messages.push(
|
269
270
|
...[
|
270
271
|
`Intent was \`CANCELLED\` at \`${date}\`.`,
|
271
|
-
o.cancellation_reason &&
|
272
|
+
o.cancellation_reason &&
|
273
|
+
`Cancellation reason is ${o.cancellation_reason}`
|
272
274
|
].filter(Boolean)
|
273
275
|
);
|
274
276
|
}
|
@@ -283,9 +285,7 @@ export class Stripe {
|
|
283
285
|
async webhook(request, response) {
|
284
286
|
const sig = request.headers.get('Stripe-Signature');
|
285
287
|
|
286
|
-
let event
|
287
|
-
|
288
|
-
event = await this.stripe.webhooks.constructEventAsync(
|
288
|
+
let event = await this.stripe.webhooks.constructEventAsync(
|
289
289
|
request.rawBody, sig, this.config.webhook_endpoint_secret, undefined,
|
290
290
|
StripeCls.createSubtleCryptoProvider()
|
291
291
|
);
|
@@ -293,7 +293,7 @@ export class Stripe {
|
|
293
293
|
/** @type {string} */
|
294
294
|
let order_id = undefined;
|
295
295
|
|
296
|
-
/** @type {PaymentOptionsEnum[keyof PaymentOptionsEnum]} */
|
296
|
+
/** @type {typeof PaymentOptionsEnum[keyof typeof PaymentOptionsEnum]} */
|
297
297
|
let payment_status = PaymentOptionsEnum.unpaid;
|
298
298
|
|
299
299
|
// Handle the event
|
@@ -309,13 +309,14 @@ export class Stripe {
|
|
309
309
|
if(payment_intent.status==='requires_capture')
|
310
310
|
payment_status = PaymentOptionsEnum.authorized;
|
311
311
|
else if(payment_intent.status==='canceled')
|
312
|
-
payment_status = PaymentOptionsEnum.
|
312
|
+
payment_status = PaymentOptionsEnum.cancelled;
|
313
313
|
else if(payment_intent.status==='processing')
|
314
314
|
payment_status = PaymentOptionsEnum.unpaid;
|
315
315
|
else if(payment_intent.status==='requires_action')
|
316
316
|
payment_status = PaymentOptionsEnum.unpaid;
|
317
317
|
else if(payment_intent.status==='succeeded')
|
318
318
|
payment_status = PaymentOptionsEnum.captured;
|
319
|
+
|
319
320
|
break;
|
320
321
|
}
|
321
322
|
case 'charge.refunded':
|
@@ -351,9 +352,8 @@ export class Stripe {
|
|
351
352
|
|
352
353
|
/**
|
353
354
|
* @description Retrieve latest order payload
|
354
|
-
*
|
355
|
-
*
|
356
|
-
*
|
355
|
+
* @param {CheckoutCreateResult} create_result
|
356
|
+
* first create result, holds `Stripe` intent
|
357
357
|
*/
|
358
358
|
retrieve_order = (create_result) => {
|
359
359
|
return this.stripe.paymentIntents.retrieve(
|
@@ -368,7 +368,6 @@ export class Stripe {
|
|
368
368
|
|
369
369
|
/**
|
370
370
|
* @description todo: logic for if user wanted capture at approval
|
371
|
-
*
|
372
371
|
* @param {CheckoutCreateResult} create_result
|
373
372
|
*/
|
374
373
|
async cancel(create_result) {
|
@@ -384,7 +383,6 @@ export class Stripe {
|
|
384
383
|
|
385
384
|
/**
|
386
385
|
* @description todo: logic for if user wanted capture at approval
|
387
|
-
*
|
388
386
|
* @param {CheckoutCreateResult} create_result
|
389
387
|
*/
|
390
388
|
async capture(create_result) {
|
@@ -400,7 +398,6 @@ export class Stripe {
|
|
400
398
|
|
401
399
|
/**
|
402
400
|
* @description todo: logic for if user wanted capture at approval
|
403
|
-
*
|
404
401
|
* @param {CheckoutCreateResult} create_result
|
405
402
|
*/
|
406
403
|
async refund(create_result) {
|