@vendure/payments-plugin 1.4.7 → 1.5.2
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/README.md +3 -0
- package/package/braintree/types.d.ts +2 -1
- package/package/braintree/types.js +1 -0
- package/package/braintree/types.js.map +1 -1
- package/package/mollie/graphql/generated-shop-types.d.ts +2894 -0
- package/package/mollie/graphql/generated-shop-types.js +952 -0
- package/package/mollie/graphql/generated-shop-types.js.map +1 -0
- package/package/mollie/mollie-shop-schema.d.ts +1 -0
- package/package/mollie/mollie-shop-schema.js +21 -0
- package/package/mollie/mollie-shop-schema.js.map +1 -0
- package/package/mollie/mollie.controller.d.ts +3 -7
- package/package/mollie/mollie.controller.js +11 -61
- package/package/mollie/mollie.controller.js.map +1 -1
- package/package/mollie/mollie.handler.d.ts +4 -0
- package/package/mollie/mollie.handler.js +18 -51
- package/package/mollie/mollie.handler.js.map +1 -1
- package/package/mollie/mollie.plugin.d.ts +13 -19
- package/package/mollie/mollie.plugin.js +24 -20
- package/package/mollie/mollie.plugin.js.map +1 -1
- package/package/mollie/mollie.resolver.d.ts +11 -0
- package/package/mollie/mollie.resolver.js +56 -0
- package/package/mollie/mollie.resolver.js.map +1 -0
- package/package/mollie/mollie.service.d.ts +27 -0
- package/package/mollie/mollie.service.js +189 -0
- package/package/mollie/mollie.service.js.map +1 -0
- package/package/stripe/constants.d.ts +2 -0
- package/package/stripe/constants.js +6 -0
- package/package/stripe/constants.js.map +1 -0
- package/package/stripe/index.d.ts +2 -0
- package/package/stripe/index.js +15 -0
- package/package/stripe/index.js.map +1 -0
- package/package/stripe/raw-body.middleware.d.ts +6 -0
- package/package/stripe/raw-body.middleware.js +17 -0
- package/package/stripe/raw-body.middleware.js.map +1 -0
- package/package/stripe/stripe.controller.d.ts +14 -0
- package/package/stripe/stripe.controller.js +127 -0
- package/package/stripe/stripe.controller.js.map +1 -0
- package/package/stripe/stripe.handler.d.ts +5 -0
- package/package/stripe/stripe.handler.js +72 -0
- package/package/stripe/stripe.handler.js.map +1 -0
- package/package/stripe/stripe.plugin.d.ts +80 -0
- package/package/stripe/stripe.plugin.js +140 -0
- package/package/stripe/stripe.plugin.js.map +1 -0
- package/package/stripe/stripe.resolver.d.ts +8 -0
- package/package/stripe/stripe.resolver.js +46 -0
- package/package/stripe/stripe.resolver.js.map +1 -0
- package/package/stripe/stripe.service.d.ts +19 -0
- package/package/stripe/stripe.service.js +114 -0
- package/package/stripe/stripe.service.js.map +1 -0
- package/package/stripe/types.d.ts +40 -0
- package/package/stripe/types.js +4 -0
- package/package/stripe/types.js.map +1 -0
- package/package.json +9 -7
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
15
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
16
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
17
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
18
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
19
|
+
};
|
|
20
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
21
|
+
if (mod && mod.__esModule) return mod;
|
|
22
|
+
var result = {};
|
|
23
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
24
|
+
__setModuleDefault(result, mod);
|
|
25
|
+
return result;
|
|
26
|
+
};
|
|
27
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
28
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
29
|
+
};
|
|
30
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
31
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
32
|
+
};
|
|
33
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
34
|
+
exports.MollieService = void 0;
|
|
35
|
+
const api_client_1 = __importStar(require("@mollie/api-client"));
|
|
36
|
+
const common_1 = require("@nestjs/common");
|
|
37
|
+
const core_1 = require("@vendure/core");
|
|
38
|
+
const generated_graphql_shop_errors_1 = require("@vendure/core/dist/common/error/generated-graphql-shop-errors");
|
|
39
|
+
const constants_1 = require("./constants");
|
|
40
|
+
const generated_shop_types_1 = require("./graphql/generated-shop-types");
|
|
41
|
+
class PaymentIntentError {
|
|
42
|
+
constructor(message) {
|
|
43
|
+
this.message = message;
|
|
44
|
+
this.errorCode = generated_shop_types_1.ErrorCode.ORDER_PAYMENT_STATE_ERROR;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
let MollieService = class MollieService {
|
|
48
|
+
constructor(paymentMethodService, options, activeOrderService, orderService, channelService, entityHydrator) {
|
|
49
|
+
this.paymentMethodService = paymentMethodService;
|
|
50
|
+
this.options = options;
|
|
51
|
+
this.activeOrderService = activeOrderService;
|
|
52
|
+
this.orderService = orderService;
|
|
53
|
+
this.channelService = channelService;
|
|
54
|
+
this.entityHydrator = entityHydrator;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Creates a redirectUrl to Mollie for the given paymentMethod and current activeOrder
|
|
58
|
+
*/
|
|
59
|
+
async createPaymentIntent(ctx, paymentMethodCode) {
|
|
60
|
+
var _a, _b;
|
|
61
|
+
const [order, paymentMethods] = await Promise.all([
|
|
62
|
+
this.activeOrderService.getOrderFromContext(ctx),
|
|
63
|
+
this.paymentMethodService.findAll(ctx),
|
|
64
|
+
]);
|
|
65
|
+
if (!order) {
|
|
66
|
+
return new PaymentIntentError('No active order found for session');
|
|
67
|
+
}
|
|
68
|
+
await this.entityHydrator.hydrate(ctx, order, { relations: ['lines', 'customer', 'shippingLines'] });
|
|
69
|
+
if (!((_a = order.lines) === null || _a === void 0 ? void 0 : _a.length)) {
|
|
70
|
+
return new PaymentIntentError('Cannot create payment intent for empty order');
|
|
71
|
+
}
|
|
72
|
+
if (!order.customer) {
|
|
73
|
+
return new PaymentIntentError('Cannot create payment intent for order without customer');
|
|
74
|
+
}
|
|
75
|
+
if (!((_b = order.shippingLines) === null || _b === void 0 ? void 0 : _b.length)) {
|
|
76
|
+
return new PaymentIntentError('Cannot create payment intent for order without shippingMethod');
|
|
77
|
+
}
|
|
78
|
+
const paymentMethod = paymentMethods.items.find(pm => pm.code === paymentMethodCode);
|
|
79
|
+
if (!paymentMethod) {
|
|
80
|
+
return new PaymentIntentError(`No paymentMethod found with code ${paymentMethodCode}`);
|
|
81
|
+
}
|
|
82
|
+
const apiKeyArg = paymentMethod.handler.args.find(arg => arg.name === 'apiKey');
|
|
83
|
+
const redirectUrlArg = paymentMethod.handler.args.find(arg => arg.name === 'redirectUrl');
|
|
84
|
+
if (!apiKeyArg || !redirectUrlArg) {
|
|
85
|
+
core_1.Logger.warn(`CreatePaymentIntent failed, because no apiKey or redirect is configured for ${paymentMethod.code}`, constants_1.loggerCtx);
|
|
86
|
+
return new PaymentIntentError(`Paymentmethod ${paymentMethod.code} has no apiKey or redirectUrl configured`);
|
|
87
|
+
}
|
|
88
|
+
const apiKey = apiKeyArg.value;
|
|
89
|
+
let redirectUrl = redirectUrlArg.value;
|
|
90
|
+
const mollieClient = api_client_1.default({ apiKey });
|
|
91
|
+
redirectUrl = redirectUrl.endsWith('/') ? redirectUrl.slice(0, -1) : redirectUrl; // remove appending slash
|
|
92
|
+
const vendureHost = this.options.vendureHost.endsWith('/')
|
|
93
|
+
? this.options.vendureHost.slice(0, -1)
|
|
94
|
+
: this.options.vendureHost; // remove appending slash
|
|
95
|
+
const payment = await mollieClient.payments.create({
|
|
96
|
+
amount: {
|
|
97
|
+
value: `${(order.totalWithTax / 100).toFixed(2)}`,
|
|
98
|
+
currency: order.currencyCode,
|
|
99
|
+
},
|
|
100
|
+
metadata: {
|
|
101
|
+
orderCode: order.code,
|
|
102
|
+
},
|
|
103
|
+
description: `Order ${order.code}`,
|
|
104
|
+
redirectUrl: `${redirectUrl}/${order.code}`,
|
|
105
|
+
webhookUrl: `${vendureHost}/payments/mollie/${ctx.channel.token}/${paymentMethod.id}`,
|
|
106
|
+
});
|
|
107
|
+
const url = payment.getCheckoutUrl();
|
|
108
|
+
if (!url) {
|
|
109
|
+
throw Error(`Unable to getCheckoutUrl() from Mollie payment`);
|
|
110
|
+
}
|
|
111
|
+
return {
|
|
112
|
+
url,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Makes a request to Mollie to verify the given payment by id
|
|
117
|
+
*/
|
|
118
|
+
async settlePayment({ channelToken, paymentMethodId, paymentId }) {
|
|
119
|
+
var _a;
|
|
120
|
+
const ctx = await this.createContext(channelToken);
|
|
121
|
+
core_1.Logger.info(`Received payment for ${channelToken}`, constants_1.loggerCtx);
|
|
122
|
+
const paymentMethod = await this.paymentMethodService.findOne(ctx, paymentMethodId);
|
|
123
|
+
if (!paymentMethod) {
|
|
124
|
+
// Fail silently, as we don't want to expose if a paymentMethodId exists or not
|
|
125
|
+
return core_1.Logger.warn(`No paymentMethod found with id ${paymentMethodId}`, constants_1.loggerCtx);
|
|
126
|
+
}
|
|
127
|
+
const apiKey = (_a = paymentMethod.handler.args.find(a => a.name === 'apiKey')) === null || _a === void 0 ? void 0 : _a.value;
|
|
128
|
+
if (!apiKey) {
|
|
129
|
+
throw Error(`No apiKey found for payment ${paymentMethod.id} for channel ${channelToken}`);
|
|
130
|
+
}
|
|
131
|
+
const client = api_client_1.default({ apiKey });
|
|
132
|
+
const molliePayment = await client.payments.get(paymentId);
|
|
133
|
+
const orderCode = molliePayment.metadata.orderCode;
|
|
134
|
+
if (molliePayment.status !== api_client_1.PaymentStatus.paid) {
|
|
135
|
+
return core_1.Logger.warn(`Received payment for ${channelToken} for order ${orderCode} with status ${molliePayment.status}`, constants_1.loggerCtx);
|
|
136
|
+
}
|
|
137
|
+
if (!orderCode) {
|
|
138
|
+
throw Error(`Molliepayment does not have metadata.orderCode, unable to settle payment ${molliePayment.id}!`);
|
|
139
|
+
}
|
|
140
|
+
core_1.Logger.info(`Received payment ${molliePayment.id} for order ${orderCode} with status ${molliePayment.status}`, constants_1.loggerCtx);
|
|
141
|
+
const order = await this.orderService.findOneByCode(ctx, orderCode);
|
|
142
|
+
if (!order) {
|
|
143
|
+
throw Error(`Unable to find order ${orderCode}, unable to settle payment ${molliePayment.id}!`);
|
|
144
|
+
}
|
|
145
|
+
if (order.state !== 'ArrangingPayment') {
|
|
146
|
+
const transitionToStateResult = await this.orderService.transitionToState(ctx, order.id, 'ArrangingPayment');
|
|
147
|
+
if (transitionToStateResult instanceof generated_graphql_shop_errors_1.OrderStateTransitionError) {
|
|
148
|
+
throw Error(`Error transitioning order ${order.code} from ${transitionToStateResult.fromState} to ${transitionToStateResult.toState}: ${transitionToStateResult.message}`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
const addPaymentToOrderResult = await this.orderService.addPaymentToOrder(ctx, order.id, {
|
|
152
|
+
method: paymentMethod.code,
|
|
153
|
+
metadata: {
|
|
154
|
+
paymentId: molliePayment.id,
|
|
155
|
+
mode: molliePayment.mode,
|
|
156
|
+
method: molliePayment.method,
|
|
157
|
+
profileId: molliePayment.profileId,
|
|
158
|
+
settlementAmount: molliePayment.settlementAmount,
|
|
159
|
+
customerId: molliePayment.customerId,
|
|
160
|
+
authorizedAt: molliePayment.authorizedAt,
|
|
161
|
+
paidAt: molliePayment.paidAt,
|
|
162
|
+
},
|
|
163
|
+
});
|
|
164
|
+
if (!(addPaymentToOrderResult instanceof core_1.Order)) {
|
|
165
|
+
throw Error(`Error adding payment to order ${orderCode}: ${addPaymentToOrderResult.message}`);
|
|
166
|
+
}
|
|
167
|
+
core_1.Logger.info(`Payment for order ${molliePayment.metadata.orderCode} settled`, constants_1.loggerCtx);
|
|
168
|
+
}
|
|
169
|
+
async createContext(channelToken) {
|
|
170
|
+
const channel = await this.channelService.getChannelFromToken(channelToken);
|
|
171
|
+
return new core_1.RequestContext({
|
|
172
|
+
apiType: 'admin',
|
|
173
|
+
isAuthorized: true,
|
|
174
|
+
authorizedAsOwnerOnly: false,
|
|
175
|
+
channel,
|
|
176
|
+
languageCode: core_1.LanguageCode.en,
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
MollieService = __decorate([
|
|
181
|
+
common_1.Injectable(),
|
|
182
|
+
__param(1, common_1.Inject(constants_1.PLUGIN_INIT_OPTIONS)),
|
|
183
|
+
__metadata("design:paramtypes", [core_1.PaymentMethodService, Object, core_1.ActiveOrderService,
|
|
184
|
+
core_1.OrderService,
|
|
185
|
+
core_1.ChannelService,
|
|
186
|
+
core_1.EntityHydrator])
|
|
187
|
+
], MollieService);
|
|
188
|
+
exports.MollieService = MollieService;
|
|
189
|
+
//# sourceMappingURL=mollie.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mollie.service.js","sourceRoot":"","sources":["../../src/mollie/mollie.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iEAAuE;AACvE,2CAAoD;AACpD,wCAUuB;AACvB,iHAA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oB;QAC5C,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC,mBAAmB,CAAC,YAAY,CAAC,CAAC;QAC5E,OAAO,IAAI,qBAAc,CAAC;YACtB,OAAO,EAAE,OAAO;YAChB,YAAY,EAAE,IAAI;YAClB,qBAAqB,EAAE,KAAK;YAC5B,OAAO;YACP,YAAY,EAAE,mBAAY,CAAC,EAAE;SAChC,CAAC,CAAC;IACP,CAAC;CACJ,CAAA;AAnJY,aAAa;IADzB,mBAAU,EAAE;IAIJ,WAAA,eAAM,CAAC,+BAAmB,CAAC,CAAA;qCADE,2BAAoB,UAEtB,yBAAkB;QACxB,mBAAY;QACV,qBAAc;QACd,qBAAc;GAPjC,aAAa,CAmJzB;AAnJY,sCAAa"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.STRIPE_PLUGIN_OPTIONS = exports.loggerCtx = void 0;
|
|
4
|
+
exports.loggerCtx = 'StripePlugin';
|
|
5
|
+
exports.STRIPE_PLUGIN_OPTIONS = Symbol('STRIPE_PLUGIN_OPTIONS');
|
|
6
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/stripe/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,SAAS,GAAG,cAAc,CAAC;AAC3B,QAAA,qBAAqB,GAAG,MAAM,CAAC,uBAAuB,CAAC,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
+
};
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
__exportStar(require("./stripe.plugin"), exports);
|
|
14
|
+
__exportStar(require("./"), exports);
|
|
15
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/stripe/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,kDAAgC;AAChC,qCAAmB"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.rawBodyMiddleware = void 0;
|
|
4
|
+
const body_parser_1 = require("body-parser");
|
|
5
|
+
/**
|
|
6
|
+
* Middleware which adds the raw request body to the incoming message object. This is needed by
|
|
7
|
+
* Stripe to properly verify webhook events.
|
|
8
|
+
*/
|
|
9
|
+
exports.rawBodyMiddleware = body_parser_1.json({
|
|
10
|
+
verify(req, _, buf) {
|
|
11
|
+
if (Buffer.isBuffer(buf)) {
|
|
12
|
+
req.rawBody = Buffer.from(buf);
|
|
13
|
+
}
|
|
14
|
+
return true;
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
//# sourceMappingURL=raw-body.middleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"raw-body.middleware.js","sourceRoot":"","sources":["../../src/stripe/raw-body.middleware.ts"],"names":[],"mappings":";;;AAAA,6CAAmC;AAKnC;;;GAGG;AACU,QAAA,iBAAiB,GAAG,kBAAI,CAAC;IAClC,MAAM,CAAC,GAA+B,EAAE,CAAiB,EAAE,GAAW;QAClE,IAAI,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE;YACtB,GAAG,CAAC,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;SAClC;QAED,OAAO,IAAI,CAAC;IAChB,CAAC;CACJ,CAAC,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ChannelService, OrderService, TransactionalConnection } from '@vendure/core';
|
|
2
|
+
import { Response } from 'express';
|
|
3
|
+
import { StripeService } from './stripe.service';
|
|
4
|
+
import { IncomingMessageWithRawBody } from './types';
|
|
5
|
+
export declare class StripeController {
|
|
6
|
+
private connection;
|
|
7
|
+
private channelService;
|
|
8
|
+
private orderService;
|
|
9
|
+
private stripeService;
|
|
10
|
+
constructor(connection: TransactionalConnection, channelService: ChannelService, orderService: OrderService, stripeService: StripeService);
|
|
11
|
+
webhook(signature: string | undefined, request: IncomingMessageWithRawBody, response: Response): Promise<void>;
|
|
12
|
+
private createContext;
|
|
13
|
+
private getPaymentMethod;
|
|
14
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.StripeController = void 0;
|
|
16
|
+
const common_1 = require("@nestjs/common");
|
|
17
|
+
const core_1 = require("@vendure/core");
|
|
18
|
+
const generated_graphql_shop_errors_1 = require("@vendure/core/dist/common/error/generated-graphql-shop-errors");
|
|
19
|
+
const constants_1 = require("./constants");
|
|
20
|
+
const stripe_handler_1 = require("./stripe.handler");
|
|
21
|
+
const stripe_service_1 = require("./stripe.service");
|
|
22
|
+
const missingHeaderErrorMessage = 'Missing stripe-signature header';
|
|
23
|
+
const signatureErrorMessage = 'Error verifying Stripe webhook signature';
|
|
24
|
+
const noPaymentIntentErrorMessage = 'No payment intent in the event payload';
|
|
25
|
+
let StripeController = class StripeController {
|
|
26
|
+
constructor(connection, channelService, orderService, stripeService) {
|
|
27
|
+
this.connection = connection;
|
|
28
|
+
this.channelService = channelService;
|
|
29
|
+
this.orderService = orderService;
|
|
30
|
+
this.stripeService = stripeService;
|
|
31
|
+
}
|
|
32
|
+
async webhook(signature, request, response) {
|
|
33
|
+
var _a;
|
|
34
|
+
if (!signature) {
|
|
35
|
+
core_1.Logger.error(missingHeaderErrorMessage, constants_1.loggerCtx);
|
|
36
|
+
response.status(common_1.HttpStatus.BAD_REQUEST).send(missingHeaderErrorMessage);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
let event = null;
|
|
40
|
+
try {
|
|
41
|
+
event = this.stripeService.constructEventFromPayload(request.rawBody, signature);
|
|
42
|
+
}
|
|
43
|
+
catch (e) {
|
|
44
|
+
core_1.Logger.error(`${signatureErrorMessage} ${signature}: ${e.message}`, constants_1.loggerCtx);
|
|
45
|
+
response.status(common_1.HttpStatus.BAD_REQUEST).send(signatureErrorMessage);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
const paymentIntent = event.data.object;
|
|
49
|
+
if (!paymentIntent) {
|
|
50
|
+
core_1.Logger.error(noPaymentIntentErrorMessage, constants_1.loggerCtx);
|
|
51
|
+
response.status(common_1.HttpStatus.BAD_REQUEST).send(noPaymentIntentErrorMessage);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const { metadata: { channelToken, orderCode, orderId } = {} } = paymentIntent;
|
|
55
|
+
if (event.type === 'payment_intent.payment_failed') {
|
|
56
|
+
const message = (_a = paymentIntent.last_payment_error) === null || _a === void 0 ? void 0 : _a.message;
|
|
57
|
+
core_1.Logger.warn(`Payment for order ${orderCode} failed: ${message}`, constants_1.loggerCtx);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
if (event.type !== 'payment_intent.succeeded') {
|
|
61
|
+
// This should never happen as the webhook is configured to receive
|
|
62
|
+
// payment_intent.succeeded and payment_intent.payment_failed events only
|
|
63
|
+
core_1.Logger.info(`Received ${event.type} status update for order ${orderCode}`, constants_1.loggerCtx);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
const ctx = await this.createContext(channelToken);
|
|
67
|
+
const order = await this.orderService.findOneByCode(ctx, orderCode);
|
|
68
|
+
if (!order) {
|
|
69
|
+
throw Error(`Unable to find order ${orderCode}, unable to settle payment ${paymentIntent.id}!`);
|
|
70
|
+
}
|
|
71
|
+
if (order.state !== 'ArrangingPayment') {
|
|
72
|
+
const transitionToStateResult = await this.orderService.transitionToState(ctx, orderId, 'ArrangingPayment');
|
|
73
|
+
if (transitionToStateResult instanceof generated_graphql_shop_errors_1.OrderStateTransitionError) {
|
|
74
|
+
core_1.Logger.error(`Error transitioning order ${orderCode} to ArrangingPayment state: ${transitionToStateResult.message}`, constants_1.loggerCtx);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const paymentMethod = await this.getPaymentMethod(ctx);
|
|
79
|
+
const addPaymentToOrderResult = await this.orderService.addPaymentToOrder(ctx, orderId, {
|
|
80
|
+
method: paymentMethod.code,
|
|
81
|
+
metadata: {
|
|
82
|
+
paymentIntentId: paymentIntent.id,
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
if (!(addPaymentToOrderResult instanceof core_1.Order)) {
|
|
86
|
+
core_1.Logger.error(`Error adding payment to order ${orderCode}: ${addPaymentToOrderResult.message}`, constants_1.loggerCtx);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
core_1.Logger.info(`Stripe payment intent id ${paymentIntent.id} added to order ${orderCode}`, constants_1.loggerCtx);
|
|
90
|
+
response.status(common_1.HttpStatus.OK).send('Ok');
|
|
91
|
+
}
|
|
92
|
+
async createContext(channelToken) {
|
|
93
|
+
const channel = await this.channelService.getChannelFromToken(channelToken);
|
|
94
|
+
return new core_1.RequestContext({
|
|
95
|
+
apiType: 'admin',
|
|
96
|
+
isAuthorized: true,
|
|
97
|
+
authorizedAsOwnerOnly: false,
|
|
98
|
+
channel,
|
|
99
|
+
languageCode: core_1.LanguageCode.en,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
async getPaymentMethod(ctx) {
|
|
103
|
+
const method = (await this.connection.getRepository(ctx, core_1.PaymentMethod).find()).find(m => m.handler.code === stripe_handler_1.stripePaymentMethodHandler.code);
|
|
104
|
+
if (!method) {
|
|
105
|
+
throw new core_1.InternalServerError(`[${constants_1.loggerCtx}] Could not find Stripe PaymentMethod`);
|
|
106
|
+
}
|
|
107
|
+
return method;
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
__decorate([
|
|
111
|
+
common_1.Post('stripe'),
|
|
112
|
+
__param(0, common_1.Headers('stripe-signature')),
|
|
113
|
+
__param(1, common_1.Req()),
|
|
114
|
+
__param(2, common_1.Res()),
|
|
115
|
+
__metadata("design:type", Function),
|
|
116
|
+
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
117
|
+
__metadata("design:returntype", Promise)
|
|
118
|
+
], StripeController.prototype, "webhook", null);
|
|
119
|
+
StripeController = __decorate([
|
|
120
|
+
common_1.Controller('payments'),
|
|
121
|
+
__metadata("design:paramtypes", [core_1.TransactionalConnection,
|
|
122
|
+
core_1.ChannelService,
|
|
123
|
+
core_1.OrderService,
|
|
124
|
+
stripe_service_1.StripeService])
|
|
125
|
+
], StripeController);
|
|
126
|
+
exports.StripeController = StripeController;
|
|
127
|
+
//# sourceMappingURL=stripe.controller.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stripe.controller.js","sourceRoot":"","sources":["../../src/stripe/stripe.controller.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAAiF;AACjF,wCAUuB;AACvB,iHAA0G;AAG1G,2CAAwC;AACxC,qDAA8D;AAC9D,qDAAiD;AAIjD,MAAM,yBAAyB,GAAG,iCAAiC,CAAC;AACpE,MAAM,qBAAqB,GAAG,0CAA0C,CAAC;AACzE,MAAM,2BAA2B,GAAG,wCAAwC,CAAC;AAG7E,IAAa,gBAAgB,GAA7B,MAAa,gBAAgB;IACzB,YACY,UAAmC,EACnC,cAA8B,EAC9B,YAA0B,EAC1B,aAA4B;QAH5B,eAAU,GAAV,UAAU,CAAyB;QACnC,mBAAc,GAAd,cAAc,CAAgB;QAC9B,iBAAY,GAAZ,YAAY,CAAc;QAC1B,kBAAa,GAAb,aAAa,CAAe;IACrC,CAAC;IAGJ,KAAK,CAAC,OAAO,CACoB,SAA6B,EACnD,OAAmC,EACnC,QAAkB;;QAEzB,IAAI,CAAC,SAAS,EAAE;YACZ,aAAM,CAAC,KAAK,CAAC,yBAAyB,EAAE,qBAAS,CAAC,CAAC;YACnD,QAAQ,CAAC,MAAM,CAAC,mBAAU,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,yBAAyB,CAAC,CAAC;YACxE,OAAO;SACV;QAED,IAAI,KAAK,GAAG,IAAI,CAAC;QACjB,IAAI;YACA,KAAK,GAAG,IAAI,CAAC,aAAa,CAAC,yBAAyB,CAAC,OAAO,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;SACpF;QAAC,OAAO,CAAM,EAAE;YACb,aAAM,CAAC,KAAK,CAAC,GAAG,q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AY,EAAE,IAAI;YAClB,qBAAqB,EAAE,KAAK;YAC5B,OAAO;YACP,YAAY,EAAE,mBAAY,CAAC,EAAE;SAChC,CAAC,CAAC;IACP,CAAC;IAEO,KAAK,CAAC,gBAAgB,CAAC,GAAmB;QAC9C,MAAM,MAAM,GAAG,CAAC,MAAM,IAAI,CAAC,UAAU,CAAC,aAAa,CAAC,GAAG,EAAE,oBAAa,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,CAChF,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,KAAK,2CAA0B,CAAC,IAAI,CAC1D,CAAC;QAEF,IAAI,CAAC,MAAM,EAAE;YACT,MAAM,IAAI,0BAAmB,CAAC,IAAI,qBAAS,uCAAuC,CAAC,CAAC;SACvF;QAED,OAAO,MAAM,CAAC;IAClB,CAAC;CACJ,CAAA;AA7GG;IADC,aAAI,CAAC,QAAQ,CAAC;IAEV,WAAA,gBAAO,CAAC,kBAAkB,CAAC,CAAA;IAC3B,WAAA,YAAG,EAAE,CAAA;IACL,WAAA,YAAG,EAAE,CAAA;;;;+CAiFT;AA7FQ,gBAAgB;IAD5B,mBAAU,CAAC,UAAU,CAAC;qCAGK,8BAAuB;QACnB,qBAAc;QAChB,mBAAY;QACX,8BAAa;GAL/B,gBAAgB,CAsH5B;AAtHY,4CAAgB"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.stripePaymentMethodHandler = void 0;
|
|
7
|
+
const core_1 = require("@vendure/core");
|
|
8
|
+
const stripe_1 = __importDefault(require("stripe"));
|
|
9
|
+
const stripe_service_1 = require("./stripe.service");
|
|
10
|
+
const { StripeError } = stripe_1.default.errors;
|
|
11
|
+
let stripeService;
|
|
12
|
+
/**
|
|
13
|
+
* The handler for Stripe payments.
|
|
14
|
+
*/
|
|
15
|
+
exports.stripePaymentMethodHandler = new core_1.PaymentMethodHandler({
|
|
16
|
+
code: 'stripe',
|
|
17
|
+
description: [{ languageCode: core_1.LanguageCode.en, value: 'Stripe payments' }],
|
|
18
|
+
args: {},
|
|
19
|
+
init(injector) {
|
|
20
|
+
stripeService = injector.get(stripe_service_1.StripeService);
|
|
21
|
+
},
|
|
22
|
+
async createPayment(ctx, _, amount, ___, metadata) {
|
|
23
|
+
// Payment is already settled in Stripe by the time the webhook in stripe.controller.ts
|
|
24
|
+
// adds the payment to the order
|
|
25
|
+
if (ctx.apiType !== 'admin') {
|
|
26
|
+
throw Error(`CreatePayment is not allowed for apiType '${ctx.apiType}'`);
|
|
27
|
+
}
|
|
28
|
+
return {
|
|
29
|
+
amount,
|
|
30
|
+
state: 'Settled',
|
|
31
|
+
transactionId: metadata.paymentIntentId,
|
|
32
|
+
};
|
|
33
|
+
},
|
|
34
|
+
settlePayment() {
|
|
35
|
+
return {
|
|
36
|
+
success: true,
|
|
37
|
+
};
|
|
38
|
+
},
|
|
39
|
+
async createRefund(ctx, input, amount, order, payment, args) {
|
|
40
|
+
const result = await stripeService.createRefund(payment.transactionId, amount);
|
|
41
|
+
if (result instanceof StripeError) {
|
|
42
|
+
return {
|
|
43
|
+
state: 'Failed',
|
|
44
|
+
transactionId: payment.transactionId,
|
|
45
|
+
metadata: {
|
|
46
|
+
type: result.type,
|
|
47
|
+
message: result.message,
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
if (result.status === 'succeeded') {
|
|
52
|
+
return {
|
|
53
|
+
state: 'Settled',
|
|
54
|
+
transactionId: payment.transactionId,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
if (result.status === 'pending') {
|
|
58
|
+
return {
|
|
59
|
+
state: 'Pending',
|
|
60
|
+
transactionId: payment.transactionId,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
state: 'Failed',
|
|
65
|
+
transactionId: payment.transactionId,
|
|
66
|
+
metadata: {
|
|
67
|
+
message: result.failure_reason,
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
//# sourceMappingURL=stripe.handler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stripe.handler.js","sourceRoot":"","sources":["../../src/stripe/stripe.handler.ts"],"names":[],"mappings":";;;;;;AAAA,wCAOuB;AACvB,oDAA4B;AAE5B,qDAAiD;AAEjD,MAAM,EAAE,WAAW,EAAE,GAAG,gBAAM,CAAC,MAAM,CAAC;AAEtC,IAAI,aAA4B,CAAC;AAEjC;;GAEG;AACU,QAAA,0BAA0B,GAAG,IAAI,2BAAoB,CAAC;IAC/D,IAAI,EAAE,QAAQ;IAEd,WAAW,EAAE,CAAC,EAAE,YAAY,EAAE,mBAAY,CAAC,EAAE,EAAE,KAAK,EAAE,iBAAiB,EAAE,CAAC;IAE1E,IAAI,EAAE,EAAE;IAER,IAAI,CAAC,QAAkB;QACnB,aAAa,GAAG,QAAQ,CAAC,GAAG,CAAC,8BAAa,CAAC,CAAC;IAChD,CAAC;IAED,KAAK,CAAC,aAAa,CAAC,GAAG,EAAE,CAAC,EAAE,MAAM,EAAE,GAAG,EAAE,QAAQ;QAC7C,uFAAuF;QACvF,gCAAgC;QAChC,IAAI,GAAG,CAAC,OAAO,KAAK,OAAO,EAAE;YACzB,MAAM,KAAK,CAAC,6CAA6C,GAAG,CAAC,OAAO,GAAG,CAAC,CAAC;SAC5E;QACD,OAAO;YACH,MAAM;YACN,KAAK,EAAE,SAAkB;YACzB,aAAa,EAAE,QAAQ,CAAC,eAAe;SAC1C,CAAC;IACN,CAAC;IAED,aAAa;QACT,OAAO;YACH,OAAO,EAAE,IAAI;SAChB,CAAC;IACN,CAAC;IAED,KAAK,CAAC,YAAY,CAAC,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,IAAI;QACvD,MAAM,MAAM,GAAG,MAAM,aAAa,CAAC,YAAY,CAAC,OAAO,CAAC,aAAa,EAAE,MAAM,CAAC,CAAC;QAE/E,IAAI,MAAM,YAAY,WAAW,EAAE;YAC/B,OAAO;gBACH,KAAK,EAAE,QAAiB;gBACxB,aAAa,EAAE,OAAO,CAAC,aAAa;gBACpC,QAAQ,EAAE;oBACN,IAAI,EAAE,MAAM,CAAC,IAAI;oBACjB,OAAO,EAAE,MAAM,CAAC,OAAO;iBAC1B;aACJ,CAAC;SACL;QAED,IAAI,MAAM,CAAC,MAAM,KAAK,WAAW,EAAE;YAC/B,OAAO;gBACH,KAAK,EAAE,SAAkB;gBACzB,aAAa,EAAE,OAAO,CAAC,aAAa;aACvC,CAAC;SACL;QAED,IAAI,MAAM,CAAC,MAAM,KAAK,SAAS,EAAE;YAC7B,OAAO;gBACH,KAAK,EAAE,SAAkB;gBACzB,aAAa,EAAE,OAAO,CAAC,aAAa;aACvC,CAAC;SACL;QAED,OAAO;YACH,KAAK,EAAE,QAAiB;YACxB,aAAa,EAAE,OAAO,CAAC,aAAa;YACpC,QAAQ,EAAE;gBACN,OAAO,EAAE,MAAM,CAAC,cAAc;aACjC;SACJ,CAAC;IACN,CAAC;CACJ,CAAC,CAAC"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { Type } from '@vendure/core';
|
|
2
|
+
import { StripePluginOptions } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* @description
|
|
5
|
+
* Plugin to enable payments through [Stripe](https://stripe.com/docs) via the Payment Intents API.
|
|
6
|
+
*
|
|
7
|
+
* ## Requirements
|
|
8
|
+
*
|
|
9
|
+
* 1. You will need to create a Stripe account and get your secret key in the dashboard.
|
|
10
|
+
* 2. Create a webhook endpoint in the Stripe dashboard which listens to the `payment_intent.succeeded` and
|
|
11
|
+
* `payment_intent.payment_failed` events. The URL should be `https://my-shop.com/payments/stripe`, where
|
|
12
|
+
* `my-shop.com` is the host of your storefront application.
|
|
13
|
+
* 3. Get the signing secret for the newly created webhook.
|
|
14
|
+
* 4. Install the Payments plugin and the Stripe Node library:
|
|
15
|
+
*
|
|
16
|
+
* `yarn add \@vendure/payments-plugin stripe`
|
|
17
|
+
*
|
|
18
|
+
* or
|
|
19
|
+
*
|
|
20
|
+
* `npm install \@vendure/payments-plugin stripe`
|
|
21
|
+
*
|
|
22
|
+
* ## Setup
|
|
23
|
+
*
|
|
24
|
+
* 1. Add the plugin to your VendureConfig `plugins` array:
|
|
25
|
+
* ```TypeScript
|
|
26
|
+
* import { StripePlugin } from '\@vendure/payments-plugin/package/stripe';
|
|
27
|
+
*
|
|
28
|
+
* // ...
|
|
29
|
+
*
|
|
30
|
+
* plugins: [
|
|
31
|
+
* StripePlugin.init({
|
|
32
|
+
* apiKey: process.env.YOUR_STRIPE_SECRET_KEY,
|
|
33
|
+
* webhookSigningSecret: process.env.YOUR_STRIPE_WEBHOOK_SIGNING_SECRET,
|
|
34
|
+
* // This prevents different customers from using the same PaymentIntent
|
|
35
|
+
* storeCustomersInStripe: true,
|
|
36
|
+
* }),
|
|
37
|
+
* ]
|
|
38
|
+
* ````
|
|
39
|
+
* 2. Create a new PaymentMethod in the Admin UI, and select "Stripe payments" as the handler.
|
|
40
|
+
*
|
|
41
|
+
* ## Storefront usage
|
|
42
|
+
*
|
|
43
|
+
* The plugin is designed to work with the [Custom payment flow](https://stripe.com/docs/payments/accept-a-payment?platform=web&ui=elements).
|
|
44
|
+
* In this flow, Stripe provides libraries which handle the payment UI and confirmation for you. You can install it in your storefront project
|
|
45
|
+
* with:
|
|
46
|
+
*
|
|
47
|
+
* ```shell
|
|
48
|
+
* yarn add \@stripe/stripe-js
|
|
49
|
+
* # or
|
|
50
|
+
* npm install \@stripe/stripe-js
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* If you are using React, you should also consider installing `@stripe/react-stripe-js`, which is a wrapper around Stripe Elements.
|
|
54
|
+
*
|
|
55
|
+
* The high-level workflow is:
|
|
56
|
+
* 1. Create a "payment intent" on the server by executing the `createStripePaymentIntent` mutation which is exposed by this plugin.
|
|
57
|
+
* 2. Use the returned client secret to instantiate the Stripe Payment Element.
|
|
58
|
+
* 3. Once the form is submitted and Stripe processes the payment, the webhook takes care of updating the order without additional action
|
|
59
|
+
* in the storefront.
|
|
60
|
+
*
|
|
61
|
+
* ## Local development
|
|
62
|
+
*
|
|
63
|
+
* Use something like [localtunnel](https://github.com/localtunnel/localtunnel) to test on localhost.
|
|
64
|
+
*
|
|
65
|
+
* ```bash
|
|
66
|
+
* npx localtunnel --port 3000 --subdomain my-shop-local-dev
|
|
67
|
+
* > your url is: https://my-shop-local-dev.loca.lt
|
|
68
|
+
* ```
|
|
69
|
+
*
|
|
70
|
+
* @docsCategory payments-plugin
|
|
71
|
+
* @docsPage StripePlugin
|
|
72
|
+
*/
|
|
73
|
+
export declare class StripePlugin {
|
|
74
|
+
static options: StripePluginOptions;
|
|
75
|
+
/**
|
|
76
|
+
* @description
|
|
77
|
+
* Initialize the Stripe payment plugin
|
|
78
|
+
*/
|
|
79
|
+
static init(options: StripePluginOptions): Type<StripePlugin>;
|
|
80
|
+
}
|