Skip to content

Commit

Permalink
fix(payment): fix mismatching invoice identifier
Browse files Browse the repository at this point in the history
* generate invoice identifier using nanoid instead of makeInvoiceNo function
* fix mismatching invoice identifier
  • Loading branch information
soyombo-baterdene committed Oct 3, 2023
1 parent 21a52e8 commit c55adb5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 24 deletions.
17 changes: 11 additions & 6 deletions packages/plugin-payment-api/src/controllers.ts
Expand Up @@ -10,7 +10,7 @@ import { sendRequest } from '@erxes/api-utils/src';
import { graphqlPubsub } from './configs';
import { isEnabled } from '@erxes/api-utils/src/serviceDiscovery';
import messageBroker from './messageBroker';
import { makeInvoiceNo } from './utils';
import { randomAlphanumeric } from '@erxes/api-utils/src/random';

const router = Router();

Expand Down Expand Up @@ -143,13 +143,18 @@ router.post('/gateway/updateInvoice', async (req, res) => {
invoice.status !== 'paid' &&
invoice.selectedPaymentId !== selectedPaymentId
) {
await models.Invoices.updateInvoice(invoice._id, {
const doc: any = {
selectedPaymentId,
paymentKind,
phone,
domain,
identifier: makeInvoiceNo(32)
});
domain
};

if (!invoice.selectedPaymentId) {
doc.identifier = randomAlphanumeric(32);
}

await models.Invoices.updateInvoice(invoice._id, doc);
}

if (!invoice) {
Expand All @@ -158,7 +163,7 @@ router.post('/gateway/updateInvoice', async (req, res) => {
selectedPaymentId,
phone,
domain,
identifier: makeInvoiceNo(32)
identifier: randomAlphanumeric(32)
});
}

Expand Down
@@ -1,6 +1,6 @@
import { getEnv } from '@erxes/api-utils/src';
import { IContext } from '../../../connectionResolver';
import { makeInvoiceNo } from '../../../utils';
import { randomAlphanumeric } from '@erxes/api-utils/src/random';

type InvoiceParams = {
amount: number;
Expand Down Expand Up @@ -59,7 +59,7 @@ const mutations = {
const invoice = await models.Invoices.create({
...params,
data,
identifier: makeInvoiceNo(32)
identifier: randomAlphanumeric(32)
});

const base64 = Buffer.from(
Expand Down
13 changes: 7 additions & 6 deletions packages/plugin-payment-api/src/models/Invoices.ts
Expand Up @@ -3,13 +3,13 @@ import { Model } from 'mongoose';

import { IModels } from '../connectionResolver';
import redisUtils from '../redisUtils';
import { makeInvoiceNo } from '../utils';
import {
IInvoice,
IInvoiceDocument,
invoiceSchema
} from './definitions/invoices';
import ErxesPayment from '../api/ErxesPayment';
import { randomAlphanumeric } from '@erxes/api-utils/src/random';

export interface IInvoiceModel extends Model<IInvoiceDocument> {
getInvoice(doc: any, leanObject?: boolean): IInvoiceDocument;
Expand Down Expand Up @@ -47,7 +47,7 @@ export const loadInvoiceClass = (models: IModels) => {

const invoice = await models.Invoices.create({
...doc,
identifier: doc.identifier || makeInvoiceNo(32)
identifier: doc.identifier || randomAlphanumeric(32)
});

const api = new ErxesPayment(payment, doc.domain);
Expand Down Expand Up @@ -89,7 +89,7 @@ export const loadInvoiceClass = (models: IModels) => {
);

const api = new ErxesPayment(payment, doc.domain);
invoice.identifier = doc.identifier || makeInvoiceNo(32);
invoice.identifier = doc.identifier || randomAlphanumeric(32);

const apiResponse = await api.createInvoice(invoice);

Expand All @@ -100,7 +100,8 @@ export const loadInvoiceClass = (models: IModels) => {
apiResponse,
paymentKind: payment.kind,
selectedPaymentId: payment._id,
createdAt: new Date()
createdAt: new Date(),
identifier: doc.identifier
}
}
);
Expand All @@ -119,7 +120,7 @@ export const loadInvoiceClass = (models: IModels) => {
);

const api = new ErxesPayment(payment, doc.domain);
invoice.identifier = doc.identifier || makeInvoiceNo(32);
invoice.identifier = doc.identifier || randomAlphanumeric(32);

const apiResponse = await api.createInvoice(invoice);

Expand Down Expand Up @@ -179,7 +180,7 @@ export const loadInvoiceClass = (models: IModels) => {
new ErxesPayment(prevPayment).cancelInvoice(invoice);

try {
invoice.identifier = doc.identifier || makeInvoiceNo(32);
invoice.identifier = doc.identifier || randomAlphanumeric(32);
const apiResponse = await new ErxesPayment(
newPayment,
doc.domain
Expand Down
10 changes: 0 additions & 10 deletions packages/plugin-payment-api/src/utils.ts
Expand Up @@ -92,13 +92,3 @@ export const callbackHandler = async (req, res) => {

return res.status(200).send('OK');
};

export const makeInvoiceNo = length => {
let result = '';
const characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
};

0 comments on commit c55adb5

Please sign in to comment.