@ngn-net/payment-provider-sdk 1.0.17 → 2.0.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/dist/dto/common.interface.d.ts +60 -0
- package/dist/dto/common.interface.js +6 -0
- package/dist/dto/stripe.dto.d.ts +2 -0
- package/dist/dto/stripe.dto.js +12 -0
- package/dist/payment.provider.d.ts +3 -1
- package/dist/payment.provider.js +12 -20
- package/dist/payment.signature.d.ts +27 -2
- package/dist/payment.signature.js +71 -2
- package/dist/providers/base.provider.d.ts +3 -2
- package/dist/providers/base.provider.js +7 -20
- package/dist/providers/stripe.provider.d.ts +2 -1
- package/dist/providers/stripe.provider.js +4 -0
- package/package.json +2 -2
- package/dist/app.controller.d.ts +0 -6
- package/dist/app.controller.js +0 -33
- package/dist/app.module.d.ts +0 -3
- package/dist/app.module.js +0 -27
- package/dist/app.service.d.ts +0 -6
- package/dist/app.service.js +0 -37
|
@@ -25,6 +25,9 @@ export declare const PaymentRabbitApi: {
|
|
|
25
25
|
stripe_checkout: {
|
|
26
26
|
key: string;
|
|
27
27
|
};
|
|
28
|
+
stripe_checkout_expire: {
|
|
29
|
+
key: string;
|
|
30
|
+
};
|
|
28
31
|
stripe_intent: {
|
|
29
32
|
key: string;
|
|
30
33
|
};
|
|
@@ -49,6 +52,9 @@ export declare const PaymentRabbitApi: {
|
|
|
49
52
|
intent_status: {
|
|
50
53
|
key: string;
|
|
51
54
|
};
|
|
55
|
+
checkout_token: {
|
|
56
|
+
key: string;
|
|
57
|
+
};
|
|
52
58
|
available_methods: {
|
|
53
59
|
key: string;
|
|
54
60
|
};
|
|
@@ -63,10 +69,25 @@ export interface RpcClientContext {
|
|
|
63
69
|
export interface RpcEnvelope<T = unknown> extends RpcClientContext {
|
|
64
70
|
data: T;
|
|
65
71
|
callbackRoutingKey?: string;
|
|
72
|
+
timestamp?: number;
|
|
66
73
|
}
|
|
67
74
|
export interface GetIntentByOrderId {
|
|
68
75
|
orderId: string;
|
|
69
76
|
}
|
|
77
|
+
export interface ExpireStripeCheckoutDto {
|
|
78
|
+
checkoutId: string;
|
|
79
|
+
}
|
|
80
|
+
export interface IssueCheckoutTokenDto {
|
|
81
|
+
orderId: string;
|
|
82
|
+
amount: number;
|
|
83
|
+
productName: string;
|
|
84
|
+
successUrl: string;
|
|
85
|
+
cancelUrl: string;
|
|
86
|
+
}
|
|
87
|
+
export interface CheckoutTokenResult {
|
|
88
|
+
token: string;
|
|
89
|
+
expiresInMs: number;
|
|
90
|
+
}
|
|
70
91
|
export interface RefundStripeIntentDto {
|
|
71
92
|
orderId: string;
|
|
72
93
|
/** USD dollars — set for a partial refund; omit/undefined for full */
|
|
@@ -116,11 +137,13 @@ export interface StripePaymentCompleteEvent {
|
|
|
116
137
|
orderId: string;
|
|
117
138
|
amount: number;
|
|
118
139
|
amountRecieved: number;
|
|
140
|
+
currency?: string;
|
|
119
141
|
}
|
|
120
142
|
export interface CreateCryptoIntentResponse {
|
|
121
143
|
id: string;
|
|
122
144
|
paymentId?: string | null;
|
|
123
145
|
amount: number;
|
|
146
|
+
payAddress?: string | null;
|
|
124
147
|
payAdress?: string | null;
|
|
125
148
|
payAmount?: number | null;
|
|
126
149
|
invoiceUrl?: string;
|
|
@@ -144,9 +167,45 @@ export interface NPCompleteEvent {
|
|
|
144
167
|
/** asset ticker (btc, trx, …) */
|
|
145
168
|
payCurrency?: string | null;
|
|
146
169
|
}
|
|
170
|
+
export interface NPOrphanEvent {
|
|
171
|
+
id: string;
|
|
172
|
+
orderId: string;
|
|
173
|
+
/** cents USD */
|
|
174
|
+
amount: number;
|
|
175
|
+
/** dollars USD — what wallet credit should use */
|
|
176
|
+
amountUsd: number;
|
|
177
|
+
paymentId?: string | null;
|
|
178
|
+
invoiceId?: string | null;
|
|
179
|
+
payAddress?: string | null;
|
|
180
|
+
payAmount?: number | null;
|
|
181
|
+
payCurrency?: string | null;
|
|
182
|
+
reason: "abandoned_invoice" | "superseded_rail" | string;
|
|
183
|
+
provider: "nowPayment" | string;
|
|
184
|
+
}
|
|
185
|
+
export interface StripeRefundEvent {
|
|
186
|
+
id: string;
|
|
187
|
+
orderId: string;
|
|
188
|
+
amount: number;
|
|
189
|
+
amountRefunded: number;
|
|
190
|
+
full: boolean;
|
|
191
|
+
}
|
|
192
|
+
export interface StripeOrphanEvent {
|
|
193
|
+
id: string;
|
|
194
|
+
orderId: string;
|
|
195
|
+
/** cents USD */
|
|
196
|
+
amount: number;
|
|
197
|
+
/** dollars USD */
|
|
198
|
+
amountUsd: number;
|
|
199
|
+
paymentId?: string | null;
|
|
200
|
+
reason: "superseded_rail" | string;
|
|
201
|
+
provider: "stripe" | string;
|
|
202
|
+
}
|
|
147
203
|
export interface EventDataMap {
|
|
148
204
|
stripe_succeeded: StripePaymentCompleteEvent;
|
|
205
|
+
stripe_refunded: StripeRefundEvent;
|
|
206
|
+
stripe_orphan: StripeOrphanEvent;
|
|
149
207
|
nowPayment_succeeded: NPCompleteEvent;
|
|
208
|
+
nowPayment_orphan: NPOrphanEvent;
|
|
150
209
|
}
|
|
151
210
|
export type PaymentEvent<T extends keyof EventDataMap = keyof EventDataMap> = {
|
|
152
211
|
id: string;
|
|
@@ -168,4 +227,5 @@ export interface IPaymentProvider {
|
|
|
168
227
|
setPaymentMethodEnabled(payload: UpdatePaymentMethodDto): Promise<AvailableMethodsResponse>;
|
|
169
228
|
availableMethods<T = unknown>(): Promise<T>;
|
|
170
229
|
refundStripeIntent(payload: RefundStripeIntentDto): Promise<OrderActionResponse>;
|
|
230
|
+
issueCheckoutToken(payload: IssueCheckoutTokenDto): Promise<CheckoutTokenResult>;
|
|
171
231
|
}
|
|
@@ -33,6 +33,9 @@ exports.PaymentRabbitApi = {
|
|
|
33
33
|
stripe_checkout: {
|
|
34
34
|
key: "stripeCheckout",
|
|
35
35
|
},
|
|
36
|
+
stripe_checkout_expire: {
|
|
37
|
+
key: "stripeCheckoutExpire",
|
|
38
|
+
},
|
|
36
39
|
stripe_intent: {
|
|
37
40
|
key: "stripePaymentIntent",
|
|
38
41
|
},
|
|
@@ -57,6 +60,9 @@ exports.PaymentRabbitApi = {
|
|
|
57
60
|
intent_status: {
|
|
58
61
|
key: "intentStatus",
|
|
59
62
|
},
|
|
63
|
+
checkout_token: {
|
|
64
|
+
key: "checkoutToken",
|
|
65
|
+
},
|
|
60
66
|
available_methods: {
|
|
61
67
|
key: "availableMethods",
|
|
62
68
|
},
|
package/dist/dto/stripe.dto.d.ts
CHANGED
package/dist/dto/stripe.dto.js
CHANGED
|
@@ -46,6 +46,18 @@ __decorate([
|
|
|
46
46
|
(0, class_validator_1.IsString)(),
|
|
47
47
|
__metadata("design:type", String)
|
|
48
48
|
], CreateStripeCheckoutDto.prototype, "cancel_url", void 0);
|
|
49
|
+
__decorate([
|
|
50
|
+
(0, swagger_1.ApiPropertyOptional)(),
|
|
51
|
+
(0, class_validator_1.IsOptional)(),
|
|
52
|
+
(0, class_validator_1.IsBoolean)(),
|
|
53
|
+
__metadata("design:type", Boolean)
|
|
54
|
+
], CreateStripeCheckoutDto.prototype, "chargeFees", void 0);
|
|
55
|
+
__decorate([
|
|
56
|
+
(0, swagger_1.ApiPropertyOptional)(),
|
|
57
|
+
(0, class_validator_1.IsOptional)(),
|
|
58
|
+
(0, class_validator_1.IsBoolean)(),
|
|
59
|
+
__metadata("design:type", Boolean)
|
|
60
|
+
], CreateStripeCheckoutDto.prototype, "collectTax", void 0);
|
|
49
61
|
class CreateStripePaymentIntentDto {
|
|
50
62
|
}
|
|
51
63
|
exports.CreateStripePaymentIntentDto = CreateStripePaymentIntentDto;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { CreateStripeCheckoutDto, CreateStripePaymentIntentDto } from "./dto/stripe.dto";
|
|
2
2
|
import { AmqpConnection } from "@golevelup/nestjs-rabbitmq";
|
|
3
|
-
import { AvailableMethodsResponse, CreateCryptoIntentResponse, CreateStripeCheckoutResponse, CreateStripePaymentIntentResponse, GetIntentByOrderId, IntentStatusResult, OrderActionResponse, RefundStripeIntentDto, UpdatePaymentMethodDto } from "./dto/common.interface";
|
|
3
|
+
import { AvailableMethodsResponse, CheckoutTokenResult, CreateCryptoIntentResponse, CreateStripeCheckoutResponse, CreateStripePaymentIntentResponse, ExpireStripeCheckoutDto, GetIntentByOrderId, IntentStatusResult, IssueCheckoutTokenDto, OrderActionResponse, RefundStripeIntentDto, UpdatePaymentMethodDto } from "./dto/common.interface";
|
|
4
4
|
import { CreateCryptoPaymentDto } from "./dto/crypto.dto";
|
|
5
5
|
import { PaymentProviderOptions } from "./providers/base.provider";
|
|
6
6
|
export { PaymentProviderOptions } from "./providers/base.provider";
|
|
@@ -19,6 +19,8 @@ export declare class PaymentProvider {
|
|
|
19
19
|
markShipped(payload: GetIntentByOrderId): Promise<OrderActionResponse>;
|
|
20
20
|
refundStripeIntent(payload: RefundStripeIntentDto): Promise<OrderActionResponse>;
|
|
21
21
|
cancelStripeIntent(payload: GetIntentByOrderId): Promise<OrderActionResponse>;
|
|
22
|
+
expireStripeCheckout(payload: ExpireStripeCheckoutDto): Promise<OrderActionResponse>;
|
|
23
|
+
issueCheckoutToken(payload: IssueCheckoutTokenDto): Promise<CheckoutTokenResult>;
|
|
22
24
|
getAvailableMethods(): Promise<AvailableMethodsResponse>;
|
|
23
25
|
setPaymentMethodEnabled(payload: UpdatePaymentMethodDto): Promise<AvailableMethodsResponse>;
|
|
24
26
|
availableMethods<T = unknown>(): Promise<T>;
|
package/dist/payment.provider.js
CHANGED
|
@@ -18,7 +18,6 @@ const common_1 = require("@nestjs/common");
|
|
|
18
18
|
const nestjs_rabbitmq_1 = require("@golevelup/nestjs-rabbitmq");
|
|
19
19
|
const common_interface_1 = require("./dto/common.interface");
|
|
20
20
|
const payment_signature_1 = require("./payment.signature");
|
|
21
|
-
const crypto_1 = require("crypto");
|
|
22
21
|
const base_provider_1 = require("./providers/base.provider");
|
|
23
22
|
let PaymentProvider = PaymentProvider_1 = class PaymentProvider {
|
|
24
23
|
constructor(rabbit, options) {
|
|
@@ -27,25 +26,10 @@ let PaymentProvider = PaymentProvider_1 = class PaymentProvider {
|
|
|
27
26
|
this.serviceName = options?.serviceName?.trim() ?? "";
|
|
28
27
|
this.secret = options?.secret?.trim() ?? "";
|
|
29
28
|
}
|
|
30
|
-
/// Wrap a payload in the signed RPC envelope the gateway requires
|
|
31
|
-
/// (`clientRegistry.verify` rejects
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
if (!this.serviceName) {
|
|
35
|
-
throw new Error('PaymentProvider is not configured: "serviceName" is missing — ' +
|
|
36
|
-
"pass it via PaymentModule.forRoot({ serviceName, secret })");
|
|
37
|
-
}
|
|
38
|
-
if (!this.secret) {
|
|
39
|
-
throw new Error(`PaymentProvider is not configured: "secret" is missing for service ` +
|
|
40
|
-
`"${this.serviceName}" — it must match the gateway's PAYMENT_CLIENTS entry`);
|
|
41
|
-
}
|
|
42
|
-
const correlationId = (0, crypto_1.randomUUID)();
|
|
43
|
-
return {
|
|
44
|
-
serviceName: this.serviceName,
|
|
45
|
-
correlationId,
|
|
46
|
-
signature: (0, payment_signature_1.signPayload)(this.secret, data, correlationId),
|
|
47
|
-
data,
|
|
48
|
-
};
|
|
29
|
+
/// Wrap a payload in the signed RPC v2 envelope the gateway requires
|
|
30
|
+
/// (`clientRegistry.verify` rejects missing timestamp, bad signature, or replay).
|
|
31
|
+
envelope(data, callbackRoutingKey) {
|
|
32
|
+
return (0, payment_signature_1.buildSignedRpcEnvelope)({ serviceName: this.serviceName, secret: this.secret }, data, callbackRoutingKey);
|
|
49
33
|
}
|
|
50
34
|
async request(routingKey, data) {
|
|
51
35
|
const res = await this.rabbit.request({
|
|
@@ -86,6 +70,14 @@ let PaymentProvider = PaymentProvider_1 = class PaymentProvider {
|
|
|
86
70
|
async cancelStripeIntent(payload) {
|
|
87
71
|
return this.request(common_interface_1.PaymentRabbitApi.routingkeys.stripe_cancel.key, payload);
|
|
88
72
|
}
|
|
73
|
+
/// Expire a waiting Stripe Checkout session owned by this service.
|
|
74
|
+
async expireStripeCheckout(payload) {
|
|
75
|
+
return this.request(common_interface_1.PaymentRabbitApi.routingkeys.stripe_checkout_expire.key, payload);
|
|
76
|
+
}
|
|
77
|
+
/// Issue a short-lived HMAC checkout token for hosted checkout.
|
|
78
|
+
async issueCheckoutToken(payload) {
|
|
79
|
+
return this.request(common_interface_1.PaymentRabbitApi.routingkeys.checkout_token.key, payload);
|
|
80
|
+
}
|
|
89
81
|
async getAvailableMethods() {
|
|
90
82
|
return this.request(common_interface_1.PaymentRabbitApi.routingkeys.payment_methods.key, {});
|
|
91
83
|
}
|
|
@@ -1,10 +1,35 @@
|
|
|
1
|
+
import type { RpcEnvelope } from "./dto/common.interface";
|
|
2
|
+
export declare const RPC_MAX_SKEW_MS: number;
|
|
3
|
+
export interface RpcSignInput {
|
|
4
|
+
data: unknown;
|
|
5
|
+
correlationId?: string;
|
|
6
|
+
serviceName: string;
|
|
7
|
+
callbackRoutingKey?: string;
|
|
8
|
+
timestamp: number;
|
|
9
|
+
}
|
|
1
10
|
/** Stable canonical JSON: object keys are sorted recursively so that two
|
|
2
11
|
* semantically-identical payloads produce the same signature. Mirrored
|
|
3
12
|
* byte-for-byte from the gateway's `client-signature.ts`. */
|
|
4
13
|
export declare function canonicalJson(value: unknown): string;
|
|
14
|
+
/** Reply routing keys must live under this prefix and the caller's service name. */
|
|
15
|
+
export declare function callbackKeyFor(serviceName: string): string;
|
|
16
|
+
export declare function callbackKeyAllowed(serviceName: string, callbackRoutingKey?: string): boolean;
|
|
17
|
+
export declare function rpcCanonical(input: RpcSignInput): string;
|
|
18
|
+
/** RPC v2 HMAC-SHA256 over canonical data, correlationId, serviceName, callbackRoutingKey, and timestamp.
|
|
19
|
+
* Must match the gateway's `signRpcPayload` in `client-signature.ts`. */
|
|
20
|
+
export declare function signRpcPayload(secret: string, input: RpcSignInput): string;
|
|
21
|
+
/** Verify an RPC v2 HMAC-SHA256 signature. Constant-time comparison. */
|
|
22
|
+
export declare function verifyRpcPayload(secret: string, input: RpcSignInput, signature: string): boolean;
|
|
23
|
+
/** Build and sign a full RPC v2 request envelope for the gateway (`ClientRegistryService.verify`). */
|
|
24
|
+
export declare function buildSignedRpcEnvelope<T>(options: {
|
|
25
|
+
serviceName: string;
|
|
26
|
+
secret: string;
|
|
27
|
+
}, data: T, callbackRoutingKey?: string, timestamp?: number): RpcEnvelope<T> & {
|
|
28
|
+
timestamp: number;
|
|
29
|
+
};
|
|
5
30
|
/** HMAC-SHA256 over the canonical payload (+ optional correlation id).
|
|
6
|
-
*
|
|
31
|
+
* Matches the gateway's `signEvent` / `signPayload`. */
|
|
7
32
|
export declare function signPayload(secret: string, data: unknown, correlationId?: string): string;
|
|
8
|
-
/** Verify an HMAC-SHA256 signature over `data` (+ correlation id).
|
|
33
|
+
/** Verify an event HMAC-SHA256 signature over `data` (+ correlation id).
|
|
9
34
|
* Timingsafe comparison — constant time, resistant to timing attacks. */
|
|
10
35
|
export declare function verifyPayloadSignature(secret: string, data: unknown, signature: string, correlationId?: string): boolean;
|
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RPC_MAX_SKEW_MS = void 0;
|
|
3
4
|
exports.canonicalJson = canonicalJson;
|
|
5
|
+
exports.callbackKeyFor = callbackKeyFor;
|
|
6
|
+
exports.callbackKeyAllowed = callbackKeyAllowed;
|
|
7
|
+
exports.rpcCanonical = rpcCanonical;
|
|
8
|
+
exports.signRpcPayload = signRpcPayload;
|
|
9
|
+
exports.verifyRpcPayload = verifyRpcPayload;
|
|
10
|
+
exports.buildSignedRpcEnvelope = buildSignedRpcEnvelope;
|
|
4
11
|
exports.signPayload = signPayload;
|
|
5
12
|
exports.verifyPayloadSignature = verifyPayloadSignature;
|
|
6
13
|
const crypto_1 = require("crypto");
|
|
14
|
+
exports.RPC_MAX_SKEW_MS = 5 * 60 * 1000;
|
|
7
15
|
/** Stable canonical JSON: object keys are sorted recursively so that two
|
|
8
16
|
* semantically-identical payloads produce the same signature. Mirrored
|
|
9
17
|
* byte-for-byte from the gateway's `client-signature.ts`. */
|
|
@@ -19,13 +27,74 @@ function canonicalJson(value) {
|
|
|
19
27
|
}
|
|
20
28
|
return JSON.stringify(value);
|
|
21
29
|
}
|
|
30
|
+
/** Reply routing keys must live under this prefix and the caller's service name. */
|
|
31
|
+
function callbackKeyFor(serviceName) {
|
|
32
|
+
return `paymentReplies.${serviceName}.`;
|
|
33
|
+
}
|
|
34
|
+
function callbackKeyAllowed(serviceName, callbackRoutingKey) {
|
|
35
|
+
if (!callbackRoutingKey)
|
|
36
|
+
return true;
|
|
37
|
+
return callbackRoutingKey.startsWith(callbackKeyFor(serviceName));
|
|
38
|
+
}
|
|
39
|
+
function rpcCanonical(input) {
|
|
40
|
+
return `payload=${canonicalJson(input.data)};correlationId=${input.correlationId ?? ""};serviceName=${input.serviceName};callbackRoutingKey=${input.callbackRoutingKey ?? ""};timestamp=${input.timestamp}`;
|
|
41
|
+
}
|
|
42
|
+
/** RPC v2 HMAC-SHA256 over canonical data, correlationId, serviceName, callbackRoutingKey, and timestamp.
|
|
43
|
+
* Must match the gateway's `signRpcPayload` in `client-signature.ts`. */
|
|
44
|
+
function signRpcPayload(secret, input) {
|
|
45
|
+
return (0, crypto_1.createHmac)("sha256", secret).update(rpcCanonical(input)).digest("hex");
|
|
46
|
+
}
|
|
47
|
+
/** Verify an RPC v2 HMAC-SHA256 signature. Constant-time comparison. */
|
|
48
|
+
function verifyRpcPayload(secret, input, signature) {
|
|
49
|
+
if (!signature)
|
|
50
|
+
return false;
|
|
51
|
+
const expected = signRpcPayload(secret, input);
|
|
52
|
+
try {
|
|
53
|
+
return (0, crypto_1.timingSafeEqual)(Buffer.from(expected, "hex"), Buffer.from(signature, "hex"));
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/** Build and sign a full RPC v2 request envelope for the gateway (`ClientRegistryService.verify`). */
|
|
60
|
+
function buildSignedRpcEnvelope(options, data, callbackRoutingKey, timestamp = Date.now()) {
|
|
61
|
+
const serviceName = options.serviceName?.trim() ?? "";
|
|
62
|
+
const secret = options.secret?.trim() ?? "";
|
|
63
|
+
if (!serviceName) {
|
|
64
|
+
throw new Error('PaymentProvider is not configured: "serviceName" is missing — ' +
|
|
65
|
+
"pass it via PaymentModule.forRoot({ serviceName, secret })");
|
|
66
|
+
}
|
|
67
|
+
if (!secret) {
|
|
68
|
+
throw new Error(`PaymentProvider is not configured: "secret" is missing for service ` +
|
|
69
|
+
`"${serviceName}" — it must match the gateway's PAYMENT_CLIENTS entry`);
|
|
70
|
+
}
|
|
71
|
+
if (!callbackKeyAllowed(serviceName, callbackRoutingKey)) {
|
|
72
|
+
throw new Error(`callbackRoutingKey must start with ${callbackKeyFor(serviceName)}`);
|
|
73
|
+
}
|
|
74
|
+
const correlationId = (0, crypto_1.randomUUID)();
|
|
75
|
+
const signature = signRpcPayload(secret, {
|
|
76
|
+
data,
|
|
77
|
+
correlationId,
|
|
78
|
+
serviceName,
|
|
79
|
+
callbackRoutingKey,
|
|
80
|
+
timestamp,
|
|
81
|
+
});
|
|
82
|
+
return {
|
|
83
|
+
serviceName,
|
|
84
|
+
correlationId,
|
|
85
|
+
timestamp,
|
|
86
|
+
...(callbackRoutingKey ? { callbackRoutingKey } : {}),
|
|
87
|
+
signature,
|
|
88
|
+
data,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
22
91
|
/** HMAC-SHA256 over the canonical payload (+ optional correlation id).
|
|
23
|
-
*
|
|
92
|
+
* Matches the gateway's `signEvent` / `signPayload`. */
|
|
24
93
|
function signPayload(secret, data, correlationId) {
|
|
25
94
|
const canonical = `payload=${canonicalJson(data)};correlationId=${correlationId ?? ""}`;
|
|
26
95
|
return (0, crypto_1.createHmac)("sha256", secret).update(canonical).digest("hex");
|
|
27
96
|
}
|
|
28
|
-
/** Verify an HMAC-SHA256 signature over `data` (+ correlation id).
|
|
97
|
+
/** Verify an event HMAC-SHA256 signature over `data` (+ correlation id).
|
|
29
98
|
* Timingsafe comparison — constant time, resistant to timing attacks. */
|
|
30
99
|
function verifyPayloadSignature(secret, data, signature, correlationId) {
|
|
31
100
|
if (!signature)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Logger } from "@nestjs/common";
|
|
2
2
|
import { AmqpConnection } from "@golevelup/nestjs-rabbitmq";
|
|
3
|
-
import { AvailableMethodsResponse, GetIntentByOrderId, IntentStatusResult, IPaymentProvider, OrderActionResponse, RefundStripeIntentDto, RpcEnvelope, UpdatePaymentMethodDto } from "../dto/common.interface";
|
|
3
|
+
import { AvailableMethodsResponse, CheckoutTokenResult, GetIntentByOrderId, IntentStatusResult, IPaymentProvider, IssueCheckoutTokenDto, OrderActionResponse, RefundStripeIntentDto, RpcEnvelope, UpdatePaymentMethodDto } from "../dto/common.interface";
|
|
4
4
|
export interface PaymentProviderOptions {
|
|
5
5
|
serviceName: string;
|
|
6
6
|
secret: string;
|
|
@@ -12,10 +12,11 @@ export declare abstract class BasePaymentProvider implements IPaymentProvider {
|
|
|
12
12
|
protected readonly serviceName: string;
|
|
13
13
|
protected readonly secret: string;
|
|
14
14
|
constructor(rabbit: AmqpConnection, options?: PaymentProviderOptions);
|
|
15
|
-
protected envelope<T>(data: T): RpcEnvelope<T>;
|
|
15
|
+
protected envelope<T>(data: T, callbackRoutingKey?: string): RpcEnvelope<T>;
|
|
16
16
|
protected request<T>(routingKey: string, data: unknown): Promise<T>;
|
|
17
17
|
getIntentStatus(payload: GetIntentByOrderId): Promise<IntentStatusResult | null>;
|
|
18
18
|
refundStripeIntent(payload: RefundStripeIntentDto): Promise<OrderActionResponse>;
|
|
19
|
+
issueCheckoutToken(payload: IssueCheckoutTokenDto): Promise<CheckoutTokenResult>;
|
|
19
20
|
getAvailableMethods(): Promise<AvailableMethodsResponse>;
|
|
20
21
|
setPaymentMethodEnabled(payload: UpdatePaymentMethodDto): Promise<AvailableMethodsResponse>;
|
|
21
22
|
availableMethods<T = unknown>(): Promise<T>;
|
|
@@ -15,7 +15,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
15
15
|
exports.BasePaymentProvider = exports.PAYMENT_PROVIDER_OPTIONS = void 0;
|
|
16
16
|
const common_1 = require("@nestjs/common");
|
|
17
17
|
const nestjs_rabbitmq_1 = require("@golevelup/nestjs-rabbitmq");
|
|
18
|
-
const crypto_1 = require("crypto");
|
|
19
18
|
const payment_signature_1 = require("../payment.signature");
|
|
20
19
|
const common_interface_1 = require("../dto/common.interface");
|
|
21
20
|
exports.PAYMENT_PROVIDER_OPTIONS = "PAYMENT_PROVIDER_OPTIONS";
|
|
@@ -29,25 +28,10 @@ let BasePaymentProvider = class BasePaymentProvider {
|
|
|
29
28
|
this.serviceName = options?.serviceName?.trim() ?? "";
|
|
30
29
|
this.secret = options?.secret?.trim() ?? "";
|
|
31
30
|
}
|
|
32
|
-
/// Wrap a payload in the signed RPC envelope the gateway requires
|
|
33
|
-
/// (`clientRegistry.verify` rejects
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
if (!this.serviceName) {
|
|
37
|
-
throw new Error('PaymentProvider is not configured: "serviceName" is missing — ' +
|
|
38
|
-
"pass it via PaymentModule.forRoot({ serviceName, secret })");
|
|
39
|
-
}
|
|
40
|
-
if (!this.secret) {
|
|
41
|
-
throw new Error(`PaymentProvider is not configured: "secret" is missing for service ` +
|
|
42
|
-
`"${this.serviceName}" — it must match the gateway's PAYMENT_CLIENTS entry`);
|
|
43
|
-
}
|
|
44
|
-
const correlationId = (0, crypto_1.randomUUID)();
|
|
45
|
-
return {
|
|
46
|
-
serviceName: this.serviceName,
|
|
47
|
-
correlationId,
|
|
48
|
-
signature: (0, payment_signature_1.signPayload)(this.secret, data, correlationId),
|
|
49
|
-
data,
|
|
50
|
-
};
|
|
31
|
+
/// Wrap a payload in the signed RPC v2 envelope the gateway requires
|
|
32
|
+
/// (`clientRegistry.verify` rejects missing timestamp, bad signature, or replay).
|
|
33
|
+
envelope(data, callbackRoutingKey) {
|
|
34
|
+
return (0, payment_signature_1.buildSignedRpcEnvelope)({ serviceName: this.serviceName, secret: this.secret }, data, callbackRoutingKey);
|
|
51
35
|
}
|
|
52
36
|
async request(routingKey, data) {
|
|
53
37
|
const res = await this.rabbit.request({
|
|
@@ -68,6 +52,9 @@ let BasePaymentProvider = class BasePaymentProvider {
|
|
|
68
52
|
async refundStripeIntent(payload) {
|
|
69
53
|
return this.request(common_interface_1.PaymentRabbitApi.routingkeys.stripe_refund.key, payload);
|
|
70
54
|
}
|
|
55
|
+
async issueCheckoutToken(payload) {
|
|
56
|
+
return this.request(common_interface_1.PaymentRabbitApi.routingkeys.checkout_token.key, payload);
|
|
57
|
+
}
|
|
71
58
|
async getAvailableMethods() {
|
|
72
59
|
return this.request(common_interface_1.PaymentRabbitApi.routingkeys.payment_methods.key, {});
|
|
73
60
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AmqpConnection } from "@golevelup/nestjs-rabbitmq";
|
|
2
2
|
import { CreateStripeCheckoutDto, CreateStripePaymentIntentDto } from "../dto/stripe.dto";
|
|
3
|
-
import { CreateStripeCheckoutResponse, CreateStripePaymentIntentResponse, GetIntentByOrderId, OrderActionResponse } from "../dto/common.interface";
|
|
3
|
+
import { CreateStripeCheckoutResponse, CreateStripePaymentIntentResponse, ExpireStripeCheckoutDto, GetIntentByOrderId, OrderActionResponse } from "../dto/common.interface";
|
|
4
4
|
import { BasePaymentProvider, PaymentProviderOptions } from "./base.provider";
|
|
5
5
|
export declare class StripeProvider extends BasePaymentProvider {
|
|
6
6
|
constructor(rabbit: AmqpConnection, options?: PaymentProviderOptions);
|
|
@@ -8,4 +8,5 @@ export declare class StripeProvider extends BasePaymentProvider {
|
|
|
8
8
|
createStripePaymentIntent(payload: CreateStripePaymentIntentDto): Promise<CreateStripePaymentIntentResponse>;
|
|
9
9
|
markShipped(payload: GetIntentByOrderId): Promise<OrderActionResponse>;
|
|
10
10
|
cancelStripeIntent(payload: GetIntentByOrderId): Promise<OrderActionResponse>;
|
|
11
|
+
expireStripeCheckout(payload: ExpireStripeCheckoutDto): Promise<OrderActionResponse>;
|
|
11
12
|
}
|
|
@@ -37,6 +37,10 @@ let StripeProvider = class StripeProvider extends base_provider_1.BasePaymentPro
|
|
|
37
37
|
async cancelStripeIntent(payload) {
|
|
38
38
|
return this.request(common_interface_1.PaymentRabbitApi.routingkeys.stripe_cancel.key, payload);
|
|
39
39
|
}
|
|
40
|
+
/// Expire a waiting Stripe Checkout session owned by this service.
|
|
41
|
+
async expireStripeCheckout(payload) {
|
|
42
|
+
return this.request(common_interface_1.PaymentRabbitApi.routingkeys.stripe_checkout_expire.key, payload);
|
|
43
|
+
}
|
|
40
44
|
};
|
|
41
45
|
exports.StripeProvider = StripeProvider;
|
|
42
46
|
exports.StripeProvider = StripeProvider = __decorate([
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ngn-net/payment-provider-sdk",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Payment provider SDK",
|
|
5
5
|
"main": "dist/main.js",
|
|
6
6
|
"types": "dist/main.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"build": "tsc",
|
|
8
|
+
"build": "rm -rf dist && tsc",
|
|
9
9
|
"prepublishOnly": "npm run build",
|
|
10
10
|
"test": "jest",
|
|
11
11
|
"test:watch": "jest --watch"
|
package/dist/app.controller.d.ts
DELETED
package/dist/app.controller.js
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
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
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.AppController = void 0;
|
|
13
|
-
const common_1 = require("@nestjs/common");
|
|
14
|
-
const app_service_1 = require("./app.service");
|
|
15
|
-
let AppController = class AppController {
|
|
16
|
-
constructor(appService) {
|
|
17
|
-
this.appService = appService;
|
|
18
|
-
}
|
|
19
|
-
getHello() {
|
|
20
|
-
return this.appService.getHello();
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
exports.AppController = AppController;
|
|
24
|
-
__decorate([
|
|
25
|
-
(0, common_1.Get)(),
|
|
26
|
-
__metadata("design:type", Function),
|
|
27
|
-
__metadata("design:paramtypes", []),
|
|
28
|
-
__metadata("design:returntype", void 0)
|
|
29
|
-
], AppController.prototype, "getHello", null);
|
|
30
|
-
exports.AppController = AppController = __decorate([
|
|
31
|
-
(0, common_1.Controller)(),
|
|
32
|
-
__metadata("design:paramtypes", [app_service_1.AppService])
|
|
33
|
-
], AppController);
|
package/dist/app.module.d.ts
DELETED
package/dist/app.module.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
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
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.AppModule = void 0;
|
|
10
|
-
const common_1 = require("@nestjs/common");
|
|
11
|
-
const payment_provider_sdk_1 = require("@ngn-net/payment-provider-sdk");
|
|
12
|
-
const app_controller_1 = require("./app.controller");
|
|
13
|
-
const app_service_1 = require("./app.service");
|
|
14
|
-
require("dotenv/config");
|
|
15
|
-
if (!process.env.RABBIT_URI) {
|
|
16
|
-
throw new Error("RABBIT_URI is not set");
|
|
17
|
-
}
|
|
18
|
-
let AppModule = class AppModule {
|
|
19
|
-
};
|
|
20
|
-
exports.AppModule = AppModule;
|
|
21
|
-
exports.AppModule = AppModule = __decorate([
|
|
22
|
-
(0, common_1.Module)({
|
|
23
|
-
imports: [payment_provider_sdk_1.PaymentModule.forRoot({ uri: process.env.RABBIT_URI })],
|
|
24
|
-
controllers: [app_controller_1.AppController],
|
|
25
|
-
providers: [app_service_1.AppService],
|
|
26
|
-
})
|
|
27
|
-
], AppModule);
|
package/dist/app.service.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { PaymentProvider } from "@ngn-net/payment-provider-sdk";
|
|
2
|
-
export declare class AppService {
|
|
3
|
-
private readonly paymentProvider;
|
|
4
|
-
constructor(paymentProvider: PaymentProvider);
|
|
5
|
-
getHello(): Promise<import("@ngn-net/payment-provider-sdk").CreateStripeCheckoutResponse>;
|
|
6
|
-
}
|
package/dist/app.service.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
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
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.AppService = void 0;
|
|
13
|
-
const common_1 = require("@nestjs/common");
|
|
14
|
-
const payment_provider_sdk_1 = require("@ngn-net/payment-provider-sdk");
|
|
15
|
-
let AppService = class AppService {
|
|
16
|
-
constructor(paymentProvider) {
|
|
17
|
-
this.paymentProvider = paymentProvider;
|
|
18
|
-
}
|
|
19
|
-
async getHello() {
|
|
20
|
-
try {
|
|
21
|
-
return await this.paymentProvider.createStripeCheckout({
|
|
22
|
-
amount: 1000,
|
|
23
|
-
orderId: "123e4567-e89b-12d3-a456-426614174000",
|
|
24
|
-
productName: "Test Product",
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
catch (err) {
|
|
28
|
-
console.error("SDK error:", err.message);
|
|
29
|
-
throw err;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
exports.AppService = AppService;
|
|
34
|
-
exports.AppService = AppService = __decorate([
|
|
35
|
-
(0, common_1.Injectable)(),
|
|
36
|
-
__metadata("design:paramtypes", [payment_provider_sdk_1.PaymentProvider])
|
|
37
|
-
], AppService);
|