@unchainedshop/plugins 4.0.0-rc.5 → 4.0.0-rc.7
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/lib/payment/apple-iap/adapter.js +1 -1
- package/lib/payment/apple-iap/adapter.js.map +1 -1
- package/lib/payment/apple-iap/handler-express.js +1 -1
- package/lib/payment/apple-iap/handler-express.js.map +1 -1
- package/lib/payment/apple-iap/handler-fastify.js +1 -1
- package/lib/payment/apple-iap/handler-fastify.js.map +1 -1
- package/lib/payment/cryptopay/handle-webhook.js +1 -1
- package/lib/payment/cryptopay/handle-webhook.js.map +1 -1
- package/lib/payment/cryptopay/handler-express.js +1 -1
- package/lib/payment/cryptopay/handler-express.js.map +1 -1
- package/lib/payment/cryptopay/handler-fastify.d.ts.map +1 -1
- package/lib/payment/cryptopay/handler-fastify.js +1 -1
- package/lib/payment/cryptopay/handler-fastify.js.map +1 -1
- package/lib/payment/paypal-checkout.js +5 -7
- package/lib/payment/paypal-checkout.js.map +1 -1
- package/lib/payment/postfinance-checkout/api-client.d.ts +16 -0
- package/lib/payment/postfinance-checkout/api-client.d.ts.map +1 -0
- package/lib/payment/postfinance-checkout/api-client.js +75 -0
- package/lib/payment/postfinance-checkout/api-client.js.map +1 -0
- package/lib/payment/postfinance-checkout/api-types.d.ts +98 -0
- package/lib/payment/postfinance-checkout/api-types.d.ts.map +1 -0
- package/lib/payment/postfinance-checkout/api-types.js +51 -0
- package/lib/payment/postfinance-checkout/api-types.js.map +1 -0
- package/lib/payment/postfinance-checkout/api.d.ts +5 -5
- package/lib/payment/postfinance-checkout/api.d.ts.map +1 -1
- package/lib/payment/postfinance-checkout/api.js +38 -62
- package/lib/payment/postfinance-checkout/api.js.map +1 -1
- package/lib/payment/postfinance-checkout/handler-express.d.ts.map +1 -1
- package/lib/payment/postfinance-checkout/handler-express.js +5 -5
- package/lib/payment/postfinance-checkout/handler-express.js.map +1 -1
- package/lib/payment/postfinance-checkout/handler-fastify.d.ts.map +1 -1
- package/lib/payment/postfinance-checkout/handler-fastify.js +6 -6
- package/lib/payment/postfinance-checkout/handler-fastify.js.map +1 -1
- package/lib/payment/postfinance-checkout/index.d.ts +2 -1
- package/lib/payment/postfinance-checkout/index.d.ts.map +1 -1
- package/lib/payment/postfinance-checkout/index.js +25 -26
- package/lib/payment/postfinance-checkout/index.js.map +1 -1
- package/lib/payment/postfinance-checkout/utils.d.ts +3 -3
- package/lib/payment/postfinance-checkout/utils.d.ts.map +1 -1
- package/lib/payment/postfinance-checkout/utils.js +3 -4
- package/lib/payment/postfinance-checkout/utils.js.map +1 -1
- package/package.json +5 -10
- package/src/payment/apple-iap/adapter.ts +1 -1
- package/src/payment/apple-iap/handler-express.ts +1 -1
- package/src/payment/apple-iap/handler-fastify.ts +1 -1
- package/src/payment/cryptopay/handle-webhook.ts +1 -1
- package/src/payment/cryptopay/handler-express.ts +1 -1
- package/src/payment/cryptopay/handler-fastify.ts +2 -2
- package/src/payment/paypal-checkout.ts +5 -10
- package/src/payment/postfinance-checkout/api-client.ts +106 -0
- package/src/payment/postfinance-checkout/api-types.ts +111 -0
- package/src/payment/postfinance-checkout/api.ts +58 -85
- package/src/payment/postfinance-checkout/handler-express.ts +5 -7
- package/src/payment/postfinance-checkout/handler-fastify.ts +6 -8
- package/src/payment/postfinance-checkout/index.ts +41 -31
- package/src/payment/postfinance-checkout/utils.ts +5 -10
|
@@ -17,7 +17,7 @@ try {
|
|
|
17
17
|
/* */
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
const logger = createLogger('unchained:core-payment');
|
|
20
|
+
const logger = createLogger('unchained:core-payment:paypal-checkout');
|
|
21
21
|
|
|
22
22
|
const { PAYPAL_CLIENT_ID, PAYPAL_SECRET, PAYPAL_ENVIRONMENT = 'sandbox' } = process.env;
|
|
23
23
|
|
|
@@ -79,7 +79,6 @@ const PaypalCheckout: IPaymentAdapter = {
|
|
|
79
79
|
const { order } = context;
|
|
80
80
|
|
|
81
81
|
if (!orderID) {
|
|
82
|
-
logger.warn('Paypal Native Plugin: PRICE MATCH');
|
|
83
82
|
throw new Error('You have to provide orderID in paymentContext');
|
|
84
83
|
}
|
|
85
84
|
|
|
@@ -96,21 +95,17 @@ const PaypalCheckout: IPaymentAdapter = {
|
|
|
96
95
|
const paypalTotal = paypalOrder.result.purchase_units[0].amount.value;
|
|
97
96
|
|
|
98
97
|
if (ourTotal === paypalTotal) {
|
|
99
|
-
logger.info('Paypal Native Plugin: PRICE MATCH');
|
|
100
98
|
return order;
|
|
101
99
|
}
|
|
102
100
|
|
|
103
|
-
logger.warn(
|
|
104
|
-
'Paypal Native Plugin: Missmatch PAYPAL ORDER',
|
|
105
|
-
JSON.stringify(paypalOrder.result, null, 2),
|
|
106
|
-
);
|
|
101
|
+
logger.warn('Missmatch PAYPAL ORDER', JSON.stringify(paypalOrder.result, null, 2));
|
|
107
102
|
|
|
108
|
-
logger.debug('
|
|
109
|
-
logger.debug('
|
|
103
|
+
logger.debug('OUR ORDER', order);
|
|
104
|
+
logger.debug('OUR PRICE', pricing);
|
|
110
105
|
|
|
111
106
|
throw new Error(`Payment mismatch`);
|
|
112
107
|
} catch (e) {
|
|
113
|
-
logger.warn(
|
|
108
|
+
logger.warn(e);
|
|
114
109
|
throw new Error(e);
|
|
115
110
|
}
|
|
116
111
|
},
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import crypto from 'crypto';
|
|
2
|
+
|
|
3
|
+
const API_BASE_URL = 'https://checkout.postfinance.ch/api';
|
|
4
|
+
|
|
5
|
+
interface ApiConfig {
|
|
6
|
+
spaceId: number;
|
|
7
|
+
userId: number;
|
|
8
|
+
apiSecret: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface MacHeaders {
|
|
12
|
+
'x-mac-version': string;
|
|
13
|
+
'x-mac-userid': string;
|
|
14
|
+
'x-mac-timestamp': string;
|
|
15
|
+
'x-mac-value': string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export class PostFinanceApiClient {
|
|
19
|
+
private config: ApiConfig;
|
|
20
|
+
|
|
21
|
+
constructor(config: ApiConfig) {
|
|
22
|
+
this.config = config;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
private generateMacHeaders(method: string, path: string): MacHeaders {
|
|
26
|
+
const version = '1';
|
|
27
|
+
const timestamp = Math.floor(Date.now() / 1000).toString();
|
|
28
|
+
const userId = this.config.userId.toString();
|
|
29
|
+
|
|
30
|
+
// Use path as-is since it already includes /api from the URL
|
|
31
|
+
const resourcePath = path;
|
|
32
|
+
|
|
33
|
+
const dataToSign = [version, userId, timestamp, method, resourcePath].join('|');
|
|
34
|
+
|
|
35
|
+
const macValue = crypto
|
|
36
|
+
.createHmac('sha512', Buffer.from(this.config.apiSecret, 'base64'))
|
|
37
|
+
.update(dataToSign, 'utf8')
|
|
38
|
+
.digest('base64');
|
|
39
|
+
|
|
40
|
+
return {
|
|
41
|
+
'x-mac-version': version,
|
|
42
|
+
'x-mac-userid': userId,
|
|
43
|
+
'x-mac-timestamp': timestamp,
|
|
44
|
+
'x-mac-value': macValue,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async request<T>(method: string, endpoint: string, body?: any): Promise<T> {
|
|
49
|
+
const url = `${API_BASE_URL}${endpoint}`;
|
|
50
|
+
const urlObj = new URL(url);
|
|
51
|
+
// Include both pathname and search (query parameters) for MAC signature
|
|
52
|
+
const pathWithQuery = urlObj.pathname + urlObj.search;
|
|
53
|
+
const macHeaders = this.generateMacHeaders(method, pathWithQuery);
|
|
54
|
+
|
|
55
|
+
const headers: Record<string, string> = {
|
|
56
|
+
...macHeaders,
|
|
57
|
+
'Content-Type': 'application/json',
|
|
58
|
+
Accept: 'application/json',
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const options: RequestInit = {
|
|
62
|
+
method,
|
|
63
|
+
headers,
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
if (body && method !== 'GET') {
|
|
67
|
+
options.body = JSON.stringify(body);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const response = await fetch(url, options);
|
|
71
|
+
|
|
72
|
+
if (!response.ok) {
|
|
73
|
+
const errorData = await response.json().catch(() => ({}));
|
|
74
|
+
throw new Error(
|
|
75
|
+
`PostFinance API error: ${response.status} ${response.statusText} ${JSON.stringify(errorData)}`,
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const responseText = await response.text();
|
|
80
|
+
const contentType = response.headers.get('content-type');
|
|
81
|
+
|
|
82
|
+
if (contentType && contentType.includes('application/json')) {
|
|
83
|
+
try {
|
|
84
|
+
return JSON.parse(responseText);
|
|
85
|
+
} catch {
|
|
86
|
+
// If JSON parsing fails but content-type is json, return as text
|
|
87
|
+
// This handles PostFinance's payment-page-url endpoint that returns plain text with json content-type
|
|
88
|
+
return responseText as any;
|
|
89
|
+
}
|
|
90
|
+
} else {
|
|
91
|
+
return responseText as any;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
get<T>(endpoint: string): Promise<T> {
|
|
96
|
+
return this.request<T>('GET', endpoint);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
post<T>(endpoint: string, body: any): Promise<T> {
|
|
100
|
+
return this.request<T>('POST', endpoint, body);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
patch<T>(endpoint: string, body: any): Promise<T> {
|
|
104
|
+
return this.request<T>('PATCH', endpoint, body);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// PostFinance Checkout API Types
|
|
2
|
+
|
|
3
|
+
export enum TransactionState {
|
|
4
|
+
CREATE = 'CREATE',
|
|
5
|
+
PENDING = 'PENDING',
|
|
6
|
+
CONFIRMED = 'CONFIRMED',
|
|
7
|
+
PROCESSING = 'PROCESSING',
|
|
8
|
+
FAILED = 'FAILED',
|
|
9
|
+
AUTHORIZED = 'AUTHORIZED',
|
|
10
|
+
VOIDED = 'VOIDED',
|
|
11
|
+
COMPLETED = 'COMPLETED',
|
|
12
|
+
FULFILL = 'FULFILL',
|
|
13
|
+
DECLINE = 'DECLINE',
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export enum TokenizationMode {
|
|
17
|
+
FORCE_UPDATE = 'FORCE_UPDATE',
|
|
18
|
+
ALLOW = 'ALLOW',
|
|
19
|
+
ALLOW_ONE_CLICK_PAYMENT = 'ALLOW_ONE_CLICK_PAYMENT',
|
|
20
|
+
DENY = 'DENY',
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export enum TransactionCompletionBehavior {
|
|
24
|
+
COMPLETE_IMMEDIATELY = 'COMPLETE_IMMEDIATELY',
|
|
25
|
+
COMPLETE_DEFERRED = 'COMPLETE_DEFERRED',
|
|
26
|
+
USE_CONFIGURATION = 'USE_CONFIGURATION',
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export enum LineItemType {
|
|
30
|
+
SHIPPING = 'SHIPPING',
|
|
31
|
+
DISCOUNT = 'DISCOUNT',
|
|
32
|
+
FEE = 'FEE',
|
|
33
|
+
PRODUCT = 'PRODUCT',
|
|
34
|
+
TIP = 'TIP',
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export enum RefundType {
|
|
38
|
+
MERCHANT_INITIATED_ONLINE = 'MERCHANT_INITIATED_ONLINE',
|
|
39
|
+
MERCHANT_INITIATED_OFFLINE = 'MERCHANT_INITIATED_OFFLINE',
|
|
40
|
+
CUSTOMER_INITIATED_AUTOMATIC = 'CUSTOMER_INITIATED_AUTOMATIC',
|
|
41
|
+
CUSTOMER_INITIATED_MANUAL = 'CUSTOMER_INITIATED_MANUAL',
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export enum CreationEntityState {
|
|
45
|
+
CREATE = 'CREATE',
|
|
46
|
+
ACTIVE = 'ACTIVE',
|
|
47
|
+
INACTIVE = 'INACTIVE',
|
|
48
|
+
DELETING = 'DELETING',
|
|
49
|
+
DELETED = 'DELETED',
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface LineItem {
|
|
53
|
+
name: string;
|
|
54
|
+
type: LineItemType;
|
|
55
|
+
quantity: number;
|
|
56
|
+
uniqueId: string;
|
|
57
|
+
amountIncludingTax: number;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface TransactionCreate {
|
|
61
|
+
currency: string;
|
|
62
|
+
customerId?: string;
|
|
63
|
+
tokenizationMode?: TokenizationMode;
|
|
64
|
+
completionBehavior?: TransactionCompletionBehavior;
|
|
65
|
+
lineItems?: LineItem[];
|
|
66
|
+
successUrl?: string;
|
|
67
|
+
failedUrl?: string;
|
|
68
|
+
metaData?: Record<string, any>;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface Transaction {
|
|
72
|
+
id: number;
|
|
73
|
+
state: TransactionState;
|
|
74
|
+
currency: string;
|
|
75
|
+
completedAmount?: number;
|
|
76
|
+
authorizationAmount?: number;
|
|
77
|
+
metaData?: Record<string, any>;
|
|
78
|
+
token?: {
|
|
79
|
+
id: number;
|
|
80
|
+
[key: string]: any;
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface TransactionCompletion {
|
|
85
|
+
id: number;
|
|
86
|
+
state: string;
|
|
87
|
+
transaction: number;
|
|
88
|
+
linkedTransaction?: number;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export interface Token {
|
|
92
|
+
id: number;
|
|
93
|
+
state: CreationEntityState;
|
|
94
|
+
name?: string;
|
|
95
|
+
customerId?: string;
|
|
96
|
+
enabledForOneClickPayment?: boolean;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export interface RefundCreate {
|
|
100
|
+
transaction: number;
|
|
101
|
+
externalId: string;
|
|
102
|
+
amount: number;
|
|
103
|
+
type: RefundType;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export interface Refund {
|
|
107
|
+
id: number;
|
|
108
|
+
state: string;
|
|
109
|
+
amount: number;
|
|
110
|
+
transaction: number;
|
|
111
|
+
}
|
|
@@ -1,91 +1,55 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { PostFinanceApiClient } from './api-client.js';
|
|
2
|
+
import {
|
|
3
|
+
Transaction,
|
|
4
|
+
TransactionCreate,
|
|
5
|
+
TransactionCompletion,
|
|
6
|
+
Token,
|
|
7
|
+
RefundCreate,
|
|
8
|
+
RefundType,
|
|
9
|
+
} from './api-types.js';
|
|
2
10
|
|
|
3
|
-
const { PostFinanceCheckout } = pf;
|
|
4
11
|
const { PFCHECKOUT_SPACE_ID, PFCHECKOUT_USER_ID, PFCHECKOUT_SECRET } = process.env;
|
|
5
12
|
const SPACE_ID = parseInt(PFCHECKOUT_SPACE_ID as string, 10);
|
|
6
13
|
const USER_ID = parseInt(PFCHECKOUT_USER_ID as string, 10);
|
|
7
14
|
|
|
8
|
-
|
|
9
|
-
return {
|
|
10
|
-
space_id: SPACE_ID,
|
|
11
|
-
user_id: USER_ID,
|
|
12
|
-
api_secret: PFCHECKOUT_SECRET,
|
|
13
|
-
};
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
const getTransactionService = () => {
|
|
17
|
-
return new PostFinanceCheckout.api.TransactionService(getConfig());
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
const getTransactionCompletionService = () => {
|
|
21
|
-
return new PostFinanceCheckout.api.TransactionCompletionService(getConfig());
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
const getTransactionVoidService = () => {
|
|
25
|
-
return new PostFinanceCheckout.api.TransactionVoidService(getConfig());
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
const getRefundService = () => {
|
|
29
|
-
return new PostFinanceCheckout.api.RefundService(getConfig());
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
const getTransactionPaymentPageService = () => {
|
|
33
|
-
return new PostFinanceCheckout.api.TransactionPaymentPageService(getConfig());
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
const getTransactionIframeService = () => {
|
|
37
|
-
return new PostFinanceCheckout.api.TransactionIframeService(getConfig());
|
|
38
|
-
};
|
|
15
|
+
let apiClient: PostFinanceApiClient;
|
|
39
16
|
|
|
40
|
-
const
|
|
41
|
-
|
|
17
|
+
const getApiClient = () => {
|
|
18
|
+
if (!apiClient) {
|
|
19
|
+
apiClient = new PostFinanceApiClient({
|
|
20
|
+
spaceId: SPACE_ID,
|
|
21
|
+
userId: USER_ID,
|
|
22
|
+
apiSecret: PFCHECKOUT_SECRET as string,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return apiClient;
|
|
42
26
|
};
|
|
43
27
|
|
|
44
|
-
const
|
|
45
|
-
|
|
28
|
+
export const getTransaction = async (transactionId: string): Promise<Transaction> => {
|
|
29
|
+
const client = getApiClient();
|
|
30
|
+
return client.get<Transaction>(`/transaction/read?spaceId=${SPACE_ID}&id=${transactionId}`);
|
|
46
31
|
};
|
|
47
32
|
|
|
48
|
-
export const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const transactionService = getTransactionService();
|
|
52
|
-
const transaction = await transactionService.read(SPACE_ID, parseInt(transactionId, 10));
|
|
53
|
-
return transaction.body;
|
|
33
|
+
export const getTransactionCompletion = async (id: string): Promise<TransactionCompletion> => {
|
|
34
|
+
const client = getApiClient();
|
|
35
|
+
return client.get<TransactionCompletion>(`/transaction-completion/read?spaceId=${SPACE_ID}&id=${id}`);
|
|
54
36
|
};
|
|
55
37
|
|
|
56
|
-
export const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
const transactionCompletionService = getTransactionCompletionService();
|
|
60
|
-
const transactionCompletion = await transactionCompletionService.read(
|
|
61
|
-
SPACE_ID,
|
|
62
|
-
parseInt(entityId, 10),
|
|
63
|
-
);
|
|
64
|
-
return transactionCompletion.body;
|
|
38
|
+
export const getToken = async (spaceId: number, tokenId: number): Promise<Token> => {
|
|
39
|
+
const client = getApiClient();
|
|
40
|
+
return client.get<Token>(`/token/read?spaceId=${spaceId || SPACE_ID}&id=${tokenId}`);
|
|
65
41
|
};
|
|
66
42
|
|
|
67
|
-
export const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const tokenService = getTokenService();
|
|
72
|
-
const token = await tokenService.read(spaceId || SPACE_ID, tokenId);
|
|
73
|
-
return token.body;
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
export const createTransaction = async (
|
|
77
|
-
transaction: pf.PostFinanceCheckout.model.TransactionCreate,
|
|
78
|
-
): Promise<number | null> => {
|
|
79
|
-
const transactionService = getTransactionService();
|
|
80
|
-
const transactionCreateRes = await transactionService.create(SPACE_ID, transaction);
|
|
81
|
-
const transactionCreate = transactionCreateRes.body;
|
|
82
|
-
return transactionCreate.id || null;
|
|
43
|
+
export const createTransaction = async (transaction: TransactionCreate): Promise<number | null> => {
|
|
44
|
+
const client = getApiClient();
|
|
45
|
+
const result = await client.post<Transaction>(`/transaction/create?spaceId=${SPACE_ID}`, transaction);
|
|
46
|
+
return result.id || null;
|
|
83
47
|
};
|
|
84
48
|
|
|
85
49
|
export const voidTransaction = async (transactionId: string): Promise<boolean> => {
|
|
86
|
-
const
|
|
50
|
+
const client = getApiClient();
|
|
87
51
|
try {
|
|
88
|
-
await
|
|
52
|
+
await client.post(`/transaction-void/voidOnline?spaceId=${SPACE_ID}&id=${transactionId}`, {});
|
|
89
53
|
return true;
|
|
90
54
|
} catch {
|
|
91
55
|
return false;
|
|
@@ -97,15 +61,15 @@ export const refundTransaction = async (
|
|
|
97
61
|
orderId: string,
|
|
98
62
|
amount: number,
|
|
99
63
|
): Promise<boolean> => {
|
|
100
|
-
const
|
|
101
|
-
const refund:
|
|
64
|
+
const client = getApiClient();
|
|
65
|
+
const refund: RefundCreate = {
|
|
102
66
|
transaction: parseInt(transactionId, 10),
|
|
103
67
|
externalId: orderId,
|
|
104
68
|
amount,
|
|
105
|
-
type:
|
|
69
|
+
type: RefundType.MERCHANT_INITIATED_ONLINE,
|
|
106
70
|
};
|
|
107
71
|
try {
|
|
108
|
-
await
|
|
72
|
+
await client.post(`/refund/refund?spaceId=${SPACE_ID}`, refund);
|
|
109
73
|
return true;
|
|
110
74
|
} catch {
|
|
111
75
|
return false;
|
|
@@ -113,9 +77,12 @@ export const refundTransaction = async (
|
|
|
113
77
|
};
|
|
114
78
|
|
|
115
79
|
export const confirmDeferredTransaction = async (transactionId: string): Promise<boolean> => {
|
|
116
|
-
const
|
|
80
|
+
const client = getApiClient();
|
|
117
81
|
try {
|
|
118
|
-
await
|
|
82
|
+
await client.post(
|
|
83
|
+
`/transaction-completion/completeOnline?spaceId=${SPACE_ID}&transactionId=${transactionId}`,
|
|
84
|
+
{},
|
|
85
|
+
);
|
|
119
86
|
return true;
|
|
120
87
|
} catch {
|
|
121
88
|
return false;
|
|
@@ -123,19 +90,25 @@ export const confirmDeferredTransaction = async (transactionId: string): Promise
|
|
|
123
90
|
};
|
|
124
91
|
|
|
125
92
|
export const getPaymentPageUrl = async (transactionId: number): Promise<string> => {
|
|
126
|
-
const
|
|
127
|
-
const
|
|
128
|
-
|
|
93
|
+
const client = getApiClient();
|
|
94
|
+
const result = await client.get<string>(
|
|
95
|
+
`/transaction-payment-page/payment-page-url?spaceId=${SPACE_ID}&id=${transactionId}`,
|
|
96
|
+
);
|
|
97
|
+
return result;
|
|
129
98
|
};
|
|
130
99
|
|
|
131
100
|
export const getLightboxJavascriptUrl = async (transactionId: number): Promise<string> => {
|
|
132
|
-
const
|
|
133
|
-
const
|
|
134
|
-
|
|
101
|
+
const client = getApiClient();
|
|
102
|
+
const result = await client.get<string>(
|
|
103
|
+
`/transaction-lightbox/javascript-url?spaceId=${SPACE_ID}&id=${transactionId}`,
|
|
104
|
+
);
|
|
105
|
+
return result;
|
|
135
106
|
};
|
|
136
107
|
|
|
137
108
|
export const getIframeJavascriptUrl = async (transactionId: number): Promise<string> => {
|
|
138
|
-
const
|
|
139
|
-
const
|
|
140
|
-
|
|
109
|
+
const client = getApiClient();
|
|
110
|
+
const result = await client.get<string>(
|
|
111
|
+
`/transaction-iframe/javascript-url?spaceId=${SPACE_ID}&id=${transactionId}`,
|
|
112
|
+
);
|
|
113
|
+
return result;
|
|
141
114
|
};
|
|
@@ -3,7 +3,7 @@ import { createLogger } from '@unchainedshop/logger';
|
|
|
3
3
|
import { WebhookData } from './types.js';
|
|
4
4
|
import { getTransaction, getTransactionCompletion } from './api.js';
|
|
5
5
|
|
|
6
|
-
const logger = createLogger('unchained:core-payment:postfinance-checkout');
|
|
6
|
+
const logger = createLogger('unchained:core-payment:postfinance-checkout:handler');
|
|
7
7
|
|
|
8
8
|
export const postfinanceCheckoutHandler = async (req, res) => {
|
|
9
9
|
const context = req.unchainedContext as Context;
|
|
@@ -13,7 +13,7 @@ export const postfinanceCheckoutHandler = async (req, res) => {
|
|
|
13
13
|
try {
|
|
14
14
|
const transactionCompletion = await getTransactionCompletion(data.entityId as unknown as string);
|
|
15
15
|
const transaction = await getTransaction(
|
|
16
|
-
transactionCompletion
|
|
16
|
+
transactionCompletion ? (transactionCompletion as any).linkedTransaction : data.entityId,
|
|
17
17
|
);
|
|
18
18
|
const { orderPaymentId } = transaction.metaData as { orderPaymentId: string };
|
|
19
19
|
const orderPayment = await modules.orders.payments.findOrderPayment({
|
|
@@ -27,19 +27,17 @@ export const postfinanceCheckoutHandler = async (req, res) => {
|
|
|
27
27
|
},
|
|
28
28
|
});
|
|
29
29
|
logger.info(
|
|
30
|
-
`
|
|
30
|
+
`Transaction ${transactionCompletion.linkedTransaction} marked order payment ID ${transaction.metaData.orderPaymentId} as paid`,
|
|
31
31
|
);
|
|
32
32
|
res.writeHead(200);
|
|
33
33
|
res.end(`Order marked as paid: ${order.orderNumber}`);
|
|
34
34
|
} catch (e) {
|
|
35
|
-
logger.error(
|
|
35
|
+
logger.error(e);
|
|
36
36
|
res.writeHead(500);
|
|
37
37
|
res.end(JSON.stringify({ name: e.name, code: e.code, message: e.message }));
|
|
38
38
|
}
|
|
39
39
|
} else {
|
|
40
|
-
logger.error(
|
|
41
|
-
`PostFinance Checkout Webhook: Received unknown listenerEntityTechnicalName ${data.listenerEntityTechnicalName}`,
|
|
42
|
-
);
|
|
40
|
+
logger.error(`Received unknown listenerEntityTechnicalName ${data.listenerEntityTechnicalName}`);
|
|
43
41
|
res.writeHead(404);
|
|
44
42
|
res.end();
|
|
45
43
|
}
|
|
@@ -4,7 +4,7 @@ import { WebhookData } from './types.js';
|
|
|
4
4
|
import { getTransaction, getTransactionCompletion } from './api.js';
|
|
5
5
|
import { FastifyRequest, RouteHandlerMethod } from 'fastify';
|
|
6
6
|
|
|
7
|
-
const logger = createLogger('unchained:core-payment:postfinance-checkout');
|
|
7
|
+
const logger = createLogger('unchained:core-payment:postfinance-checkout:handler');
|
|
8
8
|
|
|
9
9
|
export const postfinanceCheckoutHandler: RouteHandlerMethod = async (
|
|
10
10
|
req: FastifyRequest & {
|
|
@@ -19,7 +19,7 @@ export const postfinanceCheckoutHandler: RouteHandlerMethod = async (
|
|
|
19
19
|
try {
|
|
20
20
|
const transactionCompletion = await getTransactionCompletion(data.entityId as unknown as string);
|
|
21
21
|
const transaction = await getTransaction(
|
|
22
|
-
transactionCompletion
|
|
22
|
+
transactionCompletion ? (transactionCompletion as any).linkedTransaction : data.entityId,
|
|
23
23
|
);
|
|
24
24
|
const { orderPaymentId } = transaction.metaData as { orderPaymentId: string };
|
|
25
25
|
const orderPayment = await modules.orders.payments.findOrderPayment({
|
|
@@ -29,23 +29,21 @@ export const postfinanceCheckoutHandler: RouteHandlerMethod = async (
|
|
|
29
29
|
|
|
30
30
|
const order = await services.orders.checkoutOrder(orderPayment.orderId, {
|
|
31
31
|
paymentContext: {
|
|
32
|
-
transactionId:
|
|
32
|
+
transactionId: transaction.id,
|
|
33
33
|
},
|
|
34
34
|
});
|
|
35
35
|
logger.info(
|
|
36
|
-
`
|
|
36
|
+
`Transaction ${transaction.id} marked order payment ID ${transaction.metaData.orderPaymentId} as paid`,
|
|
37
37
|
);
|
|
38
38
|
reply.status(200);
|
|
39
39
|
return reply.send(`Order marked as paid: ${order.orderNumber}`);
|
|
40
40
|
} catch (e) {
|
|
41
|
-
logger.error(
|
|
41
|
+
logger.error(e);
|
|
42
42
|
reply.status(500);
|
|
43
43
|
return reply.send({ name: e.name, code: e.code, message: e.message });
|
|
44
44
|
}
|
|
45
45
|
} else {
|
|
46
|
-
logger.error(
|
|
47
|
-
`PostFinance Checkout Webhook: Received unknown listenerEntityTechnicalName ${data.listenerEntityTechnicalName}`,
|
|
48
|
-
);
|
|
46
|
+
logger.error(`Received unknown listenerEntityTechnicalName ${data.listenerEntityTechnicalName}`);
|
|
49
47
|
reply.status(404);
|
|
50
48
|
return reply.send();
|
|
51
49
|
}
|
|
@@ -8,7 +8,6 @@ import {
|
|
|
8
8
|
PaymentDirector,
|
|
9
9
|
PaymentError,
|
|
10
10
|
} from '@unchainedshop/core';
|
|
11
|
-
import * as pf from 'postfinancecheckout';
|
|
12
11
|
|
|
13
12
|
import {
|
|
14
13
|
confirmDeferredTransaction,
|
|
@@ -23,8 +22,15 @@ import {
|
|
|
23
22
|
} from './api.js';
|
|
24
23
|
import { orderIsPaid } from './utils.js';
|
|
25
24
|
import { CompletionModes, IntegrationModes, SignResponse } from './types.js';
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
import {
|
|
26
|
+
TransactionCreate,
|
|
27
|
+
LineItem,
|
|
28
|
+
LineItemType,
|
|
29
|
+
TokenizationMode,
|
|
30
|
+
TransactionCompletionBehavior,
|
|
31
|
+
TransactionState,
|
|
32
|
+
CreationEntityState,
|
|
33
|
+
} from './api-types.js';
|
|
28
34
|
|
|
29
35
|
const {
|
|
30
36
|
PFCHECKOUT_SPACE_ID,
|
|
@@ -34,7 +40,7 @@ const {
|
|
|
34
40
|
PFCHECKOUT_FAILED_URL,
|
|
35
41
|
} = process.env;
|
|
36
42
|
|
|
37
|
-
const logger = createLogger('unchained:core-payment:postfinance');
|
|
43
|
+
const logger = createLogger('unchained:core-payment:postfinance-checkout');
|
|
38
44
|
|
|
39
45
|
const newError = ({ code, message }: { code: string; message: string }) => {
|
|
40
46
|
const error = new Error(message);
|
|
@@ -92,7 +98,7 @@ const PostfinanceCheckout: IPaymentAdapter = {
|
|
|
92
98
|
if (!credentials.meta) return false;
|
|
93
99
|
const { linkedSpaceId } = credentials.meta;
|
|
94
100
|
const tokenData = await getToken(linkedSpaceId, credentials.token);
|
|
95
|
-
return tokenData.state ===
|
|
101
|
+
return tokenData.state === CreationEntityState.ACTIVE;
|
|
96
102
|
},
|
|
97
103
|
|
|
98
104
|
sign: async (transactionContext: any = {}) => {
|
|
@@ -107,37 +113,38 @@ const PostfinanceCheckout: IPaymentAdapter = {
|
|
|
107
113
|
});
|
|
108
114
|
|
|
109
115
|
const totalAmount = pricing?.total({ useNetPrice: false }).amount;
|
|
110
|
-
const transaction =
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
116
|
+
const transaction: TransactionCreate = {
|
|
117
|
+
currency: order.currencyCode,
|
|
118
|
+
metaData: {
|
|
119
|
+
orderPaymentId: orderPayment._id,
|
|
120
|
+
},
|
|
121
|
+
successUrl: `${transactionContext?.successUrl || PFCHECKOUT_SUCCESS_URL}?order_id=${
|
|
122
|
+
orderPayment.orderId
|
|
123
|
+
}`,
|
|
124
|
+
failedUrl: `${transactionContext?.failedUrl || PFCHECKOUT_FAILED_URL}?order_id=${
|
|
125
|
+
orderPayment.orderId
|
|
126
|
+
}`,
|
|
127
|
+
customerId: order?.userId || context?.userId,
|
|
128
|
+
tokenizationMode: TokenizationMode.ALLOW_ONE_CLICK_PAYMENT,
|
|
115
129
|
};
|
|
116
|
-
|
|
117
|
-
orderPayment.orderId
|
|
118
|
-
}`;
|
|
119
|
-
transaction.failedUrl = `${transactionContext?.failedUrl || PFCHECKOUT_FAILED_URL}?order_id=${
|
|
120
|
-
orderPayment.orderId
|
|
121
|
-
}`;
|
|
122
|
-
transaction.customerId = userId;
|
|
123
|
-
transaction.tokenizationMode =
|
|
124
|
-
PostFinanceCheckout.model.TokenizationMode.ALLOW_ONE_CLICK_PAYMENT;
|
|
130
|
+
|
|
125
131
|
if (completionMode === CompletionModes.Immediate) {
|
|
126
|
-
transaction.completionBehavior =
|
|
127
|
-
PostFinanceCheckout.model.TransactionCompletionBehavior.COMPLETE_IMMEDIATELY;
|
|
132
|
+
transaction.completionBehavior = TransactionCompletionBehavior.COMPLETE_IMMEDIATELY;
|
|
128
133
|
} else if (completionMode === CompletionModes.Deferred) {
|
|
129
|
-
transaction.completionBehavior =
|
|
130
|
-
PostFinanceCheckout.model.TransactionCompletionBehavior.COMPLETE_DEFERRED;
|
|
134
|
+
transaction.completionBehavior = TransactionCompletionBehavior.COMPLETE_DEFERRED;
|
|
131
135
|
}
|
|
136
|
+
|
|
132
137
|
if (totalAmount) {
|
|
133
|
-
const lineItemSum =
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
138
|
+
const lineItemSum: LineItem = {
|
|
139
|
+
name: `Bestellung ${orderPayment.orderId}`,
|
|
140
|
+
type: LineItemType.FEE,
|
|
141
|
+
quantity: 1,
|
|
142
|
+
uniqueId: orderPayment.orderId,
|
|
143
|
+
amountIncludingTax: totalAmount / 100,
|
|
144
|
+
};
|
|
139
145
|
transaction.lineItems = [lineItemSum];
|
|
140
146
|
}
|
|
147
|
+
|
|
141
148
|
const transactionId = await createTransaction(transaction);
|
|
142
149
|
let location = null;
|
|
143
150
|
if (integrationMode === IntegrationModes.PaymentPage) {
|
|
@@ -214,7 +221,7 @@ const PostfinanceCheckout: IPaymentAdapter = {
|
|
|
214
221
|
return false;
|
|
215
222
|
}
|
|
216
223
|
const transaction = await getTransaction(transactionId);
|
|
217
|
-
const refund = transaction.state ===
|
|
224
|
+
const refund = transaction.state === TransactionState.FULFILL;
|
|
218
225
|
const pricing = OrderPricingSheet({
|
|
219
226
|
calculation: order.calculation,
|
|
220
227
|
currencyCode: order.currencyCode,
|
|
@@ -235,7 +242,7 @@ const PostfinanceCheckout: IPaymentAdapter = {
|
|
|
235
242
|
return false;
|
|
236
243
|
}
|
|
237
244
|
const transaction = await getTransaction(transactionId);
|
|
238
|
-
if (transaction.state ===
|
|
245
|
+
if (transaction.state === TransactionState.AUTHORIZED) {
|
|
239
246
|
return confirmDeferredTransaction(transactionId);
|
|
240
247
|
}
|
|
241
248
|
return false;
|
|
@@ -247,3 +254,6 @@ const PostfinanceCheckout: IPaymentAdapter = {
|
|
|
247
254
|
};
|
|
248
255
|
|
|
249
256
|
PaymentDirector.registerAdapter(PostfinanceCheckout);
|
|
257
|
+
|
|
258
|
+
export * from './types.js';
|
|
259
|
+
export * from './api-types.js';
|