@live-change/stripe-service 0.8.108
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/config.js +24 -0
- package/definition.js +13 -0
- package/index.js +9 -0
- package/package.json +34 -0
- package/payment.js +125 -0
- package/webhook.js +34 -0
package/config.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import definition from './definition.js'
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
secretKey,
|
|
5
|
+
publishableKey,
|
|
6
|
+
webhookSecret,
|
|
7
|
+
baseHref = process.env.BASE_HREF || 'http://localhost:8001',
|
|
8
|
+
checkoutSuccessUrl,
|
|
9
|
+
checkoutCancelUrl
|
|
10
|
+
} = definition.config
|
|
11
|
+
|
|
12
|
+
definition.clientConfig = {
|
|
13
|
+
publishableKey
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const config = {
|
|
17
|
+
secretKey,
|
|
18
|
+
publishableKey,
|
|
19
|
+
webhookSecret,
|
|
20
|
+
checkoutSuccessUrl: checkoutSuccessUrl ?? `${baseHref}/stripe/success`,
|
|
21
|
+
checkoutCancelUrl: checkoutCancelUrl ?? `${baseHref}/stripe/cancel`
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export default config
|
package/definition.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import App from '@live-change/framework'
|
|
2
|
+
const app = App.app()
|
|
3
|
+
|
|
4
|
+
import userService from '@live-change/user-service'
|
|
5
|
+
import relationsPlugin from '@live-change/relations-plugin'
|
|
6
|
+
import accessControlService from '@live-change/access-control-service'
|
|
7
|
+
|
|
8
|
+
const definition = app.createServiceDefinition({
|
|
9
|
+
name: "stripe",
|
|
10
|
+
use: [ userService, relationsPlugin, accessControlService ]
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
export default definition
|
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@live-change/stripe-service",
|
|
3
|
+
"version": "0.8.108",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "NODE_ENV=test tape tests/*"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/live-change/live-change-stack.git"
|
|
12
|
+
},
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/live-change/live-change-stack/issues"
|
|
16
|
+
},
|
|
17
|
+
"homepage": "https://github.com/live-change/live-change-stack",
|
|
18
|
+
"author": {
|
|
19
|
+
"email": "michal@laszczewski.pl",
|
|
20
|
+
"name": "Michał Łaszczewski",
|
|
21
|
+
"url": "https://www.viamage.com/"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@live-change/framework": "^0.8.108",
|
|
25
|
+
"@live-change/relations-plugin": "^0.8.108",
|
|
26
|
+
"lru-cache": "^7.12.0",
|
|
27
|
+
"pluralize": "^8.0.0",
|
|
28
|
+
"progress-stream": "^2.0.0",
|
|
29
|
+
"prosemirror-model": "^1.18.1",
|
|
30
|
+
"stripe": "^17.2.1"
|
|
31
|
+
},
|
|
32
|
+
"gitHead": "b272a01967e326d10874fea13353ab8fa5c30fe8",
|
|
33
|
+
"type": "module"
|
|
34
|
+
}
|
package/payment.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import App from '@live-change/framework'
|
|
2
|
+
const app = App.app()
|
|
3
|
+
|
|
4
|
+
import definition from './definition.js'
|
|
5
|
+
import config from './config.js'
|
|
6
|
+
|
|
7
|
+
import stripe from 'stripe'
|
|
8
|
+
|
|
9
|
+
const itemsArray = {
|
|
10
|
+
type: Array,
|
|
11
|
+
of: {
|
|
12
|
+
type: Object,
|
|
13
|
+
properties: {
|
|
14
|
+
name: {
|
|
15
|
+
type: String,
|
|
16
|
+
validation: ['nonEmpty']
|
|
17
|
+
},
|
|
18
|
+
price: {
|
|
19
|
+
type: Number,
|
|
20
|
+
validation: ['nonEmpty', 'number']
|
|
21
|
+
},
|
|
22
|
+
currency: {
|
|
23
|
+
type: String,
|
|
24
|
+
validation: ['nonEmpty']
|
|
25
|
+
},
|
|
26
|
+
quantity: {
|
|
27
|
+
type: Number,
|
|
28
|
+
validation: ['nonEmpty', 'number']
|
|
29
|
+
},
|
|
30
|
+
description: {
|
|
31
|
+
type: String
|
|
32
|
+
},
|
|
33
|
+
taxCode: {
|
|
34
|
+
type: String
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const paymentParameters = {
|
|
41
|
+
items: itemsArray,
|
|
42
|
+
causeType: {
|
|
43
|
+
type: String,
|
|
44
|
+
validation: ['nonEmpty']
|
|
45
|
+
},
|
|
46
|
+
cause: {
|
|
47
|
+
type: String,
|
|
48
|
+
validation: ['nonEmpty']
|
|
49
|
+
},
|
|
50
|
+
successUrl: {
|
|
51
|
+
type: String,
|
|
52
|
+
validation: ['nonEmpty']
|
|
53
|
+
},
|
|
54
|
+
cancelUrl: {
|
|
55
|
+
type: String,
|
|
56
|
+
validation: ['nonEmpty']
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
definition.model({
|
|
61
|
+
name: "Payment",
|
|
62
|
+
entity:{
|
|
63
|
+
},
|
|
64
|
+
properties: {
|
|
65
|
+
...paymentParameters,
|
|
66
|
+
state: {
|
|
67
|
+
type: String,
|
|
68
|
+
options: ['created', 'succeeded', 'failed', 'refunded']
|
|
69
|
+
},
|
|
70
|
+
stripeSession: {
|
|
71
|
+
type: String
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
indexes: {
|
|
75
|
+
}
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
definition.trigger({
|
|
79
|
+
name: 'startPayment',
|
|
80
|
+
properties: {
|
|
81
|
+
...paymentParameters
|
|
82
|
+
},
|
|
83
|
+
async execute({ items, causeType, cause, successUrl, cancelUrl }, { client, service }, emit) {
|
|
84
|
+
console.log("STRIPE SECRET KEY", config.secretKey)
|
|
85
|
+
const payment = app.generateUid()
|
|
86
|
+
const stripeClient = stripe(config.secretKey)
|
|
87
|
+
console.log("URLS", {
|
|
88
|
+
success_url: successUrl ?? (config.checkoutSuccessUrl + '/' + payment),
|
|
89
|
+
cancel_url: cancelUrl ?? (config.checkoutCancelUrl + '/' + payment)
|
|
90
|
+
})
|
|
91
|
+
const session = await stripeClient.checkout.sessions.create({
|
|
92
|
+
payment_method_types: ['card'],
|
|
93
|
+
line_items: items.map(item => ({
|
|
94
|
+
price_data: {
|
|
95
|
+
currency: item.currency,
|
|
96
|
+
product_data: {
|
|
97
|
+
name: item.name,
|
|
98
|
+
description: item.description,
|
|
99
|
+
tax_code: item.taxCode
|
|
100
|
+
},
|
|
101
|
+
unit_amount: item.price
|
|
102
|
+
},
|
|
103
|
+
quantity: item.quantity
|
|
104
|
+
})),
|
|
105
|
+
mode: 'payment',
|
|
106
|
+
success_url: successUrl ?? (config.checkoutSuccessUrl + '/' + payment),
|
|
107
|
+
cancel_url: cancelUrl ?? (config.checkoutCancelUrl + '/' + payment)
|
|
108
|
+
})
|
|
109
|
+
emit({
|
|
110
|
+
type: 'PaymentCreated',
|
|
111
|
+
payment,
|
|
112
|
+
data: {
|
|
113
|
+
items, causeType, cause,
|
|
114
|
+
stripeSession: session.id,
|
|
115
|
+
state: 'created'
|
|
116
|
+
}
|
|
117
|
+
})
|
|
118
|
+
return {
|
|
119
|
+
payment,
|
|
120
|
+
stripeSession: session.id,
|
|
121
|
+
redirectUrl: session.url
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
})
|
|
125
|
+
|
package/webhook.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import App from '@live-change/framework'
|
|
2
|
+
const app = App.app()
|
|
3
|
+
import definition from './definition.js'
|
|
4
|
+
|
|
5
|
+
import crypto from "crypto"
|
|
6
|
+
import express from "express"
|
|
7
|
+
import stripe from "stripe"
|
|
8
|
+
|
|
9
|
+
definition.endpoint({
|
|
10
|
+
name: 'webhook',
|
|
11
|
+
create() {
|
|
12
|
+
const expressApp = express()
|
|
13
|
+
console.log("create stripe webhook")
|
|
14
|
+
expressApp.get('/', (request, response) => {
|
|
15
|
+
// send information that this is stripe webhook
|
|
16
|
+
response.writeHead(200, { "Content-Type": "text/plain" })
|
|
17
|
+
response.end("STRIPE WEBHOOK!")
|
|
18
|
+
})
|
|
19
|
+
expressApp.post('/', express.raw({type: 'application/json'}), async (request, response) => {
|
|
20
|
+
try {
|
|
21
|
+
const event = stripe.webhooks.constructEvent(request.body, sig, config.webhookSecret)
|
|
22
|
+
console.log("STRIPE WEBHOOK", event)
|
|
23
|
+
await app.triggerService({
|
|
24
|
+
service: 'stripe',
|
|
25
|
+
type: `stripe.${event.type}`
|
|
26
|
+
})
|
|
27
|
+
response.json({ received: true })
|
|
28
|
+
} catch (err) {
|
|
29
|
+
response.status(400).send(`Webhook Error: ${err.message}`);
|
|
30
|
+
}
|
|
31
|
+
})
|
|
32
|
+
return expressApp
|
|
33
|
+
}
|
|
34
|
+
})
|