Skip to content

Commit

Permalink
Chore: dump new version
Browse files Browse the repository at this point in the history
Chore: dump new version
  • Loading branch information
NghiaDTr authored Aug 6, 2024
2 parents cade8d1 + b1fcfce commit 1574924
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 62 deletions.
27 changes: 14 additions & 13 deletions processor/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions processor/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "shopmacher-mollie-processor",
"description": "Integration between commercetools and mollie payment service provider",
"version": "0.0.22",
"version": "0.0.23",
"main": "index.js",
"private": true,
"scripts": {
Expand Down Expand Up @@ -40,6 +40,7 @@
"@types/express": "^4.17.21",
"@types/jest": "^29.5.12",
"@types/node": "^18.19.39",
"@types/node-fetch": "^2.6.11",
"@types/validator": "^13.12.0",
"@typescript-eslint/eslint-plugin": "7.13.1",
"@typescript-eslint/parser": "7.13.1",
Expand Down Expand Up @@ -67,10 +68,10 @@
"@commercetools/sdk-client-v2": "^2.5.0",
"@mollie/api-client": "^3.7.0",
"@types/uuid": "^10.0.0",
"axios": "^1.7.2",
"body-parser": "^1.20.2",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"node-fetch": "^2.7.0",
"proxy-from-env": "^1.1.0",
"uuid": "^10.0.0",
"validator": "^13.12.0"
Expand Down
42 changes: 30 additions & 12 deletions processor/src/mollie/payment.mollie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import {
import { initMollieClient } from '../client/mollie.client';
import CustomError from '../errors/custom.error';
import { logger } from '../utils/logger.utils';
import axios, { AxiosError } from 'axios';
import { readConfiguration } from '../utils/config.utils';
import { LIBRARY_NAME, LIBRARY_VERSION } from '../utils/constant.utils';
import { CustomPayment } from '../types/mollie.types';
import fetch from 'node-fetch';

/**
* Creates a Mollie payment using the provided payment parameters.
Expand Down Expand Up @@ -104,29 +104,47 @@ export const cancelPayment = async (paymentId: string): Promise<void> => {
};

export const createPaymentWithCustomMethod = async (paymentParams: PaymentCreateParams): Promise<CustomPayment> => {
let errorMessage;

try {
const { mollie } = readConfiguration();

const headers = {
'Content-Type': 'application/json',
Authorization: `Bearer ${mollie.apiKey}`,
versionStrings: `${LIBRARY_NAME}/${LIBRARY_VERSION}`,
};

const response = await axios.post('https://api.mollie.com/v2/payments', paymentParams, { headers });
const response = await fetch('https://api.mollie.com/v2/payments', {
method: 'POST',
headers,
body: JSON.stringify(paymentParams),
});

return response.data;
} catch (error: unknown) {
let errorMessage;
const data = await response.json();

if (error instanceof AxiosError) {
errorMessage = `SCTM - createPaymentWithCustomMethod - error: ${error.response?.data?.detail}, field: ${error.response?.data?.field}`;
} else {
errorMessage = 'SCTM - createPaymentWithCustomMethod - Failed to create a payment with unknown errors';
if (response.status !== 201) {
if (response.status === 422 || response.status === 503) {
errorMessage = `SCTM - createPaymentWithCustomMethod - error: ${data?.detail}, field: ${data?.field}`;
} else {
errorMessage = 'SCTM - createPaymentWithCustomMethod - Failed to create a payment with unknown errors';
}

logger.error(errorMessage, {
response: data,
});

throw new CustomError(400, errorMessage);
}

logger.error(errorMessage, {
error,
});
return data;
} catch (error: unknown) {
if (!errorMessage) {
errorMessage = 'SCTM - createPaymentWithCustomMethod - Failed to create a payment with unknown errors';
logger.error(errorMessage, {
error,
});
}

throw new CustomError(400, errorMessage);
}
Expand Down
Loading

0 comments on commit 1574924

Please sign in to comment.